【发布时间】: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