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

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

Open
ammarvohra opened this issue May 1, 2024 · 8 comments
Open

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

ammarvohra opened this issue May 1, 2024 · 8 comments

Comments

@ammarvohra
Copy link

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
Copy link
Author

ammarvohra commented May 3, 2024

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Contributor

@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
Copy link
Author

ammarvohra commented May 3, 2024

@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
Copy link
Contributor

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

@ammarvohra
Copy link
Author

Okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants