y-pakorn / flutter_web3

Web3 Ethereum, Etherjs and Wallet Connect wrapper for Flutter Web.
Other
136 stars 46 forks source link

Can you provide more informative example of rpc requests #21

Closed CharikovAI closed 3 years ago

CharikovAI commented 3 years ago

I'd like to call rpc methods with object parameters but always get an error that object fields are undefined. I'm playing with wallet_switchEthereumChain metamask method ( https://docs.metamask.io/guide/rpc-api.html#usage-with-wallet-switchethereumchain )

I tried:

await ethereum!.request(
    'wallet_switchEthereumChain',
    [
       { "chainId" : "0xf00"}
    ]
)
await ethereum!.request(
    'wallet_switchEthereumChain',
    [
       '{ chainId : "0xf00"}'
    ]
)

Still none is working. Can you explain how to do it?

y-pakorn commented 3 years ago

Please use Ethereum.walletSwitchChain to use Metamask's wallet_switchEthereumChain request.

Or if you want to use primitive request, Dart map object do not automatically translate to JS object. Wrap them with jsify first.

For example,

await ethereum!.request(
  'wallet_switchEthereumChain',
  [
    jsify({"chainId": '0x38'}) // 56 in decimal, Binance smart chain.
  ],
);