I wrote a very simple smart contract. The goal is to:
- transfer some Ether from an account A to the smart-contract
- and then from the smart-contract to another account B.
Here is the contract:
pragma solidity ^0.8.0;
contract MyContract {
function fundContract() external payable {
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function transferAmount(address payable _recipient) external payable {
_recipient.transfer(msg.value);
}
function callAmount(address payable _recipient) external payable {
(bool success, bytes memory data) = _recipient.call{value:msg.value}("");
require(success, "Transaction Failed!");
}
}
I was able to achieve to transfer 5 ETH from an account A to the smart contract by using the following commands on the truffle console
:
MyContract.deployed().then(function(i) { contract=i;})
contract.fundContract({value: 500000000000000000})
contract.getBalance().then(function(r){result=r;})
parseFloat(result)
However when I try to send 1 ETH from the smart contract to another account B using these line of code:
contract.transferAmount('0x60407B3b7a3acd2dAE922610129433B915A3C238', {value:100000000000000000})
or
contract.callAmount('0x60407B3b7a3acd2dAE922610129433B915A3C238', {value:100000000000000000})
the amount is subtracted from the user's balance (account A) rather than the smart-contract, which appears to have 5 ETH instead of 4 ETH (since I am trying to transfer 1 ETH). What am I doing wrong? Would you be able to suggest a smart and elegant way to achieve this goal (send 1 ETH from the smart-contract balance to account B)?