-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use actual input output difference (#7817)
- Loading branch information
1 parent
1029292
commit 9c73047
Showing
13 changed files
with
79 additions
and
19 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 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
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
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
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
57 changes: 57 additions & 0 deletions
57
src/components/MultiHopTrade/hooks/useInputOutputDifference.ts
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,57 @@ | ||
import type { SupportedTradeQuoteStepIndex } from '@shapeshiftoss/swapper' | ||
import { getHopByIndex, type TradeQuote } from '@shapeshiftoss/swapper' | ||
import { bn, bnOrZero, fromBaseUnit } from '@shapeshiftoss/utils' | ||
import { useMemo } from 'react' | ||
import { selectUsdRateByAssetId } from 'state/slices/selectors' | ||
import { useAppSelector } from 'state/store' | ||
|
||
export const useInputOutputDifferenceDecimalPercentage = (tradeQuote: TradeQuote | undefined) => { | ||
const numSteps = tradeQuote?.steps.length ?? 0 | ||
const sellAsset = tradeQuote?.steps[0].sellAsset | ||
const buyAsset = tradeQuote?.steps[numSteps - 1].buyAsset | ||
|
||
const sellAssetUsdRate = useAppSelector(state => | ||
selectUsdRateByAssetId(state, sellAsset?.assetId ?? ''), | ||
) | ||
|
||
const buyAssetUsdRate = useAppSelector(state => | ||
selectUsdRateByAssetId(state, buyAsset?.assetId ?? ''), | ||
) | ||
|
||
const sellAmountIncludingFeesUsd = useMemo(() => { | ||
if (!tradeQuote || !sellAsset || !sellAssetUsdRate) return | ||
|
||
// A quote always has a first hop | ||
const firstHop = getHopByIndex(tradeQuote, 0)! | ||
const sellAmountIncludingProtocolFeesCryptoBaseUnit = | ||
firstHop.sellAmountIncludingProtocolFeesCryptoBaseUnit | ||
|
||
const sellAmountIncludingProtocolFeesCryptoPrecision = fromBaseUnit( | ||
sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
sellAsset.precision, | ||
) | ||
|
||
return bn(sellAmountIncludingProtocolFeesCryptoPrecision).times(sellAssetUsdRate).toFixed() | ||
}, [sellAsset, sellAssetUsdRate, tradeQuote]) | ||
|
||
const buyAmountAfterFeesUsd = useMemo(() => { | ||
if (!tradeQuote || !buyAsset || !buyAssetUsdRate) return | ||
|
||
const lastHopIndex = (numSteps - 1) as SupportedTradeQuoteStepIndex | ||
// A quote always has a last hop since it always has a first hop | ||
const lastHop = getHopByIndex(tradeQuote, lastHopIndex)! | ||
const buyAmountAfterProtocolFeesCryptoBaseUnit = lastHop.buyAmountAfterFeesCryptoBaseUnit | ||
|
||
const buyAmountAfterProtocolFeesCryptoPrecision = fromBaseUnit( | ||
buyAmountAfterProtocolFeesCryptoBaseUnit, | ||
buyAsset.precision, | ||
) | ||
|
||
return bn(buyAmountAfterProtocolFeesCryptoPrecision).times(buyAssetUsdRate).toFixed() | ||
}, [buyAsset, buyAssetUsdRate, numSteps, tradeQuote]) | ||
|
||
if (buyAmountAfterFeesUsd === undefined || sellAmountIncludingFeesUsd === undefined) | ||
return undefined | ||
|
||
return bn(1).minus(bnOrZero(buyAmountAfterFeesUsd).div(sellAmountIncludingFeesUsd)).toString() | ||
} |