示例#1
0
        /// <summary>
        /// Coin
        /// </summary>
        private static bool WithdrawCGas(byte[] sender, BigInteger count)
        {
            if (sender.Length != 20)
            {
                Runtime.Log("Owner error.");
                return(false);
            }

            if (Runtime.CheckWitness(sender))
            {
                BigInteger newBalance        = 0;
                byte[]     ownerBalanceBytes = DataAccess.GetUserBalanceAsBytes(sender);
                if (ownerBalanceBytes.Length > 0)
                {
                    newBalance = ownerBalanceBytes.AsBigInteger();
                }

                if (count <= 0 || count > newBalance)
                {
                    count = newBalance;
                }

                object[] args = new object[]
                {
                    ExecutionEngine.ExecutingScriptHash,
                    sender,
                    count,
                    ExecutionEngine.ExecutingScriptHash
                };

                byte[]         cgasHash       = DataAccess.GetCGasScriptHashAsBytes();
                dynamicAppCall dyncall        = (dynamicAppCall)cgasHash.ToDelegate();
                bool           appCallSuccess = (bool)dyncall("transferAPP", args);
                if (!appCallSuccess)
                {
                    return(false);
                }

                newBalance -= count;
                DecreaseCGasBalance(count);

                byte[] senderBalanceKey = Keys.UserBalanceKey(sender);
                if (newBalance > 0)
                {
                    Storage.Put(Storage.CurrentContext, senderBalanceKey, newBalance.AsByteArray());
                }
                else
                {
                    Storage.Delete(Storage.CurrentContext, senderBalanceKey);
                }

                return(true);
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Use txid to recharge
        /// </summary>
        private static bool RefreshBalance(byte[] owner, byte[] txid)
        {
            if (owner.Length != 20)
            {
                Runtime.Log("Owner error.");
                return(false);
            }

            byte[] bytes = DataAccess.GetTransactionAsBytes(txid);
            if (bytes.Length > 0)
            {
                // Has been processed
                return(false);
            }

            object[] args = new object[] { txid };

            byte[]         cgasHash = DataAccess.GetCGasScriptHashAsBytes();
            dynamicAppCall dyncall  = (dynamicAppCall)cgasHash.ToDelegate();

            object[] appCallResult = (object[])dyncall("getTxInfo", args);
            if (appCallResult.Length > 0)
            {
                byte[]     from  = (byte[])appCallResult[0];
                byte[]     to    = (byte[])appCallResult[1];
                BigInteger value = (BigInteger)appCallResult[2];

                if (from == owner)
                {
                    if (to == ExecutionEngine.ExecutingScriptHash)
                    {
                        Storage.Put(Storage.CurrentContext, Keys.TransactionKey(txid), value);

                        BigInteger newBalance      = 0;
                        byte[]     ownerBalanceKey = Keys.UserBalanceKey(owner);
                        byte[]     ownerBalance    = Storage.Get(Storage.CurrentContext, ownerBalanceKey);
                        if (ownerBalance.Length > 0)
                        {
                            newBalance = ownerBalance.AsBigInteger();
                        }

                        newBalance += value;

                        IncreaseCGasBalance(value);

                        Storage.Put(Storage.CurrentContext, ownerBalanceKey, newBalance.AsByteArray());

                        return(true);
                    }
                }
            }

            return(false);
        }
示例#3
0
        private static BigInteger GetAccumulatedFees()
        {
            object[]       args           = new object[] { ExecutionEngine.ExecutingScriptHash };
            byte[]         cgasScriptHash = DataAccess.GetCGasScriptHashAsBytes();
            dynamicAppCall cgasAppCall    = (dynamicAppCall)cgasScriptHash.ToDelegate();

            BigInteger availableBalance   = (BigInteger)cgasAppCall("balanceOf", args);
            BigInteger totalAvailableCGas = DataAccess.GetCGasBalanceAsBytes().AsBigInteger();

            BigInteger maxWithdrawAmount = availableBalance - totalAvailableCGas;

            return(maxWithdrawAmount);
        }
        /// <summary>
        /// Withdrawal of income to contract owner
        /// </summary>
        private static bool Withdraw(BigInteger flag, BigInteger count)
        {
            if (Runtime.CheckWitness(ContractMain.ContractOwner))
            {
                object[]       args     = new object[] { ExecutionEngine.ExecutingScriptHash };
                byte[]         cgasHash = DataAccess.GetCGasScriptHashAsBytes();
                dynamicAppCall dyncall  = (dynamicAppCall)cgasHash.ToDelegate();

                BigInteger actualBalance = (BigInteger)dyncall("balanceOf", args);

                if (flag == 0)
                {
                    BigInteger localBalance = DataAccess.GetCGasBalanceAsBytes().AsBigInteger();
                    BigInteger maxWithdraw  = actualBalance - localBalance;
                    if (count <= 0 || count > maxWithdraw)
                    {
                        count = maxWithdraw;
                    }
                }
                else
                {
                    count = actualBalance;
                    DataAccess.SetCGasBalance(0);
                }

                args = new object[]
                {
                    ExecutionEngine.ExecutingScriptHash,
                    ContractMain.ContractOwner,
                    count
                };

                dynamicAppCall dyncall2       = (dynamicAppCall)cgasHash.ToDelegate();
                bool           appCallSuccess = (bool)dyncall2("transfer", args);
                if (!appCallSuccess)
                {
                    return(false);
                }

                // booking cwt should not be accounted for here
                //_subTotal(count);
                return(true);
            }

            return(false);
        }