Can't call contract function in truffle console

2.4k views Asked by At

I'm compiling and deploying the following contract to testrpc:

pragma solidity ^0.4.4;

contract Adoption {

    address[] public adopters;

    function adopt(uint petId) public returns (uint) {

        require(petId >= 0 && petId <= 15);

        adopters[petId] = msg.sender;

        return petId;
    }
}

Then I go to terminal and:

truffle compile
truffle migrate --reset

Everything works as expected. Then, I try to call adopt() in truffle console:

truffle(development)> const adoption = Adoption.deployed()
// undefined
truffle(development)> adoption.adopt(1).then(console.log)
// TypeError: adoption.adopt is not a function

If I try:

truffle(development)> Adoption.deployed()
    .then((instance) => {instance.adopt(1)})
    .then(console.log)
// Error: VM Exception while processing transaction: invalid opcode

What is wrong with my approach? How can I call adopt()?

1

There are 1 answers

0
HackaZach On BEST ANSWER

Inspect the object adoption within the console. You will notice the methods are under the namespace contract. Call you functions like:

adoption.contract.adopt(1)