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

feat: gas estimation #27

Merged
merged 5 commits into from
Sep 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
with:
fetch-depth: 0
- name: Download WebGL Build Artifact
uses: actions/download-artifact@v4.1.7
uses: actions/download-artifact@v3
with:
name: Build-WebGL
path: Build/WebGL
Expand Down
46 changes: 46 additions & 0 deletions Packages/com.walletconnect.web3modal/Runtime/Evm/EvmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,49 @@ public Task<string> SendTransactionAsync(string addressTo, BigInteger value, str

return SendTransactionAsyncCore(addressTo, value, data);
}


// -- Send Raw Transaction ------------------------------------

public Task<string> SendRawTransactionAsync(string signedTransaction)
{
if (string.IsNullOrWhiteSpace(signedTransaction))
throw new ArgumentNullException(nameof(signedTransaction));

return SendRawTransactionAsyncCore(signedTransaction);
}


// -- Estimate Gas --------------------------------------------

public Task<BigInteger> EstimateGasAsync(string addressTo, BigInteger value, string data = null)
{
if (string.IsNullOrWhiteSpace(addressTo))
throw new ArgumentNullException(nameof(addressTo));

return EstimateGasAsyncCore(addressTo, value, data);
}

public Task<BigInteger> EstimateGasAsync(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
{
if (string.IsNullOrWhiteSpace(contractAddress))
throw new ArgumentNullException(nameof(contractAddress));
if (string.IsNullOrWhiteSpace(contractAbi))
throw new ArgumentNullException(nameof(contractAbi));
if (string.IsNullOrWhiteSpace(methodName))
throw new ArgumentNullException(nameof(methodName));

return EstimateGasAsyncCore(contractAddress, contractAbi, methodName, value, arguments);
}


// -- Gas Price ------------------------------------------------

public Task<BigInteger> GetGasPriceAsync()
{
return GetGasPriceAsyncCore();
}

protected abstract Task InitializeAsyncCore();
protected abstract Task<BigInteger> GetBalanceAsyncCore(string address);
protected abstract Task<string> SignMessageAsyncCore(string message);
Expand All @@ -119,5 +161,9 @@ public Task<string> SendTransactionAsync(string addressTo, BigInteger value, str
protected abstract Task<TReturn> ReadContractAsyncCore<TReturn>(string contractAddress, string contractAbi, string methodName, object[] arguments = null);
protected abstract Task<string> WriteContractAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, BigInteger gas = default, params object[] arguments);
protected abstract Task<string> SendTransactionAsyncCore(string addressTo, BigInteger value, string data = null);
protected abstract Task<string> SendRawTransactionAsyncCore(string signedTransaction);
protected abstract Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null);
protected abstract Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments);
protected abstract Task<BigInteger> GetGasPriceAsyncCore();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using Nethereum.Contracts;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Signer;
Expand Down Expand Up @@ -123,7 +124,7 @@ protected override async Task<string> WriteContractAsyncCore(string contractAddr
{
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
var function = contract.GetFunction(methodName);

return await function.SendTransactionAsync(
null, // will be automatically filled by interceptor
new HexBigInteger(gas),
Expand All @@ -140,5 +141,40 @@ protected override Task<string> SendTransactionAsyncCore(string addressTo, BigIn
var transactionInput = new TransactionInput(data, addressTo, new HexBigInteger(value));
return Web3.Client.SendRequestAsync<string>("eth_sendTransaction", null, transactionInput);
}


// -- Send Raw Transaction ------------------------------------

protected override Task<string> SendRawTransactionAsyncCore(string signedTransaction)
{
return Web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(signedTransaction);
}


// -- Estimate Gas ---------------------------------------------

protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
{
var transactionInput = new TransactionInput(data, addressTo, new HexBigInteger(value));
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
}

protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
{
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
var function = contract.GetFunction(methodName);

var transactionInput = new TransactionInput(function.GetData(arguments), contractAddress, new HexBigInteger(value));
return await Web3.Eth.Transactions.EstimateGas.SendRequestAsync(transactionInput);
}


// -- Get Gas Price -------------------------------------------

protected override async Task<BigInteger> GetGasPriceAsyncCore()
{
var hexBigInt = await Web3.Eth.GasPrice.SendRequestAsync();
return hexBigInt.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Numerics;
using System.Threading.Tasks;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.ABI.Model;
using Nethereum.Contracts;
using UnityEngine;
using WalletConnect.Web3Modal.WebGl.Wagmi;

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

protected override Task<string> SignTypedDataAsyncCore(string dataJson)
{
return WagmiInterop.SignTypedDataAsync(dataJson);;
return WagmiInterop.SignTypedDataAsync(dataJson);
}

protected override Task<bool> VerifyTypedDataSignatureAsyncCore(string address, string dataJson, string signature)
Expand All @@ -52,6 +56,35 @@ protected override Task<string> SendTransactionAsyncCore(string addressTo, BigIn
{
return WagmiInterop.SendTransactionAsync(addressTo, value.ToString(), data);
}

protected override Task<string> SendRawTransactionAsyncCore(string signedTransaction)
{
throw new System.NotImplementedException();
}

protected override async Task<BigInteger> EstimateGasAsyncCore(string addressTo, BigInteger value, string data = null)
{
var result = await WagmiInterop.EstimateGasAsync(addressTo, value.ToString(), data);
return BigInteger.Parse(result);
}

protected override async Task<BigInteger> EstimateGasAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, params object[] arguments)
{
var contract = new ContractBuilder(contractAbi, contractAddress);
var function = contract.GetFunctionAbi(methodName);

var functionBuilder = new FunctionBuilder(contractAddress, function);
var data = functionBuilder.GetData(arguments);

var result = await WagmiInterop.EstimateGasAsync(contractAddress, value.ToString(), data);
return BigInteger.Parse(result);
}

protected override async Task<BigInteger> GetGasPriceAsyncCore()
{
var result = await WagmiInterop.GetGasPriceAsync();
return BigInteger.Parse(result);
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ public static Task<string> SendTransactionAsync(SendTransactionParameter paramet
{
return InteropCallAsync<SendTransactionParameter, string>(WagmiMethods.SendTransaction, parameter);
}


// -- Estimate Gas --------------------------------------------

public static Task<string> EstimateGasAsync(string to, string value = "0", string data = null)
{
var parameter = new EstimateGasParameter
{
to = to,
value = value,
data = data
};

return InteropCallAsync<EstimateGasParameter, string>(WagmiMethods.EstimateGas, parameter);
}


// -- Get Gas Price -------------------------------------------

public static Task<string> GetGasPriceAsync()
{
return InteropCallAsync<object, string>(WagmiMethods.GetGasPrice, null);
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,12 @@ public struct AbiParam
public string name;
public string type;
}

[Serializable]
public class EstimateGasParameter
{
public string to;
public string value;
public string data;
}
}
4 changes: 2 additions & 2 deletions Samples/AppKit Sample/Assets/Scripts/AppKitInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace WalletConnect.Web3Modal.Sample
public class AppKitInit : MonoBehaviour
{
[SerializeField] private SceneReference _menuScene;

private async void Start()
{
Debug.Log($"[AppKit Init] Initializing AppKit...");
await Web3Modal.InitializeAsync();

var wc = WalletConnectConnector.WalletConnectInstance;
if (wc is { IsInitialized: true })
{
Expand Down
7 changes: 6 additions & 1 deletion Samples/AppKit Sample/Assets/Scripts/Dapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,14 @@ public async void OnPersonalSignButton()
var account = await Web3Modal.GetAccountAsync();

const string message = "Hello from Unity!";

Debug.Log("Signing message...");
var signature = await Web3Modal.Evm.SignMessageAsync(message);
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);

Debug.Log("Verifying signature...");
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);

Debug.Log($"Signature valid: {isValid}");
Notification.ShowMessage($"Signature valid: {isValid}");
}
catch (RpcResponseException e)
Expand Down
12 changes: 6 additions & 6 deletions Samples/AppKit Sample/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.sprite": "1.0.0",
"com.unity.ugui": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.imageconversion": "1.0.0",
"com.unity.2d.sprite": "1.0.0",
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.physics": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.physics2d": "1.0.0",
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.uielements": "1.0.0",
"com.unity.modules.imageconversion": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.unitywebrequesttexture": "1.0.0",
"com.unity.modules.unitywebrequestwww": "1.0.0"
"com.unity.modules.unitywebrequestwww": "1.0.0",
"com.unity.modules.unitywebrequesttexture": "1.0.0"
},
"url": "https://packages.unity.com"
},
Expand Down
Loading