0

I'm trying to develop a DApp using React and Solidity using an ERC20 contract, I've deployed my smart contract on Rinkeby and am able to interact with it using name() and symbol(), however, I cannot execute payable functions like transfer(), the following error is displayed each time:

MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'}

I have tried simply calling contract.transfer( ... ) and putting quotes around the amount being sent, neither works.


JS:

await contract.transfer.call(account, 50)

Sol:


interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    // Omitted other functions...

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract MyCoin is IERC20 {

function transfer(address receiver, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

}
TylerH
  • 20,799
  • 66
  • 75
  • 101
KylianMbappe
  • 1,896
  • 2
  • 15
  • 25

1 Answers1

0

first of all, you haven't marked the transfer() method with "payable" modifier. So, you're not calling any payable method, as you asked.

Secondly, when you send any amount from a metamask wallet, the contract should have either of these methods, with the logic, in case your contract receives any amount :

fallback() external payable;
receive() external payable;

as then only, your contract knows what to do with incoming amount;


Interacting with js:

also, to send any "amount* and interact with the contract with js. you can use sendTransaction({}):

await contractInstance.sendTransaction({
        from: accountAddress,
        value: web3.utils.toWei("0.001", "ether")
});

Don't do await contractInstance.receive({}) and call the receive() method directly.

you can see this answer, or this blog for more on this.