From 8cdce55b4ea11a5c1320075a638e478f5f40e45a Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Thu, 5 Dec 2024 19:27:02 +1100 Subject: [PATCH 1/2] fix: update params breaking changes --- src/app/api/getNetworkInfo.ts | 27 +++++++++++++++++--- src/app/hooks/useVersionByHeight.ts | 30 +++++++++++++++++------ src/app/types/networkInfo.ts | 2 ++ src/utils/delegations/signWithdrawalTx.ts | 2 +- src/utils/{ => params}/globalParams.ts | 0 tests/utils/globalParams.test.ts | 2 +- 6 files changed, 51 insertions(+), 12 deletions(-) rename src/utils/{ => params}/globalParams.ts (100%) diff --git a/src/app/api/getNetworkInfo.ts b/src/app/api/getNetworkInfo.ts index 3f8df94d..b5272e06 100644 --- a/src/app/api/getNetworkInfo.ts +++ b/src/app/api/getNetworkInfo.ts @@ -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 => { @@ -64,8 +66,7 @@ export const getNetworkInfo = async (): Promise => { 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, @@ -77,8 +78,28 @@ export const getNetworkInfo = async (): Promise => { }, 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, ); diff --git a/src/app/hooks/useVersionByHeight.ts b/src/app/hooks/useVersionByHeight.ts index fe54933d..24677a3d 100644 --- a/src/app/hooks/useVersionByHeight.ts +++ b/src/app/hooks/useVersionByHeight.ts @@ -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); +}; diff --git a/src/app/types/networkInfo.ts b/src/app/types/networkInfo.ts index 56d73f6b..8b618585 100644 --- a/src/app/types/networkInfo.ts +++ b/src/app/types/networkInfo.ts @@ -5,6 +5,8 @@ export interface BbnStakingParamsVersion extends StakingParams { minCommissionRate: string; maxActiveFinalityProviders: number; delegationCreationBaseGasFee: number; + btcActivationHeight: number; + allowListExpirationHeight: number; } export interface BtcEpochCheckParamsVersion { diff --git a/src/utils/delegations/signWithdrawalTx.ts b/src/utils/delegations/signWithdrawalTx.ts index de08e11d..9789c211 100644 --- a/src/utils/delegations/signWithdrawalTx.ts +++ b/src/utils/delegations/signWithdrawalTx.ts @@ -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"; diff --git a/src/utils/globalParams.ts b/src/utils/params/globalParams.ts similarity index 100% rename from src/utils/globalParams.ts rename to src/utils/params/globalParams.ts diff --git a/tests/utils/globalParams.test.ts b/tests/utils/globalParams.test.ts index 2fd07f4b..d7316a4b 100644 --- a/tests/utils/globalParams.test.ts +++ b/tests/utils/globalParams.test.ts @@ -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"; From db3e39fb581d0d06532c6b522a92d48d6570751d Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Thu, 5 Dec 2024 19:48:12 +1100 Subject: [PATCH 2/2] use toSorted --- src/app/api/getNetworkInfo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/api/getNetworkInfo.ts b/src/app/api/getNetworkInfo.ts index b5272e06..e5b0f701 100644 --- a/src/app/api/getNetworkInfo.ts +++ b/src/app/api/getNetworkInfo.ts @@ -83,10 +83,10 @@ export const getNetworkInfo = async (): Promise => { })); // Verify that version numbers and BTC activation heights are consistently ordered - const sortedByVersion = [...stakingVersions].sort( + const sortedByVersion = stakingVersions.toSorted( (a, b) => b.version - a.version, ); - const sortedByHeight = [...stakingVersions].sort( + const sortedByHeight = stakingVersions.toSorted( (a, b) => b.btcActivationHeight - a.btcActivationHeight, );