0

I'm trying to test my contract functions in Truffle v5+ but getting TypeError: myContract.methods.register is not a function.

Contract MyContract {
    function register(uint256 amount) external {...}
}

In my Truffle migration file:

const MyContract = artifacts.require("MyContract");

module.exports = async function(deployer) {
    deployer.logger.log("Deploying MyContract");
    await deployer.deploy(MyContract, someConstructorParam);
}

In my Truffle test file:

contract("MyContract", accounts => {
  let myContract;

  before(async () => {
    myContract = await MyContract.deployed();
  });

  it("can register", async () => {
     await myContract.methods.register(1000).send({ from: accounts[1] }); // <<<< This is where the error is thrown.
     ...
  });
}

When I console.log(myContract.methods), included in the methods is:

'register(uint256)': [Function] {
   call: [Function],
   sendTransaction: [Function],
   estimateGas: [Function],
   request: [Function]
}

And when I change it to await myContract.register(1000, { from: accounts[1] }); I get Error: Returned error: VM Exception while processing transaction: revert. What am I doing wrong?

  • The first error is because the Truffle contract object is somewhat different from the Web3 contract object (the methods field is viable only in the latter). – goodvibration Jun 1 at 7:17
  • The second error is because of something that you do inside the register function (mostly likely a falsified require statement). – goodvibration Jun 1 at 7:17
  • Thanks @goodvibration. So the invocation itself that results in the second error is correct, which is the problem I'm asking about. Now, my next challenge is fixing the falsified require inside register. You are on point, yet again. Thanks man! – slider Jun 1 at 7:24
  • Well, since you have not provided the code of function register, my notion about the falsified require statement is only an educated guess. The revert could also be the result of a bunch of other reasons. No one can tell without seeing your actual code. – goodvibration Jun 1 at 7:26
  • I should've mentioned that I did identify the falsified require statement as the issue by sticking a return; just above it, which calls the function without error. – slider Jun 1 at 7:27
1

I'm trying to test my contract functions in Truffle v5+ but getting TypeError: myContract.methods.register is not a function.

Truffle's logical contract object, which can be generated by either one of the following:

  • artifacts.require("...").new(<construction arguments>)
  • artifacts.require("...").at(<deployment address>)
  • artifacts.require("...").deployed()

Is somewhat different from Web3's logical contract object.

In particularly starting from Web3 v1, where all contract functions are accessible via the methods field of the contract object, while in Truffle they are accessible directly via the contract object.

| improve this answer | |
  • Thanks. I ganache-cli --forked Kovan to let my contract interact with already-deployed contracts, so I didn't know I was dealing with Truffle contracts and Web3 objects. – slider Jun 1 at 8:05

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.