Skip to content

Commit 9a21671

Browse files
committed
misc
1 parent ea5a902 commit 9a21671

File tree

8 files changed

+226
-121
lines changed

8 files changed

+226
-121
lines changed

src/components/transactions/Swap/constants/cow.constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ export const COW_PROTOCOL_ETH_FLOW_ADDRESS_BY_ENV = (env: CowEnv) => {
7070
export const COW_CREATE_ORDER_ABI =
7171
'function createOrder((address,address,uint256,uint256,bytes32,uint256,uint32,bool,int64)) returns (bytes32)';
7272

73-
export const HEADER_WIDGET_APP_CODE = 'aave-v3-interface-widget';
74-
export const ADAPTER_APP_CODE = 'aave-v3-interface-aps'; // Use this one for contract adapters so we have different dashboards
7573
export const COW_PARTNER_FEE = (tokenFromSymbol: string, tokenToSymbol: string) => ({
7674
volumeBps: getAssetGroup(tokenFromSymbol) == getAssetGroup(tokenToSymbol) ? 15 : 25,
7775
recipient: COW_EVM_RECIPIENT,

src/components/transactions/Swap/errors/shared/console.helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function buildErrorPayload(state: SwapState, error: SwapError) {
3131
useFlashloan: state.useFlashloan ?? false,
3232
side: state.side,
3333
orderType: state.orderType,
34+
swapType: state.swapType,
3435
slippage: state.slippage,
3536
input: {
3637
token: state.sourceToken.symbol,

src/components/transactions/Swap/hooks/useSwapQuote.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { getCowProtocolSellRates } from '../helpers/cow';
1212
import { getParaswapSellRates, getParaswapSlippage } from '../helpers/paraswap';
1313
import { getSwitchProvider } from '../helpers/shared/provider.helpers';
1414
import {
15-
OrderType,
1615
SwapParams,
1716
SwapProvider,
1817
SwapQuoteType as SwapQuoteType,
@@ -161,13 +160,13 @@ export const useSwapQuote = ({
161160

162161
// Skip update if nothing changed to avoid re-render loops
163162
if (
164-
state.provider === quote.provider &&
165-
state.swapRate?.srcSpotAmount === quote.srcSpotAmount &&
166-
state.swapRate?.destSpotAmount === quote.destSpotAmount &&
167-
state.inputAmount === nextInputAmount &&
168-
state.outputAmount === nextOutputAmount &&
169-
state.inputAmountUSD === nextInputAmountUSD &&
170-
state.outputAmountUSD === nextOutputAmountUSD
163+
state.provider == quote.provider &&
164+
state.swapRate?.srcSpotAmount == quote.srcSpotAmount &&
165+
state.swapRate?.destSpotAmount == quote.destSpotAmount &&
166+
state.inputAmount == nextInputAmount &&
167+
state.outputAmount == nextOutputAmount &&
168+
state.inputAmountUSD == nextInputAmountUSD &&
169+
state.outputAmountUSD == nextOutputAmountUSD
171170
) {
172171
return;
173172
}
@@ -422,9 +421,6 @@ const useMultiProviderSwapQuoteQuery = ({
422421
const isFlashloanDisabled = hasFlashLoanDisabled(state);
423422

424423
return (
425-
// LIMIT: fetch only once (when no quote yet). MARKET: fetch normally
426-
((state.orderType === OrderType.LIMIT && !state.swapRate) ||
427-
state.orderType === OrderType.MARKET) &&
428424
hasPositiveUserAmount &&
429425
!isSameTokenPair &&
430426
!isFlashloanDisabled &&
@@ -440,11 +436,9 @@ const useMultiProviderSwapQuoteQuery = ({
440436
throwOnError: false,
441437
refetchOnWindowFocus: (query) => (query.state.error ? false : true),
442438
refetchInterval: (() => {
443-
// LIMIT: never refetch periodically after we got the first quote
444439
const isInsufficientBalance = hasInsufficientBalance(state);
445440
const isFlashloanDisabled = hasFlashLoanDisabled(state);
446-
return state.orderType !== OrderType.LIMIT &&
447-
!state.actionsLoading &&
441+
return !state.actionsLoading &&
448442
!state.quoteRefreshPaused &&
449443
!state.mainTxState.success &&
450444
!state.mainTxState.txHash &&

src/components/transactions/Swap/inputs/LimitOrderInputs.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,23 @@ export const LimitOrderInputs = ({
113113
!state.error
114114
}
115115
onChange={swapState.handleInputChange}
116-
onClear={() =>
116+
onClear={() => {
117117
setState({
118118
inputAmount: '',
119119
debouncedInputAmount: '',
120120
inputAmountUSD: '',
121121
quoteRefreshPaused: true,
122122
quoteLastUpdatedAt: undefined,
123-
})
124-
}
123+
});
124+
if (state.outputAmount === '') {
125+
// Both reset to listen quotes
126+
setState({
127+
swapRate: undefined,
128+
quoteRefreshPaused: false,
129+
quoteLastUpdatedAt: undefined,
130+
});
131+
}
132+
}}
125133
usdValue={state.inputAmountUSD || '0'}
126134
onSelect={swapState.handleSelectedInputToken}
127135
selectedAsset={state.sourceToken}
@@ -230,15 +238,23 @@ export const LimitOrderInputs = ({
230238
onChange={(value) => {
231239
swapState.handleOutputChange(value);
232240
}}
233-
onClear={() =>
241+
onClear={() => {
234242
setState({
235243
outputAmount: '',
236244
debouncedOutputAmount: '',
237245
outputAmountUSD: '',
238246
quoteRefreshPaused: true,
239247
quoteLastUpdatedAt: undefined,
240-
})
241-
}
248+
});
249+
if (state.inputAmount === '') {
250+
// Both reset to listen quotes
251+
setState({
252+
swapRate: undefined,
253+
quoteRefreshPaused: false,
254+
quoteLastUpdatedAt: undefined,
255+
});
256+
}
257+
}}
242258
onSelect={swapState.handleSelectedOutputToken}
243259
disableInput={false}
244260
selectedAsset={state.destinationToken}
@@ -250,12 +266,12 @@ export const LimitOrderInputs = ({
250266

251267
<PriceInput
252268
originAsset={state.sourceToken}
269+
originAssetAmount={state.inputAmount}
270+
originAssetAmountUSD={state.inputAmountUSD}
253271
targetAsset={state.destinationToken}
272+
targetAssetAmount={state.outputAmount}
273+
targetAssetAmountUSD={state.outputAmountUSD}
254274
loading={state.ratesLoading}
255-
inputAmount={state.inputAmount}
256-
outputAmount={state.outputAmount}
257-
inputAmountUSD={state.inputAmountUSD}
258-
outputAmountUSD={state.outputAmountUSD}
259275
handleRateChange={swapState.handleRateChange}
260276
disabled={!state.swapRate?.srcSpotAmount || !state.swapRate?.destSpotAmount} // No initial rate set yet
261277
/>

src/components/transactions/Swap/inputs/SwapInputs.tsx

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,29 @@ export const SwapInputs = ({
8080
if (value === '-1') {
8181
const maxAmount = state.sourceToken.balance;
8282
setState({
83-
quoteRefreshPaused: false,
84-
quoteLastUpdatedAt: undefined,
85-
quoteTimerPausedAt: undefined,
86-
quoteTimerPausedAccumMs: undefined,
83+
...(state.orderType === OrderType.LIMIT && state.swapRate
84+
? {
85+
quoteRefreshPaused: true,
86+
quoteLastUpdatedAt: undefined,
87+
quoteTimerPausedAt: undefined,
88+
quoteTimerPausedAccumMs: undefined,
89+
}
90+
: {}),
8791
inputAmount: maxAmount,
8892
inputAmountUSD: computeUSD(maxAmount),
8993
isMaxSelected: true,
9094
side: 'sell',
9195
});
9296
} else {
9397
setState({
94-
quoteRefreshPaused: false,
95-
quoteLastUpdatedAt: undefined,
96-
quoteTimerPausedAt: undefined,
97-
quoteTimerPausedAccumMs: undefined,
98+
...(state.orderType === OrderType.LIMIT && state.swapRate
99+
? {
100+
quoteRefreshPaused: true,
101+
quoteLastUpdatedAt: undefined,
102+
quoteTimerPausedAt: undefined,
103+
quoteTimerPausedAccumMs: undefined,
104+
}
105+
: {}),
98106
inputAmount: value,
99107
inputAmountUSD: computeUSD(value),
100108
isMaxSelected: value === state.forcedMaxValue,
@@ -121,21 +129,29 @@ export const SwapInputs = ({
121129
if (value === '-1') {
122130
const maxAmount = state.destinationToken.balance;
123131
setState({
124-
quoteRefreshPaused: false,
125-
quoteLastUpdatedAt: undefined,
126-
quoteTimerPausedAt: undefined,
127-
quoteTimerPausedAccumMs: undefined,
132+
...(state.orderType === OrderType.LIMIT && state.swapRate
133+
? {
134+
quoteRefreshPaused: true,
135+
quoteLastUpdatedAt: undefined,
136+
quoteTimerPausedAt: undefined,
137+
quoteTimerPausedAccumMs: undefined,
138+
}
139+
: {}),
128140
outputAmount: maxAmount,
129141
outputAmountUSD: computeUSD(maxAmount),
130142
isMaxSelected: true,
131143
side: 'buy',
132144
});
133145
} else {
134146
setState({
135-
quoteRefreshPaused: false,
136-
quoteLastUpdatedAt: undefined,
137-
quoteTimerPausedAt: undefined,
138-
quoteTimerPausedAccumMs: undefined,
147+
...(state.orderType === OrderType.LIMIT && state.swapRate
148+
? {
149+
quoteRefreshPaused: true,
150+
quoteLastUpdatedAt: undefined,
151+
quoteTimerPausedAt: undefined,
152+
quoteTimerPausedAccumMs: undefined,
153+
}
154+
: {}),
139155
outputAmount: value,
140156
outputAmountUSD: computeUSD(value),
141157
isMaxSelected: false,
@@ -153,16 +169,17 @@ export const SwapInputs = ({
153169
// User changed custom price, pause quote refresh in limit orders
154170
setState({ quoteRefreshPaused: true });
155171

156-
// When rate is changed, new output amount is calculated based on the input amount
157-
let newOutputAmount: string;
158-
if (rateFromAsset.addressToSwap === state.sourceToken.addressToSwap) {
159-
newOutputAmount = valueToBigNumber(state.inputAmount).multipliedBy(newRate).toString();
160-
} else {
161-
newOutputAmount = valueToBigNumber(state.outputAmount).dividedBy(newRate).toString();
162-
}
172+
// Normalize the rate to always be dest per 1 source, then recompute output deterministically
173+
const isBaseSource = rateFromAsset.addressToSwap === state.sourceToken.addressToSwap;
174+
const rateDestPerSrc = isBaseSource
175+
? valueToBigNumber(newRate)
176+
: valueToBigNumber(1).dividedBy(newRate);
177+
const newOutputAmount = valueToBigNumber(state.inputAmount || '0')
178+
.multipliedBy(rateDestPerSrc)
179+
.toString();
163180

164181
setState({
165-
quoteRefreshPaused: false,
182+
quoteRefreshPaused: true,
166183
quoteLastUpdatedAt: undefined,
167184
quoteTimerPausedAt: undefined,
168185
quoteTimerPausedAccumMs: undefined,

0 commit comments

Comments
 (0)