|
| 1 | +/** |
| 2 | + * Field mappings for CEL filter expressions |
| 3 | + * Maps human-readable field names to short field names used in the API |
| 4 | + * Organized by subscription method to handle field name conflicts |
| 5 | + */ |
| 6 | + |
| 7 | +export interface FieldMapping { |
| 8 | + readonly [key: string]: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface MethodFieldMappings { |
| 12 | + readonly [methodName: string]: FieldMapping; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Field mappings organized by subscription method |
| 17 | + * Each method can have its own field mappings to handle conflicts |
| 18 | + */ |
| 19 | +export const CEL_FIELD_MAPPINGS: MethodFieldMappings = { |
| 20 | + // Wallet balance subscription fields |
| 21 | + "subscribeWalletBalance": { |
| 22 | + "walletAddress": "a", |
| 23 | + "tokenAddress": "ta", |
| 24 | + "tokenPriceInUsd": "tpiu", |
| 25 | + "balance": "b", |
| 26 | + "timestamp": "t", |
| 27 | + }, |
| 28 | + |
| 29 | + // Token candles subscription fields |
| 30 | + "subscribeTokenCandles": { |
| 31 | + "open": "o", |
| 32 | + "close": "c", |
| 33 | + "high": "h", |
| 34 | + "low": "l", |
| 35 | + "volume": "v", |
| 36 | + "resolution": "r", |
| 37 | + "time": "t", |
| 38 | + "number": "n", |
| 39 | + }, |
| 40 | + |
| 41 | + // Token stats subscription fields |
| 42 | + "subscribeTokenStats": { |
| 43 | + "address": "a", |
| 44 | + "timestamp": "t", |
| 45 | + "buys1m": "b1m", |
| 46 | + "sells1m": "s1m", |
| 47 | + "buyers1m": "be1m", |
| 48 | + "sellers1m": "se1m", |
| 49 | + "buyVolumeInUsd1m": "bviu1m", |
| 50 | + "sellVolumeInUsd1m": "sviu1m", |
| 51 | + "price1m": "p1m", |
| 52 | + "openInUsd1m": "oiu1m", |
| 53 | + "closeInUsd1m": "ciu1m", |
| 54 | + "buys5m": "b5m", |
| 55 | + "sells5m": "s5m", |
| 56 | + "buyers5m": "be5m", |
| 57 | + "sellers5m": "se5m", |
| 58 | + "buyVolumeInUsd5m": "bviu5m", |
| 59 | + "sellVolumeInUsd5m": "sviu5m", |
| 60 | + "price5m": "p5m", |
| 61 | + "openInUsd5m": "oiu5m", |
| 62 | + "closeInUsd5m": "ciu5m", |
| 63 | + "buys15m": "b15m", |
| 64 | + "sells15m": "s15m", |
| 65 | + "buyers15m": "be15m", |
| 66 | + "sellers15m": "se15m", |
| 67 | + "buyVolumeInUsd15m": "bviu15m", |
| 68 | + "sellVolumeInUsd15m": "sviu15m", |
| 69 | + "price15m": "p15m", |
| 70 | + "openInUsd15m": "oiu15m", |
| 71 | + "closeInUsd15m": "ciu15m", |
| 72 | + "buys30m": "b30m", |
| 73 | + "sells30m": "s30m", |
| 74 | + "buyers30m": "be30m", |
| 75 | + "sellers30m": "se30m", |
| 76 | + "buyVolumeInUsd30m": "bviu30m", |
| 77 | + "sellVolumeInUsd30m": "sviu30m", |
| 78 | + "price30m": "p30m", |
| 79 | + "openInUsd30m": "oiu30m", |
| 80 | + "closeInUsd30m": "ciu30m", |
| 81 | + "buys1h": "b1h", |
| 82 | + "sells1h": "s1h", |
| 83 | + "buyers1h": "be1h", |
| 84 | + "sellers1h": "se1h", |
| 85 | + "buyVolumeInUsd1h": "bviu1h", |
| 86 | + "sellVolumeInUsd1h": "sviu1h", |
| 87 | + "price1h": "p1h", |
| 88 | + "openInUsd1h": "oiu1h", |
| 89 | + "closeInUsd1h": "ciu1h", |
| 90 | + "buys4h": "b4h", |
| 91 | + "sells4h": "s4h", |
| 92 | + "buyers4h": "be4h", |
| 93 | + "sellers4h": "se4h", |
| 94 | + "buyVolumeInUsd4h": "bviu4h", |
| 95 | + "sellVolumeInUsd4h": "sviu4h", |
| 96 | + "price4h": "p4h", |
| 97 | + "openInUsd4h": "oiu4h", |
| 98 | + "closeInUsd4h": "ciu4h", |
| 99 | + "buys24h": "b24h", |
| 100 | + "sells24h": "s24h", |
| 101 | + "buyers24h": "be24h", |
| 102 | + "sellers24h": "se24h", |
| 103 | + "buyVolumeInUsd24h": "bviu24h", |
| 104 | + "sellVolumeInUsd24h": "sviu24h", |
| 105 | + "price24h": "p24h", |
| 106 | + "price": "p", |
| 107 | + "openInUsd24h": "oiu24h", |
| 108 | + "closeInUsd24h": "ciu24h", |
| 109 | + }, |
| 110 | + |
| 111 | + // Token holder subscription fields |
| 112 | + "subscribeTokenHolders": { |
| 113 | + "tokenAddress": "a", |
| 114 | + "holders": "h", |
| 115 | + "top100Amount": "t100a", |
| 116 | + "top10Amount": "t10a", |
| 117 | + "top100Holders": "t100h", |
| 118 | + "top10Holders": "t10h", |
| 119 | + "top100Ratio": "t100r", |
| 120 | + "top10Ratio": "t10r", |
| 121 | + "timestamp": "ts", |
| 122 | + }, |
| 123 | + |
| 124 | + // New token subscription fields |
| 125 | + "subscribeNewToken": { |
| 126 | + "tokenAddress": "a", |
| 127 | + "name": "n", |
| 128 | + "symbol": "s", |
| 129 | + "createdAtMs": "cts", |
| 130 | + }, |
| 131 | + |
| 132 | + // Token supply subscription fields |
| 133 | + "subscribeTokenSupply": { |
| 134 | + "tokenAddress": "a", |
| 135 | + "supply": "s", |
| 136 | + "timestamp": "ts", |
| 137 | + }, |
| 138 | + |
| 139 | + // Dex pool balance subscription fields |
| 140 | + "subscribeDexPoolBalance": { |
| 141 | + "poolAddress": "a", |
| 142 | + "tokenAAddress": "taa", |
| 143 | + "tokenALiquidityInUsd": "taliu", |
| 144 | + "tokenBAddress": "tba", |
| 145 | + "tokenBLiquidityInUsd": "tbliu", |
| 146 | + }, |
| 147 | + |
| 148 | + // Token liquidity subscription fields |
| 149 | + "subscribeTokenLiquidity": { |
| 150 | + "tokenAddress": "a", |
| 151 | + "metricType": "t", |
| 152 | + "value": "v", |
| 153 | + "timestamp": "ts", |
| 154 | + }, |
| 155 | + |
| 156 | + // New token metadata subscription fields |
| 157 | + "subscribeNewTokensMetadata": { |
| 158 | + "tokenAddress": "a", |
| 159 | + "name": "n", |
| 160 | + "symbol": "s", |
| 161 | + "imageUrl": "iu", |
| 162 | + "description": "de", |
| 163 | + "socialMedia": "sm", |
| 164 | + "createdAtMs": "cts", |
| 165 | + }, |
| 166 | + |
| 167 | + // Token trades subscription fields |
| 168 | + "subscribeTokenTrades": { |
| 169 | + "tokenAddress": "a", |
| 170 | + "timestamp": "t", |
| 171 | + "kind": "k", |
| 172 | + "buyAmount": "ba", |
| 173 | + "buyAmountInUsd": "baiu", |
| 174 | + "buyTokenAddress": "btma", |
| 175 | + "buyTokenName": "btn", |
| 176 | + "buyTokenSymbol": "bts", |
| 177 | + "buyWalletAddress": "bwa", |
| 178 | + "sellAmount": "sa", |
| 179 | + "sellAmountInUsd": "saiu", |
| 180 | + "sellTokenAddress": "stma", |
| 181 | + "sellTokenName": "stn", |
| 182 | + "sellTokenSymbol": "sts", |
| 183 | + "sellWalletAddress": "swa", |
| 184 | + "txHash": "h", |
| 185 | + }, |
| 186 | + |
| 187 | + // Wallet token PnL subscription fields |
| 188 | + "subscribeWalletPnl": { |
| 189 | + "walletAddress": "a", |
| 190 | + "tokenAddress": "ta", |
| 191 | + "tokenPriceInUsd": "tpiu", |
| 192 | + "timestamp": "t", |
| 193 | + "opentime": "ot", |
| 194 | + "lasttime": "lt", |
| 195 | + "closetime": "ct", |
| 196 | + "buyAmount": "ba", |
| 197 | + "buyAmountInUsd": "baiu", |
| 198 | + "buyCount": "bs", |
| 199 | + "buyCount30d": "bs30d", |
| 200 | + "buyCount7d": "bs7d", |
| 201 | + "sellAmount": "sa", |
| 202 | + "sellAmountInUsd": "saiu", |
| 203 | + "sellCount": "ss", |
| 204 | + "sellCount30d": "ss30d", |
| 205 | + "sellCount7d": "ss7d", |
| 206 | + "heldDurationTimestamp": "hdts", |
| 207 | + "averageBuyPriceInUsd": "abpiu", |
| 208 | + "averageSellPriceInUsd": "aspiu", |
| 209 | + "unrealizedProfitInUsd": "upiu", |
| 210 | + "unrealizedProfitRatio": "upr", |
| 211 | + "realizedProfitInUsd": "rpiu", |
| 212 | + "realizedProfitRatio": "rpr", |
| 213 | + "totalRealizedProfitInUsd": "trpiu", |
| 214 | + "totalRealizedProfitRatio": "trr", |
| 215 | + }, |
| 216 | +} as const; |
| 217 | + |
| 218 | +/** |
| 219 | + * Get field mappings for a specific subscription method |
| 220 | + * @param methodName - The name of the subscription method |
| 221 | + * @returns Field mapping object for the method, or empty object if not found |
| 222 | + */ |
| 223 | +export function getFieldMappings(methodName: string): FieldMapping { |
| 224 | + return CEL_FIELD_MAPPINGS[methodName] || {}; |
| 225 | +} |
| 226 | + |
| 227 | +/** |
| 228 | + * Replace long field names with short field names in a filter expression |
| 229 | + * Automatically adds meta. prefix if not present |
| 230 | + * @param filter - Original filter expression |
| 231 | + * @param methodName - The name of the subscription method |
| 232 | + * @returns Filter expression with short field names and meta. prefix |
| 233 | + */ |
| 234 | +export function replaceFilterFields(filter: string, methodName: string): string { |
| 235 | + if (!filter) {return filter} |
| 236 | + |
| 237 | + const fieldMappings = getFieldMappings(methodName); |
| 238 | + let result = filter; |
| 239 | + |
| 240 | + // Replace all long field names with short field names |
| 241 | + for (const [longField, shortField] of Object.entries(fieldMappings)) { |
| 242 | + // Handle both cases: with and without meta. prefix |
| 243 | + const patterns = [ |
| 244 | + // Pattern 1: fieldName (without meta. prefix) |
| 245 | + new RegExp(`\\b${longField}\\b`, "g"), |
| 246 | + // Pattern 2: meta.fieldName (with meta. prefix) |
| 247 | + new RegExp(`\\bmeta\\.${longField}\\b`, "g"), |
| 248 | + ]; |
| 249 | + |
| 250 | + patterns.forEach((pattern) => { |
| 251 | + result = result.replace(pattern, `meta.${shortField}`); |
| 252 | + }); |
| 253 | + } |
| 254 | + |
| 255 | + return result; |
| 256 | +} |
| 257 | + |
| 258 | +/** |
| 259 | + * Type-safe field mapping for specific subscription methods |
| 260 | + */ |
| 261 | +export type SubscriptionMethod = keyof typeof CEL_FIELD_MAPPINGS; |
| 262 | + |
| 263 | +/** |
| 264 | + * Get available field names for a specific subscription method |
| 265 | + * @param methodName - The name of the subscription method |
| 266 | + * @returns Array of available field names |
| 267 | + */ |
| 268 | +export function getAvailableFields(methodName: string): string[] { |
| 269 | + const mappings = getFieldMappings(methodName); |
| 270 | + return Object.keys(mappings); |
| 271 | +} |
0 commit comments