Skip to content

Commit 7927495

Browse files
committed
Refactor GmDepositWithdrawalBox
1 parent 7d97554 commit 7927495

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

src/components/Synthetics/GmSwap/GmSwapBox/GmDepositWithdrawalBox/GmDepositWithdrawalBox.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { Swap } from "../Swap";
4949
import { Mode, Operation } from "../types";
5050
import { useGmWarningState } from "../useGmWarningState";
5151
import { InfoRows } from "./InfoRows";
52+
import { TokenInputState } from "./types";
5253
import { useDepositWithdrawalAmounts } from "./useDepositWithdrawalAmounts";
5354
import { useDepositWithdrawalFees } from "./useDepositWithdrawalFees";
5455
import { useGmDepositWithdrawalBoxState } from "./useGmDepositWithdrawalBoxState";
@@ -195,17 +196,21 @@ export function GmSwapBoxDepositWithdrawal(p: GmSwapBoxProps) {
195196
shortTokenInputState,
196197
// undefined when not paying with GM
197198
fromMarketTokenInputState,
198-
} = useMemo(() => {
199+
} = useMemo((): Partial<{
200+
longTokenInputState: TokenInputState;
201+
shortTokenInputState: TokenInputState;
202+
fromMarketTokenInputState: TokenInputState;
203+
}> => {
199204
if (!marketInfo) {
200205
return {};
201206
}
202207

203208
const inputs: {
204209
address: string;
205210
value: string;
206-
amount?: bigint;
207-
isMarketToken?: boolean;
208-
usd?: bigint;
211+
amount: bigint | undefined;
212+
isMarketToken: boolean;
213+
usd: bigint | undefined;
209214
setValue: (val: string) => void;
210215
}[] = [];
211216

@@ -560,12 +565,12 @@ export function GmSwapBoxDepositWithdrawal(p: GmSwapBoxProps) {
560565

561566
const handleFirstTokenInputValueChange = useCallback(
562567
(e) => {
563-
if (firstToken) {
568+
if (firstTokenAddress) {
564569
setFirstTokenInputValue(e.target.value);
565-
onFocusedCollateralInputChange(firstToken.address);
570+
onFocusedCollateralInputChange(firstTokenAddress);
566571
}
567572
},
568-
[firstToken, onFocusedCollateralInputChange, setFirstTokenInputValue]
573+
[firstTokenAddress, onFocusedCollateralInputChange, setFirstTokenInputValue]
569574
);
570575

571576
const marketTokenInputClickMax = useCallback(() => {
@@ -630,6 +635,7 @@ export function GmSwapBoxDepositWithdrawal(p: GmSwapBoxProps) {
630635

631636
// #region Effects
632637
useUpdateInputAmounts({
638+
chainId,
633639
marketToken,
634640
marketInfo,
635641
fromMarketTokenInputState,
@@ -851,11 +857,7 @@ export function GmSwapBoxDepositWithdrawal(p: GmSwapBoxProps) {
851857
shouldShowWarningForExecutionFee={shouldShowWarningForExecutionFee}
852858
/>
853859
</div>
854-
{/* <SwitchToSettlementChainWarning topic="liquidity" /> */}
855-
<div className="Exchange-swap-button-container mb-14 border-b border-stroke-primary pb-14">
856-
{/* <SwitchToSettlementChainButtons>{}</SwitchToSettlementChainButtons> */}
857-
{submitButton}
858-
</div>
860+
<div className="Exchange-swap-button-container mb-14 border-b border-stroke-primary pb-14">{submitButton}</div>
859861
<InfoRows fees={fees} executionFee={executionFee} isDeposit={isDeposit} />
860862
</form>
861863
</>
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { TokenData } from "domain/synthetics/tokens";
2-
31
export type TokenInputState = {
42
address: string;
53
value: string;
6-
amount?: bigint | undefined;
7-
usd?: bigint | undefined;
8-
token?: TokenData | undefined;
4+
amount: bigint | undefined;
5+
usd: bigint | undefined;
96
setValue: (val: string) => void;
10-
isMarketToken?: boolean;
7+
isMarketToken: boolean;
118
};
129

1310
export type GmOrGlvPaySource = "settlementChain" | "gmxAccount" | "sourceChain";

src/components/Synthetics/GmSwap/GmSwapBox/GmDepositWithdrawalBox/useDepositWithdrawalAmounts.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ export function useDepositWithdrawalAmounts({
100100
}
101101

102102
const longTokenAmount = marketInfo.isSameCollaterals ? halfOfLong ?? 0n : longTokenInputState?.amount ?? 0n;
103-
const shortTokenAmount = marketInfo.isSameCollaterals
104-
? longTokenInputState?.amount !== undefined
105-
? longTokenInputState?.amount - longTokenAmount
106-
: undefined ?? 0n
107-
: shortTokenInputState?.amount ?? 0n;
103+
const shortTokenAmount =
104+
(marketInfo.isSameCollaterals
105+
? longTokenInputState?.amount !== undefined
106+
? longTokenInputState?.amount - longTokenAmount
107+
: 0n
108+
: shortTokenInputState?.amount) ?? 0n;
108109

109110
return getWithdrawalAmounts({
110111
marketInfo,

src/components/Synthetics/GmSwap/GmSwapBox/GmDepositWithdrawalBox/useUpdateInputAmounts.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { GlvInfo, MarketInfo } from "domain/synthetics/markets/types";
44
import { TokenData } from "domain/synthetics/tokens";
55
import type { DepositAmounts, WithdrawalAmounts } from "domain/synthetics/trade";
66
import { formatAmountFree } from "lib/numbers";
7+
import { ContractsChainId } from "sdk/configs/chains";
8+
import { getToken } from "sdk/configs/tokens";
79

810
import { TokenInputState } from "./types";
911

1012
export function useUpdateInputAmounts({
13+
chainId,
1114
marketToken,
1215
marketInfo,
1316
longTokenInputState,
@@ -25,6 +28,7 @@ export function useUpdateInputAmounts({
2528
setFirstTokenInputValue,
2629
setSecondTokenInputValue,
2730
}: {
31+
chainId: ContractsChainId;
2832
marketToken: TokenData | undefined;
2933
glvToken: TokenData | undefined;
3034
marketInfo: MarketInfo | undefined;
@@ -48,9 +52,9 @@ export function useUpdateInputAmounts({
4852
return;
4953
}
5054

51-
const longToken = longTokenInputState?.token;
52-
const shortToken = shortTokenInputState?.token;
53-
const fromMarketToken = fromMarketTokenInputState?.token;
55+
const longToken = longTokenInputState ? getToken(chainId, longTokenInputState.address) : undefined;
56+
const shortToken = shortTokenInputState ? getToken(chainId, shortTokenInputState.address) : undefined;
57+
const fromMarketToken = marketToken;
5458

5559
if (isDeposit) {
5660
if (["longCollateral", "shortCollateral"].includes(focusedInput)) {
@@ -185,6 +189,7 @@ export function useUpdateInputAmounts({
185189
glvInfo,
186190
glvToken,
187191
glvTokenAmount,
192+
chainId,
188193
]
189194
);
190195
}

0 commit comments

Comments
 (0)