Skip to content

Commit

Permalink
fix: update params breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Dec 5, 2024
1 parent 8f0c314 commit 8cdce55
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
27 changes: 24 additions & 3 deletions src/app/api/getNetworkInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export interface BbnParams {
slashing_pk_script: string;
min_slashing_tx_fee_sat: number;
slashing_rate: string;
min_unbonding_time_blocks: number;
unbonding_time_blocks: number;
unbonding_fee_sat: number;
min_commission_rate: string;
max_active_finality_providers: number;
delegation_creation_base_gas_fee: number;
btc_activation_height: number;
allow_list_expiration_height: number;
}

export const getNetworkInfo = async (): Promise<NetworkInfo> => {
Expand All @@ -64,8 +66,7 @@ export const getNetworkInfo = async (): Promise<NetworkInfo> => {
maxStakingValueSat: v.max_staking_value_sat,
minStakingTimeBlocks: v.min_staking_time_blocks,
maxStakingTimeBlocks: v.max_staking_time_blocks,
// TODO: To be reverted after https://github.com/babylonlabs-io/babylon/issues/263
unbondingTime: v.min_unbonding_time_blocks + 1,
unbondingTime: v.unbonding_time_blocks,
unbondingFeeSat: v.unbonding_fee_sat,
minCommissionRate: v.min_commission_rate,
maxActiveFinalityProviders: v.max_active_finality_providers,
Expand All @@ -77,8 +78,28 @@ export const getNetworkInfo = async (): Promise<NetworkInfo> => {
},
maxStakingAmountSat: v.max_staking_value_sat,
minStakingAmountSat: v.min_staking_value_sat,
btcActivationHeight: v.btc_activation_height,
allowListExpirationHeight: v.allow_list_expiration_height,
}));

// Verify that version numbers and BTC activation heights are consistently ordered
const sortedByVersion = [...stakingVersions].sort(
(a, b) => b.version - a.version,
);
const sortedByHeight = [...stakingVersions].sort(
(a, b) => b.btcActivationHeight - a.btcActivationHeight,
);

// Verify both sorts produce same order
const areEqual = sortedByVersion.every(
(param, index) => param === sortedByHeight[index],
);
if (!areEqual) {
throw new Error(
"Version numbers and BTC activation heights are not consistently ordered",
);
}

const latestStakingParam = stakingVersions.reduce((prev, current) =>
current.version > prev.version ? current : prev,
);
Expand Down
30 changes: 23 additions & 7 deletions src/app/hooks/useVersionByHeight.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { useMemo } from "react";

import { getCurrentGlobalParamsVersion } from "@/utils/globalParams";
import { BbnStakingParamsVersion } from "../types/networkInfo";

import { useVersions } from "./client/api/useVersions";

export function useVersionByHeight(height: number) {
const { data: versions } = useVersions();
import { useNetworkInfo } from "./client/api/useNetworkInfo";

export function useParamByHeight(height: number) {
const { data: networkInfo } = useNetworkInfo();
return useMemo(
() => getCurrentGlobalParamsVersion(height, versions ?? []),
[versions, height],
() =>
getBbnParamByBtcHeight(
height,
networkInfo?.params.bbnStakingParams.versions ?? [],
),
[networkInfo, height],
);
}

const getBbnParamByBtcHeight = (
height: number,
bbnParams: BbnStakingParamsVersion[],
) => {
// Sort by btcActivationHeight in ascending order
const sortedParams = [...bbnParams].sort(
(a, b) => a.btcActivationHeight - b.btcActivationHeight,
);

// Find first param where height is >= btcActivationHeight
return sortedParams.find((param) => height >= param.btcActivationHeight);
};
2 changes: 2 additions & 0 deletions src/app/types/networkInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface BbnStakingParamsVersion extends StakingParams {
minCommissionRate: string;
maxActiveFinalityProviders: number;
delegationCreationBaseGasFee: number;
btcActivationHeight: number;
allowListExpirationHeight: number;
}

export interface BtcEpochCheckParamsVersion {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/delegations/signWithdrawalTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Psbt, Transaction, networks } from "bitcoinjs-lib";
import { getGlobalParams } from "@/app/api/getGlobalParams";
import { Delegation as DelegationInterface } from "@/app/types/delegations";
import { apiDataToStakingScripts } from "@/utils/apiDataToStakingScripts";
import { getCurrentGlobalParamsVersion } from "@/utils/globalParams";
import { getCurrentGlobalParamsVersion } from "@/utils/params/globalParams";

import { getFeeRateFromMempool } from "../getFeeRateFromMempool";
import { Fees } from "../wallet/btc_wallet_provider";
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/utils/globalParams.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GlobalParamsVersion } from "@/app/types/globalParams";
import { getCurrentGlobalParamsVersion } from "@/utils/globalParams";
import { getCurrentGlobalParamsVersion } from "@/utils/params/globalParams";

import { testingNetworks } from "../helper";

Expand Down

0 comments on commit 8cdce55

Please sign in to comment.