Skip to content

Commit

Permalink
chore: add validation for quotes with incorrect sell amount (#6241)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture authored Feb 19, 2024
1 parent f8f876b commit 99509e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const getQuoteErrorTranslation = (
case SwapperTradeQuoteError.InternalError:
case TradeQuoteValidationError.QueryFailed:
case SwapperTradeQuoteError.QueryFailed:
case TradeQuoteValidationError.QuoteSellAmountInvalid:
return 'trade.errors.quoteError'
default:
assertUnreachable(error)
Expand Down
7 changes: 7 additions & 0 deletions src/state/apis/swapper/helpers/validateTradeQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const validateTradeQuote = async (
isTradingActiveOnSellPool,
isTradingActiveOnBuyPool,
sendAddress,
inputSellAmountCryptoBaseUnit,
}: {
swapperName: SwapperName
quote: TradeQuote | undefined
Expand All @@ -51,6 +52,7 @@ export const validateTradeQuote = async (
// TODO(gomes): this should most likely live in the quote alongside the receiveAddress,
// summoning @woodenfurniture WRT implications of that, this works for now
sendAddress: string | undefined
inputSellAmountCryptoBaseUnit: string
},
): Promise<{
errors: ErrorWithMeta<TradeQuoteError>[]
Expand Down Expand Up @@ -252,6 +254,10 @@ export const validateTradeQuote = async (
return false
})()

// ensure the trade is not selling an amount higher than the user input
const invalidQuoteSellAmount =
inputSellAmountCryptoBaseUnit !== firstHop.sellAmountIncludingProtocolFeesCryptoBaseUnit

return {
errors: [
!!disableSmartContractSwap && {
Expand Down Expand Up @@ -300,6 +306,7 @@ export const validateTradeQuote = async (
},
},
feesExceedsSellAmount && { error: TradeQuoteValidationError.SellAmountBelowTradeFee },
invalidQuoteSellAmount && { error: TradeQuoteValidationError.QuoteSellAmountInvalid },

...insufficientBalanceForProtocolFeesErrors,
].filter(isTruthy),
Expand Down
12 changes: 10 additions & 2 deletions src/state/apis/swapper/swapperApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ export const swapperApi = createApi({
getTradeQuote: build.query<Record<string, ApiQuote>, TradeQuoteRequest>({
queryFn: async (tradeQuoteInput: TradeQuoteRequest, { dispatch, getState }) => {
const state = getState() as ReduxState
const { swapperName, sendAddress, receiveAddress, sellAsset, buyAsset, affiliateBps } =
tradeQuoteInput
const {
swapperName,
sendAddress,
receiveAddress,
sellAsset,
buyAsset,
affiliateBps,
sellAmountIncludingProtocolFeesCryptoBaseUnit,
} = tradeQuoteInput

const isCrossAccountTrade = sendAddress !== receiveAddress
const featureFlags: FeatureFlags = selectFeatureFlags(state)
Expand Down Expand Up @@ -158,6 +165,7 @@ export const swapperApi = createApi({
isTradingActiveOnSellPool,
isTradingActiveOnBuyPool,
sendAddress,
inputSellAmountCryptoBaseUnit: sellAmountIncludingProtocolFeesCryptoBaseUnit,
})
return {
id: quoteSource,
Expand Down
1 change: 1 addition & 0 deletions src/state/apis/swapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum TradeQuoteValidationError {
InsufficientSecondHopFeeAssetBalance = 'InsufficientSecondHopFeeAssetBalance',
InsufficientFundsForProtocolFee = 'InsufficientFundsForProtocolFee',
IntermediaryAssetNotNotSupportedByWallet = 'IntermediaryAssetNotNotSupportedByWallet',
QuoteSellAmountInvalid = 'QuoteSellAmountInvalid',
QueryFailed = 'QueryFailed',
UnknownError = 'UnknownError',
}
Expand Down

0 comments on commit 99509e1

Please sign in to comment.