【问题标题】:calling a smart contract function with web3 js使用 web3 js 调用智能合约函数
【发布时间】:2019-01-24 04:23:46
【问题描述】:

试图弄清楚如何从 web3 智能合约调用中获取返回数据。到目前为止,我有创建合同的 ABI 和合同地址,这里是代码:

    const web3 = new Web3(window.web3.currentProvider);

  //  Initialize the contract instance

    const kittyContract = new web3.eth.Contract(
      KittyCoreABI, // import the contracts's ABI and use it here
      CONTRACT_ADDRESS,
    );

ABI 有一个名为 getKitty 的函数:

{
    "constant": true,
    "inputs": [
        {
            "name": "_id",
            "type": "uint256"
        }
    ],
    "name": "getKitty",
    "outputs": [
        {
            "name": "isGestating",
            "type": "bool"
        },
        {
            "name": "isReady",
            "type": "bool"
        },
        {
            "name": "cooldownIndex",
            "type": "uint256"
        },
        {
            "name": "nextActionAt",
            "type": "uint256"
        },
        {
            "name": "siringWithId",
            "type": "uint256"
        },
        {
            "name": "birthTime",
            "type": "uint256"
        },
        {
            "name": "matronId",
            "type": "uint256"
        },
        {
            "name": "sireId",
            "type": "uint256"
        },
        {
            "name": "generation",
            "type": "uint256"
        },
        {
            "name": "genes",
            "type": "uint256"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}

我正在尝试调用它,现在只是控制台记录输出,例如:

console.log(kittyContract.methods.getKitty(887674))

只是想看看它返回了什么,但我得到了一个像这样的对象:

    {call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, arguments: Array(1), …}
arguments
:
[887674]
call
:
ƒ ()
encodeABI
:
ƒ ()
estimateGas
:
ƒ ()
send
:
ƒ ()
_ethAccounts
:
Accounts {_requestManager: RequestManager, givenProvider: MetamaskInpageProvider, providers: {…}, _provider: MetamaskInpageProvider, …}
_method
:
{constant: true, inputs: Array(1), name: "getKitty", outputs: Array(10), payable: false, …}
_parent
:
Contract {_requestManager: RequestManager, givenProvider: MetamaskInpageProvider, providers: {…}, _provider: MetamaskInpageProvider, …}
__proto__
:
Object

对区块链和智能合约来说是全新的,因此感谢您提供任何帮助。谢谢。

【问题讨论】:

    标签: javascript blockchain smartcontracts web3


    【解决方案1】:

    我假设您使用的是 web3.js 1.0-beta。

    你需要这样的东西:

    console.log(await kittyContract.methods.getKitty(887674).send());
    

    或(如果不使用异步/等待):

    kittyContract.methods.getKitty(887674).send().then(function (result) {
        console.log(result);
    });
    

    根据您的设置,您可能需要传递来自地址和/或其他参数,例如:

    kittyContract.methods.getKitty(887674).send({ from: ..., ... }).then(...);
    

    【讨论】:

    • 是的,我没有意识到它会返回一个承诺。我实际上最终使用了 .call() 并且效果很好。现在我需要弄清楚如何将 uint256 数字转换为日期。
    • 抱歉,是的,对于视图功能,call 是您想要的(不是 send)。
    【解决方案2】:

    最后还需要call()函数,也可以使用回调函数。

    kittyContract.methods.getKitty(887674).call({from: address}, function(error, result){
    console.log(result)
    })
    

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 2019-08-27
      • 2019-12-26
      • 2018-12-28
      • 2022-08-10
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多