Closed laureveler closed 10 months ago
WalletConnect protocol does technically support this RPC however, this requires the dApp and wallet to agree on allowing this method(s) via required and optional namespaces. And according to @pedrouid
no wallet that (I know of) accepts [them] (
eth_getBalance
)
Interesting - I do have this working in editor on a version of the demo app from a few months ago ? This was WCV2, when the demo app would only work in editor (so this is only editor tested). I was connecting with a Metamask wallet on the Mumbai Testnet. I did run into problems when attempting the request more than once, but the 1st request yields a result (and still does - I've just re-tested).
[RpcMethod("eth_getBalance"), RpcRequestOptions(Clock.ONE_MINUTE, 99998)]
public class EthGetBalance : List<string>
{
public EthGetBalance(string address, BigInteger? blockNumber = null) : base(new[] { address, blockNumber != null ? blockNumber.ToString() : "latest" }) { }
}
public IEnumerator GetBalanceRoutine(Action<float> onComplete)
{
Debug.Log("Getting balance routine started");
_task = WCSignClient.Instance.Request<EthGetBalance, string>(_wallet.Session.Topic, _request, _wallet.ChainId);
_wait = new WaitForTaskResult<string>(_task);
yield return _wait;
Debug.Log(_task.Result);
_longResult = Convert.ToInt64(_task.Result, 16);
Debug.Log(_longResult);
_latestBalance = _longResult / _divisor;
onComplete?.Invoke(_latestBalance);
Debug.Log("Got balance: " + _latestBalance);
}
Gives me the following result:
Getting balance routine started
UnityEngine.Debug:Log (object)
WalletBalance/<GetBalanceRoutine>d__12:MoveNext () (at Assets/Scripts/WalletBalance.cs:53)
0xc7c15bea0ea74a8
UnityEngine.Debug:Log (object)
WalletBalance/<GetBalanceRoutine>d__12:MoveNext () (at Assets/Scripts/WalletBalance.cs:59)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
899617934054945960
UnityEngine.Debug:Log (object)
WalletBalance/<GetBalanceRoutine>d__12:MoveNext () (at Assets/Scripts/WalletBalance.cs:61)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
Got balance: 0.899618
UnityEngine.Debug:Log (object)
WalletBalance/<GetBalanceRoutine>d__12:MoveNext () (at Assets/Scripts/WalletBalance.cs:65)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
I'm going to double check using this exact code in the newer version in the coming days/week, hopefully (in writing this post I've dug out the old project where I had it working).
Yes, so using the same code as above in the newest version, I get the following error:
WalletConnectException: Missing or invalid parameter
WalletConnectSharp.Sign.Engine.WalletConnectSharp.Sign.Interfaces.IEnginePrivate.IsValidRequest[T] (System.String topic, WalletConnectSharp.Network.Models.JsonRpcRequest`1[T] request, System.String chainId) (at <3727d9dac3144b508fbc52930398e78a>:0)
WalletConnectSharp.Sign.Engine.Request[T,TR] (System.String topic, T data, System.String chainId, System.Nullable`1[T] expiry) (at <3727d9dac3144b508fbc52930398e78a>:0)
Rethrow as AggregateException: One or more errors occurred. (Missing or invalid parameter)
Any updates on when a fix for this bug might be coming?
Most wallets still don't support eth_getBalance
. We recommend sending this request directly to RPC node. Added explanation and code sample to the WalletConnect Modal docs.
tl;dr
Use this code to call eth_getBalance
private static async Task GetAccountBalance()
{
var session = WalletConnect.Instance.ActiveSession;
// Because one session can have multiple namespaces, we need to select one.
// In most cases, especially in games, dapp will use only one namespace.
var @namespace = session.RequiredNamespaces.First();
var address = session.CurrentAddress(@namespace.Key).Address;
var config = ProjectConfiguration.Load();
// Using WalletConnect Blockchain API: https://docs.walletconnect.com/cloud/blockchain-api
var url = $"https://rpc.walletconnect.com/v1?chainId={@namespace.Value.Chains[0]}&projectId={config.Id}";
var web3 = new Nethereum.Web3.Web3(url);
var balance = await web3.Eth.GetBalance.SendRequestAsync(address);
Debug.Log($"Balance of {address} in Wei: {balance.Value}");
var etherAmount = Nethereum.Web3.Web3.Convert.FromWei(balance.Value);
Debug.Log($"Balance of {address} in Ether: {etherAmount}");
}
I had this working on v2 prior to the recent updates. Pseudo-code:
Now the await statement never ends. Anyone had any luck with this?