lovewebconnection 0.0.0-stage → 0.2.0

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.
Files changed (68) hide show
  1. package/.env.example +38 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1915 -2
  4. package/dist/alert-queue.d.ts +72 -0
  5. package/dist/alert-queue.js +155 -0
  6. package/dist/alert-store.d.ts +29 -0
  7. package/dist/alert-store.js +75 -0
  8. package/dist/balances.d.ts +221 -0
  9. package/dist/balances.js +1232 -0
  10. package/dist/cache.d.ts +37 -0
  11. package/dist/cache.js +128 -0
  12. package/dist/call.d.ts +46 -0
  13. package/dist/call.js +377 -0
  14. package/dist/chain.d.ts +97 -0
  15. package/dist/chain.js +419 -0
  16. package/dist/cli.d.ts +2 -0
  17. package/dist/cli.js +1011 -0
  18. package/dist/compare-history.d.ts +82 -0
  19. package/dist/compare-history.js +340 -0
  20. package/dist/compare.d.ts +98 -0
  21. package/dist/compare.js +492 -0
  22. package/dist/contract.d.ts +107 -0
  23. package/dist/contract.js +162 -0
  24. package/dist/default-abi.d.ts +53 -0
  25. package/dist/default-abi.js +85 -0
  26. package/dist/history.d.ts +83 -0
  27. package/dist/history.js +422 -0
  28. package/dist/index.d.ts +54 -0
  29. package/dist/index.js +27 -0
  30. package/dist/market-page.d.ts +8 -0
  31. package/dist/market-page.js +554 -0
  32. package/dist/market-server.d.ts +46 -0
  33. package/dist/market-server.js +473 -0
  34. package/dist/market.d.ts +323 -0
  35. package/dist/market.js +835 -0
  36. package/dist/metadata-cache.d.ts +72 -0
  37. package/dist/metadata-cache.js +197 -0
  38. package/dist/metrics.d.ts +64 -0
  39. package/dist/metrics.js +138 -0
  40. package/dist/plot.d.ts +60 -0
  41. package/dist/plot.js +317 -0
  42. package/dist/presets-history.d.ts +372 -0
  43. package/dist/presets-history.js +1243 -0
  44. package/dist/presets-summary.d.ts +17 -0
  45. package/dist/presets-summary.js +55 -0
  46. package/dist/presets.d.ts +59 -0
  47. package/dist/presets.js +181 -0
  48. package/dist/prices.d.ts +63 -0
  49. package/dist/prices.js +154 -0
  50. package/dist/rotate.d.ts +19 -0
  51. package/dist/rotate.js +34 -0
  52. package/dist/scan.d.ts +81 -0
  53. package/dist/scan.js +501 -0
  54. package/dist/sessions.d.ts +104 -0
  55. package/dist/sessions.js +515 -0
  56. package/dist/signatures.d.ts +13 -0
  57. package/dist/signatures.js +43 -0
  58. package/dist/tokenfile.d.ts +50 -0
  59. package/dist/tokenfile.js +218 -0
  60. package/dist/views.d.ts +63 -0
  61. package/dist/views.js +415 -0
  62. package/dist/watch.d.ts +306 -0
  63. package/dist/watch.js +1121 -0
  64. package/dist/webhook.d.ts +17 -0
  65. package/dist/webhook.js +35 -0
  66. package/examples/erc20.abi.json +57 -0
  67. package/examples/xlayer.tokens.json +4 -0
  68. package/package.json +77 -4
