Skip to content

Commit

Permalink
Merge branch 'develop' into feat_virtualized_markets_page
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG authored Jan 14, 2025
2 parents cd26642 + a26d13f commit 1e32e47
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AssetId } from '@shapeshiftoss/caip'
import { CHAIN_NAMESPACE, fromAssetId, solAssetId } from '@shapeshiftoss/caip'
import type { GetFeeDataInput } from '@shapeshiftoss/chain-adapters'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { assertUnreachable } from '@shapeshiftoss/utils'
import { assertUnreachable, bnOrZero, toBaseUnit } from '@shapeshiftoss/utils'
import type { Result } from '@sniptt/monads'
import { Err, Ok } from '@sniptt/monads'
import type { AxiosError } from 'axios'
Expand Down Expand Up @@ -297,6 +297,15 @@ export const getQuoteOrRate = async (
singleQuoteResponse.boostQuote.egressAmountNative!,
)

// This is not really a buyAmount before fees but rather an input/output calculation to get the sell amount
// prorated to the buy asset price to determine price impact
const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit(
bnOrZero(singleQuoteResponse.boostQuote.ingressAmount!).times(
singleQuoteResponse.estimatedPrice!,
),
buyAsset.precision,
)

const boostTradeRateOrQuote = {
id: uuid(),
rate: boostRate,
Expand All @@ -310,7 +319,7 @@ export const getQuoteOrRate = async (
getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Chainflip),
steps: [
{
buyAmountBeforeFeesCryptoBaseUnit: singleQuoteResponse.boostQuote.egressAmountNative!,
buyAmountBeforeFeesCryptoBaseUnit,
buyAmountAfterFeesCryptoBaseUnit: singleQuoteResponse.boostQuote.egressAmountNative!,
sellAmountIncludingProtocolFeesCryptoBaseUnit:
singleQuoteResponse.boostQuote.ingressAmountNative!,
Expand Down Expand Up @@ -349,6 +358,13 @@ export const getQuoteOrRate = async (
singleQuoteResponse.egressAmountNative!,
)

// This is not really a buyAmount before fees but rather an input/output calculation to get the sell amount
// prorated to the buy asset price to determine price impact
const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit(
bnOrZero(singleQuoteResponse.ingressAmount!).times(singleQuoteResponse.estimatedPrice!),
buyAsset.precision,
)

const tradeRateOrQuote = {
id: uuid(),
rate,
Expand All @@ -362,7 +378,7 @@ export const getQuoteOrRate = async (
getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Chainflip),
steps: [
{
buyAmountBeforeFeesCryptoBaseUnit: singleQuoteResponse.egressAmountNative!,
buyAmountBeforeFeesCryptoBaseUnit,
buyAmountAfterFeesCryptoBaseUnit: singleQuoteResponse.egressAmountNative!,
sellAmountIncludingProtocolFeesCryptoBaseUnit: singleQuoteResponse.ingressAmountNative!,
feeData: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export const getTradeQuote = async (
affiliateBps,
receiveAddress,
slippageTolerancePercentageDecimal,
priceImpactPercentageDecimal: priceResponse.priceImpactPct,
steps: [
{
accountNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export const getTradeRate = async (
receiveAddress,
potentialAffiliateBps: affiliateBps,
affiliateBps,
priceImpactPercentageDecimal: priceResponse.priceImpactPct,
slippageTolerancePercentageDecimal,
steps: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ type TradeQuoteBase = {
potentialAffiliateBps: string // even if the swapper does not support affiliateBps, we need to zero-them out or view-layer will be borked
affiliateBps: string // even if the swapper does not support affiliateBps, we need to zero-them out or view-layer will be borked
isStreaming?: boolean
priceImpactPercentageDecimal?: string
slippageTolerancePercentageDecimal: string | undefined // undefined if slippage limit is not provided or specified by the swapper
isLongtail?: boolean
quoteOrRate: 'quote' | 'rate'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const usePriceImpact = (tradeQuote: TradeQuote | TradeRate | undefined) =
const priceImpactPercentage = useMemo(() => {
if (!tradeQuote || !buyAsset || !buyAssetUsdRate || !sellAmountBeforeFeesUsd) return

if (tradeQuote.priceImpactPercentageDecimal) {
return bnOrZero(tradeQuote.priceImpactPercentageDecimal).times(100).abs()
}

// price impact calculation must use buyAmountBeforeFees because it relates to the liquidity in
// the pool rather than a rate of input versus output

Expand Down
13 changes: 8 additions & 5 deletions src/components/MultiHopTrade/hooks/useReceiveAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const useReceiveAddress = ({
return false
}, [buyAsset, wallet])

const { data: walletReceiveAddress, isLoading } = useQuery({
const { data: walletReceiveAddress, isLoading } = useQuery<string | null>({
queryKey: [
'receiveAddress',
buyAsset?.assetId,
Expand All @@ -67,7 +67,7 @@ export const useReceiveAddress = ({
? async () => {
// Already partially covered in isInitializing, but TypeScript lyfe mang.
if (!buyAsset || !wallet || !buyAccountId || !buyAccountMetadata || !deviceId) {
return undefined
return null
}

const buyAssetChainId = buyAsset.chainId
Expand All @@ -80,7 +80,7 @@ export const useReceiveAddress = ({
* super dangerous - don't use the wrong bip44 params to generate receive addresses
*/
if (buyAssetChainId !== buyAssetAccountChainId) {
return undefined
return null
}

if (isUtxoAccountId(buyAccountId) && !buyAccountMetadata?.accountType)
Expand All @@ -95,11 +95,14 @@ export const useReceiveAddress = ({
pubKey: shouldFetchUnchainedAddress ? fromAccountId(buyAccountId).account : undefined,
})

return walletReceiveAddress
return walletReceiveAddress ?? null
}
: skipToken,
staleTime: Infinity,
})

return { walletReceiveAddress, isLoading: isInitializing || isLoading }
return {
walletReceiveAddress: walletReceiveAddress ?? undefined,
isLoading: isInitializing || isLoading,
}
}

0 comments on commit 1e32e47

Please sign in to comment.