Esempio n. 1
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
                Witnesses       = Array.Empty <Witness>()
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = long.Parse(result.GasConsumed);
            context      = new ContractParametersContext(Tx);
            signStore    = new List <SignItem>();

            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <param name="cosigners">Transaction Cosigners</param>
        /// <param name="networkFee">Transaction NetworkFee, will set to estimate value(with only basic signatures) when networkFee is 0</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null, Cosigner[] cosigners = null, long networkFee = 0)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? new TransactionAttribute[0],
                Cosigners       = cosigners ?? new Cosigner[0],
                Witnesses       = new Witness[0]
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = Math.Max(long.Parse(result.GasConsumed) - ApplicationEngine.GasFree, 0);
            if (Tx.SystemFee > 0)
            {
                long d         = (long)NativeContract.GAS.Factor;
                long remainder = Tx.SystemFee % d;
                if (remainder > 0)
                {
                    Tx.SystemFee += d - remainder;
                }
                else if (remainder < 0)
                {
                    Tx.SystemFee -= remainder;
                }
            }

            context = new ContractParametersContext(Tx);

            // set networkfee to estimate value when networkFee is 0
            Tx.NetworkFee = networkFee == 0 ? EstimateNetworkFee() : networkFee;

            var gasBalance = nep5API.BalanceOf(NativeContract.GAS.Hash, sender);

            if (gasBalance >= Tx.SystemFee + Tx.NetworkFee)
            {
                return(this);
            }
            throw new InvalidOperationException($"Insufficient GAS in address: {sender.ToAddress()}");
        }
Esempio n. 3
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <param name="cosigners">Transaction Cosigners</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null, Cosigner[] cosigners = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
                Cosigners       = cosigners ?? Array.Empty <Cosigner>(),
                Witnesses       = Array.Empty <Witness>()
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = Math.Max(long.Parse(result.GasConsumed) - ApplicationEngine.GasFree, 0);
            if (Tx.SystemFee > 0)
            {
                long d         = (long)NativeContract.GAS.Factor;
                long remainder = Tx.SystemFee % d;
                if (remainder > 0)
                {
                    Tx.SystemFee += d - remainder;
                }
                else if (remainder < 0)
                {
                    Tx.SystemFee -= remainder;
                }
            }

            context   = new ContractParametersContext(Tx);
            signStore = new List <SignItem>();

            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, Signer[] signers = null, TransactionAttribute[] attributes = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Signers         = signers,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
            };

            RpcInvokeResult result = rpcClient.InvokeScript(script, signers);

            Tx.SystemFee = long.Parse(result.GasConsumed);
            context      = new ContractParametersContext(Tx);
            signStore    = new List <SignItem>();

            return(this);
        }
Esempio n. 5
0
        /// <summary>
        /// Get unclaimed gas
        /// </summary>
        /// <param name="account">account scripthash</param>
        /// <returns></returns>
        public decimal GetUnclaimedGas(UInt160 account)
        {
            UInt160    scriptHash = NativeContract.NEO.Hash;
            BigInteger balance    = nep5API.TestInvoke(scriptHash, "unclaimedGas", account, rpcClient.GetBlockCount() - 1)
                                    .Stack.Single().ToStackItem().GetBigInteger();

            return(((decimal)balance) / (long)NativeContract.GAS.Factor);
        }