public static async Task <MaticTransactionOptions> GetTransactionEstimate(ERC721SafeTransferFromModel safeTransferModel, MaticTransactionOptions options, Function function)
        {
            try
            {
                //Get the Account and set up the sender's Address
                Account account = GetAccount(options.SenderPrivateKey, options.ChainId);

                //Get the Gas Limit
                HexBigInteger gasLimit = await function.EstimateGasAsync(account.Address, null, null, safeTransferModel.From, safeTransferModel.To, safeTransferModel.TokenId);

                //Get the Gas Price Estimate
                GasPriceEstimator gasPriceEstimator = new GasPriceEstimator();
                GasPriceEstimate  gasPriceEstimate  = await gasPriceEstimator.GetRecommendedGasPriceFromNetwork();

                //Fill the options
                options.GasPrice = (decimal)gasPriceEstimate.AverageGwei;
                options.GasLimit = gasLimit;
                options.From     = account.Address;

                return(options);
            } catch (Exception ex)
            {
                throw new Exception("Could not fetch the transaction estimate because " + ex.Message);
            }
        }
示例#2
0
        /// <summary>
        /// Deposit ERC721 token into Matic chain.(older ERC721 or some newer contracts will not support this.
        /// In that case, first call `approveERC721TokenForDeposit` and `depositERC721Tokens`)
        /// </summary>
        /// <param name="rootChainAddress"></param>
        /// <param name="tokenId"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <string> SafeDepositERC721Tokens(string tokenAddress, int?tokenId, MaticTransactionOptions options)
        {
            //check if the options are correctly set
            if ((options != null) && (String.IsNullOrEmpty(options.From) || String.IsNullOrEmpty(tokenAddress) || tokenId == null))
            {
                throw new Exception("Parameters required for the transaction on Matic Network are missing");
            }

            //If the Private Key was not sent in the options assign the one set in the wallet. If both are null, throw an exception
            if (string.IsNullOrEmpty(options.SenderPrivateKey))
            {
                options.SenderPrivateKey = Wallet;
            }
            if (String.IsNullOrEmpty(options.SenderPrivateKey))
            {
                throw new Exception("Please provide the private Key first, using 'Matic.Wallet = <PrivateKey>'");
            }


            //Get the contract using the Token Address
            ERC721TokenContract         erc721Contract    = new ERC721TokenContract(ParentProvider, tokenAddress);
            ERC721SafeTransferFromModel safeTransferModel = new ERC721SafeTransferFromModel()
            {
                From    = options.From,
                To      = tokenAddress,
                TokenId = tokenId.Value
            };

            string response = await erc721Contract.SafeTransferFrom(safeTransferModel, options);

            return(response);
        }
        /// <summary>
        /// Deposit ERC721 token into Matic chain.(older ERC721 or some newer contracts will not support this.
        /// In that case, first call `approveERC721TokenForDeposit` and `depositERC721Tokens`)
        /// </summary>
        /// <param name="rootChainAddress"></param>
        /// <param name="tokenId"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <string> SafeTransferFrom(ERC721SafeTransferFromModel safeTransferModel, MaticTransactionOptions options)
        {
            //Get the Contract instance by Creating a Web3 client from the Sender's Private Key
            Web3     web3Client       = Web3ClientHelper.GetWeb3Client(ProviderUrl, options.SenderPrivateKey);
            Contract contractInstance = web3Client.Eth.GetContract(ABI, ContractAddress);
            Function function         = contractInstance.GetFunction("SafeTransferFrom");

            options = await TransactionEstimateHelper.GetTransactionEstimate(safeTransferModel, options, function);

            string transactionHash = await function.SendTransactionAsync(options.From, new HexBigInteger(options.GasLimit), new HexBigInteger(options.GasPrice.ToString()), null, safeTransferModel.From, safeTransferModel.To, safeTransferModel.TokenId);

            return(transactionHash);
        }