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

Unable to Choose Chain on Trust Wallet - Standalone #1871

Closed
glitch-txs opened this issue Jan 16, 2023 · 11 comments
Closed

Unable to Choose Chain on Trust Wallet - Standalone #1871

glitch-txs opened this issue Jan 16, 2023 · 11 comments
Labels
type: bug Something isn't working

Comments

@glitch-txs
Copy link

glitch-txs commented Jan 16, 2023

I'm not able to choose a specific chain in Trust wallet.

I'm using the standalone version with the universal-provider and ethers.js

My code:

import { UniversalProvider } from '@walletconnect/universal-provider';
import { Web3Modal } from '@web3modal/standalone'
import { ethers } from 'ethers';

const web3Modal = new Web3Modal({ 
  // projectId: process.env.NEXT_PUBLIC_PROJECT_ID 
  standaloneChains: ["eip155:1", "eip155:137"],
})

export default function HomePage() {

const [childProvider, setChildProvider] = useState<any>()

 async function onInitializeSignClient() {
    
    const provider = await UniversalProvider.init({
      projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
      metadata: {
        name: "Glitch Dapp",
        description: "Glitch Dapp",
        url: "mywebsite.com",
        icons: ["url-icon.com"],
      },
    });

    setChildProvider(provider)

  }

  async function onOpenModal() {

    await childProvider?.connect({
      namespaces: {
        eip155: {
          methods: [
            "eth_sendTransaction",
            "eth_signTransaction",
            "eth_sign",
            "personal_sign",
            "eth_signTypedData",
          ],
          chains: ["eip155:1", "eip155:137"],
          events: ["chainChanged", "accountsChanged", "connect", "disconnect"],
        },
      },
    })

    web3Modal?.closeModal();
        
    //  Create Web3 Provider
    const web3Provider = new ethers.providers.Web3Provider(childProvider);

    const signer = web3Provider.getSigner()
    const address = await signer.getAddress()
    const chainId = await signer.getChainId()
     console.log(address, chainId)
  }

  useEffect(() => {
    onInitializeSignClient().catch((e: any)=> console.log(e))
  }, [])

  return (
    <>
    <button onClick={onOpenModal}>Connect Wallet</button>
    </>
  )
}

This is what I get when I connect with Trust Wallet:
1673887075875

1673887075863

So I'm not able to choose the chain I want to connect like this where as you can see there's an arrow that allows me to select different chains:

1673887075844

My question:
Does this mean that I'm connecting myself to both chains at the same time? If so is the case, how do I handle this on the dapp with universal-provider?
How do I select the chain that will be used to trigger the smart contract function request to the user's wallet?
And what if I want the user to choose the chain before connecting?

@glitch-txs glitch-txs added the type: bug Something isn't working label Jan 16, 2023
@ganchoradkov
Copy link
Member

hey @glitch-txs, I'm seeing you have configured chains chains: ["eip155:1", "eip155:137"],. In v2 protocol all chains are 'connected' simultaneously meaning you can send transaction requests for all chains without the need to switch the chain first.

If you'd like to listen for a chain switch on the dapp side, you should subscribe to session_event
Snippet from our docs

signClient.on("session_event", (event) => {
  // Handle session events, such as "chainChanged", "accountsChanged", etc.

  interface Event {
    id: number;
    topic: string;
    params: {
      event: { name: string; data: any };
      chainId: string;
    };
  }
});

@glitch-txs
Copy link
Author

Thanks for your comment, unfortunately I'm not hable to choose/switch chain using Trust Wallet and it picks up the first chain I configured. I'm also not able to choose the chain Id in ethers js as it depends on the wallets currently chain connection.

So basically my issue is, I'm not able to choose chain we I'm connected to Trust Wallet.

@ganchoradkov
Copy link
Member

I just tested with Trust Wallet, using our example dapp at https://react-app.walletconnect.com/ and I'm able to connect and use/sign transactions with multiple chains without having to switch. I'm not able to find/see the arrow you mentioned in the first comment

@glitch-txs
Copy link
Author

The arrow appears on v1 that it allows to switch, not on v2.

Right, the thing is, when I send a transaction to the wallet it will pick up the first chain, but if I want to sign that tx using other chain I'm not able to switch

@ganchoradkov
Copy link
Member

ganchoradkov commented Jan 18, 2023

Could you test with our example dapp if that's the case too? As I'm able to sign transactions with any of the chains connected

@glitch-txs
Copy link
Author

glitch-txs commented Jan 18, 2023

I see you can choose the chain from the dapp on the example you mentioned, I assume this is using the request function from SignClient where you can set the chain Id.

But this is not possible on ethers js at least not that I'm aware of

When you instantiate a Contract you add the abi, the contract address and the signer. The chain Id is implicit in the signer.

IMG_20230118_045740.jpg

@ganchoradkov
Copy link
Member

Seeing the examples above, while in ethers.js you can have a chain active, you should be able to request on any chain configured in the universal-provider using childProvider.request(args, <the chainId e.g. eip155:1>)

@glitch-txs
Copy link
Author

Yes, but I don't want to use the signClient to make the request, I'm instead using ethers.js which doesn't allows you to choose chain Id when creating a request.

If I use the signClient then I need to do some hex conversions which ethers.js simplifies.

@glitch-txs
Copy link
Author

glitch-txs commented Jan 18, 2023

This is how I can call a contract with ethers (here I'm calling the read-only function "name" of an ERC20), as you can see there's no parameter for chain Id.

const erc20Contract = new ethers.Contract(contractAddress, ERC20Abi, signer) 
  
     const name = await erc20Contract.name()

@ganchoradkov
Copy link
Member

Unfortunately, that is a shortcoming of ethers being single-chain.
What you could do is call provider.setDefaultChain('eip155:1') before the request, this will switch the chain in ethers and you will be able to make the call for the correct chain

@glitch-txs
Copy link
Author

It seems so, they are landing v6 soon, I'll check on that or request this feature.

Thanks for your time, I'll try a work around with that function 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants