Skip to content

Commit

Permalink
fix: sbtc swap validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Jan 23, 2025
1 parent 5df2687 commit 7a032bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/app/pages/swap/form/use-swap-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
convertAmountToBaseUnit,
convertAmountToFractionalUnit,
createMoney,
isUndefined,
} from '@leather.io/utils';

import { FormErrorMessages } from '@shared/error-messages';
Expand Down Expand Up @@ -95,6 +96,14 @@ export function useSwapForm<T extends BaseSwapContext<T>>() {
return true;
},
})
.test({
message: 'sBTC protocol temporarily disabled',
test() {
if (!isCrossChainSwap) return true;
if (isUndefined(sBtcLimits)) return false;
return true;
},
})
.test({
message: `Min amount is ${convertAmountToBaseUnit(sBtcDepositCapMin).toString()} BTC`,
test(value) {
Expand Down Expand Up @@ -139,8 +148,11 @@ export function useSwapForm<T extends BaseSwapContext<T>>() {
swapAssetBase.balance.decimals
)
);
if (!remainingSbtcPegCapSupply) return true;
if (valueInFractionalUnit.isGreaterThan(remainingSbtcPegCapSupply)) return false;
if (
!remainingSbtcPegCapSupply ||
valueInFractionalUnit.isGreaterThan(remainingSbtcPegCapSupply)
)
return false;
return true;
},
})
Expand Down
19 changes: 11 additions & 8 deletions src/app/query/sbtc/sbtc-limits.query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { z } from 'zod';

import { useStacksClient } from '@leather.io/query';
import { getStacksContractIdStringParts } from '@leather.io/stacks';
Expand All @@ -16,21 +17,23 @@ export const defaultSbtcLimits = {
accountCaps: {},
};

interface GetSbtcLimitsResponse {
pegCap: number;
perDepositCap: number;
perWithdrawalCap: number;
perDepositMinimum: number;
accountCaps: Record<any, any>;
}
export const sbtcLimitsResponseSchema = z.object({
pegCap: z.number(),
perDepositMinimum: z.number(),
perDepositCap: z.number(),
perWithdrawalCap: z.number(),
accountCaps: z.record(z.any()),
});

type GetSbtcLimitsResponse = z.infer<typeof sbtcLimitsResponseSchema>;

async function getSbtcLimits(apiUrl: string): Promise<GetSbtcLimitsResponse> {
const resp = await axios.get(`${apiUrl}/limits`, {
headers: {
'Content-Type': 'application/json',
},
});
return resp.data;
return sbtcLimitsResponseSchema.parse(resp.data);
}

export function useGetSbtcLimits() {
Expand Down

0 comments on commit 7a032bc

Please sign in to comment.