Truffle: Incorrect ABI generated when compiling after modifying a contract

Created on 24 May 2019  路  4Comments  路  Source: trufflesuite/truffle

Issue

Since truffle 5.0.13, when compiling a specific contract after adding a variable to it, the resulting ABI is incorrect.

Steps to Reproduce

We have narrowed down the issue to these specific two contracts. Let's call them V1:

pragma solidity ^0.5.0;

contract MyContract {
  uint256 public x;
  string public s;

  function initialize(uint256 _x, string memory _s) public {
    x = _x;
    s = _s;
  }
}

And V2:

pragma solidity ^0.5.0;

contract MyContract {
  uint256 public x;
  string public s;
  uint256 public t;

  function initialize(uint256 _x, string memory _s, uint256 _t) public {
    x = _x;
    s = _s;
    t = _t;
  }
}

Note that the difference between the two is the added t public field, and adding it as a parameter in the initialize function.

When compiling V2 directly, the resulting ABI is correct, and the definition for the t public function is:

$ jq .abi[2] build/contracts/MyContract.json 
{
  "constant": true,
  "inputs": [],
  "name": "t",
  "outputs": [
    {
      "name": "",
      "type": "uint256"
    }
  ],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}

However, if I first compile V1, then modify the Solidity file changing it to V2, and recompile, the resulting ABI looks like the following for public t:

{
  "constant": true,
  "inputs": [
    {
      "name": "_x",
      "type": "uint256"
    },
    {
      "name": "_s",
      "type": "string"
    }
  ],
  "name": "t",
  "outputs": [
    {
      "name": "",
      "type": "uint256"
    }
  ],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}

Note that, in V1, the third item of the ABI was the initialize function, which had that exact same look (except for the name). It would seem like the new function (public t) is getting mixed with the previous ABI definition.

If I clear out build/contracts and recompile, the ABI generated is the correct one.

Environment

Have reproduced the issue with truffle 5.0.13 and 5.0.18. Version 5.0.12 seems to work fine, so the error should have been introduced in 13.

  • Operating System: Ubuntu 18.04
  • Truffle version (truffle version):
$ npx truffle version
Truffle v5.0.13 (core: 5.0.13)
Solidity v0.5.0 (solc-js)
Node v10.13.0
Web3.js v1.0.0-beta.37
  • node version (node --version): 10.13.0
  • npm version (npm --version): 6.9.0
Artifacts Fixed bug

Most helpful comment

Thanks for the quick fix :-)

All 4 comments

Ah @spalladino nice find! Sounds like we inadvertently oversimplified the artifactor merging behavior. #1894 might be the culprit; I'll take a look.

(@CruzMolina is beating me to it)

Thanks for the quick fix :-)

Awesome, thanks!!

Was this page helpful?
0 / 5 - 0 ratings