Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1559 #19

Closed
wants to merge 1 commit into from
Closed

1559 #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Thirdweb.Tests/Thirdweb.Transactions.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ public async Task EstimateTotalCosts_HigherThanGasCostsByValue()
Assert.True(totalCosts.wei - gasCosts.wei == transaction.Input.Value.Value);
}

[Fact]
public async Task EstimateGasFees_ReturnsCorrectly()
{
var transaction = await CreateSampleTransaction();
_ = transaction.SetValue(new BigInteger(1000));
_ = transaction.SetTo(Constants.ADDRESS_ZERO);

(var maxFee, var maxPrio) = await ThirdwebTransaction.EstimateGasFees(transaction);

Assert.NotEqual(BigInteger.Zero, maxFee);
Assert.NotEqual(BigInteger.Zero, maxPrio);
}

[Fact]
public async Task EstimateGasPrice_BumpsCorrectly()
{
Expand Down
55 changes: 52 additions & 3 deletions Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Nethereum.Contracts;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.Hex.HexConvertors.Extensions;
using Newtonsoft.Json.Linq;

namespace Thirdweb
{
Expand Down Expand Up @@ -101,6 +102,44 @@ public static async Task<TotalCosts> EstimateTotalCosts(ThirdwebTransaction tran
return new TotalCosts { ether = (value + gasCosts.wei).ToString().ToEth(18, false), wei = value + gasCosts.wei };
}

public static async Task<(BigInteger, BigInteger)> EstimateGasFees(ThirdwebTransaction transaction, bool withBump = true)
{
var rpc = ThirdwebRPC.GetRpcInstance(transaction._client, transaction.Input.ChainId.Value);
var chainId = transaction.Input.ChainId.Value;
var gasPrice = await EstimateGasPrice(transaction, withBump);

try
{
var block = await rpc.SendRequestAsync<JObject>("eth_getBlockByNumber", "latest", true);
var maxPriorityFeePerGas = await rpc.SendRequestAsync<BigInteger?>("eth_maxPriorityFeePerGas");
var baseBlockFee = block["result"]?["baseFeePerGas"]?.ToObject<BigInteger>() ?? new BigInteger(100);

if (chainId == 42220 || chainId == 44787 || chainId == 62320)
{
return (gasPrice, gasPrice);
}

if (!maxPriorityFeePerGas.HasValue)
{
maxPriorityFeePerGas = new BigInteger(2);
}

var preferredMaxPriorityFeePerGas = maxPriorityFeePerGas.Value * 10 / 9;
var maxFeePerGas = (baseBlockFee * 2) + preferredMaxPriorityFeePerGas;

if (withBump)
{
maxFeePerGas *= 10 / 9;
}

return (maxFeePerGas, preferredMaxPriorityFeePerGas);
}
catch
{
return (gasPrice, gasPrice);
}
}

public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transaction, bool withBump = true)
{
var rpc = ThirdwebRPC.GetRpcInstance(transaction._client, transaction.Input.ChainId.Value);
Expand Down Expand Up @@ -145,10 +184,20 @@ public static async Task<string> Send(ThirdwebTransaction transaction)
transaction.Input.From ??= await transaction._wallet.GetAddress();
transaction.Input.Value ??= new HexBigInteger(0);
transaction.Input.Data ??= "0x";
transaction.Input.MaxFeePerGas = null;
transaction.Input.MaxPriorityFeePerGas = null;
transaction.Input.Gas ??= new HexBigInteger(await EstimateGasLimit(transaction));
transaction.Input.GasPrice ??= new HexBigInteger(await EstimateGasPrice(transaction));

if (transaction.Input.GasPrice == null)
{
(var maxFee, var maxPrio) = await EstimateGasFees(transaction);
transaction.Input.MaxFeePerGas ??= new HexBigInteger(maxFee);
transaction.Input.MaxPriorityFeePerGas ??= new HexBigInteger(maxPrio);
}
else
{
transaction.Input.MaxFeePerGas = null;
transaction.Input.MaxPriorityFeePerGas = null;
transaction.Input.GasPrice ??= new HexBigInteger(await EstimateGasPrice(transaction));
}

var rpc = ThirdwebRPC.GetRpcInstance(transaction._client, transaction.Input.ChainId.Value);
string hash;
Expand Down
Loading