From 4770f41c1fdd11224213e87d9415fb87ab02e032 Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Mon, 6 Jan 2025 13:31:23 +1100 Subject: [PATCH 1/3] fix: slashing modal text --- src/app/components/Modals/SlashingModal.tsx | 17 +++++++++++++---- src/app/hooks/services/useDelegationService.ts | 13 +++++++++++-- .../components/DelegationModal.tsx | 4 ++++ .../delegations/DelegationList/index.tsx | 8 +++++--- src/config/network/index.ts | 17 +++++++++++++++++ 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/config/network/index.ts diff --git a/src/app/components/Modals/SlashingModal.tsx b/src/app/components/Modals/SlashingModal.tsx index e27c55e6..122cd6e4 100644 --- a/src/app/components/Modals/SlashingModal.tsx +++ b/src/app/components/Modals/SlashingModal.tsx @@ -1,5 +1,8 @@ import { Text } from "@babylonlabs-io/bbn-core-ui"; +import { BbnStakingParamsVersion } from "@/app/types/networkInfo"; +import { NetworkConfig } from "@/config/network"; + import { ConfirmationModal } from "./ConfirmationModal"; interface UnbondModalProps { @@ -7,15 +10,21 @@ interface UnbondModalProps { open: boolean; onClose: () => void; onSubmit: () => void; + networkConfig: NetworkConfig; + param: BbnStakingParamsVersion | null; } export const SlashingModal = (props: UnbondModalProps) => { + const { networkConfig, param } = props; + const slashingRate = (param?.slashing?.slashingRate ?? 0) * 100; + return ( - + - The Finality Provider you delegated to has been slashed and removed from - the network. You can withdraw your non-slashed balance after the - timelock period ends. Slashed funds cannot be recovered. + Your finality provider equivocated(double-voted) leading to{" "} + {slashingRate}% of your stake getting slashed. You are about to withdraw + the remaining balance. A transaction fee will be deducted from your + stake by the {networkConfig.btc.networkName} network. ); diff --git a/src/app/hooks/services/useDelegationService.ts b/src/app/hooks/services/useDelegationService.ts index a15e6cef..fba1093c 100644 --- a/src/app/hooks/services/useDelegationService.ts +++ b/src/app/hooks/services/useDelegationService.ts @@ -7,7 +7,9 @@ import { DelegationV2, DelegationV2StakingState as State, } from "@/app/types/delegationsV2"; +import { BbnStakingParamsVersion } from "@/app/types/networkInfo"; import { validateDelegation } from "@/utils/delegations"; +import { getBbnParamByVersion } from "@/utils/params"; import { useTransactionService } from "./useTransactionService"; @@ -40,6 +42,7 @@ type DelegationCommand = (props: TxProps) => Promise; interface ConfirmationModalState { action: ActionType; delegation: DelegationV2; + param: BbnStakingParamsVersion; } export function useDelegationService() { @@ -49,7 +52,7 @@ export function useDelegationService() { Record >({}); - const { availableUTXOs = [] } = useAppState(); + const { availableUTXOs = [], networkInfo } = useAppState(); const { delegations = [], fetchMoreDelegations, @@ -230,12 +233,18 @@ export function useDelegationService() { const openConfirmationModal = useCallback( (action: ActionType, delegation: DelegationV2) => { + const param = getBbnParamByVersion( + delegation.paramsVersion, + networkInfo?.params.bbnStakingParams.versions || [], + ); + setConfirmationModal({ action, delegation, + param, }); }, - [], + [networkInfo], ); const closeConfirmationModal = useCallback( diff --git a/src/components/delegations/DelegationList/components/DelegationModal.tsx b/src/components/delegations/DelegationList/components/DelegationModal.tsx index fc8723a6..4f756ac1 100644 --- a/src/components/delegations/DelegationList/components/DelegationModal.tsx +++ b/src/components/delegations/DelegationList/components/DelegationModal.tsx @@ -7,6 +7,8 @@ import { WithdrawModal } from "@/app/components/Modals/WithdrawModal"; import { DELEGATION_ACTIONS as ACTIONS } from "@/app/constants"; import { ActionType } from "@/app/hooks/services/useDelegationService"; import { DelegationV2 } from "@/app/types/delegationsV2"; +import { BbnStakingParamsVersion } from "@/app/types/networkInfo"; +import { NetworkConfig } from "@/config/network"; interface ConfirmationModalProps { processing: boolean; @@ -14,6 +16,8 @@ interface ConfirmationModalProps { delegation: DelegationV2 | null; onSubmit: (action: ActionType, delegation: DelegationV2) => void; onClose: () => void; + networkConfig: NetworkConfig; + param: BbnStakingParamsVersion | null; } export function DelegationModal({ diff --git a/src/components/delegations/DelegationList/index.tsx b/src/components/delegations/DelegationList/index.tsx index d64d2735..3bd181dd 100644 --- a/src/components/delegations/DelegationList/index.tsx +++ b/src/components/delegations/DelegationList/index.tsx @@ -7,7 +7,7 @@ import { import { type DelegationV2 } from "@/app/types/delegationsV2"; import { GridTable, type TableColumn } from "@/components/common/GridTable"; import { FinalityProviderMoniker } from "@/components/delegations/DelegationList/components/FinalityProviderMoniker"; -import { getNetworkConfigBBN } from "@/config/network/bbn"; +import { getNetworkConfig } from "@/config/network"; import { ActionButton } from "./components/ActionButton"; import { Amount } from "./components/Amount"; @@ -21,7 +21,7 @@ type TableParams = { handleActionClick: (action: ActionType, delegation: DelegationV2) => void; }; -const { networkFullName: bbnNetworkFullName } = getNetworkConfigBBN(); +const networkConfig = getNetworkConfig(); const columns: TableColumn[] = [ { @@ -90,7 +90,7 @@ export function DelegationList() { return (
- {bbnNetworkFullName} Stakes + {networkConfig.bbn.networkFullName} Stakes
); diff --git a/src/config/network/index.ts b/src/config/network/index.ts new file mode 100644 index 00000000..881be1e1 --- /dev/null +++ b/src/config/network/index.ts @@ -0,0 +1,17 @@ +import { BBNConfig, BTCConfig } from "@babylonlabs-io/bbn-wallet-connect"; + +import { getNetworkConfigBBN } from "./bbn"; +import { getNetworkConfigBTC } from "./btc"; + +export interface NetworkConfig { + bbn: BBNConfig; + btc: BTCConfig; +} + +// Get all network configs +export const getNetworkConfig = (): NetworkConfig => { + return { + bbn: getNetworkConfigBBN(), + btc: getNetworkConfigBTC(), + }; +}; From 477640eb98829fcd1cac1e340674579263620a3a Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Tue, 7 Jan 2025 14:09:10 +1100 Subject: [PATCH 2/3] fix: use new withdraw slashing tx method --- package-lock.json | 8 +-- package.json | 2 +- .../hooks/services/useDelegationService.ts | 6 +- .../hooks/services/useTransactionService.ts | 63 +++++++++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index cff715f7..de4994c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.5", "@babylonlabs-io/bbn-core-ui": "0.6.10", "@babylonlabs-io/bbn-wallet-connect": "0.1.31", - "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5", + "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5-test-1", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", "@bitcoinerlab/secp256k1": "^1.1.1", "@cosmjs/proto-signing": "^0.32.4", @@ -2002,9 +2002,9 @@ } }, "node_modules/@babylonlabs-io/btc-staking-ts": { - "version": "0.4.0-canary.5", - "resolved": "https://registry.npmjs.org/@babylonlabs-io/btc-staking-ts/-/btc-staking-ts-0.4.0-canary.5.tgz", - "integrity": "sha512-0lXma1rtYf/mu02ITWPBIrZoEOs2LjZZ/+NmcT3dqvLUQREU/sTIEw+pkxHzh09f+I+DmUxWujNs8PBzX/9XqA==", + "version": "0.4.0-canary.5-test-1", + "resolved": "https://registry.npmjs.org/@babylonlabs-io/btc-staking-ts/-/btc-staking-ts-0.4.0-canary.5-test-1.tgz", + "integrity": "sha512-/scJrez9F0g1yRBcnJyF+bqLH0nY2TI6D2AGYiIeZ6hkwfq99BxEPFp7cUZB/Ow2T+hAnzPC2nr5s7rpde/mFA==", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", diff --git a/package.json b/package.json index b6285472..b270ae87 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.5", "@babylonlabs-io/bbn-core-ui": "0.6.10", "@babylonlabs-io/bbn-wallet-connect": "0.1.31", - "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5", + "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5-test-1", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", "@bitcoinerlab/secp256k1": "^1.1.1", "@cosmjs/proto-signing": "^0.32.4", diff --git a/src/app/hooks/services/useDelegationService.ts b/src/app/hooks/services/useDelegationService.ts index fba1093c..4bb75454 100644 --- a/src/app/hooks/services/useDelegationService.ts +++ b/src/app/hooks/services/useDelegationService.ts @@ -66,6 +66,7 @@ export function useDelegationService() { submitUnbondingTx, submitEarlyUnbondedWithdrawalTx, submitTimelockUnbondedWithdrawalTx, + submitSlashingWithdrawalTx, } = useTransactionService(); const validations = useMemo( @@ -170,7 +171,7 @@ export function useDelegationService() { ); } - await submitEarlyUnbondedWithdrawalTx( + await submitSlashingWithdrawalTx( stakingInput, paramsVersion, slashing.unbondingSlashingTxHex, @@ -210,7 +211,7 @@ export function useDelegationService() { throw new Error("Slashing tx not found, can't submit withdrawal"); } - await submitTimelockUnbondedWithdrawalTx( + await submitSlashingWithdrawalTx( stakingInput, paramsVersion, slashing.stakingSlashingTxHex, @@ -228,6 +229,7 @@ export function useDelegationService() { submitUnbondingTx, submitEarlyUnbondedWithdrawalTx, submitTimelockUnbondedWithdrawalTx, + submitSlashingWithdrawalTx, ], ); diff --git a/src/app/hooks/services/useTransactionService.ts b/src/app/hooks/services/useTransactionService.ts index 97c7cb1c..52692c02 100644 --- a/src/app/hooks/services/useTransactionService.ts +++ b/src/app/hooks/services/useTransactionService.ts @@ -606,6 +606,68 @@ export const useTransactionService = () => { ], ); + /** + * Submit the withdrawal transaction for a slashed staking + * + * @param stakingInput - The staking inputs + * @param paramVersion - The param version of the EOI + * @param slashingTxHex - The slashing transaction hex that to be withdrawn + */ + const submitSlashingWithdrawalTx = useCallback( + async ( + stakingInput: BtcStakingInputs, + paramVersion: number, + slashingTxHex: string, + ) => { + // Perform checks + if (!versionedParams || versionedParams?.length === 0) { + throw new Error("Params not loaded"); + } + if (!btcConnected || !btcNetwork) + throw new Error("BTC Wallet not connected"); + + validateStakingInput(stakingInput); + + const p = getBbnParamByVersion(paramVersion, versionedParams); + if (!p) + throw new Error( + `Unable to find staking params for version ${paramVersion}`, + ); + + const staking = new Staking( + btcNetwork!, + { + address, + publicKeyNoCoordHex: publicKeyNoCoord, + }, + p, + stakingInput.finalityProviderPkNoCoordHex, + stakingInput.stakingTimelock, + ); + + const { psbt } = staking.createWithdrawSlashedStakingTransaction( + Transaction.fromHex(slashingTxHex), + defaultFeeRate, + ); + + const signedWithdrawalPsbtHex = await signPsbt(psbt.toHex()); + const signedWithdrawalTx = Psbt.fromHex( + signedWithdrawalPsbtHex, + ).extractTransaction(); + await pushTx(signedWithdrawalTx.toHex()); + }, + [ + versionedParams, + btcConnected, + btcNetwork, + address, + publicKeyNoCoord, + signPsbt, + pushTx, + defaultFeeRate, + ], + ); + return { createDelegationEoi, estimateStakingFee, @@ -614,6 +676,7 @@ export const useTransactionService = () => { submitUnbondingTx, submitEarlyUnbondedWithdrawalTx, submitTimelockUnbondedWithdrawalTx, + submitSlashingWithdrawalTx, }; }; From 0c12fb6e1ae0e793e562abdc43e215c0ad64ab8d Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Tue, 7 Jan 2025 21:14:00 +1100 Subject: [PATCH 3/3] chore: bump btc-staking-ts version to canary 6 --- package-lock.json | 18 +++++++++--------- package.json | 6 +++--- .../hooks/services/useTransactionService.ts | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index de4994c3..19a6ef54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,8 @@ "dependencies": { "@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.5", "@babylonlabs-io/bbn-core-ui": "0.6.10", - "@babylonlabs-io/bbn-wallet-connect": "0.1.31", - "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5-test-1", + "@babylonlabs-io/bbn-wallet-connect": "0.1.34", + "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.6", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", "@bitcoinerlab/secp256k1": "^1.1.1", "@cosmjs/proto-signing": "^0.32.4", @@ -1980,9 +1980,9 @@ } }, "node_modules/@babylonlabs-io/bbn-wallet-connect": { - "version": "0.1.31", - "resolved": "https://registry.npmjs.org/@babylonlabs-io/bbn-wallet-connect/-/bbn-wallet-connect-0.1.31.tgz", - "integrity": "sha512-uIlKEkjxXs038c/Z4VxCd4YNCQ0AT57dBHeqTan7Q2XhrxDuHBR8oZAhXytqBIxd4tyRV4PbYc8jbyLksaRmAg==", + "version": "0.1.34", + "resolved": "https://registry.npmjs.org/@babylonlabs-io/bbn-wallet-connect/-/bbn-wallet-connect-0.1.34.tgz", + "integrity": "sha512-Wqb8t9fGjb4CNgHnuOtocKKIoY5Qvhn8k5bTvSHs5cLPGDImR5ItUMPYzRBA319oLgz26M1Z2Cnz4rG4FMNG2w==", "dependencies": { "@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3", "@cosmjs/stargate": "^0.32.4", @@ -1995,16 +1995,16 @@ }, "peerDependencies": { "@babylonlabs-io/bbn-core-ui": "^0.6.4", - "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5", + "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.6", "react": "^18.3.1", "react-dom": "^18.3.1", "tailwind-merge": "^2.5.4" } }, "node_modules/@babylonlabs-io/btc-staking-ts": { - "version": "0.4.0-canary.5-test-1", - "resolved": "https://registry.npmjs.org/@babylonlabs-io/btc-staking-ts/-/btc-staking-ts-0.4.0-canary.5-test-1.tgz", - "integrity": "sha512-/scJrez9F0g1yRBcnJyF+bqLH0nY2TI6D2AGYiIeZ6hkwfq99BxEPFp7cUZB/Ow2T+hAnzPC2nr5s7rpde/mFA==", + "version": "0.4.0-canary.6", + "resolved": "https://registry.npmjs.org/@babylonlabs-io/btc-staking-ts/-/btc-staking-ts-0.4.0-canary.6.tgz", + "integrity": "sha512-GPkaf+TAWISM8GAW2fj1OHFeFo/mAm7yw/7BGBa/MAPp9iyr8pRLTVnmZJP937fA0/maRvJHrHXQgrtsbvxZng==", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", diff --git a/package.json b/package.json index b270ae87..bc09a4bd 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "dependencies": { "@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.5", "@babylonlabs-io/bbn-core-ui": "0.6.10", - "@babylonlabs-io/bbn-wallet-connect": "0.1.31", - "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.5-test-1", + "@babylonlabs-io/bbn-wallet-connect": "0.1.34", + "@babylonlabs-io/btc-staking-ts": "0.4.0-canary.6", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", "@bitcoinerlab/secp256k1": "^1.1.1", "@cosmjs/proto-signing": "^0.32.4", @@ -104,4 +104,4 @@ "tr46": "4.0.0" } } -} +} \ No newline at end of file diff --git a/src/app/hooks/services/useTransactionService.ts b/src/app/hooks/services/useTransactionService.ts index 52692c02..0dc6deb8 100644 --- a/src/app/hooks/services/useTransactionService.ts +++ b/src/app/hooks/services/useTransactionService.ts @@ -645,7 +645,7 @@ export const useTransactionService = () => { stakingInput.stakingTimelock, ); - const { psbt } = staking.createWithdrawSlashedStakingTransaction( + const { psbt } = staking.createWithdrawSlashingTransaction( Transaction.fromHex(slashingTxHex), defaultFeeRate, );