/// <summary> /// Estimates the gas limit of a certain function of a contract. /// </summary> /// <param name="promise"> Promise of the estimated gas limit of a transaction. </param> /// <param name="callInput"> The transaction input to estimate the gas limit for. </param> /// <param name="overEstimate"> Whether the gas limit should be slightly overestimated. </param> /// <returns> The time taken to retrieve the estimated gas limit. </returns> private static IEnumerator _EstimateGasLimitCoroutine(EthCallPromise <BigInteger> promise, CallInput callInput, bool overEstimate) { var request = new EthEstimateGasUnityRequest(NetworkProvider.GetNetworkChainUrl()); yield return(request.SendRequest(callInput)); promise.Build(request, () => overEstimate ? (request.Result.Value * 100 / 90) : request.Result.Value); }
/// <summary> /// Estimates the gas price based on current network congestion. /// </summary> /// <param name="promise"> Promise of the eventual gas price estimate. </param> /// <param name="gasPriceTarget"> The GasPriceTarget to aim for. </param> /// <returns> The time taken to retrieve the estimated gas limit. </returns> private static IEnumerator _EstimateGasPriceCoroutine(EthCallPromise <BigInteger> promise, GasPriceTarget gasPriceTarget) { var request = new EthGasPriceUnityRequest(NetworkProvider.GetNetworkChainUrl()); yield return(request.SendRequest()); promise.Build(request, () => ModifyGasPrice(gasPriceTarget, request.Result.Value)); }
/// <summary> /// Starts the coroutine for getting the details of a transaction. /// </summary> /// <param name="txHash"> The transaction hash. </param> /// <param name="onTxReceived"> Action to call once the transaction details have been received. </param> /// <returns> The transaction request to await. </returns> private static IEnumerator _GetTransactionDetailsCoroutine(string txHash, Action <Transaction> onTxReceived) { var request = new EthGetTransactionByHashUnityRequest(NetworkProvider.GetNetworkChainUrl()); yield return(request.SendRequest(txHash)); onTxReceived?.Invoke(request.Result); }
/// <summary> /// Gets the ether balance of a certain wallet. /// </summary> /// <param name="promise"> Promise of an eventual eth balance returned. </param> /// <param name="address"> The address to check the ether balance for. </param> /// <returns> The time waited for the request to complete. </returns> private static IEnumerator _AddressEthBalanceCoroutine(EthCallPromise <dynamic> promise, string address) { var request = new EthGetBalanceUnityRequest(NetworkProvider.GetNetworkChainUrl()); yield return(request.SendRequest(address, BlockParameter.CreateLatest())); promise.Build(request, () => SolidityUtils.ConvertFromUInt(request.Result.Value, 18)); }
/// <summary> /// Coroutine which queries some data from an ethereum smart contract. /// </summary> /// <typeparam name="TFunc"> The <see cref="FunctionMessage"/> of the smart contract to execute which will return us some data. </typeparam> /// <typeparam name="TOut"> The <see cref="IFunctionOutputDTO"/> which represents the data which was returned from the <see cref="ContractFunction"/>. </typeparam> /// <param name="function"> The contract function to query data from. </param> /// <param name="promise"> Promise of eventually returning the data from the contract query. </param> /// <param name="contractAddress"> The contract address to execute the <see cref="FunctionMessage"/> on. </param> /// <param name="senderAddress"> The address of the sender requesting this data. </param> private static IEnumerator _QueryContractCoroutine <TFunc, TOut>( TFunc function, EthCallPromise <TOut> promise, string contractAddress, string senderAddress) where TFunc : FunctionMessage, new() where TOut : IFunctionOutputDTO, new() { function.SetDefaultFromAddressIfNotSet(senderAddress); var queryRequest = new QueryUnityRequest <TFunc, TOut>(NetworkProvider.GetNetworkChainUrl(), senderAddress); yield return(queryRequest.Query(function, contractAddress)); promise.Build(queryRequest, () => queryRequest.Result); }
/// <summary> /// Sends ether from one address to another. /// </summary> /// <param name="promise"> Promise of the transaction result of sending ether. </param> /// <param name="walletAddress"> The address of the wallet sending the ether. </param> /// <param name="gasLimit"> The gas limit of the ether send transaction. </param> /// <param name="gasPrice"> The gas price of the ether send transaction. </param> /// <param name="privateKey"> The private key of the address sending the transaction. </param> /// <param name="addressTo"> The address the ether is being sent to. </param> /// <param name="amount"> The amount to send in ether. </param> /// <returns> The time waited for the request to be broadcast to the network. </returns> private static IEnumerator _SendEtherCoroutine( EthTransactionPromise promise, BigInteger gasLimit, BigInteger gasPrice, string privateKey, string addressTo, dynamic amount) { EthECKey ethKey = new EthECKey(privateKey); TransactionSignedUnityRequest unityRequest = new TransactionSignedUnityRequest(NetworkProvider.GetNetworkChainUrl(), privateKey, ethKey.GetPublicAddress()); TransactionInput transactionInput = new TransactionInput("", addressTo, ethKey.GetPublicAddress(), new HexBigInteger(gasLimit), new HexBigInteger(gasPrice), new HexBigInteger(SolidityUtils.ConvertToUInt(amount, 18))); yield return(unityRequest.SignAndSendTransaction(transactionInput)); promise.Build(unityRequest, () => unityRequest.Result, () => NetworkProvider.GetNetworkChainUrl()); }
/// <summary> /// Coroutine which sends a message to an ethereum smart contract. /// </summary> /// <typeparam name="TFunc"> The <see cref="FunctionMessage"/> to execute on the blockchain given the contract address. </typeparam> /// <param name="function"> The function to execute. </param> /// <param name="promise"> Promise of the transaction result of sending the contract message. </param> /// <param name="contractAddress"> The contract address to execute the <see cref="FunctionMessage"/> on. </param> /// <param name="privateKey"> The private key of the address sending the transaction. </param> /// <param name="gasPrice"> The <see cref="BigInteger"/> gas price to use with the transaction. </param> /// <param name="gasLimit"> The <see cref="BigInteger"/> gas limit to use with the transaction. </param> private static IEnumerator _SendContractMessageCoroutine <TFunc>( TFunc function, EthTransactionPromise promise, string contractAddress, string privateKey, BigInteger gasPrice, BigInteger gasLimit) where TFunc : FunctionMessage { EthECKey ethKey = new EthECKey(privateKey); function.SetDefaultFromAddressIfNotSet(ethKey.GetPublicAddress()); function.Gas = gasLimit; function.GasPrice = gasPrice; TransactionSignedUnityRequest unityRequest = new TransactionSignedUnityRequest(NetworkProvider.GetNetworkChainUrl(), privateKey, ethKey.GetPublicAddress()); yield return(unityRequest.SignAndSendTransaction(function.CreateTransactionInput(contractAddress))); promise.Build(unityRequest, () => unityRequest.Result, NetworkProvider.GetNetworkChainUrl); }