From 1abc87c45b7620add79c7f874fbd02bc58cfa69c Mon Sep 17 00:00:00 2001 From: nfmelendez Date: Thu, 14 Nov 2024 14:40:37 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20server:=20poke=20when=20create=20ac?= =?UTF-8?q?count=20receipt=20timeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/hooks/activity.ts | 11 +++++- server/test/hooks/activity.test.ts | 54 ++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/server/hooks/activity.ts b/server/hooks/activity.ts index 3b4a8504..026d2a41 100644 --- a/server/hooks/activity.ts +++ b/server/hooks/activity.ts @@ -19,7 +19,13 @@ import createDebug from "debug"; import { inArray } from "drizzle-orm"; import { Hono } from "hono"; import * as v from "valibot"; -import { BaseError, bytesToBigInt, ContractFunctionRevertedError, zeroAddress } from "viem"; +import { + BaseError, + bytesToBigInt, + ContractFunctionRevertedError, + WaitForTransactionReceiptTimeoutError, + zeroAddress, +} from "viem"; import { optimism } from "viem/chains"; import database, { credentials } from "../database"; @@ -146,6 +152,9 @@ export default app.post( return receipt.status === "success"; } catch (error: unknown) { captureException(error, { level: "error" }); + if (error instanceof WaitForTransactionReceiptTimeoutError) { + return true; + } return false; } })) diff --git a/server/test/hooks/activity.test.ts b/server/test/hooks/activity.test.ts index 05f9cf61..31f593cf 100644 --- a/server/test/hooks/activity.test.ts +++ b/server/test/hooks/activity.test.ts @@ -17,6 +17,7 @@ import { bytesToHex, numberToBytes, type PrivateKeyAccount, + WaitForTransactionReceiptTimeoutError, } from "viem"; import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; import { afterEach, beforeEach, describe, expect, inject, it, vi } from "vitest"; @@ -137,11 +138,13 @@ describe("address activity", () => { expect(response.status).toBe(200); }); - it("fails with transaction timeout", async () => { + it.todo("fails with transaction timeout", async () => { const captureException = vi.spyOn(sentry, "captureException"); captureException.mockImplementation(() => ""); - vi.spyOn(publicClient, "waitForTransactionReceipt").mockRejectedValue(new Error("Transaction Timeout")); + vi.spyOn(publicClient, "waitForTransactionReceipt").mockRejectedValue( + new WaitForTransactionReceiptTimeoutError({ hash: zeroHash }), + ); const deposit = parseEther("5"); await anvilClient.setBalance({ address: account, value: deposit }); @@ -159,12 +162,57 @@ describe("address activity", () => { const deposits = await waitForDeposit(account, 1); - expect(captureException).toHaveBeenCalledWith(new Error("Transaction Timeout"), expect.anything()); + expect(captureException).toHaveBeenCalledWith( + new WaitForTransactionReceiptTimeoutError({ hash: zeroHash }), + expect.anything(), + ); expect(deposits).toHaveLength(0); expect(response.status).toBe(200); }); + it("pokesETH with transaction timeout", async () => { + const captureException = vi.spyOn(sentry, "captureException"); + captureException.mockImplementation(() => ""); + + vi.spyOn(publicClient, "waitForTransactionReceipt").mockRejectedValueOnce( + new WaitForTransactionReceiptTimeoutError({ hash: zeroHash }), + ); + + const deposit = parseEther("5"); + await anvilClient.setBalance({ address: account, value: deposit }); + + const response = await appClient.index.$post({ + ...activityPayload, + json: { + ...activityPayload.json, + event: { + ...activityPayload.json.event, + activity: [{ ...activityPayload.json.event.activity[0], toAddress: account }], + }, + }, + }); + + await waitForDeposit(account, 1); + + expect(captureException).toHaveBeenCalledWith( + new WaitForTransactionReceiptTimeoutError({ hash: zeroHash }), + expect.anything(), + ); + + const exactly = await publicClient.readContract({ + address: previewerAddress, + functionName: "exactly", + abi: previewerAbi, + args: [account], + }); + + const market = exactly.find((m) => m.asset === wethAddress); + + expect(market?.floatingDepositAssets).toBe(deposit); + expect(response.status).toBe(200); + }); + it("pokes eth", async () => { const deposit = parseEther("5"); await anvilClient.setBalance({ address: account, value: deposit });