Skip to content

Commit

Permalink
fix: update slashing api response (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Dec 20, 2024
1 parent 30c7c1e commit 3bbf413
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 99 deletions.
133 changes: 51 additions & 82 deletions src/app/api/getDelegationsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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 (
Expand All @@ -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,
}),
),
};
};
25 changes: 13 additions & 12 deletions src/app/hooks/services/useDelegationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Expand Down Expand Up @@ -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",
);
Expand All @@ -168,7 +171,7 @@ export function useDelegationService() {
await submitEarlyUnbondedWithdrawalTx(
stakingInput,
paramsVersion,
unbondingSlashingTxHex,
slashing.unbondingSlashingTxHex,
);

updateDelegationStatus(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -260,8 +263,7 @@ export function useDelegationService() {
unbondingTxHex,
covenantUnbondingSignatures,
state,
slashingTxHex,
unbondingSlashingTxHex,
slashing,
} = delegation;

const finalityProviderPk = finalityProviderBtcPksHex[0];
Expand All @@ -284,8 +286,7 @@ export function useDelegationService() {
covenantUnbondingSignatures,
state,
stakingInput,
slashingTxHex,
unbondingSlashingTxHex,
slashing,
});

closeConfirmationModal();
Expand Down
7 changes: 5 additions & 2 deletions src/app/hooks/storage/useDelegationStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export function useDelegationStorage(
stakingSlashingTxHex: "",
bbnInceptionHeight: 0,
bbnInceptionTime: new Date().toISOString(),
slashingTxHex: "",
unbondingSlashingTxHex: "",
slashing: {
stakingSlashingTxHex: "",
unbondingSlashingTxHex: "",
spendingHeight: 0,
},
}) as DelegationV2,
);

Expand Down
8 changes: 5 additions & 3 deletions src/app/types/delegationsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface DelegationLike {

export interface DelegationV2 extends DelegationLike {
stakingTxHex: string;
stakingSlashingTxHex: string;
paramsVersion: number;
finalityProviderBtcPksHex: string[];
stakerBtcPkHex: string;
Expand All @@ -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 {
Expand Down

0 comments on commit 3bbf413

Please sign in to comment.