diff --git a/src/app/api/getDelegationsV2.ts b/src/app/api/getDelegationsV2.ts index a5a67194..313074e5 100644 --- a/src/app/api/getDelegationsV2.ts +++ b/src/app/api/getDelegationsV2.ts @@ -32,7 +32,10 @@ interface DelegationV2API { end_height: number; bbn_inception_height: number; bbn_inception_time: string; - slashing_tx_hex: string; + slashing: { + slashing_tx_hex: string; + spending_height: number; + }; }; delegation_unbonding: { unbonding_timelock: number; @@ -41,7 +44,10 @@ interface DelegationV2API { covenant_btc_pk_hex: string; signature_hex: string; }[]; - slashing_tx_hex: string; + slashing: { + unbonding_slashing_tx_hex: string; + spending_height: number; + }; }; state: string; } @@ -56,53 +62,14 @@ export const getDelegationV2 = async ( staking_tx_hash_hex: stakingTxHashHex, }; - const response = await apiWrapper( + const { data: delegationAPIResponse } = await apiWrapper( "GET", "/v2/delegation", "Error getting delegation v2", { query: params }, ); - const delegationAPIResponse: DelegationV2APIResponse = response.data; - - const state = getDelegationV2StakingState(delegationAPIResponse.data.state); - - return { - finalityProviderBtcPksHex: - delegationAPIResponse.data.finality_provider_btc_pks_hex, - stakingTxHex: delegationAPIResponse.data.delegation_staking.staking_tx_hex, - paramsVersion: delegationAPIResponse.data.params_version, - stakerBtcPkHex: delegationAPIResponse.data.staker_btc_pk_hex, - stakingAmount: delegationAPIResponse.data.delegation_staking.staking_amount, - stakingTimelock: - delegationAPIResponse.data.delegation_staking.staking_timelock, - stakingTxHashHex: - delegationAPIResponse.data.delegation_staking.staking_tx_hash_hex, - startHeight: delegationAPIResponse.data.delegation_staking.start_height, - endHeight: delegationAPIResponse.data.delegation_staking.end_height, - bbnInceptionHeight: - delegationAPIResponse.data.delegation_staking.bbn_inception_height, - bbnInceptionTime: - delegationAPIResponse.data.delegation_staking.bbn_inception_time, - stakingSlashingTxHex: - delegationAPIResponse.data.delegation_staking.slashing_tx_hex, - state, - unbondingTimelock: - delegationAPIResponse.data.delegation_unbonding.unbonding_timelock, - unbondingTxHex: - delegationAPIResponse.data.delegation_unbonding.unbonding_tx, - slashingTxHex: - delegationAPIResponse.data.delegation_staking.slashing_tx_hex, - covenantUnbondingSignatures: - delegationAPIResponse.data.delegation_unbonding.covenant_unbonding_signatures?.map( - (signature) => ({ - covenantBtcPkHex: signature.covenant_btc_pk_hex, - signatureHex: signature.signature_hex, - }), - ), - unbondingSlashingTxHex: - delegationAPIResponse.data.delegation_unbonding.slashing_tx_hex, - }; + return apiToDelegationV2(delegationAPIResponse); }; export const getDelegationsV2 = async ( @@ -117,52 +84,54 @@ export const getDelegationsV2 = async ( pagination_key: pageKey ? pageKey : "", }; - const response = await apiWrapper( + const { data: delegationsAPIResponse } = await apiWrapper( "GET", "/v2/delegations", "Error getting delegations v2", { query: params }, ); - const delegationsAPIResponse: DelegationsV2APIResponse = response.data; - - const delegations: DelegationV2[] = delegationsAPIResponse.data.map( - (apiDelegation: DelegationV2API): DelegationV2 => { - const state = getDelegationV2StakingState(apiDelegation.state); - return { - finalityProviderBtcPksHex: apiDelegation.finality_provider_btc_pks_hex, - stakingTxHex: apiDelegation.delegation_staking.staking_tx_hex, - paramsVersion: apiDelegation.params_version, - stakerBtcPkHex: apiDelegation.staker_btc_pk_hex, - stakingAmount: apiDelegation.delegation_staking.staking_amount, - stakingTimelock: apiDelegation.delegation_staking.staking_timelock, - stakingTxHashHex: apiDelegation.delegation_staking.staking_tx_hash_hex, - startHeight: apiDelegation.delegation_staking.start_height, - endHeight: apiDelegation.delegation_staking.end_height, - bbnInceptionHeight: - apiDelegation.delegation_staking.bbn_inception_height, - bbnInceptionTime: apiDelegation.delegation_staking.bbn_inception_time, - stakingSlashingTxHex: apiDelegation.delegation_staking.slashing_tx_hex, - state, - unbondingTimelock: - apiDelegation.delegation_unbonding.unbonding_timelock, - unbondingTxHex: apiDelegation.delegation_unbonding.unbonding_tx, - unbondingSlashingTxHex: - apiDelegation.delegation_unbonding.slashing_tx_hex, - covenantUnbondingSignatures: - apiDelegation.delegation_unbonding.covenant_unbonding_signatures?.map( - (signature) => ({ - covenantBtcPkHex: signature.covenant_btc_pk_hex, - signatureHex: signature.signature_hex, - }), - ), - slashingTxHex: apiDelegation.delegation_staking.slashing_tx_hex, - }; - }, - ); - const pagination: Pagination = { next_key: delegationsAPIResponse.pagination.next_key, }; - return { delegations: delegations, pagination }; + return { + delegations: delegationsAPIResponse.data.map(apiToDelegationV2), + pagination, + }; +}; + +const apiToDelegationV2 = (apiDelegation: DelegationV2API): DelegationV2 => { + const state = getDelegationV2StakingState(apiDelegation.state); + + return { + finalityProviderBtcPksHex: apiDelegation.finality_provider_btc_pks_hex, + stakingTxHex: apiDelegation.delegation_staking.staking_tx_hex, + paramsVersion: apiDelegation.params_version, + stakerBtcPkHex: apiDelegation.staker_btc_pk_hex, + stakingAmount: apiDelegation.delegation_staking.staking_amount, + stakingTimelock: apiDelegation.delegation_staking.staking_timelock, + stakingTxHashHex: apiDelegation.delegation_staking.staking_tx_hash_hex, + startHeight: apiDelegation.delegation_staking.start_height, + endHeight: apiDelegation.delegation_staking.end_height, + bbnInceptionHeight: apiDelegation.delegation_staking.bbn_inception_height, + bbnInceptionTime: apiDelegation.delegation_staking.bbn_inception_time, + state, + unbondingTimelock: apiDelegation.delegation_unbonding.unbonding_timelock, + unbondingTxHex: apiDelegation.delegation_unbonding.unbonding_tx, + slashing: { + stakingSlashingTxHex: + apiDelegation.delegation_staking.slashing.slashing_tx_hex, + unbondingSlashingTxHex: + apiDelegation.delegation_unbonding.slashing.unbonding_slashing_tx_hex, + spendingHeight: + apiDelegation.delegation_unbonding.slashing.spending_height, + }, + covenantUnbondingSignatures: + apiDelegation.delegation_unbonding.covenant_unbonding_signatures?.map( + (signature) => ({ + covenantBtcPkHex: signature.covenant_btc_pk_hex, + signatureHex: signature.signature_hex, + }), + ), + }; }; diff --git a/src/app/hooks/services/useDelegationService.ts b/src/app/hooks/services/useDelegationService.ts index fce8350e..2c5939cc 100644 --- a/src/app/hooks/services/useDelegationService.ts +++ b/src/app/hooks/services/useDelegationService.ts @@ -28,8 +28,11 @@ interface TxProps { stakingAmountSat: number; stakingTimelock: number; }; - slashingTxHex: string; - unbondingSlashingTxHex: string; + slashing: { + stakingSlashingTxHex: string; + unbondingSlashingTxHex: string; + spendingHeight: number; + }; } type DelegationCommand = (props: TxProps) => Promise; @@ -157,9 +160,9 @@ export function useDelegationService() { stakingTxHashHex, stakingInput, paramsVersion, - unbondingSlashingTxHex, + slashing, }) => { - if (!unbondingSlashingTxHex) { + if (!slashing.unbondingSlashingTxHex) { throw new Error( "Unbonding slashing tx not found, can't submit withdrawal", ); @@ -168,7 +171,7 @@ export function useDelegationService() { await submitEarlyUnbondedWithdrawalTx( stakingInput, paramsVersion, - unbondingSlashingTxHex, + slashing.unbondingSlashingTxHex, ); updateDelegationStatus( @@ -199,16 +202,16 @@ export function useDelegationService() { stakingInput, paramsVersion, stakingTxHashHex, - slashingTxHex, + slashing, }) => { - if (!slashingTxHex) { + if (!slashing.stakingSlashingTxHex) { throw new Error("Slashing tx not found, can't submit withdrawal"); } await submitTimelockUnbondedWithdrawalTx( stakingInput, paramsVersion, - slashingTxHex, + slashing.stakingSlashingTxHex, ); updateDelegationStatus( @@ -260,8 +263,7 @@ export function useDelegationService() { unbondingTxHex, covenantUnbondingSignatures, state, - slashingTxHex, - unbondingSlashingTxHex, + slashing, } = delegation; const finalityProviderPk = finalityProviderBtcPksHex[0]; @@ -284,8 +286,7 @@ export function useDelegationService() { covenantUnbondingSignatures, state, stakingInput, - slashingTxHex, - unbondingSlashingTxHex, + slashing, }); closeConfirmationModal(); diff --git a/src/app/hooks/storage/useDelegationStorage.ts b/src/app/hooks/storage/useDelegationStorage.ts index 1067bee3..4cb058ab 100644 --- a/src/app/hooks/storage/useDelegationStorage.ts +++ b/src/app/hooks/storage/useDelegationStorage.ts @@ -45,8 +45,11 @@ export function useDelegationStorage( stakingSlashingTxHex: "", bbnInceptionHeight: 0, bbnInceptionTime: new Date().toISOString(), - slashingTxHex: "", - unbondingSlashingTxHex: "", + slashing: { + stakingSlashingTxHex: "", + unbondingSlashingTxHex: "", + spendingHeight: 0, + }, }) as DelegationV2, ); diff --git a/src/app/types/delegationsV2.ts b/src/app/types/delegationsV2.ts index 62fad34b..c0977803 100644 --- a/src/app/types/delegationsV2.ts +++ b/src/app/types/delegationsV2.ts @@ -7,7 +7,6 @@ export interface DelegationLike { export interface DelegationV2 extends DelegationLike { stakingTxHex: string; - stakingSlashingTxHex: string; paramsVersion: number; finalityProviderBtcPksHex: string[]; stakerBtcPkHex: string; @@ -22,8 +21,11 @@ export interface DelegationV2 extends DelegationLike { covenantBtcPkHex: string; signatureHex: string; }[]; - slashingTxHex: string; - unbondingSlashingTxHex: string; + slashing: { + stakingSlashingTxHex: string; + unbondingSlashingTxHex: string; + spendingHeight: number; + }; } export enum DelegationV2StakingState {