0

I deployed an ERC20 token smart contract and an exchange smart contract which allows for buying the token with Ether.

I deployed them to the mainnet using Infura and Hardhat and am able to buy tokens and view them with MetaMask.

How can I call public methods on the smart contract?

2 Answers 2

0

You have to initialize your contract using abi and contract address using web3 and then call the function

 const mycontract = new web3.eth.Contract(abi , contractAddress)
 mycontract.methods.withDraw("arguments").send({
 fom:fromAddress, gas:3000000})
 .on('confirmation', (confirmations, receipt) => {
        console.log('CONFIRMATION');
        console.log(confirmations);
        console.log(receipt);
 }
-1

On your website, you can install web3 that allow you to interact with your contract. You need to declare as constant :

  • Smart contract Address
  • Smart contract ABI

Then with web3 you can call any method. You can add a private key into your web3 variable to sign transactions, but you can also just ask for an account so you will sign transaction with metamask or other wallet. Examble for web3.js method :

  async function getCurrentAccount() {
        const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
        return accounts[0];
    }

  async function withdraw() {
        const account = await getCurrentAccount();
        const doMethod = await MyContract.methods.withdraw().send({ from: account })
    }
2
  • So I've loaded the contract with the ABI, connected to my Infura provider, and retrieved my current. I see the withdraw method under MyContract.methods.withdraw() but I get this error: "The method eth_sendTransaction does not exist/is not available" Commented Jul 2, 2021 at 2:03
  • Your example doesn't show web3. Is the ethereum object web3? Where did MyContract come from?
    – NickJ
    Commented Sep 4, 2021 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.