/// <summary> /// transfer an amount from the "from" account to the "to" acount if the "spender" has been approved to transfer the requested amount. /// </summary> /// <param name="spender"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="amount"></param> /// <param name="caller"></param> /// <param name="entry"></param> /// <returns></returns> public static bool TransferFrom(byte[] spender, byte[] from, byte[] to, BigInteger amount, byte[] caller, byte[] entry) { if (caller != entry && !Helpers.IsContractWhitelistedDEX(caller)) { spender = caller; } if (spender.Length != 20 || from.Length != 20 || to.Length != 20) { Runtime.Log("TransferFrom() (originator|from|to).Length != 20"); return(false); } if (amount < 0 || from == to) { Runtime.Log("TransferFrom() invalid transfer amount must be >= 0"); throw new Exception(); } BigInteger approvedTransferAmount = Allowance(from, spender); // how many tokens is the spender authorised to transfer from the "from" account if (!Runtime.CheckWitness(spender)) { // ensure transaction is signed properly by the request originator Runtime.Log("TransferFrom() CheckWitness failed"); return(false); } //At this point, vesting is complete for all but founders and project. //Check if vesting applies and resolve. BigInteger recipientAmountSubjectToVesting = 0; BigInteger senderAmountSubjectToVesting = 0; // reduce gas usage by only checking vesting criteria if the from address is a project or founders key if (Helpers.IsProjectKey(from)) { senderAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(from); } BigInteger fromBalance = BalanceOfRaw(from); // retrieve balance of authorised account if (approvedTransferAmount < amount || fromBalance - senderAmountSubjectToVesting < amount) { // don't transfer if funds not available Runtime.Notify("TransferFrom() (authorisedAmount|fromBalance) < transferValue", approvedTransferAmount, fromBalance, amount); return(false); } BigInteger recipientBalance = BalanceOfRaw(to); BigInteger newBalance = fromBalance - amount; Helpers.SetBalanceOf(from, newBalance); // remove balance from originating account Helpers.SetBalanceOf(to, recipientBalance + amount); // set new balance for destination account Helpers.SetAllowanceAmount(from.Concat(spender), approvedTransferAmount - amount); // deduct transferred amount from allowance transfer(from, to, amount); return(true); }
/// <summary> /// transfer an amount from the "from" account to the "to" acount if the "spender" has been approved to transfer the requested amount. /// </summary> /// <param name="spender"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="amount"></param> /// <param name="caller"></param> /// <param name="entry"></param> /// <returns></returns> public static bool TransferFrom(byte[] spender, byte[] from, byte[] to, BigInteger amount, byte[] caller, byte[] entry) { if (caller != entry && !Helpers.IsContractWhitelistedDEX(caller)) { spender = caller; } if (spender.Length != 20 || from.Length != 20 || to.Length != 20) { Runtime.Log("TransferFrom() (spender|from|to).Length != 20"); return(false); } if (amount < 0) { Runtime.Log("TransferFrom() invalid transfer amount must be >= 0"); throw new Exception(); } BigInteger approvedTransferAmount = Allowance(from, spender); // how many tokens is this address authorised to transfer BigInteger fromBalance = BalanceOf(from); // retrieve balance of authorised account if (approvedTransferAmount < amount || fromBalance < amount) { // don't transfer if funds not available Runtime.Notify("TransferFrom() (authorisedAmount|fromBalance) < transferValue", approvedTransferAmount, fromBalance, amount); return(false); } BigInteger senderAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(from); if (amount == 0 || from == to || fromBalance - senderAmountSubjectToVesting < amount) { // don't accept a meaningless value Runtime.Log("TransferFrom() empty transfer amount or from==to"); transfer(from, to, amount); return(true); // as per nep5 standard - return true when amount is 0 or from == to } if (!Runtime.CheckWitness(spender)) { // ensure transaction is signed properly by the spender Runtime.Log("TransferFrom() CheckWitness failed"); return(false); } BigInteger recipientBalance = BalanceOf(to); BigInteger recipientAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(to); BigInteger newBalance = fromBalance - amount; Helpers.SetBalanceOf(from, newBalance + senderAmountSubjectToVesting); // remove balance from originating account Helpers.SetBalanceOf(to, recipientBalance + recipientAmountSubjectToVesting + amount); // set new balance for destination account Helpers.SetAllowanceAmount(from.Concat(spender), approvedTransferAmount - amount); // deduct transferred amount from allowance transfer(from, to, amount); return(true); }
/// <summary> /// NEP5: Transfer tokens from one account to another /// </summary> /// <param name="from">sender address</param> /// <param name="to">recipient address</param> /// <param name="amount">number of tokens to transfer</param> /// <param name="caller"></param> /// <param name="entry"></param> /// <returns></returns> public static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] caller, byte[] entry) { if (caller != entry && !Helpers.IsContractWhitelistedDEX(caller)) { from = caller; } if (from.Length != 20 || to.Length != 20) { Runtime.Log("Transfer() (from|to).Length != 20"); return(false); } if (amount < 0 || from == to) { Runtime.Log("Transfer() invalid transfer amount must be >= 0"); throw new Exception(); } if (!Runtime.CheckWitness(from)) { // ensure transaction is signed properly by the owner of the tokens Runtime.Log("Transfer() CheckWitness failed"); return(false); } //At this point, vesting is complete for all but founders and project. //Check if vesting applies and resolve. BigInteger recipientAmountSubjectToVesting = 0; BigInteger senderAmountSubjectToVesting = 0; // reduce gas usage by only checking vesting criteria if the from address is a project or founders key if (Helpers.IsProjectKey(from)) { senderAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(from); } BigInteger fromBalance = BalanceOfRaw(from); // retrieve balance of originating account if (fromBalance - senderAmountSubjectToVesting < amount) { Runtime.Log("Transfer() fromBalance < transferValue"); // don't transfer if funds not available return(false); } BigInteger recipientBalance = BalanceOfRaw(to); BigInteger newBalance = fromBalance - amount; Helpers.SetBalanceOf(from, newBalance); // remove balance from originating account Helpers.SetBalanceOf(to, recipientBalance + amount); // set new balance for destination account transfer(from, to, amount); return(true); }
public static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] caller, byte[] entry) { if (caller != entry && !Helpers.IsContractWhitelistedDEX(caller)) { from = caller; } if (from.Length != 20 || to.Length != 20) { Runtime.Log("Transfer() (from|to).Length != 20"); return(false); } if (amount < 0) { Runtime.Log("Transfer() invalid transfer amount must be >= 0"); throw new Exception(); } BigInteger fromBalance = BalanceOf(from); // retrieve balance of originating account if (fromBalance < amount) { Runtime.Log("Transfer() fromBalance < transferValue"); // don't transfer if funds not available return(false); } if (amount == 0 || from == to) { // don't accept a meaningless value Runtime.Log("Transfer() empty transfer amount or from==to"); transfer(from, to, amount); return(true); // as per nep5 standard - return true when amount is 0 or from == to } if (!Runtime.CheckWitness(from)) { // ensure transaction is signed properly by the owner of the tokens Runtime.Log("Transfer() CheckWitness failed"); return(false); } BigInteger recipientBalance = BalanceOf(to); BigInteger recipientAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(to); BigInteger senderAmountSubjectToVesting = TokenSale.SubjectToVestingPeriod(from); BigInteger newBalance = fromBalance - amount; Helpers.SetBalanceOf(from, newBalance + senderAmountSubjectToVesting); // remove balance from originating account Helpers.SetBalanceOf(to, recipientBalance + recipientAmountSubjectToVesting + amount); // set new balance for destination account transfer(from, to, amount); return(true); }
public static bool Approve(byte[] originator, byte[] to, BigInteger amount, byte[] caller, byte[] entry) { if (caller != entry && !Helpers.IsContractWhitelistedDEX(caller)) { originator = caller; } if (amount < 0 || originator == to) { // don't accept a meaningless value Runtime.Log("Approve() invalid transfer amount or from==to"); return(false); } if (originator.Length != 20 || to.Length != 20) { Runtime.Log("Approve() (to|originator).Length != 20"); return(false); } if (!Runtime.CheckWitness(originator)) { // ensure transaction is signed properly by the owner of the tokens Runtime.Log("Approve() CheckWitness failed"); return(false); } BigInteger fromBalance = BalanceOf(originator); // retrieve balance of originating account if (fromBalance < amount) { Runtime.Log("Approve() fromBalance < approveValue"); // don't approve if funds not available return(false); } Helpers.SetAllowanceAmount(originator.Concat(to), amount); //OnApprove(originator, to, amount) return(true); }