The code below gives me the exceeds allowance or always failing transaction error but when i check my contract on remix i can see that the transaction goes through, makes the correct state changes and works as expected. In The assignment executionMetrics
becomes undefined. I've been sitting trying to figure out the problem for 4hrs and I'm completely stuck. Anyone knows what's going on?
async function someAsyncFunc(){
bookingPromiseArr = []
let promise = instance.methods.bookTrip(tripKey).send({
from: accounts[0],
gasPrice: GAS_PRICE,
value: 1,})
bookingPromiseArr.push(promise)
let executionMetrics = await executePromises(bookingPromiseArr, txStartTime)
}
async function executePromises(promisesArr, txStartTime) {
await Promise.all(promisesArr)
.then((receipts) => {
let totalGasUsed = 0
let txElapsedTime = Date.now() - txStartTime
let lastBlock = receipts[receipts.length - 1].blockNumber
receipts.forEach((receipt) => {
totalGasUsed += receipt.gasUsed
lastBlock =
receipt.blockNumber > lastBlock ? receipt.blockNumber : lastBlock
})
// I've console logged here and seen that I've reached this point
return {
txElapsedTime: txElapsedTime,
totalGasUsed: totalGasUsed,
lastBlock: lastBlock,
}
})
.catch((error) => {
console.log('multipleTX():', error)
process.exit(1)
})
}
The smart contract function invoked:
function bookTrip(uint256 key) external payable requireTrip(key) nonBookedPassenger(key){
Trip storage trip = trips[key];
trip.passengers[msg.sender] = trip.price;
trip.passengerCount++;
emit LogNewTripPassenger(msg.sender, key, trip.price);
}
Error log:
(node:23524) UnhandledPromiseRejectionWarning: Error: gas required exceeds allowance (8000029
) or always failing transaction
at C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\tqdt33\node_modules\@truffle\hdw
allet-provider\node_modules\web3-provider-engine\subproviders\provider.js:18:36
at XMLHttpRequest.request.onreadystatechange (C:\Users\Diddi\Documents\mjukvaruteknik\exa
mensarbete\tqdt33\node_modules\web3-providers-http\src\index.js:96:13)
at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Diddi\Documents\mjukvaruteknik\exame
nsarbete\tqdt33\node_modules\xhr2-cookies\xml-http-request-event-target.ts:44:13)
at XMLHttpRequest._setReadyState (C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\t
qdt33\node_modules\xhr2-cookies\xml-http-request.ts:219:8)
at XMLHttpRequest._onHttpResponseEnd (C:\Users\Diddi\Documents\mjukvaruteknik\examensarbe
te\tqdt33\node_modules\xhr2-cookies\xml-http-request.ts:345:8)
at IncomingMessage.<anonymous> (C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\tqd
t33\node_modules\xhr2-cookies\xml-http-request.ts:311:39)
at IncomingMessage.emit (events.js:228:7)
at endReadableNT (_stream_readable.js:1185:12)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
(node:23524) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error origin
ated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 1)
(node:23524) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In th
e future, promise rejections that are not handled will terminate the Node.js process with a n
on-zero exit code.
TypeError: Cannot read property 'txElapsedTime' of undefined
at multipleTx (C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\tqdt33\client\cl-int
eraction\contractInteract.js:65:24)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at test (C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\tqdt33\client\cl-interacti
on\profile1.js:26:5)
await
only inside anasync
function. The first line of code in your question does not abide to that rule. And since your question is specifically about this assignment becoming undefined, you need to explain exactly how you are doing things. This code will not even start running! – goodvibration Apr 28 '20 at 9:56someAsyncFunc
anywhere in your code! Even if you did, it is nor returning anything, not is it assigning any non-local variable. So what exactly is the purpose of this function, and how do you expect the local variableexecutionMetrics
to have any meaning outside of it??? – goodvibration Apr 28 '20 at 11:02