forked from wormhole-foundation/wormhole-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Route priority sorting (wormhole-foundation#2419)
* feat(redesign): sort routes by auto/manual, eta and fees * fix(redesign): remove redundant quote fetches * fix(redesign): lint fix * fix(redesign): route sorting code style fixes * refactor(redesign): refactor multiple quote fetcher hook * fix(redesign): linting fixes * fix(redesign): make Chain non optional for quote fetching * refactor(redesign/quotes): replace legacy quotes with SDKv2 quotes * fix(redesign/qoute): use correct relayFee token for bridgeFee calculation in quotes and TX Details
- Loading branch information
Showing
10 changed files
with
293 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useState, useEffect, useMemo } from 'react'; | ||
import { Chain, routes } from '@wormhole-foundation/sdk'; | ||
|
||
import config from 'config'; | ||
|
||
type RoutesQuotesBulkParams = { | ||
sourceChain?: Chain; | ||
sourceToken: string; | ||
destChain?: Chain; | ||
destToken: string; | ||
amount: string; | ||
nativeGas: number; | ||
}; | ||
|
||
type QuoteResult = routes.QuoteResult<routes.Options>; | ||
|
||
type HookReturn = { | ||
quotesMap: Record<string, QuoteResult | undefined>; | ||
isFetching: boolean; | ||
}; | ||
|
||
const useRoutesQuotesBulk = ( | ||
routes: string[], | ||
params: RoutesQuotesBulkParams, | ||
): HookReturn => { | ||
const [isFetching, setIsFetching] = useState(false); | ||
const [quotes, setQuotes] = useState<QuoteResult[]>([]); | ||
|
||
useEffect(() => { | ||
let unmounted = false; | ||
if ( | ||
!params.sourceChain || | ||
!params.sourceToken || | ||
!params.destChain || | ||
!params.destToken || | ||
!params.amount | ||
) { | ||
return; | ||
} | ||
|
||
// Forcing TS to infer that fields are non-optional | ||
const rParams = params as Required<RoutesQuotesBulkParams>; | ||
|
||
setIsFetching(true); | ||
config.routes | ||
.computeMultipleQuotes(routes, rParams) | ||
.then((quoteResults) => { | ||
if (!unmounted) { | ||
setQuotes(quoteResults); | ||
setIsFetching(false); | ||
} | ||
}); | ||
|
||
return () => { | ||
unmounted = true; | ||
}; | ||
}, [ | ||
routes.join(), | ||
params.sourceChain, | ||
params.sourceToken, | ||
params.destChain, | ||
params.destToken, | ||
params.amount, | ||
params.nativeGas, | ||
]); | ||
|
||
const quotesMap = useMemo( | ||
() => | ||
routes.reduce((acc, route, index) => { | ||
acc[route] = quotes[index]; | ||
return acc; | ||
}, {} as Record<string, QuoteResult | undefined>), | ||
[routes.join(), quotes], | ||
); | ||
|
||
return { | ||
quotesMap, | ||
isFetching, | ||
}; | ||
}; | ||
|
||
export default useRoutesQuotesBulk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.