Summary: Unable to call contract function from web3
that has uint as parameter, but can call the same from truffle console
directly.
I have a contract function as follows:
function clearOrders(
uint256[] calldata prices,
uint256 firstId,
uint256 lastId
) external returns (string memory status) {
uint256 i = 0;
while (firstId <= lastId) {
processOrder(
firstId,
prices[i]
);
i++;
firstId++;
}
return 'success';
}
function processOrder(
uint256 id,
uint256 price
) internal {
// do something
}
Now calling the function from truffle console works fine:
truffle(ganache)> (await myContract.clearOrders([125444000, 135494000], 1, 2, {from: accounts[0]}))
but when calling the function clearOrders
from web3:
let prices = []
let a = 125.444
let b = 135.494
prices.push(web3.utils.toWei(a.toString(), 'mwei'))
prices.push(web3.utils.toWei(b.toString(), 'mwei'))
console.log('prices[]: ' + prices)
// prices[]: 125444000,135494000
let firstId = 1
let lastId = 2
try {
let result = await myContract.methods
.clearOrders(
prices,
firstId,
lastId,
)
.send({from: accounts[0]})
.on("transactionHash", hash => {
console.log('Submitted to block and received transaction hash: ' + hash)
})
.on("error", error => {
console.log('error 1: ' + error)
return false
})
.then(function(receipt) {
if(receipt.status) {
console.log('Success: ' + receipt.transactionHash)
return true
} else {
console.log('error 2')
return false
}
})
console.log('result: ' + result)
} catch(error) {
console.log('error 3: ' + error)
}
gives output:
prices[]: 125444000,135494000
error 3: Error: Returned error: VM Exception while processing transaction: revert
error 1: Error: Returned error: VM Exception while processing transaction: revert
Error: Returned error: VM Exception while processing transaction: revert
at Object.ErrorResponse (/Users/megatower/projects/my-project/node_modules/web3-core-helpers/lib/errors.js:28:19)
at /Users/megatower/projects/my-project/node_modules/web3-core-requestmanager/lib/index.js:291:36
at XMLHttpRequest.request.onreadystatechange (/Users/megatower/projects/my-project/node_modules/web3-providers-http/lib/index.js:98:13)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/megatower/projects/my-project/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (/Users/megatower/projects/my-project/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (/Users/megatower/projects/my-project/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (/Users/megatower/projects/my-project/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
at IncomingMessage.emit (node:events:388:22)
at endReadableNT (node:internal/streams/readable:1295:12)
at processTicksAndRejections (node:internal/process/task_queues:80:21) {
data: {
'0x9ae3c26045af8fc5dffcd16bc1233ce730f26f6af149a4d737ae6921647fbda8': { error: 'revert', program_counter: 1726, return: '0x' },
stack: 'c: VM Exception while processing transaction: revert\n' +
' at Function.c.fromResults (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:4:194812)\n' +
' at w.processBlock (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:42:53376)\n' +
' at runMicrotasks (<anonymous>)\n' +
' at processTicksAndRejections (node:internal/process/task_queues:93:5)',
name: 'c'
}
}
Am assuming the error has something to do with BigNumber as I read that parameters passed to contract function as array need to be BN? I’m not sure what is happening here.