-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.tsx
253 lines (228 loc) · 8.31 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { useAtomValue } from 'jotai'
import React, { useMemo } from 'react'
import ICON_TOKENS from '@cowprotocol/assets/svg/tokens.svg'
import { isSellOrder } from '@cowprotocol/common-utils'
import { BannerOrientation, ClosableBanner, InlineBanner } from '@cowprotocol/ui'
import { Field } from 'legacy/state/types'
import { LimitOrdersWarnings } from 'modules/limitOrders/containers/LimitOrdersWarnings'
import { useLimitOrdersWidgetActions } from 'modules/limitOrders/containers/LimitOrdersWidget/hooks/useLimitOrdersWidgetActions'
import { TradeButtons } from 'modules/limitOrders/containers/TradeButtons'
import { TradeWidget, TradeWidgetSlots, useIsWrapOrUnwrap, useTradePriceImpact } from 'modules/trade'
import { useTradeConfirmState } from 'modules/trade'
import { BulletListItem, UnlockWidgetScreen } from 'modules/trade/pure/UnlockWidgetScreen'
import { useSetTradeQuoteParams, useTradeQuote } from 'modules/tradeQuote'
import { useRateInfoParams } from 'common/hooks/useRateInfoParams'
import { CurrencyInfo } from 'common/pure/CurrencyInputPanel/types'
import { LimitOrdersProps, limitOrdersPropsChecker } from './limitOrdersPropsChecker'
import * as styledEl from './styled'
import { useLimitOrdersDerivedState } from '../../hooks/useLimitOrdersDerivedState'
import { LimitOrdersFormState, useLimitOrdersFormState } from '../../hooks/useLimitOrdersFormState'
import { useUpdateLimitOrdersRawState } from '../../hooks/useLimitOrdersRawState'
import { useTradeFlowContext } from '../../hooks/useTradeFlowContext'
import { InfoBanner } from '../../pure/InfoBanner'
import { limitOrdersSettingsAtom } from '../../state/limitOrdersSettingsAtom'
import { limitRateAtom } from '../../state/limitRateAtom'
import { DeadlineInput } from '../DeadlineInput'
import { LimitOrdersConfirmModal } from '../LimitOrdersConfirmModal'
import { RateInput } from '../RateInput'
import { SettingsWidget } from '../SettingsWidget'
import { TradeRateDetails } from '../TradeRateDetails'
export const LIMIT_BULLET_LIST_CONTENT: BulletListItem[] = [
{ content: 'Set any limit price and time horizon' },
{ content: 'FREE order placement and cancellation' },
{ content: 'Place multiple orders using the same balance' },
{ content: 'Receive surplus of your order' },
{ content: 'Protection from MEV by default' },
{
content: <span>Place orders for higher than available balance!</span>,
},
]
const UNLOCK_SCREEN = {
title: 'Want to try out limit orders?',
subtitle: 'Get started!',
orderType: 'partially fillable',
buttonText: 'Get started with limit orders',
buttonLink:
'https://medium.com/@cow-protocol/cow-swap-improves-the-limit-order-experience-with-partially-fillable-limit-orders-45f19143e87d',
}
const ZERO_BANNER_STORAGE_KEY = 'limitOrdersZeroBalanceBanner:v0'
export function LimitOrdersWidget() {
const {
inputCurrency,
outputCurrency,
inputCurrencyAmount,
outputCurrencyAmount,
inputCurrencyBalance,
outputCurrencyBalance,
inputCurrencyFiatAmount,
outputCurrencyFiatAmount,
recipient,
isUnlocked,
orderKind,
} = useLimitOrdersDerivedState()
const settingsState = useAtomValue(limitOrdersSettingsAtom)
const isSell = isSellOrder(orderKind)
const { feeAmount } = useAtomValue(limitRateAtom)
const { isLoading: isRateLoading } = useTradeQuote()
const rateInfoParams = useRateInfoParams(inputCurrencyAmount, outputCurrencyAmount)
const widgetActions = useLimitOrdersWidgetActions()
const isWrapOrUnwrap = useIsWrapOrUnwrap()
const { showRecipient: showRecipientSetting } = settingsState
const showRecipient = showRecipientSetting || !!recipient
const priceImpact = useTradePriceImpact()
const quoteAmount = useMemo(
() => (isSell ? inputCurrencyAmount : outputCurrencyAmount),
[isSell, inputCurrencyAmount, outputCurrencyAmount],
)
useSetTradeQuoteParams(quoteAmount)
const inputCurrencyInfo: CurrencyInfo = {
field: Field.INPUT,
label: isSell ? 'Sell amount' : 'You sell at most',
currency: inputCurrency,
amount: inputCurrencyAmount,
isIndependent: isSell,
balance: inputCurrencyBalance,
fiatAmount: inputCurrencyFiatAmount,
receiveAmountInfo: null,
}
const outputCurrencyInfo: CurrencyInfo = {
field: Field.OUTPUT,
label: isSell ? 'Receive at least' : 'Buy exactly',
currency: outputCurrency,
amount: outputCurrencyAmount,
isIndependent: !isSell,
balance: outputCurrencyBalance,
fiatAmount: outputCurrencyFiatAmount,
receiveAmountInfo: null,
}
const props: LimitOrdersProps = {
inputCurrencyInfo,
outputCurrencyInfo,
isUnlocked,
isRateLoading,
showRecipient,
recipient,
rateInfoParams,
priceImpact,
settingsState,
feeAmount,
widgetActions,
isWrapOrUnwrap,
}
return <LimitOrders {...props} />
}
const LimitOrders = React.memo((props: LimitOrdersProps) => {
const {
inputCurrencyInfo,
outputCurrencyInfo,
isUnlocked,
isRateLoading,
widgetActions,
showRecipient,
recipient,
rateInfoParams,
priceImpact,
feeAmount,
isWrapOrUnwrap,
} = props
const tradeContext = useTradeFlowContext()
const updateLimitOrdersState = useUpdateLimitOrdersRawState()
const localFormValidation = useLimitOrdersFormState()
const { isOpen: isConfirmOpen } = useTradeConfirmState()
const inputCurrencyPreviewInfo = {
amount: inputCurrencyInfo.amount,
fiatAmount: inputCurrencyInfo.fiatAmount,
balance: inputCurrencyInfo.balance,
label: inputCurrencyInfo.label,
}
const outputCurrencyPreviewInfo = {
amount: outputCurrencyInfo.amount,
fiatAmount: outputCurrencyInfo.fiatAmount,
balance: outputCurrencyInfo.balance,
label: outputCurrencyInfo.label,
}
const slots: TradeWidgetSlots = {
settingsWidget: <SettingsWidget />,
lockScreen: isUnlocked ? undefined : (
<UnlockWidgetScreen
id="limit-orders"
items={LIMIT_BULLET_LIST_CONTENT}
buttonLink={UNLOCK_SCREEN.buttonLink}
title={UNLOCK_SCREEN.title}
subtitle={UNLOCK_SCREEN.subtitle}
orderType={UNLOCK_SCREEN.orderType}
buttonText={UNLOCK_SCREEN.buttonText}
handleUnlock={() => updateLimitOrdersState({ isUnlocked: true })}
/>
),
middleContent: (
<>
{!isWrapOrUnwrap &&
ClosableBanner(ZERO_BANNER_STORAGE_KEY, (onClose) => (
<InlineBanner
bannerType="success"
orientation={BannerOrientation.Horizontal}
customIcon={ICON_TOKENS}
iconSize={32}
onClose={onClose}
>
<p>
<b>NEW: </b>You can now place limit orders for amounts larger than your wallet balance. Partial fill
orders will execute until you run out of sell tokens. Fill-or-kill orders will become active once you
top up your balance.
</p>
</InlineBanner>
))}
<styledEl.RateWrapper>
<RateInput />
<DeadlineInput />
</styledEl.RateWrapper>
</>
),
bottomContent(warnings) {
return (
<>
<styledEl.FooterBox>
<TradeRateDetails rateInfoParams={rateInfoParams} />
</styledEl.FooterBox>
<LimitOrdersWarnings feeAmount={feeAmount} />
{warnings}
<styledEl.TradeButtonBox>
<TradeButtons isTradeContextReady={!!tradeContext} />
</styledEl.TradeButtonBox>
</>
)
},
outerContent: <>{isUnlocked && <InfoBanner />}</>,
}
const params = {
compactView: false,
recipient,
showRecipient,
isTradePriceUpdating: isRateLoading,
priceImpact,
disablePriceImpact: localFormValidation === LimitOrdersFormState.FeeExceedsFrom,
disableQuotePolling: isConfirmOpen,
hideTradeWarnings: !!localFormValidation,
}
return (
<TradeWidget
slots={slots}
actions={widgetActions}
params={params}
inputCurrencyInfo={inputCurrencyInfo}
outputCurrencyInfo={outputCurrencyInfo}
confirmModal={
tradeContext ? (
<LimitOrdersConfirmModal
recipient={recipient}
tradeContext={tradeContext}
priceImpact={priceImpact}
inputCurrencyInfo={inputCurrencyPreviewInfo}
outputCurrencyInfo={outputCurrencyPreviewInfo}
/>
) : null
}
/>
)
}, limitOrdersPropsChecker)