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.
What am I doing wrong here?