Skip to content

Commit 0921ede

Browse files
committed
Gas limit estimation
1 parent 84c033e commit 0921ede

7 files changed

Lines changed: 159 additions & 5 deletions

File tree

Packages/com.walletconnect.web3modal/Runtime/Evm/EvmService.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,30 @@ public Task<string> SendTransactionAsync(string addressTo, BigInteger value, str
109109

110110
return SendTransactionAsyncCore(addressTo, value, data);
111111
}
112+
113+
114+
// -- Estimate Gas --------------------------------------------
115+
116+
public Task<BigInteger> EstimateGasAsync(string addressTo, BigInteger value, string data = null)
117+
{
118+
if (string.IsNullOrWhiteSpace(addressTo))
119+
throw new ArgumentNullException(nameof(addressTo));
112120

121+
return EstimateGasAsyncCore(addressTo, value, data);
122+
}
123+
124+
public Task<BigInteger> EstimateGasAsync(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
125+
{
126+
if (string.IsNullOrWhiteSpace(contractAddress))
127+
throw new ArgumentNullException(nameof(contractAddress));
128+
if (string.IsNullOrWhiteSpace(contractAbi))
129+
throw new ArgumentNullException(nameof(contractAbi));
130+
if (string.IsNullOrWhiteSpace(methodName))
131+
throw new ArgumentNullException(nameof(methodName));
132+
133+
return EstimateGasAsyncCore(contractAddress, contractAbi, methodName, value, arguments);
134+
}
135+
113136
protected abstract Task InitializeAsyncCore();
114137
protected abstract Task<BigInteger> GetBalanceAsyncCore(string address);
115138
protected abstract Task<string> SignMessageAsyncCore(string message);
@@ -119,5 +142,7 @@ public Task<string> SendTransactionAsync(string addressTo, BigInteger value, str
119142
protected abstract Task<TReturn> ReadContractAsyncCore<TReturn>(string contractAddress, string contractAbi, string methodName, object[] arguments = null);
120143
protected abstract Task<string> WriteContractAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, BigInteger gas = default, params object[] arguments);
121144
protected abstract Task<string> SendTransactionAsyncCore(string addressTo, BigInteger value, string data = null);
145+
protected abstract Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null);
146+
protected abstract Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments);
122147
}
123148
}

Packages/com.walletconnect.web3modal/Runtime/Evm/NethereumEvmService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
using System;
12
using System.Numerics;
2-
using System.Threading;
33
using System.Threading.Tasks;
4+
using Nethereum.Contracts;
45
using Nethereum.Hex.HexConvertors.Extensions;
56
using Nethereum.RPC.Eth.DTOs;
67
using Nethereum.Signer;
@@ -123,7 +124,7 @@ protected override async Task<string> WriteContractAsyncCore(string contractAddr
123124
{
124125
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
125126
var function = contract.GetFunction(methodName);
126-
127+
127128
return await function.SendTransactionAsync(
128129
null, // will be automatically filled by interceptor
129130
new HexBigInteger(gas),
@@ -140,5 +141,23 @@ protected override Task<string> SendTransactionAsyncCore(string addressTo, BigIn
140141
var transactionInput = new TransactionInput(data, addressTo, new HexBigInteger(value));
141142
return Web3.Client.SendRequestAsync<string>("eth_sendTransaction", null, transactionInput);
142143
}
144+
145+
146+
// -- Estimate Gas ---------------------------------------------
147+
148+
protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
149+
{
150+
var transactionInput = new TransactionInput(data, addressTo, new HexBigInteger(value));
151+
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
152+
}
153+
154+
protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
155+
{
156+
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
157+
var function = contract.GetFunction(methodName);
158+
159+
var transactionInput = new TransactionInput(function.GetData(arguments), contractAddress, new HexBigInteger(value));
160+
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
161+
}
143162
}
144163
}

Packages/com.walletconnect.web3modal/Runtime/Evm/WagmiEvmService.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System.Numerics;
22
using System.Threading.Tasks;
3+
using Nethereum.ABI.FunctionEncoding;
4+
using Nethereum.ABI.Model;
5+
using Nethereum.Contracts;
6+
using UnityEngine;
37
using WalletConnect.Web3Modal.WebGl.Wagmi;
48

59
namespace WalletConnect.Web3Modal
@@ -30,7 +34,7 @@ protected override Task<bool> VerifyMessageSignatureAsyncCore(string address, st
3034

3135
protected override Task<string> SignTypedDataAsyncCore(string dataJson)
3236
{
33-
return WagmiInterop.SignTypedDataAsync(dataJson);;
37+
return WagmiInterop.SignTypedDataAsync(dataJson);
3438
}
3539

3640
protected override Task<bool> VerifyTypedDataSignatureAsyncCore(string address, string dataJson, string signature)
@@ -52,6 +56,26 @@ protected override Task<string> SendTransactionAsyncCore(string addressTo, BigIn
5256
{
5357
return WagmiInterop.SendTransactionAsync(addressTo, value.ToString(), data);
5458
}
59+
60+
protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
61+
{
62+
var result = await WagmiInterop.EstimateGasAsync(addressTo, value.ToString(), data);
63+
return BigInteger.Parse(result);
64+
}
65+
66+
protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
67+
{
68+
var contract = new ContractBuilder(contractAbi, contractAddress);
69+
var function = contract.GetFunctionAbi(methodName);
70+
71+
var functionBuilder = new FunctionBuilder(contractAddress, function);
72+
var data = functionBuilder.GetData(arguments);
73+
74+
Debug.Log($"Wagmi data: {data}");
75+
76+
var result = await WagmiInterop.EstimateGasAsync(contractAddress, value.ToString(), data);
77+
return BigInteger.Parse(result);
78+
}
5579
}
5680
#endif
5781
}

Packages/com.walletconnect.web3modal/Runtime/WebGL/Wagmi/WagmiInterop.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ public static Task<string> SendTransactionAsync(SendTransactionParameter paramet
238238
{
239239
return InteropCallAsync<SendTransactionParameter, string>(WagmiMethods.SendTransaction, parameter);
240240
}
241+
242+
243+
// -- Estimate Gas --------------------------------------------
244+
245+
public static Task<string> EstimateGasAsync(string to, string value = "0", string data = null)
246+
{
247+
var parameter = new EstimateGasParameter
248+
{
249+
to = to,
250+
value = value,
251+
data = data
252+
};
253+
254+
return InteropCallAsync<EstimateGasParameter, string>(WagmiMethods.EstimateGas, parameter);
255+
}
241256
}
242257
#endif
243258
}

