Skip to content

Commit

Permalink
Cache eth_calls in our InstrumentedEVMProvider (#406)
Browse files Browse the repository at this point in the history
* Cache eth_calls in our InstrumentedEVMProvider

* add a try/catch block

* method visibility
  • Loading branch information
mikeki authored Nov 3, 2023
1 parent 72e9695 commit 5f89c24
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/handlers/evm/provider/InstrumentedEVMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,43 @@ export type InstrumentedEVMProviderProps = {
export class InstrumentedEVMProvider extends ethers.providers.StaticJsonRpcProvider {
private readonly name: ProviderName
private readonly metricPrefix: string
private _blockCache = new Map<string, Promise<any>>()

private get blockCache() {
// If the blockCache has not yet been initialized this block, do so by
// setting a listener to clear it on the next block.
if (!this._blockCache.size) {
this.once('block', () => this._blockCache.clear())
}
return this._blockCache
}

constructor({ url, network, name }: InstrumentedEVMProviderProps) {
super(url, network)
this.name = name
this.metricPrefix = `RPC_${this.name}_${this.network.chainId}`
}

// Adds caching functionality to the RPC provider
override send(method: string, params: Array<any>): Promise<any> {
// Only cache eth_call's.
if (method !== 'eth_call') return super.send(method, params)

try {
const key = `call:${JSON.stringify(params)}`
const cached = this.blockCache.get(key)
if (cached) {
return cached
}

const result = super.send(method, params)
this.blockCache.set(key, result)
return result
} catch (e) {
return super.send(method, params)
}
}

override call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
return super
.call(transaction, blockTag)
Expand Down

0 comments on commit 5f89c24

Please sign in to comment.