Skip to content

Commit 41e9623

Browse files
[GD-55] Add gas estimation function (#2)
* Add gas estimation function * Fix example
1 parent 47013bb commit 41e9623

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Assets/AnkrSDK/Examples/UseCases/UpdateNFT/UpdateNftExample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public async void UpdateNFT()
6060
Debug.Log($"Receipt: {receipt}");
6161
}
6262

63+
public async void EstimateGas()
64+
{
65+
var info = await RequestPreparedParams(0);
66+
var gasEstimation = await _contract.EstimateGas("updateTokenWithSignedMessage", new object[] {info});
67+
Debug.Log("Gas estimation = "+gasEstimation);
68+
}
69+
6370
private async Task<ItemInfo> RequestPreparedParams(int tokenId)
6471
{
6572
var request = UnityWebRequest.Get(URL + $"hero/{tokenId}");

Assets/AnkrSDK/Scripts/Core/Implementation/Contract.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Cysharp.Threading.Tasks;
99
using Nethereum.ABI.FunctionEncoding.Attributes;
1010
using Nethereum.Contracts;
11+
using Nethereum.Hex.HexTypes;
1112
using Nethereum.RPC.Eth.DTOs;
1213
using Nethereum.Web3;
1314

@@ -108,5 +109,18 @@ private TransactionInput CreateTransactionInput(string methodName, object[] argu
108109
var callFunction = contract.GetFunction(methodName);
109110
return callFunction.CreateTransactionInput(activeSessionAccount, arguments);
110111
}
112+
113+
114+
public Task<HexBigInteger> EstimateGas(string methodName, object[] arguments = null, string gas = null,
115+
string gasPrice = null, string nonce = null)
116+
{
117+
var transactionInput = CreateTransactionInput(methodName, arguments);
118+
119+
transactionInput.Gas = gas != null ? new HexBigInteger(gas) : null;
120+
transactionInput.GasPrice = gasPrice != null ? new HexBigInteger(gasPrice) : null;
121+
transactionInput.Nonce = nonce != null ? new HexBigInteger(nonce) : null;
122+
123+
return _web3Provider.TransactionManager.EstimateGasAsync(transactionInput);
124+
}
111125
}
112126
}

Assets/AnkrSDK/Scripts/Core/Infrastructure/IContract.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using AnkrSDK.Core.Events;
55
using Nethereum.ABI.FunctionEncoding.Attributes;
66
using Nethereum.Contracts;
7+
using Nethereum.Hex.HexTypes;
78

89
namespace AnkrSDK.Core.Infrastructure
910
{
@@ -20,5 +21,8 @@ Task<List<EventLog<TEvDto>>> GetAllChanges<TEvDto>(EventFilterData evFilter = nu
2021

2122
Task<TReturnType> GetData<TFieldData, TReturnType>(TFieldData requestData = null)
2223
where TFieldData : FunctionMessage, new();
24+
25+
Task<HexBigInteger> EstimateGas(string methodName, object[] arguments = null, string gas = null,
26+
string gasPrice = null, string nonce = null);
2327
}
2428
}

0 commit comments

Comments
 (0)