Skip to content

Commit

Permalink
src/providers.ts: WithCachedCode, include prototype chain of Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Oct 3, 2024
1 parent 8b4cd2c commit a5c5fc0
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export function CompatibleProvider(provider: any): Provider {
/**
* Wrap an existing provider into one that will return a fixed getCode result for items defined in codeCache.
* The cache is treated as read-only, it will not be updated. Mainly used to avoid an extra RPC call when we already have the bytcode.
*
* For more advanced behaviours, consider copying this code and modifying it to your needs.
*
* @param provider - Any existing provider
* @param codeCache - Object containing address => code mappings
* @returns {Provider} - Provider that will return a fixed getCode result for items defined in codeCache.
Expand All @@ -99,15 +102,14 @@ export function CompatibleProvider(provider: any): Provider {
*/
export function WithCachedCode(provider: AnyProvider, codeCache: Record<string, string>): Provider {
const compatibleProvider = CompatibleProvider(provider);
return {
...compatibleProvider,
async getCode(address: string): Promise<string> {
if (codeCache[address]) {
return codeCache[address];
}
return await compatibleProvider.getCode(address);
const p = Object.create(compatibleProvider); // use compatibleProvider as the prototype
p.getCode = async function getCode(address: string): Promise<string> {
if (codeCache[address]) {
return codeCache[address];
}
return await compatibleProvider.getCode(address);
};
return p;
}


Expand Down

0 comments on commit a5c5fc0

Please sign in to comment.