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"
}
Best Answer
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.