Search code examples
ethereumsoliditysmartcontractstruffleganache

Truffle doesn't compile but remix does


So I'm trying to send some ether from a smart contract to a user, but I can't get it to compile with truffle, it does compile on remix, so I can't find the problem, everything is compiling good except this function, anyone knows what could be causing this?

function sendEther(address _to, uint _amount) public returns(bool){
    require(address(this).balance >= _amount);
    (bool success, bytes memory data) = payable(_to).call{value: _amount}("");
    require(success, "Failed to send Ether");
    return true;
}

function sellTokens(uint _amount) public{
    uint etherAmount = _amount / rate;
    token.transferFrom(msg.sender, address(this), _amount);
    sendEther(msg.sender, etherAmount);
    emit TokensSold(msg.sender, address(token), _amount, rate);
}   

It throws a parse error:

expected primary expression when the call function is called.

Solution

  • Your message is indicating this line:

     (bool success, bytes memory data) = payable(_to).call{value: _amount}("");
    

    and it is correct syntax. The reason truffle is not compiling, you might be using an old solc version. If you do not set solc version in truffle config, I think by default truffle uses version 5.6. in your truffle.config.js

    compilers: {
        solc: {
         // adjus tthis based on contract's solidity version
          version: "0.8.4",
        }
      }
    

    solidity version on contract does not need to be exactly "0.8.4". You can set it like this:

    pragma solidity >=0.4.22 <0.9.0;