Skip to content

Commit 963f06e

Browse files
committed
Improve 1559 estimation and limits
Fixes rpcs returning extreme prio fees as well like fantom
1 parent edc3202 commit 963f06e

File tree

1 file changed

+40
-79
lines changed

1 file changed

+40
-79
lines changed

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -573,101 +573,62 @@ public static string JSDateToUnixTimestamp(string dateString)
573573

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

581581
public async static Task<GasPriceParameters> GetGasPriceAsync(BigInteger chainId, string clientId = null, string bundleId = null)
582582
{
583-
BigInteger? priorityOverride = null;
584-
if (chainId == 137 || chainId == 80001)
585-
{
586-
try
587-
{
588-
return await GetPolygonGasPriceParameters((int)chainId);
589-
}
590-
catch (System.Exception e)
591-
{
592-
ThirdwebDebug.LogWarning($"Failed to get gas price from Polygon gas station, using default method: {e.Message}");
593-
priorityOverride = GweiToWei(chainId == 137 ? 40 : 1);
594-
}
595-
}
596-
597-
var web3 = GetWeb3(chainId, clientId, bundleId);
598-
var gasPrice = (await web3.Eth.GasPrice.SendRequestAsync()).Value;
583+
var client = GetWeb3(chainId, clientId, bundleId).Client;
584+
var gasPrice = await GetLegacyGasPriceAsync(chainId, clientId, bundleId);
599585

600-
if (chainId == 42220) // celo mainnet
586+
if (chainId == 137 || chainId == 80001)
601587
{
602-
gasPrice = BigInteger.Multiply(gasPrice, 3) / 2;
603-
return new GasPriceParameters(gasPrice, gasPrice);
588+
return new GasPriceParameters(gasPrice * 3 / 2, gasPrice * 4 / 3);
604589
}
605590

606-
if (
607-
chainId == 1 // mainnet
608-
|| chainId == 11155111 // sepolia
609-
|| chainId == 42161 // arbitrum
610-
|| chainId == 421614 // arbitrum sepolia
611-
|| chainId == 534352 // scroll
612-
|| chainId == 534351 // scroll sepolia
613-
|| chainId == 5000 // mantle
614-
|| chainId == 22222 // nautilus
615-
|| chainId == 8453 // base
616-
|| chainId == 53935 // dfk
617-
|| chainId == 44787 // celo alfajores
618-
|| chainId == 43114 // avalanche
619-
|| chainId == 43113 // avalanche fuji
620-
|| chainId == 8453 // base
621-
|| chainId == 84532 // base sepolia
622-
)
591+
try
623592
{
624-
gasPrice = BigInteger.Multiply(gasPrice, 10) / 9;
625-
return new GasPriceParameters(gasPrice, priorityOverride ?? gasPrice);
626-
}
627-
628-
var maxPriorityFeePerGas = new BigInteger(2000000000) > gasPrice ? gasPrice : new BigInteger(2000000000);
593+
if (
594+
// chainId == 1 // mainnet
595+
// || chainId == 11155111 // sepolia
596+
// || chainId == 42161 // arbitrum
597+
// || chainId == 421614 // arbitrum sepolia
598+
// || chainId == 534352 // scroll
599+
// || chainId == 534351 // scroll sepolia
600+
// || chainId == 5000 // mantle
601+
// || chainId == 22222 // nautilus
602+
// || chainId == 8453 // base
603+
// || chainId == 53935 // dfk
604+
// || chainId == 43114 // avalanche
605+
// || chainId == 43113 // avalanche fuji
606+
// || chainId == 8453 // base
607+
// || chainId == 84532 // base sepolia
608+
chainId == 42220 // celo
609+
|| chainId == 44787 // celo-alfajores-testnet
610+
|| chainId == 62320 // celo-baklava-testnet
611+
)
612+
{
613+
return new GasPriceParameters(gasPrice, gasPrice);
614+
}
629615

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

632-
if (feeHistory.Reward == null)
633-
{
634-
gasPrice = BigInteger.Multiply(gasPrice, 3) / 2;
635-
maxPriorityFeePerGas = gasPrice;
636-
}
637-
else
638-
{
639-
var feeAverage = feeHistory.Reward.Select(r => r[0]).Aggregate(BigInteger.Zero, (acc, cur) => cur + acc) / 10;
640-
if (feeAverage > gasPrice)
621+
if (maxPriorityFeePerGas > maxFeePerGas)
641622
{
642-
gasPrice = feeAverage;
623+
maxPriorityFeePerGas = maxFeePerGas / 2;
643624
}
644-
maxPriorityFeePerGas = gasPrice;
645-
}
646-
647-
return new GasPriceParameters(gasPrice, priorityOverride ?? maxPriorityFeePerGas);
648-
}
649625

650-
public async static Task<GasPriceParameters> GetPolygonGasPriceParameters(int chainId)
651-
{
652-
using var httpClient = new HttpClient();
653-
string gasStationUrl;
654-
switch (chainId)
626+
return new GasPriceParameters((maxFeePerGas + maxPriorityFeePerGas) * 10 / 9, maxPriorityFeePerGas * 10 / 9);
627+
}
628+
catch
655629
{
656-
case 137:
657-
gasStationUrl = "https://gasstation.polygon.technology/v2";
658-
break;
659-
case 80001:
660-
gasStationUrl = "https://gasstation-testnet.polygon.technology/v2";
661-
break;
662-
default:
663-
throw new UnityException("Unsupported chain id");
630+
return new GasPriceParameters(gasPrice, gasPrice);
664631
}
665-
666-
var response = await httpClient.GetAsync(gasStationUrl);
667-
response.EnsureSuccessStatusCode();
668-
string responseBody = await response.Content.ReadAsStringAsync();
669-
var data = JsonConvert.DeserializeObject<PolygonGasStationResult>(responseBody);
670-
return new GasPriceParameters(GweiToWei(data.fast.maxFee), GweiToWei(data.fast.maxPriorityFee));
671632
}
672633

673634
public static BigInteger GweiToWei(decimal gweiAmount)

0 commit comments

Comments
 (0)