From 8e6ad7c9e6b998ce47f4ea905a3a578f97b7ee97 Mon Sep 17 00:00:00 2001 From: Yota Hamada Date: Tue, 7 Jan 2025 18:51:50 +0900 Subject: [PATCH] autoload: Add AutoloadResult.hasCode --- src/__tests__/auto.test.ts | 9 +++++++++ src/auto.ts | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/__tests__/auto.test.ts b/src/__tests__/auto.test.ts index acd294d..acf3d4e 100644 --- a/src/__tests__/auto.test.ts +++ b/src/__tests__/auto.test.ts @@ -17,6 +17,15 @@ test('autoload throws typed error', async () => { await expect(autoload("abc.eth", { provider: fakeProvider })).rejects.toThrow(/Failed to resolve ENS/); }); +test('autoload sets hasCode to false if code is empty', async () => { + const fakeProvider = (code: string) => ({ + request: () => code, + }); + const address = "0x00000000219ab540356cBB839Cbe05303d7705Fa" + await expect(autoload(address, { provider: fakeProvider("0x") })).resolves.toMatchObject({ hasCode: false }); + await expect(autoload(address, { provider: fakeProvider("0x1234") })).resolves.toMatchObject({ hasCode: true }); +}); + online_test('autoload selectors', async ({ provider }) => { const address = "0x4A137FD5e7a256eF08A7De531A17D0BE0cc7B6b6"; // Random unverified contract const { abi } = await autoload(address, { diff --git a/src/auto.ts b/src/auto.ts index 36f7601..6507fc6 100644 --- a/src/auto.ts +++ b/src/auto.ts @@ -50,6 +50,9 @@ export type AutoloadResult = { * @experimental */ isFactory?: boolean; + + /** Set to true if the address has deployed code */ + hasCode: boolean; } @@ -157,6 +160,7 @@ export async function autoload(address: string, config: AutoloadConfig): Promise address, abi: [], proxies: [], + hasCode: false, }; let abiLoader = config.abiLoader; @@ -191,7 +195,8 @@ export async function autoload(address: string, config: AutoloadConfig): Promise }, ); } - if (!bytecode) return result; // Must be an EOA + if (!bytecode || bytecode === "0x") return result; // Must be an EOA + result.hasCode = true; const program = disasm(bytecode);