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
f9fb71a8
Unverified
Commit
f9fb71a8
authored
Mar 11, 2022
by
Zach Pomerantz
Committed by
GitHub
Mar 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(perf): cache pools to avoid cost of instantiation (#3499)
parent
59d00464
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
27 deletions
+70
-27
usePools.ts
src/hooks/usePools.ts
+70
-27
No files found.
src/hooks/usePools.ts
View file @
f9fb71a8
import
{
Interface
}
from
'
@ethersproject/abi
'
import
{
Interface
}
from
'
@ethersproject/abi
'
import
{
Currency
,
Token
}
from
'
@uniswap/sdk-core
'
import
{
BigintIsh
,
Currency
,
Token
}
from
'
@uniswap/sdk-core
'
import
IUniswapV3PoolStateJson
from
'
@uniswap/v3-core/artifacts/contracts/interfaces/pool/IUniswapV3PoolState.sol/IUniswapV3PoolState.json
'
import
IUniswapV3PoolStateJson
from
'
@uniswap/v3-core/artifacts/contracts/interfaces/pool/IUniswapV3PoolState.sol/IUniswapV3PoolState.json
'
import
{
computePoolAddress
}
from
'
@uniswap/v3-sdk
'
import
{
computePoolAddress
}
from
'
@uniswap/v3-sdk
'
import
{
FeeAmount
,
Pool
}
from
'
@uniswap/v3-sdk
'
import
{
FeeAmount
,
Pool
}
from
'
@uniswap/v3-sdk
'
import
useActiveWeb3React
from
'
hooks/useActiveWeb3React
'
import
useActiveWeb3React
from
'
hooks/useActiveWeb3React
'
import
JSBI
from
'
jsbi
'
import
{
useMultipleContractSingleData
}
from
'
lib/hooks/multicall
'
import
{
useMultipleContractSingleData
}
from
'
lib/hooks/multicall
'
import
{
useMemo
}
from
'
react
'
import
{
useMemo
}
from
'
react
'
...
@@ -14,6 +15,44 @@ const { abi: IUniswapV3PoolStateABI } = IUniswapV3PoolStateJson
...
@@ -14,6 +15,44 @@ const { abi: IUniswapV3PoolStateABI } = IUniswapV3PoolStateJson
const
POOL_STATE_INTERFACE
=
new
Interface
(
IUniswapV3PoolStateABI
)
as
IUniswapV3PoolStateInterface
const
POOL_STATE_INTERFACE
=
new
Interface
(
IUniswapV3PoolStateABI
)
as
IUniswapV3PoolStateInterface
// Classes are expensive to instantiate, so this caches the recently instantiated pools.
// This avoids re-instantiating pools as the other pools in the same request are loaded.
class
PoolCache
{
// pools is a FIFO, using unshift/pop. This makes recent entries faster to find.
private
static
pools
:
Pool
[]
=
[]
static
getPool
(
tokenA
:
Token
,
tokenB
:
Token
,
fee
:
FeeAmount
,
sqrtPriceX96
:
BigintIsh
,
liquidity
:
BigintIsh
,
tick
:
number
):
Pool
{
// Evict after 128 entries. Empirically, a swap uses 64 entries.
if
(
this
.
pools
.
length
>
128
)
{
this
.
pools
=
this
.
pools
.
slice
(
0
,
64
)
}
const
found
=
this
.
pools
.
find
(
(
pool
)
=>
pool
.
token0
===
tokenA
&&
pool
.
token1
===
tokenB
&&
pool
.
fee
===
fee
&&
JSBI
.
EQ
(
pool
.
sqrtRatioX96
,
sqrtPriceX96
)
&&
JSBI
.
EQ
(
pool
.
liquidity
,
liquidity
)
&&
pool
.
tickCurrent
===
tick
)
if
(
found
)
{
return
found
}
const
pool
=
new
Pool
(
tokenA
,
tokenB
,
fee
,
sqrtPriceX96
,
liquidity
,
tick
)
this
.
pools
.
unshift
(
pool
)
return
pool
}
}
export
enum
PoolState
{
export
enum
PoolState
{
LOADING
,
LOADING
,
NOT_EXISTS
,
NOT_EXISTS
,
...
@@ -26,58 +65,62 @@ export function usePools(
...
@@ -26,58 +65,62 @@ export function usePools(
):
[
PoolState
,
Pool
|
null
][]
{
):
[
PoolState
,
Pool
|
null
][]
{
const
{
chainId
}
=
useActiveWeb3React
()
const
{
chainId
}
=
useActiveWeb3React
()
const
transformed
:
([
Token
,
Token
,
FeeAmount
]
|
null
)[]
=
useMemo
(()
=>
{
const
poolTokens
:
([
Token
,
Token
,
FeeAmount
]
|
undefined
)[]
=
useMemo
(()
=>
{
if
(
!
chainId
)
return
new
Array
(
poolKeys
.
length
)
return
poolKeys
.
map
(([
currencyA
,
currencyB
,
feeAmount
])
=>
{
return
poolKeys
.
map
(([
currencyA
,
currencyB
,
feeAmount
])
=>
{
if
(
!
chainId
||
!
currencyA
||
!
currencyB
||
!
feeAmount
)
return
null
if
(
currencyA
&&
currencyB
&&
feeAmount
)
{
const
tokenA
=
currencyA
.
wrapped
const
tokenB
=
currencyB
.
wrapped
if
(
tokenA
.
equals
(
tokenB
))
return
undefined
const
tokenA
=
currencyA
?.
wrapped
return
tokenA
.
sortsBefore
(
tokenB
)
?
[
tokenA
,
tokenB
,
feeAmount
]
:
[
tokenB
,
tokenA
,
feeAmount
]
const
tokenB
=
currencyB
?.
wrapped
}
if
(
!
tokenA
||
!
tokenB
||
tokenA
.
equals
(
tokenB
))
return
null
return
undefined
const
[
token0
,
token1
]
=
tokenA
.
sortsBefore
(
tokenB
)
?
[
tokenA
,
tokenB
]
:
[
tokenB
,
tokenA
]
return
[
token0
,
token1
,
feeAmount
]
})
})
},
[
chainId
,
poolKeys
])
},
[
chainId
,
poolKeys
])
const
poolAddresses
:
(
string
|
undefined
)[]
=
useMemo
(()
=>
{
const
poolAddresses
:
(
string
|
undefined
)[]
=
useMemo
(()
=>
{
const
v3CoreFactoryAddress
=
chainId
&&
V3_CORE_FACTORY_ADDRESSES
[
chainId
]
const
v3CoreFactoryAddress
=
chainId
&&
V3_CORE_FACTORY_ADDRESSES
[
chainId
]
if
(
!
v3CoreFactoryAddress
)
return
new
Array
(
poolTokens
.
length
)
return
transformed
.
map
((
value
)
=>
{
return
poolTokens
.
map
(
if
(
!
v3CoreFactoryAddress
||
!
value
)
return
undefined
(
value
)
=>
return
computePoolAddress
({
value
&&
computePoolAddress
({
factoryAddress
:
v3CoreFactoryAddress
,
factoryAddress
:
v3CoreFactoryAddress
,
tokenA
:
value
[
0
],
tokenA
:
value
[
0
],
tokenB
:
value
[
1
],
tokenB
:
value
[
1
],
fee
:
value
[
2
],
fee
:
value
[
2
],
})
})
}
)
)
},
[
chainId
,
transformed
])
},
[
chainId
,
poolTokens
])
const
slot0s
=
useMultipleContractSingleData
(
poolAddresses
,
POOL_STATE_INTERFACE
,
'
slot0
'
)
const
slot0s
=
useMultipleContractSingleData
(
poolAddresses
,
POOL_STATE_INTERFACE
,
'
slot0
'
)
const
liquidities
=
useMultipleContractSingleData
(
poolAddresses
,
POOL_STATE_INTERFACE
,
'
liquidity
'
)
const
liquidities
=
useMultipleContractSingleData
(
poolAddresses
,
POOL_STATE_INTERFACE
,
'
liquidity
'
)
return
useMemo
(()
=>
{
return
useMemo
(()
=>
{
return
poolKeys
.
map
((
_key
,
index
)
=>
{
return
poolKeys
.
map
((
_key
,
index
)
=>
{
const
[
token0
,
token1
,
fee
]
=
transformed
[
index
]
??
[]
const
tokens
=
poolTokens
[
index
]
if
(
!
token0
||
!
token1
||
!
fee
)
return
[
PoolState
.
INVALID
,
null
]
if
(
!
tokens
)
return
[
PoolState
.
INVALID
,
null
]
const
[
token0
,
token1
,
fee
]
=
tokens
const
{
result
:
slot0
,
loading
:
slot0Loading
,
valid
:
slot0Valid
}
=
slot0s
[
index
]
const
{
result
:
slot0
,
loading
:
slot0Loading
,
valid
:
slot0Valid
}
=
slot0s
[
index
]
const
{
result
:
liquidity
,
loading
:
liquidityLoading
,
valid
:
liquidityValid
}
=
liquidities
[
index
]
const
{
result
:
liquidity
,
loading
:
liquidityLoading
,
valid
:
liquidityValid
}
=
liquidities
[
index
]
if
(
!
tokens
||
!
slot0Valid
||
!
liquidityValid
)
return
[
PoolState
.
INVALID
,
null
]
if
(
!
slot0Valid
||
!
liquidityValid
)
return
[
PoolState
.
INVALID
,
null
]
if
(
slot0Loading
||
liquidityLoading
)
return
[
PoolState
.
LOADING
,
null
]
if
(
slot0Loading
||
liquidityLoading
)
return
[
PoolState
.
LOADING
,
null
]
if
(
!
slot0
||
!
liquidity
)
return
[
PoolState
.
NOT_EXISTS
,
null
]
if
(
!
slot0
||
!
liquidity
)
return
[
PoolState
.
NOT_EXISTS
,
null
]
if
(
!
slot0
.
sqrtPriceX96
||
slot0
.
sqrtPriceX96
.
eq
(
0
))
return
[
PoolState
.
NOT_EXISTS
,
null
]
if
(
!
slot0
.
sqrtPriceX96
||
slot0
.
sqrtPriceX96
.
eq
(
0
))
return
[
PoolState
.
NOT_EXISTS
,
null
]
try
{
try
{
return
[
PoolState
.
EXISTS
,
new
Pool
(
token0
,
token1
,
fee
,
slot0
.
sqrtPriceX96
,
liquidity
[
0
],
slot0
.
tick
)]
const
pool
=
PoolCache
.
getPool
(
token0
,
token1
,
fee
,
slot0
.
sqrtPriceX96
,
liquidity
[
0
],
slot0
.
tick
)
return
[
PoolState
.
EXISTS
,
pool
]
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Error when constructing the pool
'
,
error
)
console
.
error
(
'
Error when constructing the pool
'
,
error
)
return
[
PoolState
.
NOT_EXISTS
,
null
]
return
[
PoolState
.
NOT_EXISTS
,
null
]
}
}
})
})
},
[
liquidities
,
poolKeys
,
slot0s
,
transformed
])
},
[
liquidities
,
poolKeys
,
slot0s
,
poolTokens
])
}
}
export
function
usePool
(
export
function
usePool
(
...
...
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