I have deployed smart contract using Reactjs, ganache and metamask. I wish to send ether from current account to another account using the deployed contract. Desired scenario:
I will enter the receiver's address in the front end and it should open up a metamask pop up to send ether to that address. But right now when I implement the function it just initiates a transaction to to the smart contract with some gas and doesnt send anything to the address. Kindly suggest me the changes needed Smart Contract Code:
function payment (uint amount, address payable beneficiary) payable external {
beneficiary.transfer(amount);
}
React Appjs Code to use this function:
function payment(e){
e.preventDefault();
project.methods.payment(1,'0xe13DC66579940552574Cbe795410423609C2BFd9').send({from: {CurrentAccount}['CurrentAccount']});
}
Please suggest the changes needed to send ether to that address
if you want to transfer ether with contract just send your amount of ether in to contract. then get amount of ether with tx.value and transfer it.
function payment (address payable beneficiary) payable external{
uint256 amount = msg.value;
beneficiary.transfer(amount);
}
then in app.js
function payment(e){
e.preventDefault();
const nonce = await web3.eth.getTransactionCount(myAddress, 'latest');
const data = project.methods.payment('0xe13DC66579940552574Cbe795410423609C2BFd9').encodeABI();
}
const transaction = {
'to': 'contract Address',
'value': 100000000000000000, // the amount of ether you will trasnfer with contract
'gas': 30000,
'maxFeePerGas': 1000000108,
'nonce': nonce,
};
const signedTx = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY);
web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
if (!error) {
console.log("🎉 The hash of your transaction is: ", hash, "\n Check Alchemy's Mempool to view the status of your transaction!");
} else {
console.log("❗Something went wrong while submitting your transaction:", error)
}
});