How to call a smart contract function with WalletConnect? (React.js, Node.js)

Viewed 432

Is it actually possible to call a smart contract function using WalletConnect?

There is a documentation on official website that explains how to establish connection with the user via WalletConnect. However, when it comes to some actions after connection is established, everyone uses a coins transfer, from one wallet to another:

// Draft transaction
const tx = {
  from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3", // Required
  to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359", // Required (for non contract deployments)
  data: "0x", // Required
  gasPrice: "0x02540be400", // Optional
  gas: "0x9c40", // Optional
  value: "0x00", // Optional
  nonce: "0x0114", // Optional
};

// Send transaction
walletConnector.sendTransaction(tx);

My question is: instead of making a coins transfer, is it possbile to call a payable function in a smart contract? The function accepts some custom parameters (like string and uint256). Smart contract is deployed. If yes - how?

Thanks!

1 Answers

value is the payable ETH amount of the function call.

You can construct this transaction much more simply by using Ethers, and providing the option {value: ethAmount} after the method parameter list, using myContract.myMethod(param0, ... , paramN, {value: ethAmount}). (See Ethers docs for info on how to get the reference to myContract for your deployed contract.)

To use Ethers with WalletConnect, you have to use Web3Provider:

https://docs.walletconnect.com/quick-start/dapps/web3-provider

Related