2

I am running truffle test with the following simple test contract, however it seems that it doesn't understand the view modifier.

Contract

pragma solidity ^0.4.18;

contract MetaCoin {
    function getBlockNumber()
    public
    view
    returns (uint)
    {
        return block.number;
    }
}

The output looks like:

$ truffle test
Compiling ./contracts/MetaCoin.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...

/contracts/MetaCoin.sol:7:5: ParserError: Expected token LBrace got 'View'
    view
    ^

The view works in Remix, what is missing in my truffle environment that is throwing this error?

The Remix compilation version is (0.4.18+commit.9cf6e910.Emscripten.clang)

2

Do $ truffle version

Unless I miss my guess, it's not compiling with 0.4.18 so it's confused.

Truffle comes with its own JS compiler that lags behind solc, so it usually works best to lock that version in Remix so you have consistency through the whole process. For example, Truffle 3.4.11 gave us solidity 0.4.15. It that's the case, then it makes sense to select 0.4.15 in Remix.

Hope it helps.

  • Thank you Rob. truffle version returns Truffle v3.4.11 (core: 3.4.11) Solidity v0.4.15 (solc-js) changing the setting to 0.4.15 in Remix gives the same error as truffle. So i think your assessment that truffle lags solc is correct. – Victory Oct 23 '17 at 23:58
  • 1
    In case it isn't clear or helps someone else, view is new. In 0.4.15 you'll be fine with constant. Thanks for the "accept". – Rob Hitchens - B9lab Oct 24 '17 at 0:06

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.