Esempio n. 1
0
        /// <summary>
        /// Calculate NetworkFee
        /// </summary>
        /// <returns></returns>
        private long CalculateNetworkFee()
        {
            long networkFee = 0;

            UInt160[] hashes = Tx.GetScriptHashesForVerifying(null);
            int       size   = Transaction.HeaderSize + Tx.Attributes.GetVarSize() + Tx.Script.GetVarSize() + IO.Helper.GetVarSize(hashes.Length);

            foreach (UInt160 hash in hashes)
            {
                byte[] witness_script = null;

                // calculate NetworkFee
                witness_script = signStore.FirstOrDefault(p => p.Contract.ScriptHash == hash)?.Contract?.Script;
                if (witness_script is null || witness_script.Length == 0)
                {
                    try
                    {
                        witness_script = rpcClient.GetContractState(hash.ToString())?.Script;
                    }
                    catch { }
                }

                if (witness_script is null)
                {
                    continue;
                }
                networkFee += Wallet.CalculateNetworkFee(witness_script, ref size);
            }
            networkFee += size * policyAPI.GetFeePerByte();
            return(networkFee);
        }
Esempio n. 2
0
        /// <summary>
        /// Estimate NetworkFee, assuming the witnesses are basic Signature Contract
        /// </summary>
        private long EstimateNetworkFee()
        {
            long networkFee = 0;

            UInt160[] hashes = Tx.GetScriptHashesForVerifying(null);
            int       size   = Transaction.HeaderSize + Tx.Attributes.GetVarSize() + Tx.Script.GetVarSize() + IO.Helper.GetVarSize(hashes.Length);

            // assume the hashes are single Signature
            foreach (var hash in hashes)
            {
                size       += 166;
                networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES64] + ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES33] + InteropService.GetPrice(InteropService.Neo_Crypto_ECDsaVerify, null);
            }

            networkFee += size * policyAPI.GetFeePerByte();
            return(networkFee);
        }
        /// <summary>
        /// Calculate NetworkFee
        /// </summary>
        /// <param name="isEstimate">assuming the witnesses are basic Signature Contract if set to true</param>
        /// <returns></returns>
        private long CalculateNetworkFee(bool isEstimate = false)
        {
            long networkFee = 0;

            UInt160[] hashes = Tx.GetScriptHashesForVerifying(null);
            int       size   = Transaction.HeaderSize + Tx.Attributes.GetVarSize() + Tx.Cosigners.GetVarSize() + Tx.Script.GetVarSize() + IO.Helper.GetVarSize(hashes.Length);

            foreach (UInt160 hash in hashes)
            {
                byte[] witness_script = null;
                if (isEstimate)
                {
                    // assuming the witnesses are basic Signature Contract
                    var dummyKey = new byte[32];
                    dummyKey[31] = 0x01;
                    KeyPair one = new KeyPair(dummyKey);
                    witness_script = Contract.CreateSignatureRedeemScript(one.PublicKey);
                }
                else
                {
                    // calculate NetworkFee with context items
                    witness_script = context.GetScript(hash);
                    if (witness_script is null || witness_script.Length == 0)
                    {
                        try
                        {
                            witness_script = rpcClient.GetContractState(hash.ToString())?.Script;
                        }
                        catch { }
                    }

                    if (witness_script is null)
                    {
                        continue;
                    }
                }

                networkFee += Wallet.CalculateNetworkFee(witness_script, ref size);
            }
            networkFee += size * policyAPI.GetFeePerByte();
            return(networkFee);
        }