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
bff3811f
Unverified
Commit
bff3811f
authored
Apr 23, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use multicall2
parent
60d1f874
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
52 additions
and
40 deletions
+52
-40
index.ts
src/constants/index.ts
+1
-1
useContract.ts
src/hooks/useContract.ts
+4
-3
useCurrentBlockTimestamp.ts
src/hooks/useCurrentBlockTimestamp.ts
+2
-2
useV3Positions.ts
src/hooks/useV3Positions.ts
+3
-7
index.tsx
src/pages/Pool/index.tsx
+4
-5
updater.tsx
src/state/multicall/updater.tsx
+36
-20
hooks.ts
src/state/wallet/hooks.ts
+2
-2
No files found.
src/constants/index.ts
View file @
bff3811f
...
@@ -10,7 +10,7 @@ import COINBASE_ICON_URL from '../assets/images/coinbaseWalletIcon.svg'
...
@@ -10,7 +10,7 @@ import COINBASE_ICON_URL from '../assets/images/coinbaseWalletIcon.svg'
import
FORTMATIC_ICON_URL
from
'
../assets/images/fortmaticIcon.png
'
import
FORTMATIC_ICON_URL
from
'
../assets/images/fortmaticIcon.png
'
import
PORTIS_ICON_URL
from
'
../assets/images/portisIcon.png
'
import
PORTIS_ICON_URL
from
'
../assets/images/portisIcon.png
'
export
const
MULTICALL_ADDRESSES
:
{
[
chainId
in
ChainId
]:
string
}
=
{
export
const
MULTICALL
2
_ADDRESSES
:
{
[
chainId
in
ChainId
]:
string
}
=
{
[
ChainId
.
MAINNET
]:
''
,
[
ChainId
.
MAINNET
]:
''
,
[
ChainId
.
ROPSTEN
]:
'
0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696
'
,
[
ChainId
.
ROPSTEN
]:
'
0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696
'
,
[
ChainId
.
KOVAN
]:
'
0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696
'
,
[
ChainId
.
KOVAN
]:
'
0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696
'
,
...
...
src/hooks/useContract.ts
View file @
bff3811f
...
@@ -26,7 +26,7 @@ import {
...
@@ -26,7 +26,7 @@ import {
MERKLE_DISTRIBUTOR_ADDRESS
,
MERKLE_DISTRIBUTOR_ADDRESS
,
V1_MIGRATOR_ADDRESS
,
V1_MIGRATOR_ADDRESS
,
UNI
,
UNI
,
MULTICALL_ADDRESSES
,
MULTICALL
2
_ADDRESSES
,
}
from
'
constants/index
'
}
from
'
constants/index
'
import
{
abi
as
NFTPositionManagerABI
}
from
'
@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json
'
import
{
abi
as
NFTPositionManagerABI
}
from
'
@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json
'
import
{
import
{
...
@@ -40,6 +40,7 @@ import { TickLens, UniswapV3Factory, UniswapV3Pool } from 'types/v3'
...
@@ -40,6 +40,7 @@ import { TickLens, UniswapV3Factory, UniswapV3Pool } from 'types/v3'
import
{
NonfungiblePositionManager
}
from
'
types/v3/NonfungiblePositionManager
'
import
{
NonfungiblePositionManager
}
from
'
types/v3/NonfungiblePositionManager
'
import
{
V3Migrator
}
from
'
types/v3/V3Migrator
'
import
{
V3Migrator
}
from
'
types/v3/V3Migrator
'
import
{
getContract
}
from
'
utils
'
import
{
getContract
}
from
'
utils
'
import
{
Multicall2
}
from
'
../abis/types
'
import
{
useActiveWeb3React
}
from
'
./index
'
import
{
useActiveWeb3React
}
from
'
./index
'
// returns null on errors
// returns null on errors
...
@@ -113,9 +114,9 @@ export function usePairContract(pairAddress?: string, withSignerIfPossible?: boo
...
@@ -113,9 +114,9 @@ export function usePairContract(pairAddress?: string, withSignerIfPossible?: boo
return
useContract
(
pairAddress
,
IUniswapV2PairABI
,
withSignerIfPossible
)
return
useContract
(
pairAddress
,
IUniswapV2PairABI
,
withSignerIfPossible
)
}
}
export
function
useMulticall
Contract
():
Contract
|
null
{
export
function
useMulticall
2Contract
():
Multicall2
|
null
{
const
{
chainId
}
=
useActiveWeb3React
()
const
{
chainId
}
=
useActiveWeb3React
()
return
useContract
(
chainId
&&
MULTICALL
_ADDRESSES
[
chainId
],
MULTICALL_ABI
,
false
)
return
useContract
(
chainId
&&
MULTICALL
2_ADDRESSES
[
chainId
],
MULTICALL_ABI
,
false
)
as
Multicall2
}
}
export
function
useMerkleDistributorContract
():
Contract
|
null
{
export
function
useMerkleDistributorContract
():
Contract
|
null
{
...
...
src/hooks/useCurrentBlockTimestamp.ts
View file @
bff3811f
import
{
BigNumber
}
from
'
ethers
'
import
{
BigNumber
}
from
'
ethers
'
import
{
useSingleCallResult
}
from
'
../state/multicall/hooks
'
import
{
useSingleCallResult
}
from
'
../state/multicall/hooks
'
import
{
useMulticallContract
}
from
'
./useContract
'
import
{
useMulticall
2
Contract
}
from
'
./useContract
'
// gets the current timestamp from the blockchain
// gets the current timestamp from the blockchain
export
default
function
useCurrentBlockTimestamp
():
BigNumber
|
undefined
{
export
default
function
useCurrentBlockTimestamp
():
BigNumber
|
undefined
{
const
multicall
=
useMulticallContract
()
const
multicall
=
useMulticall
2
Contract
()
return
useSingleCallResult
(
multicall
,
'
getCurrentBlockTimestamp
'
)?.
result
?.[
0
]
return
useSingleCallResult
(
multicall
,
'
getCurrentBlockTimestamp
'
)?.
result
?.[
0
]
}
}
src/hooks/useV3Positions.ts
View file @
bff3811f
...
@@ -69,13 +69,13 @@ export function useV3Positions(account: string | null | undefined): UseV3Positio
...
@@ -69,13 +69,13 @@ export function useV3Positions(account: string | null | undefined): UseV3Positio
const
positionManager
=
useV3NFTPositionManagerContract
()
const
positionManager
=
useV3NFTPositionManagerContract
()
const
{
loading
:
balanceLoading
,
error
:
balanceError
,
result
:
balanceResult
}
=
useSingleCallResult
(
const
{
loading
:
balanceLoading
,
error
:
balanceError
,
result
:
balanceResult
}
=
useSingleCallResult
(
positionManager
??
undefined
,
positionManager
,
'
balanceOf
'
,
'
balanceOf
'
,
[
account
??
undefined
]
[
account
??
undefined
]
)
)
// we don't expect any account balance to ever exceed the bounds of max safe int
// we don't expect any account balance to ever exceed the bounds of max safe int
const
accountBalance
:
number
|
undefined
=
balanceResult
?.[
0
]
?
Number
.
parseInt
(
balanceResult
[
0
])
:
undefined
const
accountBalance
:
number
|
undefined
=
balanceResult
?.[
0
]
?.
toNumber
()
const
tokenIdsArgs
=
useMemo
(()
=>
{
const
tokenIdsArgs
=
useMemo
(()
=>
{
if
(
accountBalance
&&
account
)
{
if
(
accountBalance
&&
account
)
{
...
@@ -88,11 +88,7 @@ export function useV3Positions(account: string | null | undefined): UseV3Positio
...
@@ -88,11 +88,7 @@ export function useV3Positions(account: string | null | undefined): UseV3Positio
return
[]
return
[]
},
[
account
,
accountBalance
])
},
[
account
,
accountBalance
])
const
tokenIdResults
=
useSingleContractMultipleData
(
const
tokenIdResults
=
useSingleContractMultipleData
(
positionManager
,
'
tokenOfOwnerByIndex
'
,
tokenIdsArgs
)
positionManager
??
undefined
,
'
tokenOfOwnerByIndex
'
,
tokenIdsArgs
)
const
tokenIds
=
useMemo
(()
=>
{
const
tokenIds
=
useMemo
(()
=>
{
if
(
account
)
{
if
(
account
)
{
...
...
src/pages/Pool/index.tsx
View file @
bff3811f
...
@@ -73,6 +73,7 @@ const ResponsiveButtonPrimary = styled(ButtonPrimary)`
...
@@ -73,6 +73,7 @@ const ResponsiveButtonPrimary = styled(ButtonPrimary)`
width: 49%;
width: 49%;
`
}
;
`
}
;
`
`
const
MainContentWrapper
=
styled
.
main
`
const
MainContentWrapper
=
styled
.
main
`
background-color:
${({
theme
})
=>
theme
.
bg0
}
;
background-color:
${({
theme
})
=>
theme
.
bg0
}
;
padding: 16px;
padding: 16px;
...
@@ -80,17 +81,14 @@ const MainContentWrapper = styled.main`
...
@@ -80,17 +81,14 @@ const MainContentWrapper = styled.main`
display: flex;
display: flex;
flex-direction: column;
flex-direction: column;
`
`
export
default
function
Pool
()
{
export
default
function
Pool
()
{
const
{
account
}
=
useActiveWeb3React
()
const
{
account
}
=
useActiveWeb3React
()
const
toggleWalletModal
=
useWalletModalToggle
()
const
toggleWalletModal
=
useWalletModalToggle
()
const
{
t
}
=
useTranslation
()
const
{
t
}
=
useTranslation
()
const
theme
=
useContext
(
ThemeContext
)
const
theme
=
useContext
(
ThemeContext
)
const
{
error
,
positions
}
=
useV3Positions
(
account
)
const
{
positions
}
=
useV3Positions
(
account
)
if
(
error
)
{
console
.
error
(
'
error fetching v3 positions
'
,
error
)
}
const
hasPositions
=
useMemo
(()
=>
Boolean
(
positions
&&
positions
.
length
>
0
),
[
positions
])
const
hasPositions
=
useMemo
(()
=>
Boolean
(
positions
&&
positions
.
length
>
0
),
[
positions
])
...
@@ -131,6 +129,7 @@ export default function Pool() {
...
@@ -131,6 +129,7 @@ export default function Pool() {
external
:
false
,
external
:
false
,
})
})
}
}
return
(
return
(
<>
<>
<
PageWrapper
>
<
PageWrapper
>
...
...
src/state/multicall/updater.tsx
View file @
bff3811f
import
{
Contract
}
from
'
@ethersproject/contract
s
'
import
{
BigNumber
}
from
'
ether
s
'
import
{
useEffect
,
useMemo
,
useRef
}
from
'
react
'
import
{
useEffect
,
useMemo
,
useRef
}
from
'
react
'
import
{
useDispatch
,
useSelector
}
from
'
react-redux
'
import
{
useDispatch
,
useSelector
}
from
'
react-redux
'
import
{
Multicall2
}
from
'
../../abis/types
'
import
{
useActiveWeb3React
}
from
'
../../hooks
'
import
{
useActiveWeb3React
}
from
'
../../hooks
'
import
{
useMulticallContract
}
from
'
../../hooks/useContract
'
import
{
useMulticall
2
Contract
}
from
'
../../hooks/useContract
'
import
useDebounce
from
'
../../hooks/useDebounce
'
import
useDebounce
from
'
../../hooks/useDebounce
'
import
chunkArray
from
'
../../utils/chunkArray
'
import
chunkArray
from
'
../../utils/chunkArray
'
import
{
CancelledError
,
retry
,
RetryableError
}
from
'
../../utils/retry
'
import
{
CancelledError
,
retry
,
RetryableError
}
from
'
../../utils/retry
'
...
@@ -18,20 +19,24 @@ import {
...
@@ -18,20 +19,24 @@ import {
/**
/**
* Fetches a chunk of calls, enforcing a minimum block number constraint
* Fetches a chunk of calls, enforcing a minimum block number constraint
* @param multicallContract multicall contract to fetch against
* @param multicall
2
Contract multicall contract to fetch against
* @param chunk chunk of calls to make
* @param chunk chunk of calls to make
* @param minBlockNumber minimum block number of the result set
* @param minBlockNumber minimum block number of the result set
*/
*/
async
function
fetchChunk
(
async
function
fetchChunk
(
multicall
Contract
:
Contract
,
multicall
2Contract
:
Multicall2
,
chunk
:
Call
[],
chunk
:
Call
[],
minBlockNumber
:
number
minBlockNumber
:
number
):
Promise
<
{
results
:
string
[];
blockNumber
:
number
}
>
{
):
Promise
<
{
console
.
debug
(
'
Fetching chunk
'
,
multicallContract
,
chunk
,
minBlockNumber
)
results
:
{
success
:
boolean
;
returnData
:
string
}[]
let
resultsBlockNumber
,
returnData
blockNumber
:
number
}
>
{
console
.
debug
(
'
Fetching chunk
'
,
multicall2Contract
,
chunk
,
minBlockNumber
)
let
resultsBlockNumber
:
BigNumber
let
results
:
{
success
:
boolean
;
returnData
:
string
}[]
try
{
try
{
;[
resultsBlockNumber
,
returnData
]
=
await
multicallContract
.
callStatic
.
a
ggregate
(
;[
resultsBlockNumber
,
,
results
]
=
await
multicall2Contract
.
callStatic
.
blockAndA
ggregate
(
chunk
.
map
((
obj
)
=>
[
obj
.
address
,
obj
.
callData
]
)
chunk
.
map
((
obj
)
=>
({
target
:
obj
.
address
,
callData
:
obj
.
callData
})
)
)
)
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
debug
(
'
Failed to fetch chunk inside retry
'
,
error
)
console
.
debug
(
'
Failed to fetch chunk inside retry
'
,
error
)
...
@@ -41,7 +46,7 @@ async function fetchChunk(
...
@@ -41,7 +46,7 @@ async function fetchChunk(
console
.
debug
(
`Fetched results for old block number:
${
resultsBlockNumber
.
toString
()}
vs.
${
minBlockNumber
}
`
)
console
.
debug
(
`Fetched results for old block number:
${
resultsBlockNumber
.
toString
()}
vs.
${
minBlockNumber
}
`
)
throw
new
RetryableError
(
'
Fetched for old block number
'
)
throw
new
RetryableError
(
'
Fetched for old block number
'
)
}
}
return
{
results
:
returnData
,
blockNumber
:
resultsBlockNumber
.
toNumber
()
}
return
{
results
,
blockNumber
:
resultsBlockNumber
.
toNumber
()
}
}
}
/**
/**
...
@@ -116,7 +121,7 @@ export default function Updater(): null {
...
@@ -116,7 +121,7 @@ export default function Updater(): null {
const
debouncedListeners
=
useDebounce
(
state
.
callListeners
,
100
)
const
debouncedListeners
=
useDebounce
(
state
.
callListeners
,
100
)
const
latestBlockNumber
=
useBlockNumber
()
const
latestBlockNumber
=
useBlockNumber
()
const
{
chainId
}
=
useActiveWeb3React
()
const
{
chainId
}
=
useActiveWeb3React
()
const
multicall
Contract
=
useMulticall
Contract
()
const
multicall
2Contract
=
useMulticall2
Contract
()
const
cancellations
=
useRef
<
{
blockNumber
:
number
;
cancellations
:
(()
=>
void
)[]
}
>
()
const
cancellations
=
useRef
<
{
blockNumber
:
number
;
cancellations
:
(()
=>
void
)[]
}
>
()
const
listeningKeys
:
{
[
callKey
:
string
]:
number
}
=
useMemo
(()
=>
{
const
listeningKeys
:
{
[
callKey
:
string
]:
number
}
=
useMemo
(()
=>
{
...
@@ -132,7 +137,7 @@ export default function Updater(): null {
...
@@ -132,7 +137,7 @@ export default function Updater(): null {
])
])
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
!
latestBlockNumber
||
!
chainId
||
!
multicallContract
)
return
if
(
!
latestBlockNumber
||
!
chainId
||
!
multicall
2
Contract
)
return
const
outdatedCallKeys
:
string
[]
=
JSON
.
parse
(
serializedOutdatedCallKeys
)
const
outdatedCallKeys
:
string
[]
=
JSON
.
parse
(
serializedOutdatedCallKeys
)
if
(
outdatedCallKeys
.
length
===
0
)
return
if
(
outdatedCallKeys
.
length
===
0
)
return
...
@@ -155,7 +160,7 @@ export default function Updater(): null {
...
@@ -155,7 +160,7 @@ export default function Updater(): null {
cancellations
.
current
=
{
cancellations
.
current
=
{
blockNumber
:
latestBlockNumber
,
blockNumber
:
latestBlockNumber
,
cancellations
:
chunkedCalls
.
map
((
chunk
,
index
)
=>
{
cancellations
:
chunkedCalls
.
map
((
chunk
,
index
)
=>
{
const
{
cancel
,
promise
}
=
retry
(()
=>
fetchChunk
(
multicallContract
,
chunk
,
latestBlockNumber
),
{
const
{
cancel
,
promise
}
=
retry
(()
=>
fetchChunk
(
multicall
2
Contract
,
chunk
,
latestBlockNumber
),
{
n
:
Infinity
,
n
:
Infinity
,
minWait
:
2500
,
minWait
:
2500
,
maxWait
:
3500
,
maxWait
:
3500
,
...
@@ -168,18 +173,29 @@ export default function Updater(): null {
...
@@ -168,18 +173,29 @@ export default function Updater(): null {
const
firstCallKeyIndex
=
chunkedCalls
.
slice
(
0
,
index
).
reduce
<
number
>
((
memo
,
curr
)
=>
memo
+
curr
.
length
,
0
)
const
firstCallKeyIndex
=
chunkedCalls
.
slice
(
0
,
index
).
reduce
<
number
>
((
memo
,
curr
)
=>
memo
+
curr
.
length
,
0
)
const
lastCallKeyIndex
=
firstCallKeyIndex
+
returnData
.
length
const
lastCallKeyIndex
=
firstCallKeyIndex
+
returnData
.
length
const
slice
=
outdatedCallKeys
.
slice
(
firstCallKeyIndex
,
lastCallKeyIndex
)
dispatch
(
dispatch
(
updateMulticallResults
({
updateMulticallResults
({
chainId
,
chainId
,
results
:
outdatedCallKeys
results
:
slice
.
reduce
<
{
[
callKey
:
string
]:
string
|
null
}
>
((
memo
,
callKey
,
i
)
=>
{
.
slice
(
firstCallKeyIndex
,
lastCallKeyIndex
)
if
(
returnData
[
i
].
success
)
{
.
reduce
<
{
[
callKey
:
string
]:
string
|
null
}
>
((
memo
,
callKey
,
i
)
=>
{
memo
[
callKey
]
=
returnData
[
i
].
returnData
??
null
memo
[
callKey
]
=
returnData
[
i
]
??
null
}
return
memo
return
memo
},
{}),
},
{}),
blockNumber
:
fetchBlockNumber
,
blockNumber
:
fetchBlockNumber
,
})
})
)
)
// todo: dispatch an error for each call that failed, i.e. returnData[i].success === false
// dispatch(
// errorFetchingMulticallResults({
// calls: // todo: compute this,
// chainId,
// fetchingBlockNumber: latestBlockNumber,
// })
// )
})
})
.
catch
((
error
:
any
)
=>
{
.
catch
((
error
:
any
)
=>
{
if
(
error
instanceof
CancelledError
)
{
if
(
error
instanceof
CancelledError
)
{
...
@@ -198,7 +214,7 @@ export default function Updater(): null {
...
@@ -198,7 +214,7 @@ export default function Updater(): null {
return
cancel
return
cancel
}),
}),
}
}
},
[
chainId
,
multicallContract
,
dispatch
,
serializedOutdatedCallKeys
,
latestBlockNumber
])
},
[
chainId
,
multicall
2
Contract
,
dispatch
,
serializedOutdatedCallKeys
,
latestBlockNumber
])
return
null
return
null
}
}
src/state/wallet/hooks.ts
View file @
bff3811f
...
@@ -3,7 +3,7 @@ import { JSBI } from '@uniswap/v2-sdk'
...
@@ -3,7 +3,7 @@ import { JSBI } from '@uniswap/v2-sdk'
import
{
useMemo
}
from
'
react
'
import
{
useMemo
}
from
'
react
'
import
{
useActiveWeb3React
}
from
'
../../hooks
'
import
{
useActiveWeb3React
}
from
'
../../hooks
'
import
{
useAllTokens
}
from
'
../../hooks/Tokens
'
import
{
useAllTokens
}
from
'
../../hooks/Tokens
'
import
{
useMulticallContract
}
from
'
../../hooks/useContract
'
import
{
useMulticall
2
Contract
}
from
'
../../hooks/useContract
'
import
{
isAddress
}
from
'
../../utils
'
import
{
isAddress
}
from
'
../../utils
'
import
{
useUserUnclaimedAmount
}
from
'
../claim/hooks
'
import
{
useUserUnclaimedAmount
}
from
'
../claim/hooks
'
import
{
useMultipleContractSingleData
,
useSingleContractMultipleData
}
from
'
../multicall/hooks
'
import
{
useMultipleContractSingleData
,
useSingleContractMultipleData
}
from
'
../multicall/hooks
'
...
@@ -18,7 +18,7 @@ import { Erc20Interface } from 'abis/types/Erc20'
...
@@ -18,7 +18,7 @@ import { Erc20Interface } from 'abis/types/Erc20'
export
function
useETHBalances
(
export
function
useETHBalances
(
uncheckedAddresses
?:
(
string
|
undefined
)[]
uncheckedAddresses
?:
(
string
|
undefined
)[]
):
{
[
address
:
string
]:
CurrencyAmount
|
undefined
}
{
):
{
[
address
:
string
]:
CurrencyAmount
|
undefined
}
{
const
multicallContract
=
useMulticallContract
()
const
multicallContract
=
useMulticall
2
Contract
()
const
addresses
:
string
[]
=
useMemo
(
const
addresses
:
string
[]
=
useMemo
(
()
=>
()
=>
...
...
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