Skip to content

Commit f9c47bb

Browse files
committed
auto settle positive pnl on withdrawal
1 parent 662aa5e commit f9c47bb

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

app/components/Assets.tsx

+6-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
useWithdraw
77
} from '@orderly.network/hooks';
88
import { AccountStatusEnum } from '@orderly.network/types';
9-
import { QuestionMarkCircledIcon } from '@radix-ui/react-icons';
10-
import { Table, Tooltip } from '@radix-ui/themes';
9+
import { Table } from '@radix-ui/themes';
1110
import { useNotifications } from '@web3-onboard/react';
1211
import { FixedNumber } from 'ethers';
1312
import { FC, useMemo } from 'react';
@@ -40,7 +39,7 @@ export const Assets: FC = () => {
4039
() => (status >= AccountStatusEnum.Connected ? deposit.balance : undefined),
4140
[status, deposit]
4241
);
43-
const { withdraw, unsettledPnL, availableWithdraw } = useWithdraw();
42+
const { withdraw, availableWithdraw } = useWithdraw();
4443

4544
return (
4645
<div className="flex flex-col gap-8">
@@ -58,26 +57,14 @@ export const Assets: FC = () => {
5857
{usdFormatter.format(collateral.availableBalance)} $
5958
</Table.Cell>
6059
</Table.Row>
61-
<Table.Row>
62-
<Table.RowHeaderCell>Unsettled PnL:</Table.RowHeaderCell>
63-
<Table.Cell className="text-right">{usdFormatter.format(unsettledPnL)} $</Table.Cell>
64-
</Table.Row>
6560
</Table.Body>
66-
<Table.Row>
67-
<Table.RowHeaderCell className="flex">
68-
<Tooltip content="The maximum withdrawable amount. 'freeCollateral - unsettledPnL'">
69-
<div className="content">
70-
Withdrawable Balance <QuestionMarkCircledIcon />
71-
</div>
72-
</Tooltip>
73-
:
74-
</Table.RowHeaderCell>
75-
<Table.Cell className="text-right">{usdFormatter.format(availableWithdraw)} $</Table.Cell>
76-
</Table.Row>
7761
</Table.Root>
7862
<OrderlyDeposit
7963
walletBalance={FixedNumber.fromString(balance ?? '0', { decimals: 6 })}
80-
orderlyBalance={FixedNumber.fromString(availableWithdraw.toPrecision(6), {
64+
orderlyBalance={FixedNumber.fromString(collateral.availableBalance.toPrecision(6), {
65+
decimals: 6
66+
})}
67+
availableWithdraw={FixedNumber.fromString(availableWithdraw.toPrecision(6), {
8168
decimals: 6
8269
})}
8370
withdraw={withdraw}

app/components/OrderlyDeposit.tsx

+29-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import { useIsTestnet } from '~/hooks';
1313
export const OrderlyDeposit: FC<{
1414
walletBalance: FixedNumber;
1515
orderlyBalance: FixedNumber;
16+
availableWithdraw: FixedNumber;
1617
withdraw: ReturnType<typeof useWithdraw>['withdraw'];
17-
}> = ({ walletBalance, orderlyBalance, withdraw }) => {
18+
}> = ({ walletBalance, orderlyBalance, availableWithdraw, withdraw }) => {
1819
const [amount, setAmount] = useState<FixedNumber>();
1920
const [open, setOpen] = useState(false);
2021
const [disabled, setDisabled] = useState(true);
@@ -174,6 +175,32 @@ export const OrderlyDeposit: FC<{
174175
}
175176
}
176177
} else {
178+
if (availableWithdraw.lt(orderlyBalance)) {
179+
const { update } = customNotification({
180+
eventCode: 'settle',
181+
type: 'pending',
182+
message: 'Settling PnL...'
183+
});
184+
try {
185+
await account.settle();
186+
update({
187+
eventCode: 'settleSuccess',
188+
type: 'success',
189+
message: 'Successfully settled PnL!',
190+
autoDismiss: 5_000
191+
});
192+
} catch (err) {
193+
console.error(err);
194+
update({
195+
eventCode: 'settleError',
196+
type: 'error',
197+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
198+
message: (err as any).message ?? 'Something went wrong',
199+
autoDismiss: 15_000
200+
});
201+
}
202+
}
203+
177204
const { update } = customNotification({
178205
eventCode: 'withdraw',
179206
type: 'pending',
@@ -223,7 +250,7 @@ export const OrderlyDeposit: FC<{
223250
const { update } = customNotification({
224251
eventCode: 'mint',
225252
type: 'pending',
226-
message: 'Minting 1k USDC on testnet...'
253+
message: 'Minting USDC on testnet...'
227254
});
228255
try {
229256
const chainNamespace = state.chainNamespace;

0 commit comments

Comments
 (0)