From a510911229c85c5cf488870829d7c6d4c6968e62 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Fri, 3 Jan 2025 17:26:44 +0100 Subject: [PATCH] fix(trace): only throw when no receipt --- .github/workflows/test.yml | 2 +- package.json | 2 +- src/format.ts | 9 ++++++--- src/middleware.ts | 5 ++--- test/traceCall.test.ts | 12 ++++++------ yarn.lock | 18 +++++++++--------- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0eb7a98..3200c53 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,7 +21,7 @@ jobs: node-version: 22 cache: yarn - - uses: foundry-rs/foundry-toolchain@v1.2.0 + - uses: foundry-rs/foundry-toolchain@v1 - run: yarn --frozen-lockfile diff --git a/package.json b/package.json index c2add96..b0a33c1 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "lodash.kebabcase": "^4.1.1", "semantic-release": "^24.2.0", "typescript": "^5.7.2", - "viem": "^2.21.54", + "viem": "^2.22.2", "viem-deal": "^2.0.4", "vitest": "^2.1.8" }, diff --git a/src/format.ts b/src/format.ts index 51380b2..49f9c21 100644 --- a/src/format.ts +++ b/src/format.ts @@ -1,6 +1,6 @@ -import { readFileSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import colors from "colors/safe"; import { type Address, @@ -189,7 +189,10 @@ export async function formatFullTrace( signatures.events[sig as Hex] = match; }); - writeFileSync(getSignaturesCachePath(), JSON.stringify(signatures)); + const path = getSignaturesCachePath(); + if (!existsSync(path)) mkdirSync(dirname(path), { recursive: true }); + + writeFileSync(path, JSON.stringify(signatures)); } else { console.warn( `Failed to fetch signatures for unknown selectors: ${unknownFunctionSelectors},${unknownEventSelectors}`, diff --git a/src/middleware.ts b/src/middleware.ts index 86f9f22..0a3a83e 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -143,9 +143,8 @@ export function traced( await new Promise((resolve) => setTimeout(resolve, 250)); } - if (receipt?.status === "0x0") throw await traceCall(); - - throw new WaitForTransactionReceiptTimeoutError({ hash: res as Hash }); + if (!receipt) throw new WaitForTransactionReceiptTimeoutError({ hash: res as Hash }); + if (receipt.status === "0x0") throw await traceCall(); } catch (error) { if (error instanceof ExecutionRevertedTraceError) throw error; diff --git a/test/traceCall.test.ts b/test/traceCall.test.ts index f8a343b..4278e35 100644 --- a/test/traceCall.test.ts +++ b/test/traceCall.test.ts @@ -81,7 +81,7 @@ describe("traceCall", () => { 0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance 1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance - Version: viem@2.21.54] + Version: viem@2.22.2] `); }); @@ -108,7 +108,7 @@ describe("traceCall", () => { 0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer from the zero address 1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer from the zero address - Version: viem@2.21.54] + Version: viem@2.22.2] `); }); @@ -133,7 +133,7 @@ describe("traceCall", () => { sender: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 Docs: https://viem.sh/docs/contract/writeContract - Version: viem@2.21.54] + Version: viem@2.22.2] `); client.transport.tracer.failed = true; @@ -173,7 +173,7 @@ describe("traceCall", () => { 0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance 1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance - Version: viem@2.21.54] + Version: viem@2.22.2] `); }); @@ -232,7 +232,7 @@ describe("traceCall", () => { sender: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 Docs: https://viem.sh/docs/contract/writeContract - Version: viem@2.21.54] + Version: viem@2.22.2] `); await expect( @@ -270,7 +270,7 @@ describe("traceCall", () => { 0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance 1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance - Version: viem@2.21.54] + Version: viem@2.22.2] `); }); }); diff --git a/yarn.lock b/yarn.lock index 6638fd1..a4e2f2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3496,10 +3496,10 @@ onetime@^7.0.0: dependencies: mimic-function "^5.0.0" -ox@0.1.2: - version "0.1.2" - resolved "https://registry.yarnpkg.com/ox/-/ox-0.1.2.tgz#0f791be2ccabeaf4928e6d423498fe1c8094e560" - integrity sha512-ak/8K0Rtphg9vnRJlbOdaX9R7cmxD2MiSthjWGaQdMk3D7hrAlDoM+6Lxn7hN52Za3vrXfZ7enfke/5WjolDww== +ox@0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/ox/-/ox-0.6.0.tgz#ba8f89d68b5e8afe717c8a947ffacc0669ea155d" + integrity sha512-blUzTLidvUlshv0O02CnLFqBLidNzPoAZdIth894avUAotTuWziznv6IENv5idRuOSSP3dH8WzcYw84zVdu0Aw== dependencies: "@adraffy/ens-normalize" "^1.10.1" "@noble/curves" "^1.6.0" @@ -4688,10 +4688,10 @@ viem-deal@^2.0.4: resolved "https://registry.yarnpkg.com/viem-deal/-/viem-deal-2.0.4.tgz#308f8817893c4342182cdc944667e2acdabced13" integrity sha512-gd1+pnLissjxYbaJ8u2UtdOOpv0o4W8RsVlSwy5w+SEasi/f5VJvrCVmE8cBrcHpvWYqoaxwj4VfJG45oLPnyQ== -viem@^2.21.54: - version "2.21.54" - resolved "https://registry.yarnpkg.com/viem/-/viem-2.21.54.tgz#76d6f86ab8809078f1ac140ac1a2beadbc86b9f6" - integrity sha512-G9mmtbua3UtnVY9BqAtWdNp+3AO+oWhD0B9KaEsZb6gcrOWgmA4rz02yqEMg+qW9m6KgKGie7q3zcHqJIw6AqA== +viem@^2.22.2: + version "2.22.2" + resolved "https://registry.yarnpkg.com/viem/-/viem-2.22.2.tgz#ec25affced2491ea3984cc8ce5d3d4b760ae85b1" + integrity sha512-HNATy2rwkQcC1JRYBJJujJTtVtDkWlQEB7GvAtn4GBXdbBROnjs8pGXwJAWhb/crErVk5yJ/Fh50yGtA9VZh8A== dependencies: "@noble/curves" "1.7.0" "@noble/hashes" "1.6.1" @@ -4699,7 +4699,7 @@ viem@^2.21.54: "@scure/bip39" "1.5.0" abitype "1.0.7" isows "1.0.6" - ox "0.1.2" + ox "0.6.0" webauthn-p256 "0.0.10" ws "8.18.0"