Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
86f3b5a0
Unverified
Commit
86f3b5a0
authored
Sep 14, 2022
by
Zach Pomerantz
Committed by
GitHub
Sep 14, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: caching provider (#4615)
* feat: caching providers * feat: clear cache per block
parent
382a44f0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
+35
-0
providers.ts
src/constants/providers.ts
+35
-0
No files found.
src/constants/providers.ts
View file @
86f3b5a0
import
{
deepCopy
}
from
'
@ethersproject/properties
'
// This is the only file which should instantiate new Providers.
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import
{
StaticJsonRpcProvider
}
from
'
@ethersproject/providers
'
import
{
isPlain
}
from
'
@reduxjs/toolkit
'
import
{
SupportedChainId
}
from
'
./chains
'
import
{
RPC_URLS
}
from
'
./networks
'
class
AppJsonRpcProvider
extends
StaticJsonRpcProvider
{
private
_blockCache
=
new
Map
<
string
,
Promise
<
any
>>
()
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
(
urls
:
string
[])
{
super
(
urls
[
0
])
}
send
(
method
:
string
,
params
:
Array
<
any
>
):
Promise
<
any
>
{
// Only cache eth_call's.
if
(
method
!==
'
eth_call
'
)
return
super
.
send
(
method
,
params
)
// Only cache if params are serializable.
if
(
!
isPlain
(
params
))
return
super
.
send
(
method
,
params
)
const
key
=
`call:
${
JSON
.
stringify
(
params
)}
`
const
cached
=
this
.
blockCache
.
get
(
key
)
if
(
cached
)
{
this
.
emit
(
'
debug
'
,
{
action
:
'
request
'
,
request
:
deepCopy
({
method
,
params
,
id
:
'
cache
'
}),
provider
:
this
,
})
return
cached
}
const
result
=
super
.
send
(
method
,
params
)
this
.
blockCache
.
set
(
key
,
result
)
return
result
}
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment