Nethereum / Nethereum.Unity

Nethereum.Unity 472/Netstandard package
MIT License
4 stars 3 forks source link

How to add new network to wallet if that is not there? #6

Open ammarvohra opened 2 months ago

ammarvohra commented 2 months ago

I want to add new network like how we add Custom Networks in Metamask but with the help of code. Also I want to import my NFT tokens to that particular network.

Logic I am looking to work as: I enter all the details(RPC, chain id, etc) of network and pass through WalletConnect (like we do for SendTransaction) and that should give me popup in wallet(let's say MetaMask) to add that network.

Question: I know this is possible on web but is it possible on Mobile? @juanfranblanco

Thanks, Ammar

ammarvohra commented 2 months ago

For those who are struggling to get this done I've managed to do it:

var chainNew = new WalletConnectUnity.Core.Evm.EthereumChain
{
    chainIdHex = "0xe9ac0ce",
    blockExplorerUrls = new string[] { "https://devnet.neonscan.org/" },
    name = "Neon EVM DevNet",
    //= new List<string>() { "https://chainid.network/static/de1fb488d8c046b95c067e0e50cfe35c/f31ef/neon.png" },
    nativeCurrency = new Currency("NEON", "NEON", 18),
    rpcUrls = new string[] { "https://devnet.neonevm.org/" }
};
WalletConnectUnity.Core.Evm.WalletAddEthereumChain walletAddEthereumChain = new WalletConnectUnity.Core.Evm.WalletAddEthereumChain
{
    chainNew
};
var connectNetwork = await WalletConnect.Instance.RequestAsync<WalletConnectUnity.Core.Evm.WalletAddEthereumChain, string>(walletAddEthereumChain);
ammarvohra commented 2 months ago

Now I need to add NFTs via

wallet_watchAsset

I've did this code:

WalletWatchParams watchParams = new WalletWatchParams();
watchParams.type = "ERC1155";
watchParams.options = new WalletWatchParamsOptions() { address = web3Params.ExternalAddress[0].Address, tokenId = "3" };
var watchTest = await WalletConnect.Instance.RequestAsync<WalletWatchParams, string>(watchParams);
[RpcMethod("wallet_watchAsset")]
[RpcRequestOptions(Clock.ONE_MINUTE, 99990)]
public class WalletWatchParams
{
    [JsonProperty(PropertyName = "type")]
    public string type = "1155";

    [JsonProperty(PropertyName = "options")]
    public WalletWatchParamsOptions options = new WalletWatchParamsOptions();

    [Preserve]
    public WalletWatchParams()
    {
    }
}

public class WalletWatchParamsOptions
{
    [JsonProperty(PropertyName = "address")]
    public string address;

    [JsonIgnore]
    public string symbol;

    [JsonIgnore]
    public uint decimals;

    [JsonIgnore]
    public string image;

    [JsonProperty(PropertyName = "tokenId")]
    public string tokenId;

    [Preserve]
    public WalletWatchParamsOptions()
    {
    }
}

This is the error message I am getting

WalletConnectException: User disapproved requested chains WalletConnectSharp.Sign.Engine.Request[T,TR] (System.String topic, T data, System.String chainId, System.Nullable`1[T] expiry) (at <1c1f65c00e50451aa8c03f421811df4e>:0)

skibitsky commented 2 months ago

Hey @ammarvohra,

I am a WalletConnect developer. Most wallets do not support wallet_watchAsset.

You should verify if the wallet has approved the wallet_watchAsset method before making that call. I recommend using a blockchain explorer API to track NFT as a fallback in case the wallet does not support wallet_watchAsset.

We have recently launched Web3Modal in Alpha for Unity. I suggest you take a look. It uses Nethereum.Unity as a dependency and offers cross-platform UI and network switching capabilities.

https://docs.walletconnect.com/web3modal/unity/about

We also have the WalletConnect Modal, which is more lightweight, but doesn't have network switching UI and Nethereum integration out of the box. https://github.com/WalletConnect/WalletConnectUnity/tree/main/Packages/com.walletconnect.modal

ammarvohra commented 2 months ago

@skibitsky thanks for your response.

I am connecting Metamask using WalletConnectModal, not Web3Modal. Metamask does support wallet_watchAsset can you please check the code above I've posted and see if I am making mistake in calling it.

Thanks

skibitsky commented 2 months ago

@ammarvohra,

Did you include wallet_watchAsset in the array of methods in namespaces? This is a list of RPC methods your dapp uses.

https://docs.walletconnect.com/advanced/walletconnectmodal/usage?platform=unity#connection-and-events

ammarvohra commented 2 months ago

@skibitsky Yes I did include that one, let me share that here:

var requiredNamespaces = new RequiredNamespaces
    {
        {
            "eip155", new ProposedNamespace
            {
                Methods = new[]
                {
                    "eth_sendTransaction",
                    "personal_sign",
                    "eth_signTypedData",
                    "wallet_watchAsset",
                    "wallet_addEthereumChain",
                    "wallet_switchEthereumChain"
                },
                Chains = new[]
                {
                    "eip155:245022926"
                },
                Events = new[]
                {
                    "chainChanged",
                    "accountsChanged"
                }
            }
        }
    };
skibitsky commented 2 months ago

@ammarvohra could you please open an issue in https://github.com/WalletConnect/WalletConnectUnity/ ?

ammarvohra commented 2 months ago

Okay