A simple cache for Ethereum RPC requests extensible with different caching strategies
npm install eth-rpc-cacheA simple cache for Ethereum RPC requests extensible with different caching strategies.
``sh`
npm i eth-rpc-cache
`ts
import { createEthRpcCache } from 'eth-rpc-cache'
import { providers } from 'ethers'
const provider = new providers.JsonRpcProvider('https://rpc.url.org')
const cache = new Map()
const cachedProvider = {
...provider,
send: createEthRpcCache((method, params) => provider.send(method, params), {
cache
})
}
Object.setPrototypeOf(cachedProvider, Object.getPrototypeOf(cachedProvider))
// Will call the RPC endpoint and retrieve the chainId
await cachedProvider.send('eth_chainId', [])
// Will retrieve the value from the cache, and while the instance is alive, it will permanently be cached
await cachedProvider.send('eth_chainId', [])
// Will call the RPC endpoint and retrieve the current number
await cachedProvider.send('eth_blockNumber', [])
// This value will be cached for ~half block, so if requested again before that time passes, it will come from the cache
await cachedProvider.send('eth_blockNumber', [])
`
Returns a function
#### Parameters
##### rpc
Type: Function
A function that follows the JSON-RPC specification. Its parameters are:
- method (string): The method to callparams
- (Array): The parameters to pass to the method
and returns a Promise which resolves to an object with the following structure
- jsonrpc (string): Version of the protocolid
- (number): The request identifierresult
- (any): The result of the request
##### options
Type: object {}
Default:
An optional configuration object
###### options.allowOthers (optional)
Type: boolean true
Default:
Whether the strategy should allow to run the RPC call to unknown methods. Throws if false
###### options.cache (optional)
Type: object new Map()
Default:
The cache storage.
Must implement these methods: has(key), set(key, value), get(key) and delete(key)
###### options.strategy (optional)
Type: object[][perBlockStrategy, permanentStrategy]`
Default:
Array of strategies to use to cache methods. If methods are repeated, the latter will take precedence over the former.