简体   繁体   中英

calling a smart contract function with web3 js

Trying to figure out how to get return data from web3 smart contract call. So far I have the ABI and contract address creating the contract here is the code:

    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,
    );

the ABI has a function called getKitty which is:

{
    "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"
}

I am trying to call it and just console log the output for now like:

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

just to see what it returns but im getting an object like:

    {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

totally new to blockchain and smart contract so any help is appreciated. thanks.

I'm assuming you're using web3.js 1.0-beta.

You need something like this:

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

or (if not using async/await):

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

Depending on your setup, you may need to pass a from address and/or other parameters, eg:

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

you need to have call() function also at the end, and also you can use callback function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM