0

I am trying to build a basic smart contract that sends a fixed amount of ether from one wallet to another. I am trying something like like this:

contract Autopayment {
    
    address  payable recipient = 0xBfD0a1880fBd2c45093c974E611Ea0A23031536f ;
    uint256 amount = 0.01 ether;

    function sendEth() public payable {
        recipient.transfer(amount);
    }
}

but when I invoke the sendEth() function it gives the error: from solidity:ParserError: Expected identifier but got '('

2 Answers 2

0

The only thing missing in your code is that an address payable needs to be initialised with payable(). The rest should work, as long as you send ether along with the call to sendEth() function.

Your code should look like this:

contract Autopayment {
    
    address  payable recipient = payable(0xBfD0a1880fBd2c45093c974E611Ea0A23031536f);
    uint256 amount = 0.01 ether;

    function sendEth() public payable {
        recipient.transfer(amount);
    }
}
2
  • Would it be possible if I want the contract to transfer the ether automatically to the wallet upon deployment without providing any user input or performing any other action after deployment. Should I call the function sendEth() in the constructor for that to work that way?
    – isenhiem
    Mar 9, 2023 at 17:14
  • Exactly, make the constructor payable and deploy with some ether sent along.
    – prc
    Mar 9, 2023 at 23:31
0

Yeah, you put an extra line of code. The correct code would as below:

EG.

contract Autopayment{
   address  payable recipient = payable(0xBfD0a1880fBd2c45093c974E611Ea0A23031536f);
   uint256 amount = 0.01 ether;

   function sendEth() public payable {
        recipient.transfer(amount);
    }
    
}

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.