Solidity

未處理的承諾拒絕

  • April 28, 2020

下面的程式碼給了我超出限額或總是失敗的交易錯誤,但是當我在 remix 上檢查我的契約時,我可以看到交易通過,進行了正確的狀態更改並按預期工作。在 分配executionMetrics變得未定義。我已經坐了 4 個小時試圖找出問題所在,但我完全被困住了。有誰知道發生了什麼?

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)
   })
}

呼叫的智能合約函式:

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

錯誤日誌:

(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)

正如 goodvibration 在他的評論中指出的那樣,這不是乙太坊或 web3 問題,而是純粹的 javascript/基本程式錯誤。程式碼中的一個大錯誤是在 - 語句中返回then()並假設它將是executePromises(). 事實並非如此,我已經糾正了它。

引用自:https://ethereum.stackexchange.com/questions/82867