Skip to content

Commit fe6a14c

Browse files
authored
Merge pull request #27 from WalletConnect/feature/gas-estimation
2 parents 508036d + d9a4f9b commit fe6a14c

9 files changed

Lines changed: 164 additions & 13 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
with:
6060
fetch-depth: 0
6161
- name: Download WebGL Build Artifact
62-
uses: actions/download-artifact@v4.1.7
62+
uses: actions/download-artifact@v3
6363
with:
6464
name: Build-WebGL
6565
path: Build/WebGL

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

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

110110
return SendTransactionAsyncCore(addressTo, value, data);
111111
}
112+
113+
114+
// -- Send Raw Transaction ------------------------------------
115+
116+
public Task<string> SendRawTransactionAsync(string signedTransaction)
117+
{
118+
if (string.IsNullOrWhiteSpace(signedTransaction))
119+
throw new ArgumentNullException(nameof(signedTransaction));
120+
121+
return SendRawTransactionAsyncCore(signedTransaction);
122+
}
123+
124+
125+
// -- Estimate Gas --------------------------------------------
126+
127+
public Task<BigInteger> EstimateGasAsync(string addressTo, BigInteger value, string data = null)
128+
{
129+
if (string.IsNullOrWhiteSpace(addressTo))
130+
throw new ArgumentNullException(nameof(addressTo));
112131

132+
return EstimateGasAsyncCore(addressTo, value, data);
133+
}
134+
135+
public Task<BigInteger> EstimateGasAsync(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
136+
{
137+
if (string.IsNullOrWhiteSpace(contractAddress))
138+
throw new ArgumentNullException(nameof(contractAddress));
139+
if (string.IsNullOrWhiteSpace(contractAbi))
140+
throw new ArgumentNullException(nameof(contractAbi));
141+
if (string.IsNullOrWhiteSpace(methodName))
142+
throw new ArgumentNullException(nameof(methodName));
143+
144+
return EstimateGasAsyncCore(contractAddress, contractAbi, methodName, value, arguments);
145+
}
146+
147+
148+
// -- Gas Price ------------------------------------------------
149+
150+
public Task<BigInteger> GetGasPriceAsync()
151+
{
152+
return GetGasPriceAsyncCore();
153+
}
154+
113155
protected abstract Task InitializeAsyncCore();
114156
protected abstract Task<BigInteger> GetBalanceAsyncCore(string address);
115157
protected abstract Task<string> SignMessageAsyncCore(string message);
@@ -119,5 +161,9 @@ public Task<string> SendTransactionAsync(string addressTo, BigInteger value, str
119161
protected abstract Task<TReturn> ReadContractAsyncCore<TReturn>(string contractAddress, string contractAbi, string methodName, object[] arguments = null);
120162
protected abstract Task<string> WriteContractAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, BigInteger gas = default, params object[] arguments);
121163
protected abstract Task<string> SendTransactionAsyncCore(string addressTo, BigInteger value, string data = null);
164+
protected abstract Task<string> SendRawTransactionAsyncCore(string signedTransaction);
165+
protected abstract Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null);
166+
protected abstract Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments);
167+
protected abstract Task<BigInteger> GetGasPriceAsyncCore();
122168
}
123169
}

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

Lines changed: 38 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,40 @@ 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+
// -- Send Raw Transaction ------------------------------------
147+
148+
protected override Task<string> SendRawTransactionAsyncCore(string signedTransaction)
149+
{
150+
return Web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(signedTransaction);
151+
}
152+
153+
154+
// -- Estimate Gas ---------------------------------------------
155+
156+
protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
157+
{
158+
var transactionInput = new TransactionInput(data, addressTo, new HexBigInteger(value));
159+
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
160+
}
161+
162+
protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
163+
{
164+
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
165+
var function = contract.GetFunction(methodName);
166+
167+
var transactionInput = new TransactionInput(function.GetData(arguments), contractAddress, new HexBigInteger(value));
168+
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
169+
}
170+
171+
172+
// -- Get Gas Price -------------------------------------------
173+
174+
protected override async Task<BigInteger> GetGasPriceAsyncCore()
175+
{
176+
var hexBigInt = await Web3.Eth.GasPrice.SendRequestAsync();
177+
return hexBigInt.Value;
178+
}
143179
}
144180
}

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

