Skip to content

Commit

Permalink
Improve 1559 estimation and limits
Browse files Browse the repository at this point in the history
Fixes rpcs returning extreme prio fees as well like fantom
  • Loading branch information
0xFirekeeper committed Jun 3, 2024
1 parent edc3202 commit 963f06e
Showing 1 changed file with 40 additions and 79 deletions.
119 changes: 40 additions & 79 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,101 +573,62 @@ public static string JSDateToUnixTimestamp(string dateString)

public async static Task<BigInteger> GetLegacyGasPriceAsync(BigInteger chainId, string clientId = null, string bundleId = null)
{
var web3 = GetWeb3(chainId, clientId, bundleId);
var gasPrice = (await web3.Eth.GasPrice.SendRequestAsync()).Value;
return BigInteger.Multiply(gasPrice, 10) / 9;
var client = GetWeb3(chainId, clientId, bundleId).Client;
var hex = new HexBigInteger(await client.SendRequestAsync<string>("eth_gasPrice"));
return BigInteger.Multiply(hex.Value, 10) / 9;
}

public async static Task<GasPriceParameters> GetGasPriceAsync(BigInteger chainId, string clientId = null, string bundleId = null)
{
BigInteger? priorityOverride = null;
if (chainId == 137 || chainId == 80001)
{
try
{
return await GetPolygonGasPriceParameters((int)chainId);
}
catch (System.Exception e)
{
ThirdwebDebug.LogWarning($"Failed to get gas price from Polygon gas station, using default method: {e.Message}");
priorityOverride = GweiToWei(chainId == 137 ? 40 : 1);
}
}

var web3 = GetWeb3(chainId, clientId, bundleId);
var gasPrice = (await web3.Eth.GasPrice.SendRequestAsync()).Value;
var client = GetWeb3(chainId, clientId, bundleId).Client;
var gasPrice = await GetLegacyGasPriceAsync(chainId, clientId, bundleId);

if (chainId == 42220) // celo mainnet
if (chainId == 137 || chainId == 80001)
{
gasPrice = BigInteger.Multiply(gasPrice, 3) / 2;
return new GasPriceParameters(gasPrice, gasPrice);
return new GasPriceParameters(gasPrice * 3 / 2, gasPrice * 4 / 3);
}

if (
chainId == 1 // mainnet
|| chainId == 11155111 // sepolia
|| chainId == 42161 // arbitrum
|| chainId == 421614 // arbitrum sepolia
|| chainId == 534352 // scroll
|| chainId == 534351 // scroll sepolia
|| chainId == 5000 // mantle
|| chainId == 22222 // nautilus
|| chainId == 8453 // base
|| chainId == 53935 // dfk
|| chainId == 44787 // celo alfajores
|| chainId == 43114 // avalanche
|| chainId == 43113 // avalanche fuji
|| chainId == 8453 // base
|| chainId == 84532 // base sepolia
)
try
{
gasPrice = BigInteger.Multiply(gasPrice, 10) / 9;
return new GasPriceParameters(gasPrice, priorityOverride ?? gasPrice);
}

var maxPriorityFeePerGas = new BigInteger(2000000000) > gasPrice ? gasPrice : new BigInteger(2000000000);
if (
// chainId == 1 // mainnet
// || chainId == 11155111 // sepolia
// || chainId == 42161 // arbitrum
// || chainId == 421614 // arbitrum sepolia
// || chainId == 534352 // scroll
// || chainId == 534351 // scroll sepolia
// || chainId == 5000 // mantle
// || chainId == 22222 // nautilus
// || chainId == 8453 // base
// || chainId == 53935 // dfk
// || chainId == 43114 // avalanche
// || chainId == 43113 // avalanche fuji
// || chainId == 8453 // base
// || chainId == 84532 // base sepolia
chainId == 42220 // celo
|| chainId == 44787 // celo-alfajores-testnet
|| chainId == 62320 // celo-baklava-testnet
)
{
return new GasPriceParameters(gasPrice, gasPrice);
}

var feeHistory = await web3.Eth.FeeHistory.SendRequestAsync(new Nethereum.Hex.HexTypes.HexBigInteger(20), Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest(), new double[] { 20 });
var block = await client.SendRequestAsync<JObject>(method: "eth_getBlockByNumber", route: null, paramList: new object[] { "latest", true });
var baseBlockFee = block["baseFeePerGas"]?.ToObject<HexBigInteger>();
var maxFeePerGas = baseBlockFee.Value * 2;
var maxPriorityFeePerGas = ((await client.SendRequestAsync<HexBigInteger>("eth_maxPriorityFeePerGas"))?.Value) ?? maxFeePerGas / 2;

if (feeHistory.Reward == null)
{
gasPrice = BigInteger.Multiply(gasPrice, 3) / 2;
maxPriorityFeePerGas = gasPrice;
}
else
{
var feeAverage = feeHistory.Reward.Select(r => r[0]).Aggregate(BigInteger.Zero, (acc, cur) => cur + acc) / 10;
if (feeAverage > gasPrice)
if (maxPriorityFeePerGas > maxFeePerGas)
{
gasPrice = feeAverage;
maxPriorityFeePerGas = maxFeePerGas / 2;
}
maxPriorityFeePerGas = gasPrice;
}

return new GasPriceParameters(gasPrice, priorityOverride ?? maxPriorityFeePerGas);
}

public async static Task<GasPriceParameters> GetPolygonGasPriceParameters(int chainId)
{
using var httpClient = new HttpClient();
string gasStationUrl;
switch (chainId)
return new GasPriceParameters((maxFeePerGas + maxPriorityFeePerGas) * 10 / 9, maxPriorityFeePerGas * 10 / 9);
}
catch
{
case 137:
gasStationUrl = "https://gasstation.polygon.technology/v2";
break;
case 80001:
gasStationUrl = "https://gasstation-testnet.polygon.technology/v2";
break;
default:
throw new UnityException("Unsupported chain id");
return new GasPriceParameters(gasPrice, gasPrice);
}

var response = await httpClient.GetAsync(gasStationUrl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<PolygonGasStationResult>(responseBody);
return new GasPriceParameters(GweiToWei(data.fast.maxFee), GweiToWei(data.fast.maxPriorityFee));
}

public static BigInteger GweiToWei(decimal gweiAmount)
Expand Down

0 comments on commit 963f06e

Please sign in to comment.