Skip to content

Commit

Permalink
✨ server: poke when create account receipt timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nfmelendez committed Nov 14, 2024
1 parent 8cf89cd commit 1abc87c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
11 changes: 10 additions & 1 deletion server/hooks/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
}))
Expand Down
54 changes: 51 additions & 3 deletions server/test/hooks/activity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
Expand Down

0 comments on commit 1abc87c

Please sign in to comment.