Error when migrating contracts using truffle "Error: One or more contracts have validation errors. Please review the items listed above and fix them, or run this command again with the --force option."

:computer: Environment

Tested with truffle version v5.0.11 (pre v5.0.13) and v5.0.39 (latest version)
OS: Ubuntu 19.04
Zeppelin SDK: “@openzeppelin/cli”: “^2.5.3”,

:memo:Details

The error I get is when I try and use truffle: Error: One or more contracts have validation errors. Please review the items listed above and fix them, or run this command again with the --force option.

Here is the truffle reference code (see: OpenZeppelin examples/truffle-migrate) that I used to implement my own)

There are only 2 other issues referencing this error on GH, but I can’t work out the similarity to my error: (see OpenZeppelin/openzeppelin-sdk#666 and OpenZeppelin/openzeppelin-sdk#912).

:1234: Code to reproduce

This works:

git clone git@github.com:wild-cards/contracts.git
cd contracts
npm i
truffle migrate
truffle test (only relevant because it shows that the contracts compile and work)

This doesn’t work:

Uncomment line line 9 and line 16.

Restart your test blockchain network, delete the zepplin artifacts, and rm -rf build.
Then run truffle migrate (or add the --force flag, doesn’t matter).

You should see the error I described.

1 Like

Hi @JasoonS,

Welcome to the community forum :wave:. Thanks for posting your question here.

As an aside, you may want to consider using OpenZeppelin CLI directly, at least whilst you are developing your contract. I haven’t used truffle migrations much for OpenZeppelin SDK and find it easier to use the CLI to create contracts, and interact with them using send-tx and call.

I ran truffle migrate and reproduced the error: Error: One or more contracts have validation errors.

I then ran an oz check and got a warning that WildcardSteward_v0 sets an initial value in a field declaration.

$ oz check
✓ Compiled contracts with solc 0.5.12 (commit.7709ece9)
Compilation warnings:
contracts/ERC721Patronage_v0.sol:25:50: Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
                                                 ^-------------^

- Contract WildcardSteward_v0 or one of its ancestors sets an initial value in a field declaration. Consider moving all field initializations to an initializer function. See https://docs.openzeppelin.com/sdk/2.5/writing_contracts.html#avoid-initial-values-in-fields-declarations.

I also get the same warning if I do an oz create

$ oz create
Nothing to compile, all contracts are up to date.
? Pick a contract to instantiate WildcardSteward_v0
? Pick a network development
✓ Added contract WildcardSteward_v0
- Contract WildcardSteward_v0 or one of its ancestors sets an initial value in a field declaration. Consider moving all field initializations to an initializer function. See https://docs.openzeppelin.com/sdk/2.5/writing_contracts.html#avoid-initial-values-in-fields-declarations.
- Contract WildcardSteward_v0 or one of its ancestors sets an initial value in a field declaration. Consider moving all field initializations to an initializer function. See https://docs.openzeppelin.com/sdk/2.5/writing_contracts.html#avoid-initial-values-in-fields-declarations.
✓ Contract ERC721Patronage_v0 deployed
✓ Contract WildcardSteward_v0 deployed
✓ Contract WildcardSteward_v0 deployed
All contracts have been deployed
? Do you want to call a function on the instance after creating it? No
Possible initialization method (initialize) found in contract. Make sure you initialize your instance.
✓ Setting everything up to create contract instances
✓ Instance created at 0x4bf749ec68270027C5910220CEAB30Cc284c7BA2
0x4bf749ec68270027C5910220CEAB30Cc284c7BA2

Initial values in field declarations is the equivalent of setting values in a constructor which we can’t do for upgradable contracts, so either need to make these constants or set in the initialize function. See the documentation for further details:
https://docs.openzeppelin.com/sdk/2.5/writing-contracts#avoid-initial-values-in-field-declarations

I removed the initial values from the declarations in WildcardSteward_v0

    // 1200% patronage
    uint256 patronageNumerator;
    uint256 patronageDenominator;

and added them to the initialize function

        patronageNumerator = 12;
        patronageDenominator = 1;

This then allows truffle migrate to migrate successfully.

1 Like

Hi @abcoathup

Thanks! I can’t believe I didn’t see that, it totally went under my radar.

I will investigate using oz cli more. I liked using truffle since you can specify things precisely with code (like piping parameters from previous outputs into the next function etc). Is there something similar for generating the arguments for the initialize functions etc rather than doing it interactively through the terminal?

Anyway, really loving the SDK! Great work :slight_smile:

1 Like

https://docs.openzeppelin.com/sdk/2.5/zos-lib
^ This looks like what I was looking for, now I totally agree with you, there isn’t too much need to use truffle anymore.

1 Like

Hi @JasoonS,

I predominantly use the CLI these days. I currently use Truffle for testing and for non-upgradeable contracts.

Glad you found Using the OpenZeppelin SDK programmatic library documentation as that is what I would recommend.