Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readme #5

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions packages/eth-rpc-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# eth-rpc-cache

![NPM Version](https://img.shields.io/npm/v/eth-rpc-cache)

A simple cache for Ethereum RPC requests extensible with different caching strategies.

## Install

```sh
npm i eth-rpc-cache
```

## Usage

```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', [])
```

## API

### createEthRpcCache(rpc, options)

Returns a function

#### Parameters

##### rpc

Type: `Function`

A function that follows the [JSON-RPC specification](https://www.jsonrpc.org/specification). Its parameters are:

- `method` (string): The method to call
- `params` (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 protocol
- `id` (number): The request identifier
- `result` (any): The result of the request

##### options

Type: `object`
Default: `{}`

An optional configuration object

###### options.allowOthers (optional)

Type: `boolean`
Default: `true`

Whether the strategy should allow to run the RPC call to unknown methods. Throws if `false`

###### options.cache (optional)

Type: `object`
Default: `new Map()`

The cache storage.
Must implement these methods: `has(key)`, `set(key, value)`, `get(key)` and `delete(key)`

###### options.strategy (optional)

Type: `object[]`
Default: `[perBlockStrategy, permanentStrategy]`

Array of strategies to use to cache methods. If methods are repeated, the latter will take precedence over the former.
4 changes: 2 additions & 2 deletions packages/eth-rpc-cache/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eth-rpc-cache",
"version": "0.0.2",
"description": "A simple cache for Ethereum RPC requests",
"version": "1.0.0",
"description": "A simple cache for Ethereum RPC requests extensible with different caching strategies",
"keywords": [
"cache",
"eth",
Expand Down