0

I'm writing a smart contract in solidity in remix Injected Web 3 environment to send eth from one wallet to another. My attempt is that send eth to the smart contract and then send all balance in the smart contract to another wallet. The following is my code. The transaction failed but I have double checked that my public key of both wallets are correct and I have entered the value when I call the function send_ETH.

pragma solidity >=0.8.0;

// Get the latest ETH/USD price from chainlink price feed
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract myContract {
    address payable[] recipients;
    mapping(address => uint256) public balanceAccount;
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    function send_ETH(address payable recipient) payable public {
        invest();
        fund(recipient);
    }

    function invest() internal{
        //transfer ETH from metadata wallet to smart contract
        payable(address(this)).send(msg.value);
    }

    function fund(address payable recipient) internal {
        //transfer ETH from this smart contract to the recipient
        recipient.send(address(this).balance);
    }

1 Answer 1

0

I tried your code in remix web 3 environment it is working fine.

contractAddress : 0xa4776edce21841bbf779667f11f8296fcef2413f

sendETH transaction : https://ropsten.etherscan.io/tx/0xc3bb6acc91d728dc321529911821c0c2741b4b42e0162acdc9f0a8c44967de49

And you don't need to invest function because when you send msg.value any contract it automatically send into contract.

you can try this :

pragma solidity >=0.8.0;


contract myContract {
    address payable[] recipients;
    mapping(address => uint256) public balanceAccount;
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    function send_ETH(address payable recipient) payable public {

        fund(recipient);
    }
   
    function fund(address payable recipient) internal {
        //transfer ETH from this smart contract to the recipient
        recipient.send(address(this).balance);
    }
}
5
  • Hi, thanks for your help. I'm trying to run my code in the Injected Web 3 environment. The account key on the left panel of remix is the public key of the first wallet. Then I add 1 GWei as value, and the public key of the second wallet as a parameter in the send_ETH function. After clicking the transac button, I got the gas estimation failed error. However, my wallet has enough eth and the gas limit is high enough Commented Feb 21, 2022 at 6:11
  • you deployed your contract? if yes please give me your contract address. Commented Feb 21, 2022 at 6:15
  • rinkeby.etherscan.io/tx/… Commented Feb 21, 2022 at 6:20
  • you can try edited code it working fine. Commented Feb 21, 2022 at 6:36
  • Thank you. The code now works. However, my recipient which is my second wallet doesn't receive any eth from my first wallet. I switched to the second account and checked the activity but there is nothing shown Commented Feb 21, 2022 at 6:46

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.