From e4273be862e5827a5662a9dfe1ffd39d6509dbc9 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:49:17 +0100 Subject: [PATCH 1/4] fix: start --- .../ChainflipSwapper/utils/getQuoteOrRate.ts | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts index 7b14c88de91..dc377879db0 100644 --- a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts +++ b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts @@ -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, convertPrecision } from '@shapeshiftoss/utils' import type { Result } from '@sniptt/monads' import { Err, Ok } from '@sniptt/monads' import type { AxiosError } from 'axios' @@ -349,6 +349,31 @@ export const getQuoteOrRate = async ( singleQuoteResponse.egressAmountNative!, ) + const buyAmountBeforeFeesCryptoBaseUnit = (() => { + let amount = bnOrZero(singleQuoteResponse.egressAmountNative!) + + // Add back ALL protocol fees since they all affect the final buy amount + const protocolFees = getProtocolFees(singleQuoteResponse) + Object.values(protocolFees).forEach(fee => { + if (!fee) return + + const feeInBuyAsset = + fee.asset.assetId === buyAsset.assetId + ? fee.amountCryptoBaseUnit + : convertPrecision({ + value: fee.amountCryptoBaseUnit, + inputExponent: fee.asset.precision, + outputExponent: buyAsset.precision, + }).toFixed() + + amount = amount.plus(feeInBuyAsset) + }) + + return amount.toFixed() + })() + + console.log({ singleQuoteResponse, buyAmountBeforeFeesCryptoBaseUnit }) + const tradeRateOrQuote = { id: uuid(), rate, @@ -362,7 +387,7 @@ export const getQuoteOrRate = async ( getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Chainflip), steps: [ { - buyAmountBeforeFeesCryptoBaseUnit: singleQuoteResponse.egressAmountNative!, + buyAmountBeforeFeesCryptoBaseUnit, buyAmountAfterFeesCryptoBaseUnit: singleQuoteResponse.egressAmountNative!, sellAmountIncludingProtocolFeesCryptoBaseUnit: singleQuoteResponse.ingressAmountNative!, feeData: { From def32548e664330a520395d6181fef4c52dac664 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:25:23 +0100 Subject: [PATCH 2/4] fix: fix trade impact --- .../ChainflipSwapper/utils/getQuoteOrRate.ts | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts index dc377879db0..0427abac753 100644 --- a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts +++ b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts @@ -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, bnOrZero, convertPrecision } 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' @@ -297,6 +297,13 @@ export const getQuoteOrRate = async ( singleQuoteResponse.boostQuote.egressAmountNative!, ) + const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit( + bnOrZero(singleQuoteResponse.boostQuote.ingressAmount!).times( + singleQuoteResponse.estimatedPrice!, + ), + buyAsset.precision, + ) + const boostTradeRateOrQuote = { id: uuid(), rate: boostRate, @@ -310,7 +317,7 @@ export const getQuoteOrRate = async ( getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Chainflip), steps: [ { - buyAmountBeforeFeesCryptoBaseUnit: singleQuoteResponse.boostQuote.egressAmountNative!, + buyAmountBeforeFeesCryptoBaseUnit, buyAmountAfterFeesCryptoBaseUnit: singleQuoteResponse.boostQuote.egressAmountNative!, sellAmountIncludingProtocolFeesCryptoBaseUnit: singleQuoteResponse.boostQuote.ingressAmountNative!, @@ -349,30 +356,10 @@ export const getQuoteOrRate = async ( singleQuoteResponse.egressAmountNative!, ) - const buyAmountBeforeFeesCryptoBaseUnit = (() => { - let amount = bnOrZero(singleQuoteResponse.egressAmountNative!) - - // Add back ALL protocol fees since they all affect the final buy amount - const protocolFees = getProtocolFees(singleQuoteResponse) - Object.values(protocolFees).forEach(fee => { - if (!fee) return - - const feeInBuyAsset = - fee.asset.assetId === buyAsset.assetId - ? fee.amountCryptoBaseUnit - : convertPrecision({ - value: fee.amountCryptoBaseUnit, - inputExponent: fee.asset.precision, - outputExponent: buyAsset.precision, - }).toFixed() - - amount = amount.plus(feeInBuyAsset) - }) - - return amount.toFixed() - })() - - console.log({ singleQuoteResponse, buyAmountBeforeFeesCryptoBaseUnit }) + const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit( + bnOrZero(singleQuoteResponse.ingressAmount!).times(singleQuoteResponse.estimatedPrice!), + buyAsset.precision, + ) const tradeRateOrQuote = { id: uuid(), From 59292a51662e80c4f55431191fc041c9460a5f09 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:29:02 +0100 Subject: [PATCH 3/4] chore: trigger ci From 0cec0f9aa5c1a9d6c47cad3ad5371369eff749ed Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Tue, 14 Jan 2025 12:13:25 +0100 Subject: [PATCH 4/4] fix: review feedbacks --- .../src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts index 0427abac753..206d35007c9 100644 --- a/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts +++ b/packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts @@ -297,6 +297,8 @@ 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!, @@ -356,6 +358,8 @@ 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,