Example #1
0
        /// <summary>
        /// Calculate NetworkFee with context items
        /// </summary>
        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 = 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);
        }
        /// <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);
        }