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);
        }
    }
  • Does you deployed smart contract has enough ether on balance to transfer? – Mikhail Vladimirov Apr 12 at 11:10
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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.