0

When I the ABI for any smart contract it always creates "methods" as just an empty object... it should contain my public and external functions, but it doesn't.

When I look at the ABI I don't see any functions at all... even when I instantiate a contract with web3, the "methods" is just an empty object. why is this happening?


Steps to Reproduce

  1. run truffle init
  2. run truffle create all Hello
  3. add a function to Hello contract:
function foooo() external pure returns (string memory) {
    return "something";
  }
  1. run truffle build
  2. Look at build/contracts/Hello.json and see that "methods" is just an empty object...
2
  • Are you refering to the methods fields under "devdoc" and "userdoc" ?
    – hroussille
    Oct 29, 2021 at 17:35
  • @hroussille yes
    – Jim
    Oct 29, 2021 at 17:50

1 Answer 1

1

Those fields are related to documentation.

For example the following dummy function :

/**
 * @dev This is a dev comment
 * @notice This is a user comment
 */
function documentedFunction() public pure returns (bool success) {
    return true;
}

When compiled, will produce the following in the artifact json output :

  "devdoc": {
    "kind": "dev",
    "methods": {
      "documentedFunction()": {
        "details": "This is a dev comment"
      }
    },
    "version": 1
  },
  "userdoc": {
    "kind": "user",
    "methods": {
      "documentedFunction()": {
        "notice": "This is a user comment"
      }
    },
    "version": 1
  }

See this link that explains it very well already.

5
  • hmm but when I use web3 to create an instance of the contract, "methods" is an empty object... that doesn't seem right.
    – Jim
    Oct 29, 2021 at 18:12
  • 1
    web3 only consumes the ABI and address of your contract. Those fields are not part of the ABI, it's basically metadata that belong to the truffle artifact, not the ABI itself. Now you seem to be referring to the methods field of the contract instance under web3 as described here : web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html. So which one is it ?
    – hroussille
    Oct 29, 2021 at 18:31
  • the methods field of the contract instance, this is always empty object
    – Jim
    Oct 29, 2021 at 18:50
  • Could you provide the code of your web3 contract instantiation ? I will try modify my answer accordingly.
    – hroussille
    Oct 29, 2021 at 20:15
  • @jim, devdoc, userdoc are generated by NatSpec and should not be used to instantiate a web3 contract abstraction. If the method property of a web3.Contract instance is empty then it is likely the Contract constructor didn't receive a proper ABI.
    – amal
    Oct 30, 2021 at 10:33

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.