@liberfi.io/types 0.1.26 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,288 @@
1
+ # @liberfi.io/types
2
+
3
+ Pure TypeScript type-definition package with zero runtime dependencies. Serves as the **single source of truth** for domain types and API contract interfaces across the entire Liberfi React SDK monorepo. Consumed by `@liberfi.io/client`, `@liberfi.io/react`, and all UI packages.
4
+
5
+ ## Design Philosophy
6
+
7
+ - **Zero runtime dependencies** — contains only TypeScript types and enums; adds no weight to the consumer's bundle beyond what is used.
8
+ - **Domain vs API contract layering** — domain models (`Token`, `Activity`, `Chain`, `Portfolio`, etc.) are cleanly separated from API contract interfaces (`IClient`, `ISubscribeClient`), with one-directional imports from domain → API.
9
+ - **Open literal types** — patterns like `TokenProtocol = SolanaTokenProtocol | (string & {})` provide autocompletion for known values while remaining extensible for future additions.
10
+ - **Interface-based abstraction** — `IClient` and `ISubscribeClient` define the API surface abstractly, enabling different implementations (production client, mocks, stubs) without coupling consumers to a concrete class.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @liberfi.io/types
16
+ ```
17
+
18
+ No peer dependencies required.
19
+
20
+ ## API Reference
21
+
22
+ ### Enums
23
+
24
+ | Enum | Description |
25
+ | --------------------- | ----------------------------------------------------------------------------------------------------------------------- |
26
+ | `Chain` | ~190 blockchain chain IDs (e.g. `Chain.ETHEREUM`, `Chain.SOLANA`, `Chain.BASE`). Values are numeric string identifiers. |
27
+ | `ChainNamespace` | Chain family namespace — `EVM` or `SOL`. |
28
+ | `SolanaTokenProtocol` | Known Solana launch protocols (e.g. `PUMP`, `RAYDIUM`, `METEORA`, `ORCA`). |
29
+ | `SwapMode` | Swap direction — `EXACT_IN` or `EXACT_OUT`. |
30
+
31
+ ### Domain Interfaces — Token
32
+
33
+ | Type | Description |
34
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------ |
35
+ | `Token` | Core token model with metadata, stats, market data, liquidity, security, and social links. |
36
+ | `TokenCreator` | Token creator info: address, share percentage, verification status. |
37
+ | `TokenLaunchedFrom` | Launch platform details: program address and protocol family. |
38
+ | `TokenMigratedTo` | Migration target: program address, protocol family, pool address, timestamp. |
39
+ | `TokenSocialMedias` | Social media URLs (twitter, telegram, website, discord, github, etc.). |
40
+ | `TokenStats` | Per-resolution (`1m`–`24h`) trading statistics container. |
41
+ | `TokenStatsByResolution` | Single-resolution stats: buys, sells, traders, volumes, OHLC prices, price change. |
42
+ | `TokenMarketData` | Market data: supply, market cap, price, TVL, holder breakdowns by tag (bluechip, KOL, sniper, pro, insider, etc.). |
43
+ | `TokenLiquidity` | Liquidity pool info: pool/pair address, protocol, TVL. |
44
+ | `TokenSecurity` | Security flags: transfer fee, freezable, closable, transferable. |
45
+ | `TokenCandle` | OHLCV candlestick data point with resolution and timestamp. |
46
+ | `TokenHolder` | Single holder: address, amount, USD value, holding ratio. |
47
+
48
+ ### Domain Interfaces — Activity
49
+
50
+ | Type | Description |
51
+ | --------------- | ---------------------------------------------------------------------------------------------------------------------- | -------- |
52
+ | `Activity` | On-chain activity record (trade, liquidity, red packet). Contains `from`/`to` tokens, dex info, status, and timestamp. |
53
+ | `ActivityToken` | Token within an activity: address, symbol, amount, USD values. |
54
+ | `ActivityDex` | DEX where the activity occurred: name, image, protocol family, program address. |
55
+ | `Trade` | Narrowed subset of `Activity` with `type: "buy" | "sell"`. |
56
+
57
+ ### Domain Interfaces — Wallet & Portfolio
58
+
59
+ | Type | Description |
60
+ | --------------------- | -------------------------------------------------------------------------------------------------- |
61
+ | `Portfolio` | Wallet's token holding: price, amount, decimals (in USD and native). |
62
+ | `PortfolioPnl` | Per-token PnL details: buy/sell volumes, avg prices, realized/unrealized profit. |
63
+ | `WalletPnl` | Wallet-level PnL summary: win rate, total trades, profit breakdown. |
64
+ | `WalletPortfolioPnls` | Paginated portfolio PnL list with wallet-level summary (extends `WalletPnl` + `CursorPagination`). |
65
+ | `WalletPortfolios` | Paginated wallet portfolios with total balance (extends `CursorPagination`). |
66
+
67
+ ### API Contract — Pagination
68
+
69
+ | Type | Description |
70
+ | ------------------- | ---------------------------------------------------------------------------------------- |
71
+ | `CursorPagination` | Shared cursor-based pagination fields: `startCursor`, `endCursor`, `hasPrev`, `hasNext`. |
72
+ | `CursorList<T>` | Generic paginated result: `data: T[]` + `CursorPagination`. |
73
+ | `CursorListOptions` | Query options: `cursor`, `limit`, `direction`. |
74
+
75
+ ### API Contract — Query Options
76
+
77
+ | Type | Description |
78
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
79
+ | `GetTokenCandlesOptions` | Candle query options: `after`, `before` (Date), `limit`. |
80
+ | `GetTokenListOptions` | Token list options: `sortBy`, `sortDirection`, `filters`, `keywords`, `excludeKeywords`. |
81
+ | `SearchTokensOptions` | Search options (extends `CursorListOptions`): `chains`, `keyword`, `filters`, `sortBy`, `sortDirection`. |
82
+ | `SearchTokenCursorList` | Search result (extends `CursorList<Token>`): adds `total` count and `extra` metadata. |
83
+ | `TokenFilterOption` | Filter descriptor: `field` (TokenFieldOption), `operator` (eq/gt/between/in/…), `value`. |
84
+ | `GetTradesOptions` | Trade query options (extends `CursorListOptions`): `before`, `after`, `beforeBlockHeight`, `afterBlockHeight`, `type`, `poolAddress`. |
85
+ | `GetActivitiesOptions` | Activity query options (extends `CursorListOptions`): same time/block filters plus `type` (ActivityType). |
86
+
87
+ ### API Contract — Swap & Transaction
88
+
89
+ | Type | Description |
90
+ | --------------- | ----------------------------------------------------------------------------- |
91
+ | `SwapParams` | Swap request: chain, addresses, mode, amount, slippage, fees, anti-MEV flag. |
92
+ | `SwapRoute` | Swap response: serialized unsigned transaction + route plans. |
93
+ | `SwapRoutePlan` | Single route plan step: protocol name, input/output tokens and amounts, fees. |
94
+ | `SendTxParams` | Send transaction request: chain, signed serialized transaction, extras. |
95
+ | `SendTxResult` | Send transaction response: `txHash` + extras. |
96
+
97
+ ### API Contract — Client Interfaces
98
+
99
+ #### `IClient`
100
+
101
+ REST API client interface. Key methods:
102
+
103
+ ```typescript
104
+ interface IClient {
105
+ getToken(chain: Chain, address: string): Promise<Token>;
106
+ getTokens(chain: Chain, addresses: string[]): Promise<Token[]>;
107
+ getTokenCandles(chain, address, resolution, options?): Promise<TokenCandle[]>;
108
+ getTokenSecurity(chain, address): Promise<TokenSecurity>;
109
+ getTokenStats(chain, address): Promise<TokenStats>;
110
+ getTokenHolders(chain, address, options?): Promise<CursorList<TokenHolder>>;
111
+ getTokenMarketData(chain, address): Promise<TokenMarketData>;
112
+ getNewTokens(chain, options?): Promise<Token[]>;
113
+ getFinalStretchTokens(chain, options?): Promise<Token[]>;
114
+ getMigratedTokens(chain, options?): Promise<Token[]>;
115
+ getTrendingTokens(chain, resolution, options?): Promise<Token[]>;
116
+ getStockTokens(chain, options?): Promise<Token[]>;
117
+ searchTokens(options?): Promise<SearchTokenCursorList>;
118
+ swapRoute(params: SwapParams): Promise<SwapRoute>;
119
+ sendTx(params: SendTxParams): Promise<SendTxResult>;
120
+ checkTxSuccess(chain, txHash, timeout?): Promise<boolean>;
121
+ getWalletPortfolios(chain, address, options?): Promise<WalletPortfolios>;
122
+ getWalletPnl(chain, address, resolution?): Promise<WalletPnl>;
123
+ getWalletPortfolioPnls(
124
+ chain,
125
+ address,
126
+ options?,
127
+ ): Promise<WalletPortfolioPnls>;
128
+ getWalletPortfoliosByTokens(
129
+ chain,
130
+ address,
131
+ tokenAddresses,
132
+ ): Promise<Portfolio[]>;
133
+ getWalletPortfolioPnlsByTokens(
134
+ chain,
135
+ address,
136
+ tokenAddresses,
137
+ ): Promise<PortfolioPnl[]>;
138
+ getWalletTrades(chain, address, options?): Promise<CursorList<Trade>>;
139
+ getTokenTrades(chain, address, options?): Promise<CursorList<Trade>>;
140
+ getWalletActivities(chain, address, options?): Promise<CursorList<Activity>>;
141
+ getTokenActivities(chain, address, options?): Promise<CursorList<Activity>>;
142
+ getPresignedUploadUrl(): Promise<string>;
143
+ }
144
+ ```
145
+
146
+ #### `ISubscribeClient`
147
+
148
+ WebSocket subscription interface for real-time updates:
149
+
150
+ ```typescript
151
+ interface ISubscribeClient {
152
+ subscribeToken(chain, address, callback): ISubscription;
153
+ subscribeTokenCandles(chain, address, resolution, callback): ISubscription;
154
+ subscribeWalletPnl(chain, address, callback): ISubscription;
155
+ subscribeWalletPortfolios(chain, address, callback): ISubscription;
156
+ subscribeWalletPortfolioPnls(chain, address, callback): ISubscription;
157
+ subscribeWalletTrades(chain, address, callback): ISubscription;
158
+ subscribeTokenTrades(chain, address, callback): ISubscription;
159
+ subscribeWalletActivities(chain, address, callback): ISubscription;
160
+ subscribeTokenActivities(chain, address, callback): ISubscription;
161
+ subscribeNewTokens(chain, callback): ISubscription;
162
+ subscribeTrendingTokens(chain, callback): ISubscription;
163
+ subscribeMigratedTokens(chain, callback): ISubscription;
164
+ subscribeFinalStretchTokens(chain, callback): ISubscription;
165
+ subscribeStockTokens(chain, callback): ISubscription;
166
+ }
167
+ ```
168
+
169
+ #### `ISubscription`
170
+
171
+ ```typescript
172
+ interface ISubscription {
173
+ unsubscribe(): void;
174
+ }
175
+ ```
176
+
177
+ ### Subscription Data Types
178
+
179
+ | Type | Description |
180
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
181
+ | `TokenSubscribed` | Incremental token update — `chain` + `address` required, all other `Token` fields optional (`undefined` = not included in this push). |
182
+ | `WalletPnlSubscribed` | `WalletPnl` extended with optional `resolution` field. |
183
+ | `PortfolioSubscribed` | Lightweight portfolio update: chain, wallet address, token address, price, amount. |
184
+ | `PortfolioPnlSubscribed` | Partial `PortfolioPnl` keyed by `walletAddress` + `tokenAddress`. |
185
+
186
+ ### Type Aliases
187
+
188
+ | Type | Definition | Description |
189
+ | -------------------- | --------------------------------------------------------------------- | -------------------------------------------------- |
190
+ | `TokenProtocol` | `SolanaTokenProtocol \| (string & {})` | Open union for token launch protocols. |
191
+ | `ActivityType` | `"buy" \| "sell" \| "liquidity_initialize" \| "liquidity_add" \| ...` | All known on-chain activity types. |
192
+ | `TokenResolution` | `"1s" \| "15s" \| "30s" \| "1m" \| ... \| "24h"` | Candlestick / stats time resolution. |
193
+ | `TrendingResolution` | `Extract<TokenResolution, "1m" \| "5m" \| "1h" \| "4h" \| "24h">` | Subset of resolutions supported by trending lists. |
194
+ | `TokenFieldOption` | 50+ string literals + `(string & {})` | Sortable / filterable token field identifiers. |
195
+
196
+ ### Constants
197
+
198
+ | Name | Type | Description |
199
+ | ------------------------ | ----------------------- | ----------------------------------------------- |
200
+ | `SOLANA_TOKEN_PROTOCOLS` | `SolanaTokenProtocol[]` | Array of all `SolanaTokenProtocol` enum values. |
201
+ | `version` | `string` | Current package version. |
202
+
203
+ ### Namespace
204
+
205
+ | Name | Description |
206
+ | ----- | ------------------------------------------------------------------------------------------------------------ |
207
+ | `API` | Re-exports all types from `./api` as a single namespace for backward-compatible access (e.g. `API.IClient`). |
208
+
209
+ ## Usage Examples
210
+
211
+ ### Import domain types
212
+
213
+ ```typescript
214
+ import { Chain, Token, Activity } from "@liberfi.io/types";
215
+
216
+ function formatToken(token: Token): string {
217
+ return `${token.symbol} on chain ${token.chain}`;
218
+ }
219
+ ```
220
+
221
+ ### Type-safe API client implementation
222
+
223
+ ```typescript
224
+ import {
225
+ IClient,
226
+ Chain,
227
+ Token,
228
+ CursorList,
229
+ TokenHolder,
230
+ } from "@liberfi.io/types";
231
+
232
+ class MyClient implements IClient {
233
+ async getToken(chain: Chain, address: string): Promise<Token> {
234
+ const res = await fetch(`/api/tokens/${chain}/${address}`);
235
+ return res.json();
236
+ }
237
+ // ... implement remaining methods
238
+ }
239
+ ```
240
+
241
+ ### Filter tokens with TokenFilterOption
242
+
243
+ ```typescript
244
+ import type { TokenFilterOption, GetTokenListOptions } from "@liberfi.io/types";
245
+
246
+ const options: GetTokenListOptions = {
247
+ sortBy: "volumes24h",
248
+ sortDirection: "desc",
249
+ filters: [
250
+ { field: "marketCap", operator: "gte", value: "100000" },
251
+ { field: "holders", operator: "gte", value: "50" },
252
+ ],
253
+ };
254
+ ```
255
+
256
+ ### Subscribe to real-time updates
257
+
258
+ ```typescript
259
+ import type {
260
+ ISubscribeClient,
261
+ Chain,
262
+ TokenSubscribed,
263
+ } from "@liberfi.io/types";
264
+
265
+ function watchToken(client: ISubscribeClient, chain: Chain, address: string) {
266
+ const sub = client.subscribeToken(
267
+ chain,
268
+ address,
269
+ (updates: TokenSubscribed[]) => {
270
+ for (const update of updates) {
271
+ if (update.marketData?.priceInUsd) {
272
+ console.log(`Price: $${update.marketData.priceInUsd}`);
273
+ }
274
+ }
275
+ },
276
+ );
277
+ return () => sub.unsubscribe();
278
+ }
279
+ ```
280
+
281
+ ## Future Improvements
282
+
283
+ - Deduplicate `version.ts` global side effect across packages into a shared utility.
284
+ - Extract `HolderTagBreakdown` sub-type from `TokenMarketData` to reduce repeated field triples.
285
+ - Generate `TokenFieldOption` via template literal types instead of maintaining 50+ manual entries.
286
+ - Complete `TokenSecurity` interface (currently has a `TODO` for additional security checks).
287
+ - Consider auto-generating the `Chain` enum from a canonical chain registry.
288
+ - Document or mitigate the `version.ts` multi-instance overwrite concern in SSR environments.