Exemple #1
0
        /// <summary>
        /// approve the "to" account to transfer "approvedValue" tokens from the originator acount.
        /// </summary>
        /// <param name="originator"></param>
        /// <param name="to"></param>
        /// <param name="amount"></param>
        /// <param name="caller"></param>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static bool Approve(byte[] originator, byte[] to, BigInteger amount, byte[] caller, byte[] entry)
        {
            if (caller != entry && !Helpers.IsContractWhitelistedTransferFrom(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);
        }
Exemple #2
0
        /// <summary>
        /// transfer an amount from the "from" account to the "to" acount if the originator has been approved to transfer the requested amount.
        /// </summary>
        /// <param name="originator"></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[] originator, byte[] from, byte[] to, BigInteger amount, byte[] caller, byte[] entry)
        {
            if (Helpers.GetBlockTimestamp() < ICOTemplate.PublicSaleEndTime())
            {
                Runtime.Log("TransferFrom() not available before ICOTemplate.PublicSaleEndTime()");
                return(false);
            }

            if (caller != entry && !Helpers.IsContractWhitelistedTransferFrom(caller))
            {
                originator = caller;
            }

            if (originator.Length != 20 || from.Length != 20 || to.Length != 20)
            {
                Runtime.Log("TransferFrom() (originator|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, to);    // 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);
            }

            if (amount == 0 || from == to)
            {
                // 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(originator))
            {
                // ensure transaction is signed properly by the request originator
                Runtime.Log("TransferFrom() 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
            Helpers.SetAllowanceAmount(from.Concat(originator), approvedTransferAmount - amount);   // deduct transferred amount from allowance

            transfer(from, to, amount);
            return(true);
        }
Exemple #3
0
        /// <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 (Helpers.GetBlockTimestamp() < ICOTemplate.PublicSaleEndTime())
            {
                Runtime.Log("Transfer() not available before ICOTemplate.PublicSaleEndTime()");
                return(false);
            }

            if (caller != entry && !Helpers.IsContractWhitelistedTransferFrom(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);
        }