2

I am able to connect or call the send txn method of ethereum using new metamask script. But i m not able to call the method of any of custom contract. Can someone tell me how can i do in javascript?

Thanks

Hamza Yasin
  • 51
  • 1
  • 5

2 Answers2

1

I am not sure what does your new metamask mean, however I can show you how to call an contract method in 2022:

( assuming you are calling a contract 's mint method )

    async function loadWeb3() {
      if (window.ethereum) {
        window.web3 = new Web3(window.ethereum);
        window.ethereum.enable();
      }
    }
async function loadContract() {
  let abi = [] // your abi here
  let address = "0xa1b2c3d4..."  // your contract address here
  return await new window.web3.eth.Contract(abi, address);
}
async function getCurrentAccount() {
  const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
  return accounts[0];
}

async function run() {
  await loadWeb3();
  window.contract = await loadContract();
  const account = await getCurrentAccount();
  let result = await window.contract.methods.mint().send({ from: account });
}

if (typeof window.ethereum !== 'undefined') {
  run()
}

refer to: http://siwei.me/blog/posts/blockchain-web3-metamask-contract

Siwei
  • 322
  • 2
  • 10
-3

you can try Web3 please check this out: https://medium.com/metamask/calling-a-smart-contract-with-a-button-d278b1e76705