Can't call contract function in truffle console

0 votes

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

How can I call adopt()?

Sep 28, 2018 in Blockchain by digger
• 26,550 points
706 views

1 answer to this question.

0 votes

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)
answered Sep 28, 2018 by slayer
• 29,170 points

Related Questions In Blockchain

0 votes
1 answer

How to call a payable contract function from truffle console?

You can use a contract function, on ...READ MORE

answered Jan 24 in Blockchain by Omkar
• 67,660 points
228 views
0 votes
0 answers
0 votes
1 answer

How to call function in one contract from another contract in private blockchain?

Please check whether you have byzantiumBlock: 0 in your ...READ MORE

answered Oct 1, 2018 in Blockchain by digger
• 26,550 points
56 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
172 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,660 points
107 views
0 votes
2 answers
0 votes
1 answer

How can I deploy a HelloWorld contract on my testrpc network?

The problem lies in the command: truffle migrate Your truffle migrate command ...READ MORE

answered Apr 27, 2018 in Blockchain by Perry
• 17,020 points

edited Aug 10, 2018 by Omkar 514 views
0 votes
1 answer
0 votes
1 answer

How to store photos in Hyperledger smart contract?

To do this, you need to represent ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,170 points
81 views