Can't call contract function in truffle console

你离开我真会死。 提交于 2019-12-11 04:16:37

问题


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:


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)


来源:https://stackoverflow.com/questions/46045814/cant-call-contract-function-in-truffle-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!