1

I am trying to call view only function, not sure whether this is correct procedure or not. Getting success in result but the result is in different format. Is this wrong ?

const NODE_ADDRESS = config.web3Provider;

    const sourceCode = fs.readFileSync('path_to_contract', 'utf8').toString();
    const compiledCode = compiler.compile(sourceCode, 1).contracts[':ContractName']
    const abi = JSON.parse(compiledCode.interface);

    async function send(web3, transaction) {
        while (true) {
            try {
                const options = {
                    to: transaction._parent._address,
                    data: transaction.encodeABI(),
                    gas: 210000,
                    gasPrice: 10000000000,
                };
                const receipt = await web3.eth.call(options);
                return receipt;
            }
            catch (error) {
                return error
            }
        }
    }

    async function run() {
        try {
            const web3 = new Web3(NODE_ADDRESS);
            const contract = new web3.eth.Contract(abi, contractAddr);
            const transaction = contract.methods.getBuyerInfo();
            const receipt = await send(web3, transaction);
            console.log(JSON.stringify(receipt, null, 4));
            if (web3.currentProvider.constructor.name == "WebsocketProvider")
                web3.currentProvider.connection.close();
            if (receipt) {
                next(null, receipt)
            }
        } catch (error) {
            next(error, null)
        }
    }
    run();

Result :-

{
    "status": 1,
    "message": "Success",
    "data": "0x0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000f61343ef22bbccc7221dcda85c5a69219ea00c2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c50756e656574204b756d61720000000000000000000000000000000000000000"
}
1

call does not return a receipt but the actual value of the function, as it is executed locally on the node.

However the returned object is a promise, so you need to await it.

| improve this answer | |
  • i have used await on call , is that what you are pointing to ? – Puneet Kumar Apr 24 at 8:55
  • Yes. The return value should be the return value of the functoin, not receipt. You get a receipt only if you send a transaction. – Mikko Ohtamaa Apr 24 at 9:15
  • Okay. Thanks :) , i changed the process and made it a simple call transaction , removing options and it worked. – Puneet Kumar Apr 24 at 9:17
  • Good! Don't forget to mark the answer as correct :) – Mikko Ohtamaa Apr 24 at 9:21
0

This worked!

const contractAddr = "0x......"
const sourceCode = fs.readFileSync('path_to_contract', 'utf8').toString();
    const compiledCode = compiler.compile(sourceCode, 1).contracts[':ContractName']
    const abi = JSON.parse(compiledCode.interface);

    const contractInstance = new web3.eth.Contract(abi , contractAddr);
    contractInstance.methods.functionName().call((err, result) => {
        if (err){
            next(err, null)
        } else {
            next (null, result)
        }
    })
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.