Lines changed: 34 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,35 @@ protected override Task<string> SendTransactionAsyncCore(string addressTo, BigIn
5256
{
5357
return WagmiInterop.SendTransactionAsync(addressTo, value.ToString(), data);
5458
}
59+
60+
protected override Task<string> SendRawTransactionAsyncCore(string signedTransaction)
61+
{
62+
throw new System.NotImplementedException();
63+
}
64+
65+
protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
66+
{
67+
var result = await WagmiInterop.EstimateGasAsync(addressTo, value.ToString(), data);
68+
return BigInteger.Parse(result);
69+
}
70+
71+
protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
72+
{
73+
var contract = new ContractBuilder(contractAbi, contractAddress);
74+
var function = contract.GetFunctionAbi(methodName);
75+
76+
var functionBuilder = new FunctionBuilder(contractAddress, function);
77+
var data = functionBuilder.GetData(arguments);
78+
79+
var result = await WagmiInterop.EstimateGasAsync(contractAddress, value.ToString(), data);
80+
return BigInteger.Parse(result);
81+
}
82+
83+
protected override async Task<BigInteger> GetGasPriceAsyncCore()
84+
{
85+
var result = await WagmiInterop.GetGasPriceAsync();
86+
return BigInteger.Parse(result);
87+
}
5588
}
5689
#endif
5790
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,29 @@ 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+
}
256+
257+
258+
// -- Get Gas Price -------------------------------------------
259+
260+
public static Task<string> GetGasPriceAsync()
261+
{
262+
return InteropCallAsync<object, string>(WagmiMethods.GetGasPrice, null);
263+
}
241264
}
242265
#endif
243266
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace WalletConnect.Web3Modal.Sample
88
public class AppKitInit : MonoBehaviour
99
{
1010
[SerializeField] private SceneReference _menuScene;
11-
11+
1212
private async void Start()
1313
{
1414
Debug.Log($"[AppKit Init] Initializing AppKit...");
1515
await Web3Modal.InitializeAsync();
16-
16+
1717
var wc = WalletConnectConnector.WalletConnectInstance;
1818
if (wc is { IsInitialized: true })
1919
{

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,14 @@ public async void OnPersonalSignButton()
212212
var account = await Web3Modal.GetAccountAsync();
213213

214214
const string message = "Hello from Unity!";
215+
216+
Debug.Log("Signing message...");
215217
var signature = await Web3Modal.Evm.SignMessageAsync(message);
216-
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);
217218

219+
Debug.Log("Verifying signature...");
220+
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);
221+
222+
Debug.Log($"Signature valid: {isValid}");
218223
Notification.ShowMessage($"Signature valid: {isValid}");
219224
}
220225
catch (RpcResponseException e)

Samples/AppKit Sample/Packages/packages-lock.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@
164164
"depth": 1,
165165
"source": "registry",
166166
"dependencies": {
167-
"com.unity.2d.sprite": "1.0.0",
168167
"com.unity.ugui": "1.0.0",
169-
"com.unity.modules.animation": "1.0.0",
170-
"com.unity.modules.imageconversion": "1.0.0",
168+
"com.unity.2d.sprite": "1.0.0",
169+
"com.unity.modules.ui": "1.0.0",
171170
"com.unity.modules.physics": "1.0.0",
171+
"com.unity.modules.animation": "1.0.0",
172172
"com.unity.modules.physics2d": "1.0.0",
173-
"com.unity.modules.ui": "1.0.0",
174173
"com.unity.modules.uielements": "1.0.0",
174+
"com.unity.modules.imageconversion": "1.0.0",
175175
"com.unity.modules.unitywebrequest": "1.0.0",
176-
"com.unity.modules.unitywebrequesttexture": "1.0.0",
177-
"com.unity.modules.unitywebrequestwww": "1.0.0"
176+
"com.unity.modules.unitywebrequestwww": "1.0.0",
177+
"com.unity.modules.unitywebrequesttexture": "1.0.0"
178178
},
179179
"url": "https://packages.unity.com"
180180
},

0 commit comments

Comments
 (0)