public static async Task <MaticTransactionOptions> GetTransactionEstimate(ERC20TransferModel transferModel, MaticTransactionOptions options, Function function) { try { //Get the Account Account account = GetAccount(options.SenderPrivateKey, options.ChainId); //Get the Gas Limit HexBigInteger gasLimit = await function.EstimateGasAsync(account.Address, null, null, transferModel.To, transferModel.Value); //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 transaction estimate because " + ex.Message); } }
/// <summary> /// The transfer function is used to Perform Transfers from one Address to another /// </summary> /// <param name="from">Source Address and Signer</param> /// <param name="To">Destination Address</param> /// <param name="Value">The Amount (In wei) of tokens to be transfered</param> /// <returns></returns> public async Task <string> Transfer(ERC20TransferModel transferModel, 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("transfer"); //Fill the options options = await TransactionEstimateHelper.GetTransactionEstimate(transferModel, options, function); string response = await function.SendTransactionAsync(options.From, new HexBigInteger(options.GasLimit), new HexBigInteger(options.GasPrice.ToString()), null, transferModel.To, (BigInteger)transferModel.Value); return(response); }
/// <summary> /// The transfer function is used to Perform Transfers from one Address to another /// </summary> /// <param name="tokenAddress">Source Address and Signer</param> /// <param name="userAddress">Address of the reciepient</param> /// <param name="amount">The Amount (In wei) of tokens to be transfered to the reciepient</param> /// <param name="options">Matic Transaction Options</param> /// <returns></returns> public async Task <string> TransferTokens(string tokenAddress, string userAddress, BigInteger amount, MaticTransactionOptions options) { //check if the options are correctly set if ((options != null) && (String.IsNullOrEmpty(options.From) || String.IsNullOrEmpty(tokenAddress) || amount == 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>'"); } string web3Provider = MaticProvider; //Set The web3 Object to the Parent Web3 Object if UseParent is set to true if (options.UseParent) { web3Provider = ParentProvider; } //Get the contract using the token Address ERC20TokenContract erc20TokenContract = new ERC20TokenContract(web3Provider, tokenAddress); //Make the Transfer ERC20TransferModel transferModel = new ERC20TransferModel() { To = userAddress, Value = new HexBigInteger(amount) }; string response = await erc20TokenContract.Transfer(transferModel, options); return(response); }