Skip to content

Commit

Permalink
fix: withdraw issue (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickk137 authored Jul 31, 2024
1 parent 40e1c1a commit e985021
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
10 changes: 10 additions & 0 deletions liquidity/components/WithdrawModal/WithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ export function WithdrawModal({
queryClient.invalidateQueries({
queryKey: [`${network?.id}-${network?.preset}`, 'LiquidityPositions', { accountId }],
});
queryClient.invalidateQueries({
queryKey: [
`${network?.id}-${network?.preset}`,
'AccountCollateralUnlockDate',
{ accountId },
],
});
queryClient.invalidateQueries({
queryKey: [`${network?.id}-${network?.preset}`, 'TokenBalance'],
});

setWithdrawAmount(ZEROWEI);
} else {
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useWithdraw/useWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ export const useWithdraw = ({
mutationFn: async () => {
if (!signer || !network || !provider) throw new Error('No signer or network');

if (!(CoreProxy && collateralTypeAddress && amount && collateralPriceIds)) return;
if (amount?.eq(0)) return;
if (!(CoreProxy && collateralTypeAddress && amount && collateralPriceIds)) {
throw new Error('Not ready');
}
if (amount?.eq(0)) {
throw new Error('Amount less than 0');
}

const walletAddress = await signer.getAddress();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export const useWithdrawBaseAndromeda = ({
const mutation = useMutation({
mutationFn: async () => {
if (!signer || !network || !provider) throw new Error('No signer or network');
if (!(CoreProxy && SpotProxy && accountId && usdTokens?.sUSD && usdTokens.snxUSD)) return;
if (!(CoreProxy && SpotProxy && accountId && usdTokens?.sUSD && usdTokens.snxUSD)) {
throw new Error('Not ready');
}

const total = snxUSDCollateral.add(sUSDCCollateral);

if (total.lt(amountToWithdraw)) {
return;
throw new Error('Exceeds balance');
}

const sUSDCAmount = amountToWithdraw.gt(sUSDCCollateral) ? sUSDCCollateral : amountToWithdraw;
Expand Down
5 changes: 3 additions & 2 deletions liquidity/lib/validatePosition/validatePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export const validatePosition = ({
const maxDebt = maybeMaxDebt.gte(0) ? maybeMaxDebt : wei(0);

const isValid =
(newCRatio.gte(targetCRatio) || newCRatio.lte(0)) &&
(newDebt.lte(0) || newCollateralAmount.gt(0));
(debtChange.eq(0) && collateralChange.eq(0)) ||
((newCRatio.gte(targetCRatio) || newCRatio.lte(0)) &&
(newDebt.lte(0) || newCollateralAmount.gt(0)));

return {
isValid,
Expand Down

0 comments on commit e985021

Please sign in to comment.