From 5508bfc26b381b08bd360bff46fc0abd9fc18a15 Mon Sep 17 00:00:00 2001 From: Apotheosis <97164662+0xApotheosis@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:26:12 +1000 Subject: [PATCH 1/3] fix: cowswap buyAmountCryptoBaseUnit logic --- .../getCowSwapTradeQuote.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts index 0e5407d87..739b32c46 100644 --- a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts +++ b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts @@ -67,7 +67,7 @@ export async function getCowSwapTradeQuote( const minQuoteSellAmount = bnOrZero(minimum).times(bn(10).exponentiatedBy(sellAsset.precision)) // making sure we do not have decimals for cowswap api (can happen at least from minQuoteSellAmount) - const normalizedSellAmount = normalizeIntegerAmount( + const normalizedSellAmountCryptoBaseUnit = normalizeIntegerAmount( bnOrZero(sellAmountBeforeFeesCryptoBaseUnit).lt(minQuoteSellAmount) ? minQuoteSellAmount : sellAmountBeforeFeesCryptoBaseUnit, @@ -82,7 +82,7 @@ export async function getCowSwapTradeQuote( partiallyFillable: false, from: DEFAULT_ADDRESS, kind: ORDER_KIND_SELL, - sellAmountBeforeFee: normalizedSellAmount, + sellAmountBeforeFee: normalizedSellAmountCryptoBaseUnit, } /** @@ -144,9 +144,15 @@ export async function getCowSwapTradeQuote( // If original sellAmount is < minQuoteSellAmount, we don't want to replace it with normalizedSellAmount // The purpose of this was to get a quote from CowSwap even with small amounts - const quoteSellAmount = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) + const quoteSellAmountCryptoBaseUnit = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) ? sellAmountBeforeFeesCryptoBaseUnit - : normalizedSellAmount + : normalizedSellAmountCryptoBaseUnit + + // Similarly, if original sellAmount is < minQuoteSellAmount, we can't use the buy amount from the quote + // because we aren't actually selling the minimum amount + const quoteBuyAmountCryptoBaseUnit = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) + ? '0' + : buyAmountCryptoBaseUnit return { rate, @@ -164,8 +170,8 @@ export async function getCowSwapTradeQuote( buyAssetTradeFeeUsd: '0', // Trade fees for buy Asset are always 0 since trade fees are subtracted from sell asset sellAssetTradeFeeUsd, }, - sellAmountBeforeFeesCryptoBaseUnit: quoteSellAmount, - buyAmountCryptoBaseUnit, + sellAmountBeforeFeesCryptoBaseUnit: quoteSellAmountCryptoBaseUnit, + buyAmountCryptoBaseUnit: quoteBuyAmountCryptoBaseUnit, sources: DEFAULT_SOURCE, allowanceContract: COW_SWAP_VAULT_RELAYER_ADDRESS, buyAsset, From bf7959bf406694e344a5912ad5f42ed7db6d383e Mon Sep 17 00:00:00 2001 From: Apotheosis <97164662+0xApotheosis@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:34:11 +1000 Subject: [PATCH 2/3] chore: improve code --- .../getCowSwapTradeQuote.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts index 739b32c46..51808ac8c 100644 --- a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts +++ b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.ts @@ -65,12 +65,13 @@ export async function getCowSwapTradeQuote( const { minimum, maximum } = await getCowSwapMinMax(deps, sellAsset, buyAsset) const minQuoteSellAmount = bnOrZero(minimum).times(bn(10).exponentiatedBy(sellAsset.precision)) + const isSellAmountBelowMinimum = bnOrZero(sellAmountBeforeFeesCryptoBaseUnit).lt( + minQuoteSellAmount, + ) // making sure we do not have decimals for cowswap api (can happen at least from minQuoteSellAmount) const normalizedSellAmountCryptoBaseUnit = normalizeIntegerAmount( - bnOrZero(sellAmountBeforeFeesCryptoBaseUnit).lt(minQuoteSellAmount) - ? minQuoteSellAmount - : sellAmountBeforeFeesCryptoBaseUnit, + isSellAmountBelowMinimum ? minQuoteSellAmount : sellAmountBeforeFeesCryptoBaseUnit, ) const apiInput: CowSwapSellQuoteApiInput = { @@ -142,15 +143,16 @@ export async function getCowSwapTradeQuote( const feeData = feeDataOptions['fast'] - // If original sellAmount is < minQuoteSellAmount, we don't want to replace it with normalizedSellAmount + const isQuoteSellAmountBelowMinimum = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) + // If isQuoteSellAmountBelowMinimum we don't want to replace it with normalizedSellAmount // The purpose of this was to get a quote from CowSwap even with small amounts - const quoteSellAmountCryptoBaseUnit = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) + const quoteSellAmountCryptoBaseUnit = isQuoteSellAmountBelowMinimum ? sellAmountBeforeFeesCryptoBaseUnit : normalizedSellAmountCryptoBaseUnit - // Similarly, if original sellAmount is < minQuoteSellAmount, we can't use the buy amount from the quote - // because we aren't actually selling the minimum amount - const quoteBuyAmountCryptoBaseUnit = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount) + // Similarly, if isQuoteSellAmountBelowMinimum we can't use the buy amount from the quote + // because we aren't actually selling the minimum amount (we are attempting to sell an amount less than it) + const quoteBuyAmountCryptoBaseUnit = isQuoteSellAmountBelowMinimum ? '0' : buyAmountCryptoBaseUnit From 69d87cf285aa33608ce18aa4a3b258d521fddbcd Mon Sep 17 00:00:00 2001 From: Apotheosis <97164662+0xApotheosis@users.noreply.github.com> Date: Mon, 3 Apr 2023 16:44:28 +1000 Subject: [PATCH 3/3] fix: tests --- .../cow/getCowSwapTradeQuote/getCowSwapTradeQuote.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.test.ts b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.test.ts index 55cae9df3..b3a0b0666 100644 --- a/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.test.ts +++ b/packages/swapper/src/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.test.ts @@ -176,7 +176,7 @@ const expectedTradeQuoteSmallAmountWethToFox: TradeQuote