/// <summary> /// Sends a given amount of ether to an address. /// </summary> /// <param name="privateKey"> The private key of the address which is sending the ether. </param> /// <param name="addressTo"> The address the ether is being sent to. </param> /// <param name="amount"> The amount of ether being sent, in eth. (not wei) </param> /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param> /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param> /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns> public static async Task <TransactionPoller> SendEther(string privateKey, string addressTo, decimal amount, BigInteger gasPrice, BigInteger gasLimit) { BigInteger value = SolidityUtils.ConvertToUInt(amount, 18); EthECKey ethECKey = new EthECKey(privateKey); InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client); TransactionSigner signer = new TransactionSigner(); string signedTxData = signer.SignTransaction( privateKey, NetworkProvider.GetActiveNetworkChain(), addressTo, value, (await nonceService.GetNextNonceAsync()).Value, gasPrice, gasLimit, string.Empty); EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client); return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData))); }
/// <summary> /// Gets the ether balance of an address. /// </summary> /// <param name="address"> The address to check for the ether balance. </param> /// <returns> Task which returns the decimal ether balance of the address. </returns> public static async Task <decimal> GetEtherBalance(string address) { EthGetBalance ethGetBalance = new EthGetBalance(NetworkProvider.GetWeb3().Client); return(SolidityUtils.ConvertFromUInt((await ethGetBalance.SendRequestAsync(address)).Value, 18)); }
/// <summary> /// Continuously checks the transaction receipt until it is found, and calls the respective event. /// </summary> /// <param name="sender"> Sender object. </param> /// <param name="eventArgs"> Timer event arguments. </param> private async void CheckTransactionReceipt(object sender, ElapsedEventArgs eventArgs) { EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt(NetworkProvider.GetWeb3().Client); TransactionReceipt receipt = await ethGetTransactionReceipt.SendRequestAsync(txHash); if (receipt == null) { return; } if (receipt.HasErrors() == true || receipt.Status.Value == 0) { OnTransactionFail?.Invoke(); } else { OnTransactionSuccess?.Invoke(); } timer.Stop(); }
/// <summary> /// Estimates the gas price of an ethereum transaction. /// </summary> /// <param name="gasPriceTarget"> The target gas price. </param> /// <returns> Task which returns the estimated gas price of an ethereum transaction. </returns> public static async Task <BigInteger> EstimateGasPrice(GasPriceTarget gasPriceTarget = GasPriceTarget.Standard) { EthGasPrice estimateGasPrice = new EthGasPrice(NetworkProvider.GetWeb3().Client); return(ModifyGasPrice(gasPriceTarget, (await estimateGasPrice.SendRequestAsync()).Value)); }
/// <summary> /// Sends a message to an ethereum smart contract with the intent to change a part of the contract on the blockchain. /// </summary> /// <typeparam name="TFunc"> The type of the function to execute. </typeparam> /// <param name="function"> The function to execute. </param> /// <param name="privateKey"> The private key of the address executing the contract function. </param> /// <param name="contractAddress"> The address of the contract which will process the message. </param> /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param> /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param> /// <param name="value"> The amount of eth (in wei) to send with the transaction. </param> /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns> public static async Task <TransactionPoller> SendContractMessage <TFunc>( TFunc function, string privateKey, string contractAddress, BigInteger gasPrice, BigInteger gasLimit, BigInteger value) where TFunc : FunctionMessage, new() { EthECKey ethECKey = new EthECKey(privateKey); function.SetDefaultFromAddressIfNotSet(ethECKey.GetPublicAddress()); function.Gas = gasLimit; function.GasPrice = gasPrice; InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client); TransactionSigner signer = new TransactionSigner(); string signedTxData = signer.SignTransaction( privateKey, NetworkProvider.GetActiveNetworkChain(), contractAddress, value, (await nonceService.GetNextNonceAsync()).Value, gasPrice, gasLimit, function.CreateTransactionInput(contractAddress).Data); EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client); return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData))); }