Skip to content

Commit

Permalink
fix: trade impact is including protocol fees for chainflip (#8556)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG authored Jan 14, 2025
1 parent c239295 commit a26d13f
Showing 1 changed file with 19 additions and 3 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

0 comments on commit a26d13f

Please sign in to comment.