• 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.

    Parameters

    • provider: AnyProvider

      Any existing provider

    • codeCache: Record<string, string>

      Object containing address => code mappings

    Returns Provider

    • Provider that will return a fixed getCode result for items defined in codeCache.
    const address = "0x0000000000000000000000000000000000000001";
    const bytecode = "0x6001600101"
    const cachedProvider = WithCachedCode(provider, {
    [address]: bytecode,
    });
    const code = await cachedProvider.getCode(address);
    console.log(code); // "0x6001600101"