0

I use these codes(@Lauri Peltonen ) however I tried 5 ether and 500, but I found it had an error in remix " like I need to add payable to constructor, but I already added". What does that mean?

  pragma solidity ^0.5.1;

    contract MyContr {
        function transfer(address payable receiver, uint amount) public {
            require(address(this).balance >= amount);
            receiver.transfer(amount);
        }
    }
1
  • Does you deployed smart contract has enough ether on balance to transfer? Commented Apr 12, 2019 at 11:10

1 Answer 1

0

From the little you have shared, I assume you have provided no way to fund your contract or didn't fund it at all.

If you have indeed added the payable keyword to the constructor, you just need to set the transaction's value to what you desire at the moment of deploying the contract.

pragma solidity ^0.5.1;

contract MyContr {

    //Allows you to fund the contract with ether at the moment of deployement
    //You need to set the value of the transaction
    constructor() public payable {}

    function transfer(address payable receiver, uint amount) public {
        require(address(this).balance >= amount);
        receiver.transfer(amount);
    }
    //Payable function that allows you to directly send ether to the contract
    //without calling a specific function
    function() external payable {}
}

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.