|
| 1 | +import type { EIP1193RequestFn, EIP1474Methods } from "viem"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { ANVIL_CHAIN } from "../../../test/src/chains.js"; |
| 4 | +import { TEST_CLIENT } from "../../../test/src/test-clients.js"; |
| 5 | +import { getBytecode } from "../../contract/actions/get-bytecode.js"; |
| 6 | +import { eth_getStorageAt } from "../../rpc/actions/eth_getStorageAt.js"; |
| 7 | +import { getRpcClient } from "../../rpc/rpc.js"; |
| 8 | +import { resolveImplementation } from "./resolveImplementation.js"; |
| 9 | + |
| 10 | +// Mock dependencies |
| 11 | +vi.mock("../../contract/actions/get-bytecode.js"); |
| 12 | +vi.mock("../../rpc/rpc.js"); |
| 13 | +vi.mock("../../rpc/actions/eth_getStorageAt.js"); |
| 14 | + |
| 15 | +describe.runIf(process.env.TW_SECRET_KEY)( |
| 16 | + "Handle beacon proxy pattern", |
| 17 | + async () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.resetAllMocks(); |
| 20 | + }); |
| 21 | + it("should handle beacon proxy pattern with short storage values", async () => { |
| 22 | + // This test verifies that the implementation can handle short storage values |
| 23 | + // which was the original issue we were trying to fix |
| 24 | + const mockContract = { |
| 25 | + address: "0x0000000000000000000000000000000000000001", |
| 26 | + client: TEST_CLIENT, |
| 27 | + chain: ANVIL_CHAIN, |
| 28 | + } as const; |
| 29 | + |
| 30 | + // Mock getBytecode to return a minimal proxy bytecode that will trigger the beacon check |
| 31 | + const mockGetBytecode = vi |
| 32 | + .fn() |
| 33 | + .mockResolvedValueOnce("") // First call for proxy (empty bytecode triggers beacon check) |
| 34 | + .mockResolvedValueOnce("0x1234"); // Second call for implementation |
| 35 | + |
| 36 | + vi.mocked(getBytecode).mockImplementation(mockGetBytecode); |
| 37 | + |
| 38 | + // Mock eth_getStorageAt to return a short value |
| 39 | + const mockRpcRequest = { request: vi.fn() }; |
| 40 | + vi.mocked(getRpcClient).mockReturnValue( |
| 41 | + mockRpcRequest as unknown as EIP1193RequestFn<EIP1474Methods>, |
| 42 | + ); |
| 43 | + vi.mocked(eth_getStorageAt).mockResolvedValue("0x1234"); // Short storage value |
| 44 | + |
| 45 | + const result = await resolveImplementation(mockContract); |
| 46 | + |
| 47 | + // Should not throw and should return some result (even if it's the original address) |
| 48 | + expect(result).toBeDefined(); |
| 49 | + expect(result.address).toBeDefined(); |
| 50 | + }); |
| 51 | + }, |
| 52 | +); |
0 commit comments