reown-com / reown-dotnet

Toolkits to build onchain UX
https://docs.reown.com/
Apache License 2.0
8 stars 1 forks source link

UNITY Object reference not set to an instance of an object at AppKit.Evm.GetBalanceAsync #12

Closed shekaryf closed 3 weeks ago

shekaryf commented 3 weeks ago

I'm using this document: https://docs.reown.com/appkit/unity/core/usage I did installation completely; Now I'm starting to get balance here:

Account account = await AppKit.GetAccountAsync();
BigInteger balance = await AppKit.Evm.GetBalanceAsync(account.Address);

Debug.Log($"Balance: {Web3.Convert.FromWei(balance.Value)} ETH");

I see this error, now:

NullReferenceException: Object reference not set to an instance of an object
Reown.AppKit.Unity.NethereumEvmService.GetBalanceAsyncCore (System.String address) (at ./Library/PackageCache/com.reown.appkit.unity@1.0.0/Runtime/Evm/NethereumEvmService.cs:67)

My unity version is 2022.3.5f1 Any help? thanks

shekaryf commented 3 weeks ago

I changed the network and the problem solved

shekaryf commented 3 weeks ago

How can I use Sepolia test network, Is that possible?

skibitsky commented 3 weeks ago

@shekaryf do you get NullReferenceException when calling AppKit.Evm.GetBalanceAsync with Sepolia, but not with other networks?

shekaryf commented 3 weeks ago

Yes, I do

skibitsky commented 3 weeks ago

Could you please share the code of your Chain object for Sepolia chain? 🙏

shekaryf commented 3 weeks ago

You mean this? let me know if I'm wrong. I put this code after OpenModal():

            AppKit.ChainChanged += (sender, eventArgs) => {
                Chain newChain = eventArgs.Chain;
                Debug.Log("Chain Changed....");
                Debug.Log(newChain.ToString());
            };

Now I'm getting this error:

NullReferenceException: Object reference not set to an instance of an object
MapMenuDialog+<>c.<SetValues>b__16_7 (System.Object sender, Reown.AppKit.Unity.NetworkController+ChainChangedEventArgs eventArgs) (at Assets/Scripts/UI/Dialogs/MapMenuDialog.cs:225)
Reown.AppKit.Unity.NetworkController.OnChainChanged (Reown.AppKit.Unity.NetworkController+ChainChangedEventArgs e) (at ./Library/PackageCache/com.reown.appkit.unity@1.0.1/Runtime/Controllers/NetworkController/NetworkController.cs:72)
Reown.AppKit.Unity.NetworkControllerCore.ConnectorAccountConnectedHandlerCore (System.Object sender, Reown.AppKit.Unity.Connector+AccountConnectedEventArgs e) (at ./Library/PackageCache/com.reown.appkit.unity@1.0.1/Runtime/Controllers/NetworkController/NetworkControllerCore.cs:66)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <31687ccd371e4dc6b0c23a1317cf9474>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <6ff3bcb667574bf9a8630184172fcfbf>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <6ff3bcb667574bf9a8630184172fcfbf>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <6ff3bcb667574bf9a8630184172fcfbf>:0)

But In Ethereum Main Network I don't see this error!

skibitsky commented 3 weeks ago

Testnets aren't supported out of the box, you need to add them to the supportedChains array in the AppKitConfig. https://docs.reown.com/appkit/unity/core/options#supported-chains

Did you add Sepolia to the supportedChains? If so, could you share the code please?

shekaryf commented 3 weeks ago

I changed the code to

        await AppKit.InitializeAsync(new AppKitConfig(
            "aaaaaaaaaaaaaaa",
            new Metadata(
                "aaaaaaaaaaaaaaaaaa",
                "aaaaaaaaaaaaaaaaa",
                "https://mmmmm.net",
                "https://mmmmm.net/Logo.png"
            ))
        {
            supportedChains = new[]
            {
                ChainConstants.Chains.Ethereum,
                ChainConstants.Chains.Polygon,
                ChainConstants.Chains.Ronin,

                new Chain(ChainConstants.Namespaces.Evm,
                    chainReference: "11155111",
                    name: "Sepolia test network",
                    nativeCurrency: new Currency(),
                    blockExplorer: new BlockExplorer(),
                    rpcUrl: "wss://sepolia.drpc.org ",
                    isTestnet: true,
                    imageUrl: $"https://chainlist.org/unknown-logo.png",
                    viemName: "Sepoliatestnetwork" // required only on WebGL
                )
            }
        });

And it works fine thank you

skibitsky commented 3 weeks ago

Nice! Could you help me understand what caused Sepolia not to work for you earlier? Did you add it to the supportedChains, or were you using a different RPC URL?

shekaryf commented 3 weeks ago

No, I didn't use supportedChains in InitializeAsync Now I use supportedChains and I got the balance Thanks