/// <summary> /// Start withdraw process with given amount for token. /// </summary> /// <param name="tokenAddress">Must be a valid ERC20 Token Address</param> /// <param name="amount">must be token amount in wei</param> /// <param name="options">Matic Transaction Options</param> /// <returns></returns> public async Task <string> StartWithdraw(string tokenAddress, BigInteger amount, MaticTransactionOptions options) { //check if the options are correctly set if ((options != null) && (String.IsNullOrEmpty(options.From) || String.IsNullOrEmpty(options.SenderPrivateKey) || 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>'"); } //Get the Contract Using the token Address ERC20TokenContract erc20Contract = new ERC20TokenContract(MaticProvider, tokenAddress); ERC20WithdrawModel withdrawModel = new ERC20WithdrawModel() { Amount = amount }; string response = await erc20Contract.Withdraw(withdrawModel, options); return(response); }
public static async Task <MaticTransactionOptions> GetTransactionEstimate(ERC20WithdrawModel withdrawModel, 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, withdrawModel.Amount); //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); } }
/// <summary> /// Withdraw /// </summary> /// <param name="amount">must be token amount in wei</param> /// <param name="options">Matic Transaction Options</param> /// <returns></returns> public async Task <string> Withdraw(ERC20WithdrawModel withdrawModel, 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("withdraw"); options = await TransactionEstimateHelper.GetTransactionEstimate(withdrawModel, options, function); string response = await function.SendTransactionAsync(options.From, new HexBigInteger(options.GasLimit), new HexBigInteger(options.GasPrice.ToString()), null, (BigInteger)withdrawModel.Amount); return(response); }