Packages/com.walletconnect.web3modal/Runtime/WebGL/Wagmi/WagmiModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,12 @@ public struct AbiParam
124124
public string name;
125125
public string type;
126126
}
127+
128+
[Serializable]
129+
public class EstimateGasParameter
130+
{
131+
public string to;
132+
public string value;
133+
public string data;
134+
}
127135
}

Samples/AppKit Sample/Assets/Scripts/AppKitInit.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,37 @@ namespace WalletConnect.Web3Modal.Sample
88
public class AppKitInit : MonoBehaviour
99
{
1010
[SerializeField] private SceneReference _menuScene;
11+
12+
private WalletConnectUnity.Core.Chain currentChain = new WalletConnectUnity.Core.Chain(
13+
chainNamespace: "eip155",
14+
chainReference: "97",
15+
name: "Binance Smart Chain Testnet",
16+
viemName: "bscTestnet",
17+
nativeCurrency: new WalletConnectUnity.Core.Currency
18+
(
19+
"Binance Coin",
20+
"tBNB",
21+
18
22+
),
23+
blockExplorer: new WalletConnectUnity.Core.BlockExplorer
24+
("BscScan", "https://testnet.bscscan.com"),
25+
rpcUrl: "https://data-seed-prebsc-1-s1.bnbchain.org:8545/",
26+
isTestnet: true,
27+
imageUrl: "https://cryptologos.cc/logos/binance-coin-bnb-logo.png" // Example image URL for Binance Coin
28+
);
1129

1230
private async void Start()
1331
{
1432
Debug.Log($"[AppKit Init] Initializing AppKit...");
15-
await Web3Modal.InitializeAsync();
33+
await Web3Modal.InitializeAsync(new Web3ModalConfig
34+
{
35+
supportedChains = new[]
36+
{
37+
currentChain
38+
}
39+
});
40+
41+
// await Web3Modal.InitializeAsync();
1642

1743
var wc = WalletConnectConnector.WalletConnectInstance;
1844
if (wc is { IsInitialized: true })

Samples/AppKit Sample/Assets/Scripts/Dapp.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ private void BuildButtons()
8282
}
8383
},
8484
new ButtonStruct
85+
{
86+
Text = "Write Contract",
87+
OnClick = OnWriteContractClicked,
88+
AccountRequired = true
89+
},
90+
new ButtonStruct
8591
{
8692
Text = "Disconnect",
8793
OnClick = OnDisconnectButton,
@@ -212,9 +218,14 @@ public async void OnPersonalSignButton()
212218
var account = await Web3Modal.GetAccountAsync();
213219

214220
const string message = "Hello from Unity!";
221+
222+
Debug.Log("Signing message...");
215223
var signature = await Web3Modal.Evm.SignMessageAsync(message);
216-
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);
217224

225+
Debug.Log("Verifying signature...");
226+
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);
227+
228+
Debug.Log($"Signature valid: {isValid}");
218229
Notification.ShowMessage($"Signature valid: {isValid}");
219230
}
220231
catch (RpcResponseException e)
@@ -355,6 +366,32 @@ public async void OnReadContractClicked()
355366
}
356367
}
357368

369+
public async void OnWriteContractClicked()
370+
{
371+
const string contractAddress = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984";
372+
const string recipientAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
373+
const string abi = @"[{""constant"":false,""inputs"":[{""name"":""to"",""type"":""address""},{""name"":""value"",""type"":""uint256""}],""name"":""transfer"",""outputs"":[{""name"":"""",""type"":""bool""}],""payable"":false,""stateMutability"":""nonpayable"",""type"":""function""}]";
374+
375+
BigInteger amount = 1;
376+
377+
Debug.Log("Let's estimate gas...");
378+
379+
var gas = await Web3Modal.Evm.EstimateGasAsync(contractAddress, abi, "transfer", value: default, recipientAddress, amount);
380+
381+
Debug.Log($"Estimated gas: {gas}");
382+
383+
384+
385+
// // Arguments for the transfer method. The order of the arguments must match the order in the method signature.
386+
// // Method signature: `function transfer(address _to, uint256 _value) public returns (bool success)`
387+
// var arguments = new object[]
388+
// {
389+
// recipientAddress,
390+
// amount
391+
// };
392+
// var result = await Web3Modal.Evm.WriteContractAsync(contractAddress, abi, "transfer", arguments);
393+
}
394+
358395
private TypedData<Domain> GetMailTypedDefinition()
359396
{
360397
return new TypedData<Domain>

0 commit comments

Comments
 (0)