Skip to content

Commit

Permalink
fix: use corresponding trade type in context
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Oct 22, 2024
1 parent 4ab395e commit d3cb5ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 10 additions & 0 deletions apps/cowswap-frontend/src/modules/trade/const/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { UiOrderType } from '@cowprotocol/types'

import { TradeType } from '../types'

export const TradeTypeToUiOrderType: Record<TradeType, UiOrderType> = {
[TradeType.SWAP]: UiOrderType.SWAP,
[TradeType.LIMIT_ORDER]: UiOrderType.LIMIT,
[TradeType.ADVANCED_ORDERS]: UiOrderType.TWAP,
[TradeType.YIELD]: UiOrderType.YIELD,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { useEffect, useRef } from 'react'

import { getCurrencyAddress } from '@cowprotocol/common-utils'
import { AtomsAndUnits, CowWidgetEvents, OnTradeParamsPayload } from '@cowprotocol/events'
import { TokenInfo, UiOrderType } from '@cowprotocol/types'
import { TokenInfo } from '@cowprotocol/types'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

import { WIDGET_EVENT_EMITTER } from 'widgetEventEmitter'

import { useDerivedTradeState } from './useDerivedTradeState'

import { TradeTypeToUiOrderType } from '../const/common'
import { TradeType } from '../types'
import { TradeDerivedState } from '../types/TradeDerivedState'

Expand All @@ -32,13 +33,6 @@ export function useNotifyWidgetTrade() {
}, [state])
}

const TradeTypeToUiOrderType: Record<TradeType, UiOrderType> = {
[TradeType.SWAP]: UiOrderType.SWAP,
[TradeType.LIMIT_ORDER]: UiOrderType.LIMIT,
[TradeType.ADVANCED_ORDERS]: UiOrderType.TWAP,
[TradeType.YIELD]: UiOrderType.YIELD,
}

function getTradeParamsEventPayload(tradeType: TradeType, state: TradeDerivedState): OnTradeParamsPayload {
return {
orderType: TradeTypeToUiOrderType[tradeType],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TokenWithLogo } from '@cowprotocol/common-const'
import { COW_PROTOCOL_VAULT_RELAYER_ADDRESS, OrderClass, SupportedChainId } from '@cowprotocol/cow-sdk'
import { UiOrderType } from '@cowprotocol/types'
import { useIsSafeWallet, useWalletDetails, useWalletInfo } from '@cowprotocol/wallet'
import { useWalletProvider } from '@cowprotocol/wallet-provider'

Expand All @@ -13,11 +12,12 @@ import { useCloseModals } from 'legacy/state/application/hooks'
import { useAppData, useAppDataHooks } from 'modules/appData'
import { useGeneratePermitHook, useGetCachedPermit, usePermitInfo } from 'modules/permit'
import { useEnoughBalanceAndAllowance } from 'modules/tokens'
import { TradeType, useDerivedTradeState, useReceiveAmountInfo, useTradeConfirmActions } from 'modules/trade'
import { useDerivedTradeState, useReceiveAmountInfo, useTradeConfirmActions, useTradeTypeInfo } from 'modules/trade'
import { getOrderValidTo, useTradeQuote } from 'modules/tradeQuote'

import { useGP2SettlementContract } from 'common/hooks/useContract'

import { TradeTypeToUiOrderType } from '../../trade/const/common'
import { TradeFlowContext } from '../types/TradeFlowContext'

export interface TradeFlowParams {
Expand All @@ -31,6 +31,9 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
const isSafeWallet = useIsSafeWallet()
const derivedTradeState = useDerivedTradeState()
const receiveAmountInfo = useReceiveAmountInfo()
const tradeTypeInfo = useTradeTypeInfo()
const tradeType = tradeTypeInfo?.tradeType
const uiOrderType = tradeType ? TradeTypeToUiOrderType[tradeType] : null

const sellCurrency = derivedTradeState?.inputCurrency
const inputAmount = receiveAmountInfo?.afterNetworkCosts.sellAmount
Expand All @@ -39,7 +42,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
const inputAmountWithSlippage = receiveAmountInfo?.afterSlippage.sellAmount
const networkFee = receiveAmountInfo?.costs.networkFee.amountInSellCurrency

const permitInfo = usePermitInfo(sellCurrency, TradeType.YIELD)
const permitInfo = usePermitInfo(sellCurrency, tradeType)
const generatePermitHook = useGeneratePermitHook()
const getCachedPermit = useGetCachedPermit()
const closeModals = useCloseModals()
Expand Down Expand Up @@ -84,7 +87,8 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
quoteResponse &&
localQuoteTimestamp &&
orderKind &&
settlementContract
settlementContract &&
uiOrderType
? [
account,
allowsOffchainSigning,
Expand Down Expand Up @@ -112,6 +116,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
typedHooks,
deadline,
orderKind,
uiOrderType,
]
: null,
([
Expand Down Expand Up @@ -141,6 +146,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
typedHooks,
deadline,
orderKind,
uiOrderType,
]) => {
return {
context: {
Expand All @@ -163,7 +169,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
recipient,
recipientAddress,
marketLabel: [inputAmount?.currency.symbol, outputAmount?.currency.symbol].join(','),
orderType: UiOrderType.YIELD,
orderType: uiOrderType,
},
contract: settlementContract,
permitInfo: !enoughAllowance ? permitInfo : undefined,
Expand Down

0 comments on commit d3cb5ef

Please sign in to comment.