@@ -0,0 +1,323 @@
1
+ /**
2
+ * A fixed-price market: every swap settles at one price that never moves, so a
3
+ * trade can never reprice (devalue) the token. The pool's job is settlement, not
4
+ * price discovery.
5
+ *
6
+ * A constant-product AMM needs `k` worth of reserves to absorb a trade, so a
7
+ * $1 pool answers a $1 trade and dies. Here the price is a fixed rational, the
8
+ * pool holds inventory at that price, and a buy is reversed exactly by a sell of
9
+ * the same size: the reserves return to where they started, minus nothing but
10
+ * the fee. Cumulative volume is therefore bounded by swap count, not by the
11
+ * dollars deposited — a $1 pool that is traded round trip a billion times has
12
+ * settled a billion dollars' worth of volume while its reserves never moved.
13
+ *
14
+ * The one honest limit is net directional flow: a buy the pool cannot fill from
15
+ * its base inventory, or a sell it cannot pay from its quote inventory, is
16
+ * refused with a reason instead of being silently repriced.
17
+ */
18
+ /** Which leg the trader sends: `buy` sends quote for base, `sell` sends base for quote. */
19
+ export type MarketSide = "buy" | "sell";
20
+ export type MarketErrorCode = "invalid-amount" | "insufficient-liquidity" | "insufficient-shares" | "invalid-pool";
21
+ export declare class MarketError extends Error {
22
+ readonly code: MarketErrorCode;
23
+ constructor(code: MarketErrorCode, message: string);
24
+ }
25
+ export declare class MarketArgError extends Error {
26
+ constructor(message: string);
27
+ }
28
+ /** Why a quote cannot be executed, or `null` when it can. */
29
+ export type MarketIssue = "amount-too-small" | "insufficient-liquidity" | null;
30
+ export interface MarketConfig {
31
+ baseSymbol: string;
32
+ quoteSymbol: string;
33
+ baseDecimals: number;
34
+ quoteDecimals: number;
35
+ /** Fixed price as a reduced rational: quote base units per base base unit. */
36
+ priceNum: bigint;
37
+ priceDen: bigint;
38
+ /** Fee taken from the input leg and left in the pool for liquidity providers. */
39
+ feeBps: number;
40
+ }
41
+ export interface ReservePair {
42
+ base: bigint;
43
+ quote: bigint;
44
+ }
45
+ export interface SwapQuote {
46
+ side: MarketSide;
47
+ /** What the trader sends, in that token's base units. */
48
+ amountIn: bigint;
49
+ /** What the trader receives, in the other token's base units. */
50
+ amountOut: bigint;
51
+ /** Fee taken from `amountIn`, left in the pool. */
52
+ fee: bigint;
53
+ /** Notional value of the trade in quote base units. */
54
+ notional: bigint;
55
+ /** Reserves the pool holds if this quote is executed. */
56
+ reservesAfter: ReservePair;
57
+ /** Always zero: a fixed-price fill cannot move the price. */
58
+ priceImpactBps: 0;
59
+ executable: boolean;
60
+ issue: MarketIssue;
61
+ /** Human-readable reason when `executable` is false. */
62
+ reason: string | null;
63
+ }
64
+ export interface LiquidityReceipt {
65
+ kind: "add" | "remove";
66
+ base: bigint;
67
+ quote: bigint;
68
+ shares: bigint;
69
+ sharesAfter: bigint;
70
+ liquidityValueQuote: bigint;
71
+ }
72
+ export interface MarketState {
73
+ baseSymbol: string;
74
+ quoteSymbol: string;
75
+ baseDecimals: number;
76
+ quoteDecimals: number;
77
+ priceNum: bigint;
78
+ priceDen: bigint;
79
+ /** The fixed price as a decimal string, quote per one whole base token. */
80
+ price: string;
81
+ feeBps: number;
82
+ baseReserve: bigint;
83
+ quoteReserve: bigint;
84
+ /** Value of both reserves at the fixed price, in quote base units. */
85
+ liquidityValueQuote: bigint;
86
+ shares: bigint;
87
+ /** Cumulative traded notional in quote base units. */
88
+ volumeQuote: bigint;
89
+ swaps: number;
90
+ /** Volume settled per unit of deposited liquidity — the turnover multiple. */
91
+ turnover: number;
92
+ /** Inventory drift from the opening reserves: the market's net exposure. */
93
+ netBase: bigint;
94
+ netQuote: bigint;
95
+ }
96
+ export interface MarketCapacity {
97
+ /** Most base one buy can take out right now. */
98
+ maxBuyBase: bigint;
99
+ /** Most base one sell can hand in right now, before quote reserves run out. */
100
+ maxSellBase: bigint;
101
+ /** Quote the pool can pay out in total. */
102
+ payQuote: bigint;
103
+ /** Value of the pool at the fixed price. */
104
+ liquidityValueQuote: bigint;
105
+ }
106
+ /** Where `lwc market --serve` listens unless told otherwise. */
107
+ export declare const DEFAULT_MARKET_HOST = "127.0.0.1";
108
+ export declare const DEFAULT_MARKET_PORT = 8787;
109
+ export declare const MAX_MARKET_PORT = 65535;
110
+ /** Requests per minute one client may make unless `--rate-limit` says otherwise. */
111
+ export declare const DEFAULT_MARKET_RATE_LIMIT = 120;
112
+ /** Render a rational as a decimal string, without trailing zeros. */
113
+ export declare function rationalToDecimal(num: bigint, den: bigint, maxDecimals?: number): string;
114
+ /**
115
+ * Turn a human price into the pool's rational. `"28.842"` with 8 base decimals
116
+ * and 6 quote decimals means one whole base token is worth 28.842 quote tokens,
117
+ * and the rational is expressed in raw units of each token.
118
+ */
119
+ export declare function fixedPrice(human: string, baseDecimals: number, quoteDecimals: number): {
120
+ num: bigint;
121
+ den: bigint;
122
+ };
123
+ /** Share counts are whole units, so they never take decimals. */
124
+ export declare function parseShares(value: string): bigint;
125
+ /** Human price of a rational, quote per one whole base token. */
126
+ export declare function humanPrice(num: bigint, den: bigint, baseDecimals: number, quoteDecimals: number): string;
127
+ /** A decimal amount as base units, with the pool's own decimals. */
128
+ export declare function parseMarketAmount(value: string, decimals: number, label: string): bigint;
129
+ /** Base units as a trimmed decimal string. */
130
+ export declare function formatMarketAmount(value: bigint, decimals: number): string;
131
+ /** The on-disk shape, with every bigint written as a decimal string. */
132
+ export interface MarketPoolJson {
133
+ version: 1;
134
+ baseSymbol: string;
135
+ quoteSymbol: string;
136
+ baseDecimals: number;
137
+ quoteDecimals: number;
138
+ priceNum: string;
139
+ priceDen: string;
140
+ feeBps: number;
141
+ baseReserve: string;
142
+ quoteReserve: string;
143
+ shares: string;
144
+ volumeQuote: string;
145
+ swaps: number;
146
+ openedBase: string;
147
+ openedQuote: string;
148
+ }
149
+ /** Convert a parsed pool file into runtime state, validating every field. */
150
+ export declare function poolFromJson(raw: unknown): MarketPool;
151
+ /** Read a pool file from disk, validating every field. */
152
+ export declare function loadPoolFile(path: string): MarketPool;
153
+ /** Write a pool file atomically, refusing to replace one without `force`. */
154
+ export declare function savePoolFile(path: string, pool: MarketPool, options?: {
155
+ force?: boolean;
156
+ }): MarketPoolJson;
157
+ export interface MarketPoolState {
158
+ baseReserve: bigint;
159
+ quoteReserve: bigint;
160
+ shares: bigint;
161
+ volumeQuote: bigint;
162
+ swaps: number;
163
+ openedBase: bigint;
164
+ openedQuote: bigint;
165
+ }
166
+ /**
167
+ * A fixed-price settlement pool. Reserves only move when flow is net
168
+ * directional, so matched volume costs the pool nothing.
169
+ */
170
+ export declare class MarketPool {
171
+ readonly config: MarketConfig;
172
+ private baseReserve;
173
+ private quoteReserve;
174
+ private sharesOut;
175
+ private volumeQuote;
176
+ private swapCount;
177
+ private readonly openedBase;
178
+ private readonly openedQuote;
179
+ constructor(config: MarketConfig, state?: Partial<MarketPoolState>);
180
+ /**
181
+ * Create a pool worth `liquidity` of the quote token, split evenly between the
182
+ * two legs at the fixed price, so one dollar on deposit really is one dollar.
183
+ */
184
+ static create(config: MarketConfig, liquidity: bigint): MarketPool;
185
+ /** Price of the base token in quote units, in raw units of each token. */
186
+ private get ratio();
187
+ /** Quote base units for a given base amount, at the fixed price (rounded down). */
188
+ quoteForBase(baseAmount: bigint): bigint;
189
+ /** Base units for a given quote amount, at the fixed price (rounded down). */
190
+ baseForQuote(quoteAmount: bigint): bigint;
191
+ /** Value of both reserves at the fixed price, in quote base units. */
192
+ liquidityValueQuote(): bigint;
193
+ /**
194
+ * Price a trade without touching the pool. A well-formed amount always gets a
195
+ * quote; `executable` and `reason` say whether the pool can settle it.
196
+ */
197
+ quote(side: MarketSide, amountIn: bigint): SwapQuote;
198
+ /** Execute a trade at the fixed price and return the fill. */
199
+ swap(side: MarketSide, amountIn: bigint): SwapQuote;
200
+ /** Deposit one or both legs and mint shares at the pre-deposit value. */
201
+ addLiquidity(input: Partial<ReservePair>): LiquidityReceipt;
202
+ /** Burn shares for their pro-rata slice of both reserves. */
203
+ removeLiquidity(shares: bigint): LiquidityReceipt;
204
+ /** What the pool can settle right now, and how much volume its liquidity backs. */
205
+ capacity(): MarketCapacity;
206
+ /** What the pool could settle right after `quote`, without applying it. */
207
+ capacityAfter(quote: SwapQuote): MarketCapacity;
208
+ state(): MarketState;
209
+ toJson(): MarketPoolJson;
210
+ private outSymbol;
211
+ private describe;
212
+ }
213
+ /** JSON form of a market state, with bigints as decimal strings. */
214
+ export declare function toMarketJson(state: MarketState, path?: string | null): {
215
+ pool: string | null;
216
+ base: {
217
+ symbol: string;
218
+ decimals: number;
219
+ reserve: string;
220
+ };
221
+ quote: {
222
+ symbol: string;
223
+ decimals: number;
224
+ reserve: string;
225
+ };
226
+ price: {
227
+ value: string;
228
+ num: string;
229
+ den: string;
230
+ fixed: boolean;
231
+ priceImpactBps: number;
232
+ };
233
+ feeBps: number;
234
+ liquidity: {
235
+ valueQuote: string;
236
+ shares: string;
237
+ };
238
+ volume: {
239
+ quote: string;
240
+ swaps: number;
241
+ turnover: number;
242
+ };
243
+ netFlow: {
244
+ base: string;
245
+ quote: string;
246
+ };
247
+ };
248
+ /** JSON form of a swap quote, with bigints as decimal strings. */
249
+ export declare function toSwapQuoteJson(quote: SwapQuote, state: MarketState): {
250
+ side: MarketSide;
251
+ amountIn: string;
252
+ amountOut: string;
253
+ fee: string;
254
+ notional: string;
255
+ inSymbol: string;
256
+ outSymbol: string;
257
+ price: string;
258
+ priceImpactBps: 0;
259
+ executable: boolean;
260
+ issue: MarketIssue;
261
+ reason: string | null;
262
+ reservesAfter: {
263
+ base: string;
264
+ quote: string;
265
+ };
266
+ };
267
+ /** Human report for `lwc market`. */
268
+ export declare function formatMarketState(state: MarketState, path: string | null): string;
269
+ /** Human report for one fill or quote. */
270
+ export declare function formatSwapQuote(quote: SwapQuote, state: MarketState, options?: {
271
+ applied?: boolean;
272
+ }): string;
273
+ /** Human report for a liquidity deposit or withdrawal. */
274
+ export declare function formatLiquidity(receipt: LiquidityReceipt, state: MarketState): string;
275
+ /** Shortest access key `--api-key` accepts before the API is exposed. */
276
+ export declare const MIN_MARKET_KEY_LENGTH = 8;
277
+ export interface MarketServeOptions {
278
+ host: string;
279
+ port: number;
280
+ /** Browser origins allowed to call the API, or `["*"]` for any. */
281
+ corsOrigins: string[];
282
+ /** Key every pool route requires; null leaves the API open. */
283
+ accessKey: string | null;
284
+ /** Requests per minute one client may make; 0 turns the limiter off. */
285
+ rateLimitPerMinute?: number;
286
+ /**
287
+ * Where the per-client counters are kept between runs. Defaults to a file
288
+ * beside the pool; null keeps them in memory only.
289
+ */
290
+ limitsPath?: string | null;
291
+ }
292
+ export type MarketOperation = {
293
+ kind: "state";
294
+ } | {
295
+ kind: "swap";
296
+ side: MarketSide;
297
+ amount: string;
298
+ } | {
299
+ kind: "add";
300
+ quote: string | null;
301
+ base: string | null;
302
+ remove: string;
303
+ };
304
+ export interface MarketArgs {
305
+ poolPath: string;
306
+ init: {
307
+ price: string;
308
+ baseSymbol: string;
309
+ quoteSymbol: string;
310
+ baseDecimals: number;
311
+ quoteDecimals: number;
312
+ feeBps: number;
313
+ seed: string;
314
+ } | null;
315
+ /** The single operation this run performs, after any init. */
316
+ operation: MarketOperation;
317
+ force: boolean;
318
+ /** Price the trade without applying it. */
319
+ dryRun: boolean;
320
+ /** Serve the pool over HTTP instead of running one operation. */
321
+ serve: MarketServeOptions | null;
322
+ }
323
+ export declare function parseMarketArgs(argv: string[]): MarketArgs;