|
| 1 | +import { getNetworkConfigBBN } from "@/ui/common/config/network/bbn"; |
| 2 | +import { Bsn } from "@/ui/common/types/bsn"; |
| 3 | +import { |
| 4 | + FinalityProviderState as FinalityProviderStateEnum, |
| 5 | + type FinalityProvider, |
| 6 | +} from "@/ui/common/types/finalityProviders"; |
| 7 | + |
| 8 | +const { chainId: BBN_CHAIN_ID } = getNetworkConfigBBN(); |
| 9 | + |
| 10 | +/** |
| 11 | + * Normalizes hex string by removing 0x prefix and converting to lowercase |
| 12 | + */ |
| 13 | +export const normalizeHex = (hex?: string): string => |
| 14 | + (hex ?? "").trim().toLowerCase().replace(/^0x/, ""); |
| 15 | + |
| 16 | +// BSN-specific status filters to handle different BSN behaviors |
| 17 | +const BABYLON_STATUS_FILTERS = { |
| 18 | + active: (fp: FinalityProvider) => |
| 19 | + fp.state === FinalityProviderStateEnum.ACTIVE, |
| 20 | + inactive: (fp: FinalityProvider) => |
| 21 | + fp.state === FinalityProviderStateEnum.INACTIVE, |
| 22 | + jailed: (fp: FinalityProvider) => |
| 23 | + fp.state === FinalityProviderStateEnum.JAILED, |
| 24 | + slashed: (fp: FinalityProvider) => |
| 25 | + fp.state === FinalityProviderStateEnum.SLASHED, |
| 26 | +}; |
| 27 | + |
| 28 | +const COSMOS_STATUS_FILTERS = { |
| 29 | + registered: (fp: FinalityProvider) => |
| 30 | + fp.state === FinalityProviderStateEnum.INACTIVE, |
| 31 | + slashed: (fp: FinalityProvider) => |
| 32 | + fp.state === FinalityProviderStateEnum.SLASHED, |
| 33 | +}; |
| 34 | + |
| 35 | +const BSN_STATUS_FILTERS = { |
| 36 | + [BBN_CHAIN_ID]: BABYLON_STATUS_FILTERS, |
| 37 | + COSMOS: COSMOS_STATUS_FILTERS, |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Creates dynamic status filters for ROLLUP BSNs that combine allowlist status with FP state |
| 42 | + */ |
| 43 | +export const createRollupStatusFilters = (selectedBsn: Bsn | undefined) => { |
| 44 | + const allowSet = new Set((selectedBsn?.allowlist || []).map(normalizeHex)); |
| 45 | + |
| 46 | + return { |
| 47 | + allowlisted: (fp: FinalityProvider) => { |
| 48 | + const isAllowlisted = allowSet.has(normalizeHex(fp.btcPk)); |
| 49 | + return isAllowlisted && fp.state !== FinalityProviderStateEnum.SLASHED; |
| 50 | + }, |
| 51 | + "not-allowlisted": (fp: FinalityProvider) => { |
| 52 | + const isAllowlisted = allowSet.has(normalizeHex(fp.btcPk)); |
| 53 | + return !isAllowlisted && fp.state !== FinalityProviderStateEnum.SLASHED; |
| 54 | + }, |
| 55 | + slashed: (fp: FinalityProvider) => |
| 56 | + fp.state === FinalityProviderStateEnum.SLASHED, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Creates allowlist filters for BSN-based filtering |
| 62 | + */ |
| 63 | +export const createAllowlistFilters = (selectedBsn: Bsn | undefined) => { |
| 64 | + const allowSet = new Set((selectedBsn?.allowlist || []).map(normalizeHex)); |
| 65 | + |
| 66 | + return { |
| 67 | + allowlisted: (fp: FinalityProvider) => allowSet.has(normalizeHex(fp.btcPk)), |
| 68 | + "not-allowlisted": (fp: FinalityProvider) => |
| 69 | + !allowSet.has(normalizeHex(fp.btcPk)), |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Applies ROLLUP-specific status filtering that combines allowlist status with FP state |
| 75 | + */ |
| 76 | +export const applyRollupStatusFilter = ( |
| 77 | + providers: FinalityProvider[], |
| 78 | + filterValue: string, |
| 79 | + selectedBsn: Bsn | undefined, |
| 80 | +): FinalityProvider[] => { |
| 81 | + const rollupStatusFilters = createRollupStatusFilters(selectedBsn); |
| 82 | + const statusFilter = |
| 83 | + rollupStatusFilters[filterValue as keyof typeof rollupStatusFilters]; |
| 84 | + return statusFilter ? providers.filter(statusFilter) : providers; |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Applies standard status filtering for BABYLON and COSMOS BSNs |
| 89 | + */ |
| 90 | +export const applyStandardStatusFilter = ( |
| 91 | + providers: FinalityProvider[], |
| 92 | + filterValue: string, |
| 93 | + bsnKey: string, |
| 94 | +): FinalityProvider[] => { |
| 95 | + const bsnStatusFilters = |
| 96 | + BSN_STATUS_FILTERS[bsnKey] || BSN_STATUS_FILTERS[BBN_CHAIN_ID]; |
| 97 | + const statusFilter = |
| 98 | + bsnStatusFilters[filterValue as keyof typeof bsnStatusFilters]; |
| 99 | + return statusFilter ? providers.filter(statusFilter) : providers; |
| 100 | +}; |
| 101 | + |
| 102 | +export interface FinalityProviderFilterState { |
| 103 | + searchTerm: string; |
| 104 | + providerStatus: |
| 105 | + | "active" |
| 106 | + | "inactive" |
| 107 | + | "registered" |
| 108 | + | "allowlisted" |
| 109 | + | "not-allowlisted" |
| 110 | + | "slashed" |
| 111 | + | ""; |
| 112 | + allowlistStatus: "allowlisted" | "not-allowlisted" | ""; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Main filtering function that applies BSN-aware filtering logic |
| 117 | + * Handles both allowlist-based and status-based filtering behaviors |
| 118 | + */ |
| 119 | +export const filterFinalityProvidersByBsn = ( |
| 120 | + providers: FinalityProvider[], |
| 121 | + filter: FinalityProviderFilterState, |
| 122 | + selectedBsn: Bsn | undefined, |
| 123 | + fpFilterBehavior: "status-based" | "allowlist-based", |
| 124 | +): FinalityProvider[] => { |
| 125 | + let filtered = providers; |
| 126 | + |
| 127 | + // Apply BSN-aware filtering based on provider status |
| 128 | + if (filter.providerStatus) { |
| 129 | + if (fpFilterBehavior === "allowlist-based") { |
| 130 | + // For rollup BSNs: filter all FPs by allowlist (regardless of active/inactive state) |
| 131 | + const allowSet = new Set( |
| 132 | + (selectedBsn?.allowlist || []).map(normalizeHex), |
| 133 | + ); |
| 134 | + |
| 135 | + filtered = filtered.filter((fp) => { |
| 136 | + const isAllowlisted = allowSet.has(normalizeHex(fp.btcPk)); |
| 137 | + |
| 138 | + if (filter.providerStatus === "active") { |
| 139 | + return isAllowlisted; |
| 140 | + } else if (filter.providerStatus === "inactive") { |
| 141 | + return !isAllowlisted; |
| 142 | + } else { |
| 143 | + // Handle slashed state for rollups |
| 144 | + return fp.state === FinalityProviderStateEnum.SLASHED; |
| 145 | + } |
| 146 | + }); |
| 147 | + } else if (fpFilterBehavior === "status-based") { |
| 148 | + // For status-based BSNs: filter by finality provider state using BSN-specific filters |
| 149 | + const bsnKey = |
| 150 | + selectedBsn?.id === BBN_CHAIN_ID |
| 151 | + ? BBN_CHAIN_ID |
| 152 | + : selectedBsn?.type || BBN_CHAIN_ID; |
| 153 | + |
| 154 | + if (bsnKey === "ROLLUP") { |
| 155 | + filtered = applyRollupStatusFilter( |
| 156 | + filtered, |
| 157 | + filter.providerStatus, |
| 158 | + selectedBsn, |
| 159 | + ); |
| 160 | + } else { |
| 161 | + filtered = applyStandardStatusFilter( |
| 162 | + filtered, |
| 163 | + filter.providerStatus, |
| 164 | + bsnKey, |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (filter.allowlistStatus) { |
| 171 | + const allowlistFilters = createAllowlistFilters(selectedBsn); |
| 172 | + const allowlistFilter = |
| 173 | + allowlistFilters[filter.allowlistStatus as keyof typeof allowlistFilters]; |
| 174 | + if (allowlistFilter) { |
| 175 | + filtered = filtered.filter(allowlistFilter); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return filtered; |
| 180 | +}; |
| 181 | + |
| 182 | +/** |
| 183 | + * Determines if a finality provider row should be selectable based on BSN rules |
| 184 | + */ |
| 185 | +export const isFinalityProviderRowSelectable = ( |
| 186 | + row: FinalityProvider, |
| 187 | + selectedBsnId: string | undefined, |
| 188 | + selectedBsn: Bsn | undefined, |
| 189 | +): boolean => { |
| 190 | + const statusAllowed = |
| 191 | + row.state === FinalityProviderStateEnum.ACTIVE || |
| 192 | + row.state === FinalityProviderStateEnum.INACTIVE; |
| 193 | + |
| 194 | + // For selection (not filtering), only restrict based on allowlist for non-Babylon BSNs |
| 195 | + const allowlistAllowed = |
| 196 | + !selectedBsnId || |
| 197 | + selectedBsnId === BBN_CHAIN_ID || |
| 198 | + (selectedBsn?.allowlist || []).some( |
| 199 | + (allowedPk) => normalizeHex(allowedPk) === normalizeHex(row.btcPk), |
| 200 | + ); |
| 201 | + |
| 202 | + return statusAllowed && allowlistAllowed; |
| 203 | +}; |
0 commit comments