Can't call contract function in truffle consol

2019-08-09 11:17发布

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条回答
Evening l夕情丶
2楼-- · 2019-08-09 11:33

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)
查看更多
登录 后发表回答