Skip to content

Commit

Permalink
test: Add tests for waitForReceipt function
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 21, 2024
1 parent 8b9dfcf commit 5a9a255
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/thirdweb/src/storage/download.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { getRequestTimeoutConfig } from "../client/client.js";
import { getClientFetch } from "../utils/fetch.js";
import { resolveScheme, type ResolveSchemeOptions } from "../utils/ipfs.js";
import type { Prettify } from "../utils/type-utils.js";

export type DownloadOptions = ResolveSchemeOptions & {
requestTimeoutMs?: number;
};
export type DownloadOptions = Prettify<
ResolveSchemeOptions & {
requestTimeoutMs?: number;
}
>;

/**
* Downloads a file from the specified URI.
Expand Down
112 changes: 112 additions & 0 deletions packages/thirdweb/src/transaction/actions/wait-for-tx-receipt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { describe, it, expect, vi, beforeEach, afterAll } from "vitest";
import { waitForReceipt } from "./wait-for-tx-receipt.js";
import type { TransactionReceipt } from "viem";
import { transfer } from "../../extensions/erc20.js";
import { TEST_WALLET_B } from "../../../test/src/addresses.js";
import { USDC_CONTRACT } from "../../../test/src/test-contracts.js";
import * as rpcExports from "../../rpc/index.js";

const MOCK_TX_HASH = "0x1234567890abcdef";

const MOCK_SUCCESS_RECEIPT: TransactionReceipt = {
transactionHash: MOCK_TX_HASH,
blockNumber: 1234n,
status: "success",
blockHash: "0xabcdef1234567890",
contractAddress: "0x1234567890abcdef",
cumulativeGasUsed: 123456n,
from: "0xabcdef1234567890",
gasUsed: 123456n,
logs: [],
logsBloom: "0xabcdef1234567890",
to: "0x1234567890abcdef",
transactionIndex: 1234,
effectiveGasPrice: 123456n,
type: "legacy",
root: "0xabcdef1234567890",
blobGasPrice: 123456n,
blobGasUsed: 123456n,
};

const TRANSACTION = transfer({
to: TEST_WALLET_B,
amount: 100,
contract: USDC_CONTRACT,
});

const mockEthGetTransactionReceipt = vi.spyOn(
rpcExports,
"eth_getTransactionReceipt",
);

let emitBlockNumber: (blockNumber: bigint) => void;

vi.spyOn(rpcExports, "watchBlockNumber").mockImplementation(
({ onNewBlockNumber }) => {
emitBlockNumber = (blockNumber: bigint) => {
onNewBlockNumber(blockNumber);
};
return () => {};
},
);

describe("waitForReceipt", () => {
afterAll(() => {
vi.restoreAllMocks();
});

beforeEach(() => {
vi.clearAllMocks();
});

it("should resolve with transaction receipt when transactionHash is provided", async () => {
mockEthGetTransactionReceipt.mockResolvedValueOnce(MOCK_SUCCESS_RECEIPT);

// can't `await` here because we still need to be able to increment the block number below
const res = waitForReceipt({
transaction: TRANSACTION,
transactionHash: MOCK_TX_HASH,
});

emitBlockNumber(1n);

await expect(res).resolves.toMatchObject(MOCK_SUCCESS_RECEIPT);
});

it("should reject with an error when neither transactionHash nor userOpHash is provided", async () => {
// @ts-expect-error - this is what we're testing
const result = waitForReceipt({ transaction: TRANSACTION });

await expect(result).rejects.toThrow(
"Transaction has no txHash to wait for, did you execute it?",
);
expect(mockEthGetTransactionReceipt).not.toHaveBeenCalled();
});

it("should reject with an error when transaction is not found after waiting 10 blocks", async () => {
const result = waitForReceipt({
transaction: TRANSACTION,
transactionHash: MOCK_TX_HASH,
});

// this is actually 11 blocks because the "first" block does not count (will fire immediately)
emitBlockNumber(1n);
emitBlockNumber(2n);
emitBlockNumber(3n);
emitBlockNumber(4n);
emitBlockNumber(5n);
emitBlockNumber(6n);
emitBlockNumber(7n);
emitBlockNumber(8n);
emitBlockNumber(9n);
emitBlockNumber(10n);
emitBlockNumber(11n);

await expect(result).rejects.toThrow(
"Transaction not found after 10 blocks",
);
expect(mockEthGetTransactionReceipt).toHaveBeenCalledTimes(10);
});

// TODO userop tests
});

0 comments on commit 5a9a255

Please sign in to comment.