Skip to content

Better error handling for transactions in tasks #52

@coinop-logan

Description

@coinop-logan

While running the create-emp, add-asset, and close-asset tasks, a few times the deploy script would crash due to an unexpected assert. Here was one in add-asset:

$ npx hardhat add-asset --network kovan --type emp --address 0x46708bA0405Fa12D6F788Ab59ad1aBA1DC2Fb328
Adding emp...
An unexpected error occurred:

AssertionError: expected '0x0EeEf8CB1C92AEF64dd36FE1f3882D19518…' to equal '0x46708bA0405Fa12D6F788Ab59ad1aBA1DC2…'
   at SimpleTaskDefinition.action (/home/oglog/dev/sumero/contracts/tasks/add-asset.js:18:78)
   at processTicksAndRejections (node:internal/process/task_queues:96:5)
   at Environment._runTaskDefinition (/home/oglog/dev/sumero/contracts/node_modules/.pnpm/hardhat@2.9.1/node_modules/hardhat/src/internal/core/runtime-environment.ts:217:14)
   at Environment.run (/home/oglog/dev/sumero/contracts/node_modules/.pnpm/hardhat@2.9.1/node_modules/hardhat/src/internal/core/runtime-environment.ts:129:14)
   at main (/home/oglog/dev/sumero/contracts/node_modules/.pnpm/hardhat@2.9.1/node_modules/hardhat/src/internal/cli/cli.ts:214:5) {
 showDiff: true,
 actual: '0x0EeEf8CB1C92AEF64dd36FE1f3882D195180AD5c',
 expected: '0x46708bA0405Fa12D6F788Ab59ad1aBA1DC2Fb328',
 operator: 'strictEqual'
}

See these three lines, the last of which caused the error:

tx = await assetManager.addEmp(args.address)
const totalEMP = await assetManager.totalEmpAssets()
expect((await assetManager.idToVerifiedEmps(totalEMP)).addr).eq(ethers.utils.getAddress(args.address))`

I believe what's happening here is that the first transaction (line 1) fails to actually broadcast the transaction, but this is missed due to there being no catch block; then since the blockchain state was never changed, we get the address of the previously added asset. This is confirmed by the fact that when this occurred, the deployment account had not broadcast anything that I could see on etherscan.

The solution would seem to be to add better error catching to when we are awaiting on outgoing transactions, so we get more instructive error messages. In my case this would have let me just try the task again instead of going on a goose hunt for a bit!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions