0

I have two interfaces:

pragma solidity ^0.8.0;

interface EscrowParticipantI
{
    function startEscrow(uint amount) external;
}

and

pragma solidity ^0.8.0;

interface EscrowServiceI {
    receive() external payable;
}

Now I have written two contracts that implement these interfaces EscrowParticipant and EscrowService. Like this:

pragma solidity ^0.8.0;

contract EscrowParticipant is EscrowParticipantI {
    EscrowServiceI escrow;
    
    function startEscrow(uint amount) public payable {
        escrow.receive({value: amount});
    }

}

contract EscrowService is EscrowServiceI {
    function receive() external payable {
        // Do something
    }
}

I am getting this error ParserError: Expected identifier but got 'receive'.

I am programming on remix IDE. I am new to solidity and have been struggling with this error for a while now. Any help with the error is much appreciated. I am also confused as to what identifier means here.

1 Answer 1

0

The receive function is a special function that receive ether and you cannot call it directly like a normal funciton. You can trigger it by sending ether using send or transfer or call link. example:

function startEscrow(uint amount) public payable {
  (bool sent, bytes memory data) = _to.call{value: msg.value}("");
  require(sent, "Failed to send Ether");
}

A contract can have at most one receive function, declared using receive() external payable { ... } (without the function keyword). This function cannot have arguments, cannot return anything and must have external visibility and payable state mutability. It can be virtual, can override and can have modifiers.

The receive function is executed on a call to the contract with empty calldata. This is the function that is executed on plain Ether transfers (e.g. via .send() or .transfer()).

source here

Additional Infos:

  • Interface's names start usually with I -> IEscrowParticipant
  • The amount of the ether that is sent with a transaction is set in msg.value. If you want to send all the ether to the escrow service contract then you don't need to give the amount as a parameter and you can just use msg.value directly

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.