Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamic sbtc network #6002

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/app/pages/swap/hooks/use-sbtc-deposit-transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { useNavigate } from 'react-router-dom';

import * as btc from '@scure/btc-signer';
import type { P2TROut } from '@scure/btc-signer/payment';
import { REGTEST, SbtcApiClientTestnet, buildSbtcDepositTx } from 'sbtc';
import { MAINNET, REGTEST, SbtcApiClientTestnet, TESTNET, buildSbtcDepositTx } from 'sbtc';

import type { BitcoinNetworkModes } from '@leather.io/models';
import { useAverageBitcoinFeeRates } from '@leather.io/query';
import { btcToSat, createMoney } from '@leather.io/utils';

Expand All @@ -19,6 +20,7 @@ import { useBreakOnNonCompliantEntity } from '@app/query/common/compliance-check
import { useBitcoinScureLibNetworkConfig } from '@app/store/accounts/blockchain/bitcoin/bitcoin-keychain';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import type { SwapSubmissionData } from '../swap.context';

Expand All @@ -36,6 +38,17 @@ interface SbtcDeposit {
// Check network for correct client
const client = new SbtcApiClientTestnet();

function getSbtcNetworkConfig(network: BitcoinNetworkModes) {
const networkMap = {
mainnet: MAINNET,
testnet: TESTNET,
regtest: REGTEST,
// Signet supported not tested, but likely uses same values as testnet
signet: TESTNET,
};
return networkMap[network];
}

export function useSbtcDepositTransaction() {
const toast = useToast();
const { setIsIdle } = useLoading(LoadingKeys.SUBMIT_SWAP_TRANSACTION);
Expand All @@ -45,6 +58,7 @@ export function useSbtcDepositTransaction() {
const signer = useCurrentAccountNativeSegwitIndexZeroSigner();
const networkMode = useBitcoinScureLibNetworkConfig();
const navigate = useNavigate();
const network = useCurrentNetwork();

// Check if the signer is compliant
useBreakOnNonCompliantEntity();
Expand All @@ -56,7 +70,7 @@ export function useSbtcDepositTransaction() {
try {
const deposit: SbtcDeposit = buildSbtcDepositTx({
amountSats: btcToSat(swapData.swapAmountQuote).toNumber(),
network: REGTEST, // TODO: Use current network, should be set by default on client
network: getSbtcNetworkConfig(network.chain.bitcoin.mode),
stacksAddress: stacksAccount.address,
signersPublicKey: await client.fetchSignersPublicKey(),
maxSignerFee,
Expand Down
Loading