Skip to content

Commit 0e367ce

Browse files
authored
Override and improve gas fees when not provided (#163)
1 parent fc36949 commit 0e367ce

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

Assets/Thirdweb/Core/Scripts/Contract.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,30 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
213213

214214
var gasPrice = transactionOverrides?.gasPrice != null ? new HexBigInteger(BigInteger.Parse(transactionOverrides?.gasPrice)) : null;
215215

216-
var hash = await function.SendTransactionAsync(
217-
from: transactionOverrides?.from ?? await ThirdwebManager.Instance.SDK.wallet.GetAddress(),
218-
gas: gas,
219-
gasPrice: gasPrice,
220-
value: value,
221-
args
222-
);
216+
string hash;
217+
if (gasPrice == null)
218+
{
219+
var gasFees = await Utils.GetGasPriceAsync(await ThirdwebManager.Instance.SDK.wallet.GetChainId());
220+
hash = await function.SendTransactionAsync(
221+
from: transactionOverrides?.from ?? await ThirdwebManager.Instance.SDK.wallet.GetAddress(),
222+
gas: gas,
223+
value: value,
224+
maxFeePerGas: new HexBigInteger(gasFees.MaxFeePerGas),
225+
maxPriorityFeePerGas: new HexBigInteger(gasFees.MaxPriorityFeePerGas),
226+
args
227+
);
228+
}
229+
else
230+
{
231+
hash = await function.SendTransactionAsync(
232+
from: transactionOverrides?.from ?? await ThirdwebManager.Instance.SDK.wallet.GetAddress(),
233+
gas: gas,
234+
gasPrice: gasPrice,
235+
value: value,
236+
args
237+
);
238+
}
239+
223240
return await Transaction.WaitForTransactionResult(hash);
224241
}
225242
}

Assets/Thirdweb/Core/Scripts/TransactionManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public static async Task<TransactionReceipt> ThirdwebWriteRawResult<TWFunction>(
9494

9595
if (!isGasless)
9696
{
97+
if (functionMessage.GasPrice == null && functionMessage.MaxFeePerGas == null && functionMessage.MaxPriorityFeePerGas == null)
98+
{
99+
var gasPrice = await Utils.GetGasPriceAsync(await ThirdwebManager.Instance.SDK.wallet.GetChainId());
100+
functionMessage.GasPrice = gasPrice.MaxFeePerGas;
101+
functionMessage.MaxFeePerGas = gasPrice.MaxFeePerGas;
102+
functionMessage.MaxPriorityFeePerGas = gasPrice.MaxPriorityFeePerGas;
103+
}
104+
97105
if (
98106
ThirdwebManager.Instance.SDK.session.ActiveWallet.GetSignerProvider() == WalletProvider.LocalWallet
99107
&& ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet

0 commit comments

Comments
 (0)