/// <summary> /// Estimates the gas price given the GasPriceTarget. /// </summary> /// <param name="gasPriceTarget"> The target gas price to aim for. </param> /// <returns> Promise of the eventual estimated gas price. </returns> public static EthCallPromise <BigInteger> EstimateGasPrice(GasPriceTarget gasPriceTarget = GasPriceTarget.Standard) { var promise = new EthCallPromise <BigInteger>(); _EstimateGasPriceCoroutine(promise, gasPriceTarget).StartCoroutine(); return(promise); }
/// <summary> /// Modifies the current gas price to reflect the GasPriceTarget. /// </summary> /// <param name="priceTarget"> The target to modify the gas price towards. </param> /// <param name="currentPrice"> The current functional gas price to modify. </param> /// <returns> The new functional gas price after modification. </returns> private static BigInteger ModifyGasPrice(GasPriceTarget priceTarget, BigInteger currentPrice) { switch (priceTarget) { case GasPriceTarget.Slow: return(currentPrice * 3 / 4); case GasPriceTarget.Fast: return(currentPrice * 2); default: return(currentPrice * 10 / 9); } }
/// <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(EthereumNetworkManager.CurrentNetwork.NetworkUrl); yield return(request.SendRequest()); promise.Build(request, () => ModifyGasPrice(gasPriceTarget, 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> /// 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)); }