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 24, 2025
1 parent 99b84d3 commit 1b00d48
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 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 bridge 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
21 changes: 12 additions & 9 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 @@ -10,27 +11,29 @@ import { useConfigSbtc } from '../common/remote-config/remote-config.query';

export const defaultSbtcLimits = {
pegCap: 1000000000000,
perDepositMinimum: 100000,
perDepositCap: 100000000,
perDepositMinimum: 100000,
perWithdrawalCap: 100000000,
accountCaps: {},
};

interface GetSbtcLimitsResponse {
pegCap: number;
perDepositCap: number;
perWithdrawalCap: number;
perDepositMinimum: number;
accountCaps: Record<any, any>;
}
const sbtcLimitsResponseSchema = z.object({
pegCap: z.number(),
perDepositCap: z.number(),
perDepositMinimum: 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 1b00d48

Please sign in to comment.