3

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!

Dmitry
  • 1,035
  • 2
  • 14
  • 27

2 Answers2

0

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

Luke Hutchison
  • 8,186
  • 2
  • 45
  • 40
0

Below is the sample code that demonstrate use of Etherjs with walletConnect's web3Modal library that provides multiple wallet options to allow users to connect to dApp using wallet of their choice.

import { ethers } from "ethers";
import Web3Modal from "web3modal";
import WalletConnectProvider from "@walletconnect/web3-provider";

    //defining supported provider
     const providerOptions = {
        injected: {
          display: {
            name: "MetaMask",
            description: "Connect with the metamask from your Browser",
          },
          package: null,
        },
        walletconnect: {
          display: {
            name: "WalletConnect",
            description: "Scan qrcode with your mobile wallet",
          },
          package: WalletConnectProvider,
          options: {
            rpc: {
              [CHAIN_ID]: RPC_URL,
            },
          },
        },
      };
        //creating web3 modal instance
        const web3Modal = new Web3Modal({
              cacheProvider: false, // optional
              providerOptions, // required
              });
        
        //invoking wallet modal 
        let instance = await web3Modal.connect();
        
        //creating etherjs instance from wallet connect
        let injectedInstance = new ethers.providers.Web3Provider(instance);
        
        //creating a signer
        const signer = provider.getSigner();
        
        //creating contract instance and invoking contract function
        const contractInstance = new ethers.Contract(
                  CONTRACT_ADDRESS,
                  CONTRACT_ABI,
                  signer
                );
        let transactionHash = await contractInstance.functionName(param1,{value:gweiAmount});
Vineeta
  • 85
  • 1
  • 11