0

I have the following smart contract function that is supposed to be called from a web app, using ethers and Metamask, running on Hardhat localhost blockchain. Its purpose is to send the specified amount to the specified wallet.

    function sendRewardToGameWinner(address payable _to, uint256 amount) external payable {
        _to.send(amount);
    }

I call this function using ethers in a Javascript React file like this:

export async function sendRewardToGameWinner(chainId, to, wonAmount) {
    const ethAmount = wonAmount.toString();
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();

    const contract = new ethers.Contract(contractAddresses[chainId][0], abi, signer);

    try {
        const transactionResponse = await contract.sendRewardToGameWinner(to, ethers.utils.parseEther(ethAmount));

        await listenForTransactionMine(transactionResponse, provider);
    } 
    catch (error) {
        console.log(error);
    }
}

The call seems to be correct, as a Metamask transaction pops up, but instead of showing, for example, 3 ETH as the transanction value, I only see the gas fees, and in the transaction history it seems like the wallet address paid the gas fees and didn't receive anything.

enter image description here enter image description here

What am I doing wrong here?

1 Answer 1

0

I see different aspects here:

  1. It is normal that you don't see the eth value in the metamask are your wallet is not sending ether to your contract. If you would like to send eth with your transaction you should change the code to something like this:

    let amount = ethers.utils.parseEther(ethAmount);
    const transactionResponse = 
    await contract.sendRewardToGameWinner(to, amount,{value: amount });
    
  2. If your contract doesn't hold eth when you call _to.send() the transaction won't revert as it returns false when the transaction reverts, but it does not break. If you are in local hardhat I would console.log to check the send() result:

    bool success = _to.send(amount);
    console.log(success)

Hope it helps!

1
  • I tried what you suggested and the ETH amount does indeed show up on the Metamask transaction now. However, in the transaction history, it shows as a deduction (-12), rather than an added fund (+12). Moreover, the wallet balance doesn't change at all after the transaction is confirmed, but it does show up on the history. Commented Jan 30, 2023 at 10:19

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.