Skip to content

Commit

Permalink
fix(swap): take slippage into account for buy orders (#5067)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Nov 5, 2024
1 parent 6ae8ca1 commit 21f39fe
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
const uiOrderType = tradeType ? TradeTypeToUiOrderType[tradeType] : null

const sellCurrency = derivedTradeState?.inputCurrency
const inputAmount = receiveAmountInfo?.afterNetworkCosts.sellAmount
const inputAmount = receiveAmountInfo?.afterSlippage.sellAmount
const outputAmount = receiveAmountInfo?.afterSlippage.buyAmount
const sellAmountBeforeFee = receiveAmountInfo?.afterNetworkCosts.sellAmount
const inputAmountWithSlippage = receiveAmountInfo?.afterSlippage.sellAmount
const networkFee = receiveAmountInfo?.costs.networkFee.amountInSellCurrency

const permitInfo = usePermitInfo(sellCurrency, tradeType)
Expand All @@ -56,7 +55,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
const checkAllowanceAddress = COW_PROTOCOL_VAULT_RELAYER_ADDRESS[chainId || SupportedChainId.MAINNET]
const { enoughAllowance } = useEnoughBalanceAndAllowance({
account,
amount: inputAmountWithSlippage,
amount: inputAmount,
checkAllowanceAddress,
})

Expand All @@ -75,7 +74,6 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
useSWR(
inputAmount &&
outputAmount &&
inputAmountWithSlippage &&
sellAmountBeforeFee &&
networkFee &&
sellToken &&
Expand Down Expand Up @@ -103,7 +101,6 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
enoughAllowance,
generatePermitHook,
inputAmount,
inputAmountWithSlippage,
networkFee,
outputAmount,
permitInfo,
Expand Down Expand Up @@ -134,7 +131,6 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
enoughAllowance,
generatePermitHook,
inputAmount,
inputAmountWithSlippage,
networkFee,
outputAmount,
permitInfo,
Expand All @@ -155,7 +151,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
chainId,
inputAmount,
outputAmount,
inputAmountWithSlippage,
inputAmountWithSlippage: inputAmount,
},
flags: {
allowsOffchainSigning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function safeBundleEthFlow(

const { spender, settlementContract, safeAppsSdk, needsApproval, wrappedNativeContract } = safeBundleContext

const { inputAmountWithSlippage, chainId, inputAmount, outputAmount } = context
const { chainId, inputAmount, outputAmount } = context

const orderParams: PostOrderParams = {
...tradeContext.orderParams,
Expand All @@ -49,7 +49,7 @@ export async function safeBundleEthFlow(
const { account, recipientAddressOrName, kind } = orderParams

tradeFlowAnalytics.wrapApproveAndPresign(swapFlowAnalyticsContext)
const nativeAmountInWei = inputAmountWithSlippage.quotient.toString()
const nativeAmountInWei = inputAmount.quotient.toString()
const tradeAmounts = { inputAmount, outputAmount }

tradeConfirmActions.onSign(tradeAmounts)
Expand All @@ -72,7 +72,7 @@ export async function safeBundleEthFlow(
const approveTx = await buildApproveTx({
erc20Contract: wrappedNativeContract as unknown as Erc20,
spender,
amountToApprove: inputAmountWithSlippage,
amountToApprove: inputAmount,
})

txs.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export interface TradeFlowContext {
chainId: number
inputAmount: CurrencyAmount<Currency>
outputAmount: CurrencyAmount<Currency>
inputAmountWithSlippage: CurrencyAmount<Currency>
}
flags: {
allowsOffchainSigning: boolean
Expand Down

0 comments on commit 21f39fe

Please sign in to comment.