Skip to content

Commit

Permalink
Merge pull request #134 from shazow/fix-withcachedcode
Browse files Browse the repository at this point in the history
providers: WithCachedCode should accept AnyProvider and make it compatible
  • Loading branch information
shazow authored Oct 2, 2024
2 parents 3f46d69 + 3590c04 commit 1a49c76
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/__tests__/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fakeProvider = whatsabi.providers.CompatibleProvider({
request: () => { },
});

test('provider WithCachedCode helper', async () => {
test('provider WithCachedCode with CompatibleProvider', async () => {
const address = "0x0000000000000000000000000000000000000001";
const provider = whatsabi.providers.WithCachedCode(fakeProvider, {
[address]: "0xf00",
Expand All @@ -16,3 +16,16 @@ test('provider WithCachedCode helper', async () => {
expect(await provider.getCode(address)).toStrictEqual("0xf00");
expect(await provider.getCode("not cached")).toBeUndefined();
});

test('provider WithCachedCode with AnyProvider', async () => {
const anyProvider = {
request: () => { },
};
const address = "0x0000000000000000000000000000000000000001";
const provider = whatsabi.providers.WithCachedCode(anyProvider, {
[address]: "0xf00",
});

expect(await provider.getCode(address)).toStrictEqual("0xf00");
expect(await provider.getCode("not cached")).toBeUndefined();
});
24 changes: 20 additions & 4 deletions src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ENSProvider {
getAddress(name: string): Promise<string>;
}

export interface Provider extends StorageProvider, CallProvider, CodeProvider, ENSProvider { };
export interface Provider extends StorageProvider, CallProvider, CodeProvider, ENSProvider {};


export interface AnyProvider { }; // TODO: Can we narrow this more?
Expand All @@ -36,7 +36,22 @@ interface EIP1193 {

// Abstract away web3 provider inconsistencies

function isCompatibleProvider(provider: any): boolean {
// FIXME: Is there a better way to use the TypeScript type system to do this?
// `provider isinstance Provider` does not work because Provider is an interface, not a class. Should it be?
return (
typeof provider.getStorageAt === "function" &&
typeof provider.call === "function" &&
typeof provider.getCode === "function" &&
typeof provider.getAddress === "function"
);
}

export function CompatibleProvider(provider: any): Provider {
if (isCompatibleProvider(provider)) {
// Already compatible, avoid rewrapping it
return provider;
}
if (typeof provider.getAddress === "function") {
return new HighLevelProvider(provider);
}
Expand Down Expand Up @@ -82,14 +97,15 @@ export function CompatibleProvider(provider: any): Provider {
* console.log(code); // "0x6001600101"
* ```
*/
export function WithCachedCode(provider: Provider, codeCache: Record<string, string>): Provider {
export function WithCachedCode(provider: AnyProvider, codeCache: Record<string, string>): Provider {
const compatibleProvider = CompatibleProvider(provider);
return {
...provider,
...compatibleProvider,
async getCode(address: string): Promise<string> {
if (codeCache[address]) {
return codeCache[address];
}
return await provider.getCode(address);
return await compatibleProvider.getCode(address);
}
};
}
Expand Down

0 comments on commit 1a49c76

Please sign in to comment.