public static async Task <MaticTransactionOptions> GetTransactionEstimate(ERC721ApproveModel approvemodel, 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, approvemodel.To, approvemodel.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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Approve ERC721 token for deposit
        /// </summary>
        /// <param name="tokenAddress">Must be a Valid ERC721 Token Address</param>
        /// <param name="tokenId">Mapped Token Id</param>
        /// <param name="options">Matic Transaction Options</param>
        /// <returns></returns>
        public async Task <string> ApproveERC721TokenForDeposit(string tokenAddress, int?tokenId, MaticTransactionOptions options = null)
        {
            //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);
            ERC721ApproveModel  approveModel   = new ERC721ApproveModel()
            {
                To      = tokenAddress,
                TokenId = tokenId.Value
            };

            string response = await erc721Contract.Approve(approveModel, options);

            return(response);
        }
        /// <summary>
        /// Approve ERC721 token for deposit
        /// </summary>
        /// <param name="tokenAddress"></param>
        /// <param name="tokenId"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <string> Approve(ERC721ApproveModel approveModel, 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("approve");

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

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

            return(response);
        }