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
aa742f41
Unverified
Commit
aa742f41
authored
Apr 23, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create some hooks for using in v3 swap routing
parent
77fa6149
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
117 additions
and
69 deletions
+117
-69
useAllCurrencyCombinations.ts
src/hooks/useAllCurrencyCombinations.ts
+62
-0
usePools.ts
src/hooks/usePools.ts
+4
-4
useV2Trade.ts
src/hooks/useV2Trade.ts
+5
-64
useV3SwapPools.ts
src/hooks/useV3SwapPools.ts
+45
-0
hooks.ts
src/state/swap/hooks.ts
+1
-1
No files found.
src/hooks/useAllCurrencyCombinations.ts
0 → 100644
View file @
aa742f41
import
{
Currency
,
Token
}
from
'
@uniswap/sdk-core
'
import
flatMap
from
'
lodash.flatmap
'
import
{
useMemo
}
from
'
react
'
import
{
ADDITIONAL_BASES
,
BASES_TO_CHECK_TRADES_AGAINST
,
CUSTOM_BASES
}
from
'
../constants
'
import
{
wrappedCurrency
}
from
'
../utils/wrappedCurrency
'
import
{
useActiveWeb3React
}
from
'
./index
'
export
function
useAllCurrencyCombinations
(
currencyA
?:
Currency
,
currencyB
?:
Currency
):
[
Token
,
Token
][]
{
const
{
chainId
}
=
useActiveWeb3React
()
const
[
tokenA
,
tokenB
]
=
chainId
?
[
wrappedCurrency
(
currencyA
,
chainId
),
wrappedCurrency
(
currencyB
,
chainId
)]
:
[
undefined
,
undefined
]
const
bases
:
Token
[]
=
useMemo
(()
=>
{
if
(
!
chainId
)
return
[]
const
common
=
BASES_TO_CHECK_TRADES_AGAINST
[
chainId
]
??
[]
const
additionalA
=
tokenA
?
ADDITIONAL_BASES
[
chainId
]?.[
tokenA
.
address
]
??
[]
:
[]
const
additionalB
=
tokenB
?
ADDITIONAL_BASES
[
chainId
]?.[
tokenB
.
address
]
??
[]
:
[]
return
[...
common
,
...
additionalA
,
...
additionalB
]
},
[
chainId
,
tokenA
,
tokenB
])
const
basePairs
:
[
Token
,
Token
][]
=
useMemo
(
()
=>
flatMap
(
bases
,
(
base
):
[
Token
,
Token
][]
=>
bases
.
map
((
otherBase
)
=>
[
base
,
otherBase
])),
[
bases
]
)
return
useMemo
(
()
=>
tokenA
&&
tokenB
?
[
// the direct pair
[
tokenA
,
tokenB
],
// token A against all bases
...
bases
.
map
((
base
):
[
Token
,
Token
]
=>
[
tokenA
,
base
]),
// token B against all bases
...
bases
.
map
((
base
):
[
Token
,
Token
]
=>
[
tokenB
,
base
]),
// each base against all bases
...
basePairs
,
]
.
filter
((
tokens
):
tokens
is
[
Token
,
Token
]
=>
Boolean
(
tokens
[
0
]
&&
tokens
[
1
]))
.
filter
(([
t0
,
t1
])
=>
t0
.
address
!==
t1
.
address
)
.
filter
(([
tokenA
,
tokenB
])
=>
{
if
(
!
chainId
)
return
true
const
customBases
=
CUSTOM_BASES
[
chainId
]
const
customBasesA
:
Token
[]
|
undefined
=
customBases
?.[
tokenA
.
address
]
const
customBasesB
:
Token
[]
|
undefined
=
customBases
?.[
tokenB
.
address
]
if
(
!
customBasesA
&&
!
customBasesB
)
return
true
if
(
customBasesA
&&
!
customBasesA
.
find
((
base
)
=>
tokenB
.
equals
(
base
)))
return
false
if
(
customBasesB
&&
!
customBasesB
.
find
((
base
)
=>
tokenA
.
equals
(
base
)))
return
false
return
true
})
:
[],
[
tokenA
,
tokenB
,
bases
,
basePairs
,
chainId
]
)
}
src/hooks/usePools.ts
View file @
aa742f41
...
...
@@ -13,10 +13,10 @@ import { Interface } from '@ethersproject/abi'
const
POOL_STATE_INTERFACE
=
new
Interface
(
IUniswapV3PoolStateABI
)
as
IUniswapV3PoolStateInterface
export
enum
PoolState
{
LOADING
=
'
LOADING
'
,
NOT_EXISTS
=
'
NOT_EXISTS
'
,
EXISTS
=
'
EXISTS
'
,
INVALID
=
'
INVALID
'
,
LOADING
,
NOT_EXISTS
,
EXISTS
,
INVALID
,
}
export
function
usePools
(
...
...
src/hooks/
v2Trades
.ts
→
src/hooks/
useV2Trade
.ts
View file @
aa742f41
import
{
Currency
,
CurrencyAmount
,
Token
}
from
'
@uniswap/sdk-core
'
import
{
Currency
,
CurrencyAmount
}
from
'
@uniswap/sdk-core
'
import
{
Pair
,
Trade
}
from
'
@uniswap/v2-sdk
'
import
flatMap
from
'
lodash.flatmap
'
import
{
useMemo
}
from
'
react
'
import
{
useUserSingleHopOnly
}
from
'
state/user/hooks
'
import
{
isTradeBetter
}
from
'
utils/trades
'
import
{
ADDITIONAL_BASES
,
BASES_TO_CHECK_TRADES_AGAINST
,
BETTER_TRADE_LESS_HOPS_THRESHOLD
,
CUSTOM_BASES
,
}
from
'
../constants
'
import
{
BETTER_TRADE_LESS_HOPS_THRESHOLD
}
from
'
../constants
'
import
{
useAllCurrencyCombinations
}
from
'
./useAllCurrencyCombinations
'
import
{
PairState
,
useV2Pairs
}
from
'
./useV2Pairs
'
import
{
wrappedCurrency
}
from
'
../utils/wrappedCurrency
'
import
{
useActiveWeb3React
}
from
'
./index
'
function
useAllCommonPairs
(
currencyA
?:
Currency
,
currencyB
?:
Currency
):
Pair
[]
{
const
{
chainId
}
=
useActiveWeb3React
(
)
const
allCurrencyCombinations
=
useAllCurrencyCombinations
(
currencyA
,
currencyB
)
const
[
tokenA
,
tokenB
]
=
chainId
?
[
wrappedCurrency
(
currencyA
,
chainId
),
wrappedCurrency
(
currencyB
,
chainId
)]
:
[
undefined
,
undefined
]
const
bases
:
Token
[]
=
useMemo
(()
=>
{
if
(
!
chainId
)
return
[]
const
common
=
BASES_TO_CHECK_TRADES_AGAINST
[
chainId
]
??
[]
const
additionalA
=
tokenA
?
ADDITIONAL_BASES
[
chainId
]?.[
tokenA
.
address
]
??
[]
:
[]
const
additionalB
=
tokenB
?
ADDITIONAL_BASES
[
chainId
]?.[
tokenB
.
address
]
??
[]
:
[]
return
[...
common
,
...
additionalA
,
...
additionalB
]
},
[
chainId
,
tokenA
,
tokenB
])
const
basePairs
:
[
Token
,
Token
][]
=
useMemo
(
()
=>
flatMap
(
bases
,
(
base
):
[
Token
,
Token
][]
=>
bases
.
map
((
otherBase
)
=>
[
base
,
otherBase
])),
[
bases
]
)
const
allPairCombinations
:
[
Token
,
Token
][]
=
useMemo
(
()
=>
tokenA
&&
tokenB
?
[
// the direct pair
[
tokenA
,
tokenB
],
// token A against all bases
...
bases
.
map
((
base
):
[
Token
,
Token
]
=>
[
tokenA
,
base
]),
// token B against all bases
...
bases
.
map
((
base
):
[
Token
,
Token
]
=>
[
tokenB
,
base
]),
// each base against all bases
...
basePairs
,
]
.
filter
((
tokens
):
tokens
is
[
Token
,
Token
]
=>
Boolean
(
tokens
[
0
]
&&
tokens
[
1
]))
.
filter
(([
t0
,
t1
])
=>
t0
.
address
!==
t1
.
address
)
.
filter
(([
tokenA
,
tokenB
])
=>
{
if
(
!
chainId
)
return
true
const
customBases
=
CUSTOM_BASES
[
chainId
]
const
customBasesA
:
Token
[]
|
undefined
=
customBases
?.[
tokenA
.
address
]
const
customBasesB
:
Token
[]
|
undefined
=
customBases
?.[
tokenB
.
address
]
if
(
!
customBasesA
&&
!
customBasesB
)
return
true
if
(
customBasesA
&&
!
customBasesA
.
find
((
base
)
=>
tokenB
.
equals
(
base
)))
return
false
if
(
customBasesB
&&
!
customBasesB
.
find
((
base
)
=>
tokenA
.
equals
(
base
)))
return
false
return
true
})
:
[],
[
tokenA
,
tokenB
,
bases
,
basePairs
,
chainId
]
)
const
allPairs
=
useV2Pairs
(
allPairCombinations
)
const
allPairs
=
useV2Pairs
(
allCurrencyCombinations
)
// only pass along valid pairs, non-duplicated pairs
return
useMemo
(
...
...
src/hooks/useV3SwapPools.ts
0 → 100644
View file @
aa742f41
import
{
Currency
,
Token
}
from
'
@uniswap/sdk-core
'
import
{
FeeAmount
,
Pool
}
from
'
@uniswap/v3-sdk
'
import
{
useMemo
}
from
'
react
'
import
{
useAllCurrencyCombinations
}
from
'
./useAllCurrencyCombinations
'
import
{
PoolState
,
usePools
}
from
'
./usePools
'
/**
* Returns all the existing pools that should be considered for swapping between an input currency and an output currency
* @param currencyIn the input currency
* @param currencyOut the output currency
*/
export
function
useV3SwapPools
(
currencyIn
?:
Currency
,
currencyOut
?:
Currency
):
{
pools
:
Pool
[]
loading
:
boolean
}
{
const
allCurrencyCombinations
=
useAllCurrencyCombinations
(
currencyIn
,
currencyOut
)
const
allCurrencyCombinationsWithAllFees
:
[
Token
,
Token
,
FeeAmount
][]
=
useMemo
(
()
=>
allCurrencyCombinations
.
reduce
<
[
Token
,
Token
,
FeeAmount
][]
>
((
list
,
[
tokenA
,
tokenB
])
=>
{
return
list
.
concat
([
[
tokenA
,
tokenB
,
FeeAmount
.
LOW
],
[
tokenA
,
tokenB
,
FeeAmount
.
MEDIUM
],
[
tokenA
,
tokenB
,
FeeAmount
.
HIGH
],
])
},
[]),
[
allCurrencyCombinations
]
)
const
pools
=
usePools
(
allCurrencyCombinationsWithAllFees
)
return
useMemo
(()
=>
{
return
{
pools
:
pools
.
filter
((
tuple
):
tuple
is
[
PoolState
.
EXISTS
,
Pool
]
=>
{
return
tuple
[
0
]
===
PoolState
.
EXISTS
&&
tuple
[
1
]
!==
null
})
.
map
(([,
pool
])
=>
pool
),
loading
:
pools
.
some
(([
state
])
=>
state
===
PoolState
.
LOADING
),
}
},
[
pools
])
}
src/state/swap/hooks.ts
View file @
aa742f41
...
...
@@ -7,7 +7,7 @@ import { useCallback, useEffect, useState } from 'react'
import
{
useDispatch
,
useSelector
}
from
'
react-redux
'
import
{
useActiveWeb3React
}
from
'
../../hooks
'
import
{
useCurrency
}
from
'
../../hooks/Tokens
'
import
{
useV2TradeExactIn
,
useV2TradeExactOut
}
from
'
../../hooks/
v2Trades
'
import
{
useV2TradeExactIn
,
useV2TradeExactOut
}
from
'
../../hooks/
useV2Trade
'
import
useParsedQueryString
from
'
../../hooks/useParsedQueryString
'
import
{
isAddress
}
from
'
../../utils
'
import
{
AppDispatch
,
AppState
}
from
'
../index
'
...
...
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