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
ca2b84ec
Unverified
Commit
ca2b84ec
authored
Apr 24, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get the best route in v3 for an exact in swap
parent
ac7cf35b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
1 deletion
+104
-1
useAllV3Routes.ts
src/hooks/useAllV3Routes.ts
+0
-1
useBestV3Route.ts
src/hooks/useBestV3Route.ts
+99
-0
hooks.ts
src/state/swap/hooks.ts
+5
-0
No files found.
src/hooks/useAllV3Routes.ts
View file @
ca2b84ec
...
...
@@ -27,7 +27,6 @@ function computeAllRoutes(
const
outputToken
=
pool
.
token0
.
equals
(
tokenIn
)
?
pool
.
token1
:
pool
.
token0
if
(
outputToken
.
equals
(
tokenOut
))
{
console
.
log
(
startCurrencyIn
,
[...
currentPath
,
pool
],
currencyOut
)
allPaths
.
push
(
new
Route
([...
currentPath
,
pool
],
startCurrencyIn
,
currencyOut
))
}
else
if
(
maxHops
>
1
)
{
computeAllRoutes
(
...
...
src/hooks/useBestV3Route.ts
0 → 100644
View file @
ca2b84ec
import
{
Token
,
ChainId
,
Currency
,
CurrencyAmount
,
TokenAmount
}
from
'
@uniswap/sdk-core
'
import
{
Route
}
from
'
@uniswap/v3-sdk
'
import
{
Pool
}
from
'
@uniswap/v3-sdk/dist/
'
import
{
useMemo
}
from
'
react
'
import
{
useSingleContractMultipleData
}
from
'
../state/multicall/hooks
'
import
{
wrappedCurrency
}
from
'
../utils/wrappedCurrency
'
import
{
useActiveWeb3React
}
from
'
./index
'
import
{
useAllV3Routes
}
from
'
./useAllV3Routes
'
import
{
useV3Quoter
}
from
'
./useContract
'
import
{
BigNumber
,
utils
}
from
'
ethers
'
/**
* Converts a route to a path
* @param route the v3 path to convert to an encoded path
* @param chainId the current chain ID, used to wrap the route's input currency
*/
function
routeToPath
(
route
:
Route
,
chainId
:
ChainId
):
string
{
const
firstInputToken
=
wrappedCurrency
(
route
.
input
,
chainId
)
if
(
!
firstInputToken
)
throw
new
Error
(
'
Could not wrap input currency
'
)
return
route
.
pools
.
reduce
(
(
{
inputToken
,
path
}:
{
inputToken
:
Token
;
path
:
string
},
pool
:
Pool
,
index
):
{
inputToken
:
Token
;
path
:
string
}
=>
{
const
outputToken
:
Token
=
pool
.
token0
.
equals
(
inputToken
)
?
pool
.
token1
:
pool
.
token0
if
(
index
===
0
)
{
return
{
inputToken
:
outputToken
,
path
:
utils
.
solidityPack
(
[
'
address
'
,
'
uint24
'
,
'
address
'
],
[
inputToken
.
address
,
pool
.
fee
,
outputToken
.
address
]
),
}
}
else
{
return
{
inputToken
:
outputToken
,
path
:
`
${
path
}${
utils
.
solidityPack
([
'
uint24
'
,
'
address
'
],
[
pool
.
fee
,
outputToken
.
address
]).
slice
(
2
)}
`
,
}
}
},
{
inputToken
:
firstInputToken
,
path
:
''
}
).
path
}
export
function
useBestV3RouteExactIn
(
amountIn
?:
CurrencyAmount
,
currencyOut
?:
Currency
):
{
route
:
Route
;
amountOut
:
CurrencyAmount
}
|
null
{
const
{
chainId
}
=
useActiveWeb3React
()
const
quoter
=
useV3Quoter
()
const
routes
=
useAllV3Routes
(
amountIn
?.
currency
,
currencyOut
)
const
paths
=
useMemo
(()
=>
{
if
(
!
chainId
)
return
[]
return
routes
.
map
((
route
)
=>
routeToPath
(
route
,
chainId
))
},
[
chainId
,
routes
])
const
quoteInputs
=
useMemo
(()
=>
{
return
paths
.
map
((
path
)
=>
[
path
,
amountIn
?
`0x
${
amountIn
.
raw
.
toString
(
16
)}
`
:
undefined
])
},
[
amountIn
,
paths
])
const
quotesResults
=
useSingleContractMultipleData
(
quoter
,
'
quoteExactInput
'
,
quoteInputs
)
return
useMemo
(()
=>
{
const
{
bestRoute
,
amountOut
}
=
quotesResults
.
reduce
(
(
best
:
{
bestRoute
:
Route
|
null
;
amountOut
:
BigNumber
|
null
},
{
valid
,
loading
,
result
},
i
)
=>
{
if
(
loading
||
!
valid
||
!
result
)
return
best
if
(
best
.
amountOut
===
null
)
{
return
{
bestRoute
:
routes
[
i
],
amountOut
:
result
.
amountOut
,
}
}
else
if
(
best
.
amountOut
.
lt
(
result
.
amountOut
))
{
return
{
bestRoute
:
routes
[
i
],
amountOut
:
result
.
amountOut
,
}
}
return
best
},
{
bestRoute
:
null
,
amountOut
:
null
,
}
)
if
(
!
bestRoute
||
!
amountOut
)
return
null
return
{
route
:
bestRoute
,
amountOut
:
currencyOut
instanceof
Token
?
new
TokenAmount
(
currencyOut
,
amountOut
.
toString
())
:
CurrencyAmount
.
ether
(
amountOut
.
toString
()),
}
},
[
currencyOut
,
quotesResults
,
routes
])
}
src/state/swap/hooks.ts
View file @
ca2b84ec
import
{
useBestV3RouteExactIn
}
from
'
../../hooks/useBestV3Route
'
import
useENS
from
'
../../hooks/useENS
'
import
{
parseUnits
}
from
'
@ethersproject/units
'
import
{
Currency
,
CurrencyAmount
,
ETHER
,
Token
,
TokenAmount
}
from
'
@uniswap/sdk-core
'
...
...
@@ -138,6 +139,10 @@ export function useDerivedSwapInfo(): {
const
bestTradeExactIn
=
useV2TradeExactIn
(
isExactIn
?
parsedAmount
:
undefined
,
outputCurrency
??
undefined
)
const
bestTradeExactOut
=
useV2TradeExactOut
(
inputCurrency
??
undefined
,
!
isExactIn
?
parsedAmount
:
undefined
)
const
bestRouteExactInV3
=
useBestV3RouteExactIn
(
isExactIn
?
parsedAmount
:
undefined
,
outputCurrency
??
undefined
)
// todo: do something with this information
console
.
log
(
'
best v3 route for the swap
'
,
bestRouteExactInV3
)
const
v2Trade
=
isExactIn
?
bestTradeExactIn
:
bestTradeExactOut
const
currencyBalances
=
{
...
...
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