Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

fix: cowswap buyAmountCryptoBaseUnit logic #1230

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const expectedTradeQuoteSmallAmountWethToFox: TradeQuote<KnownChainIds.EthereumM
networkFeeCryptoBaseUnit: '0',
},
sellAmountBeforeFeesCryptoBaseUnit: '1000000000000',
buyAmountCryptoBaseUnit: '145018118182475950905', // 14501 FOX
buyAmountCryptoBaseUnit: '0', // 0 FOX
sources: [{ name: SwapperName.CowSwap, proportion: '1' }],
allowanceContract: '0xc92e8bdf79f0507f65a392b0ab4667716bfe0110',
buyAsset: FOX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 normalizedSellAmount = normalizeIntegerAmount(
bnOrZero(sellAmountBeforeFeesCryptoBaseUnit).lt(minQuoteSellAmount)
? minQuoteSellAmount
: sellAmountBeforeFeesCryptoBaseUnit,
const normalizedSellAmountCryptoBaseUnit = normalizeIntegerAmount(
isSellAmountBelowMinimum ? minQuoteSellAmount : sellAmountBeforeFeesCryptoBaseUnit,
)

const apiInput: CowSwapSellQuoteApiInput = {
Expand All @@ -82,7 +83,7 @@ export async function getCowSwapTradeQuote(
partiallyFillable: false,
from: DEFAULT_ADDRESS,
kind: ORDER_KIND_SELL,
sellAmountBeforeFee: normalizedSellAmount,
sellAmountBeforeFee: normalizedSellAmountCryptoBaseUnit,
}

/**
Expand Down Expand Up @@ -142,11 +143,18 @@ 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 quoteSellAmount = bnOrZero(sellAmountCryptoBaseUnit).lt(minQuoteSellAmount)
const quoteSellAmountCryptoBaseUnit = isQuoteSellAmountBelowMinimum
? sellAmountBeforeFeesCryptoBaseUnit
: normalizedSellAmount
: normalizedSellAmountCryptoBaseUnit

// 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)
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
const quoteBuyAmountCryptoBaseUnit = isQuoteSellAmountBelowMinimum
? '0'
: buyAmountCryptoBaseUnit

return {
rate,
Expand All @@ -164,8 +172,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,
Expand Down