无法在松露控制台中调用合约功能 [英] Can't call contract function in truffle console

查看:125
本文介绍了无法在松露控制台中调用合约功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将以下合同编译并部署到testrpc:

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;
    }
}

然后我去终端,然后:

truffle compile
truffle migrate --reset

一切正常。然后,我尝试在松露控制台中调用accept():

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

如果我尝试:

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

我的方法有什么问题?我怎么称呼accept()?

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

推荐答案

检查对象 adoption 在控制台中。您会注意到这些方法在名称空间 contract 下。调用以下函数:

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)

这篇关于无法在松露控制台中调用合约功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