@morpho-dev/router 0.0.10 → 0.0.12

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.
@@ -0,0 +1,540 @@
1
+ import { Offer, Errors } from '@morpho-dev/mempool';
2
+ export * from '@morpho-dev/mempool';
3
+ import * as viem from 'viem';
4
+ import { Address, PublicClient } from 'viem';
5
+ import { Chain as Chain$2 } from 'viem/chains';
6
+
7
+ type Compute<type> = {
8
+ [key in keyof type]: type[key];
9
+ } & unknown;
10
+
11
+ declare const chainNames: readonly ["ethereum", "base"];
12
+ type ChainName = (typeof chainNames)[number];
13
+ declare const ChainId: {
14
+ ETHEREUM: bigint;
15
+ BASE: bigint;
16
+ };
17
+ type ChainId = (typeof ChainId)[keyof typeof ChainId];
18
+ declare const chainIds: Set<bigint>;
19
+ type Chain = Compute<Omit<Chain$2, "id" | "name"> & {
20
+ id: ChainId;
21
+ name: ChainName;
22
+ whitelistedAssets: Set<Address>;
23
+ morpho: Address;
24
+ }>;
25
+ declare function getChain(chainId: ChainId): Chain | undefined;
26
+ declare const chains: Record<ChainName, Chain>;
27
+
28
+ type Chain$1_Chain = Chain;
29
+ type Chain$1_ChainId = ChainId;
30
+ type Chain$1_ChainName = ChainName;
31
+ declare const Chain$1_chainIds: typeof chainIds;
32
+ declare const Chain$1_chainNames: typeof chainNames;
33
+ declare const Chain$1_chains: typeof chains;
34
+ declare const Chain$1_getChain: typeof getChain;
35
+ declare namespace Chain$1 {
36
+ export { type Chain$1_Chain as Chain, type Chain$1_ChainId as ChainId, type Chain$1_ChainName as ChainName, Chain$1_chainIds as chainIds, Chain$1_chainNames as chainNames, Chain$1_chains as chains, Chain$1_getChain as getChain };
37
+ }
38
+
39
+ declare const OfferStatusValues: readonly ["valid", "callback_not_supported", "callback_error", "unverified"];
40
+ type OfferStatus = (typeof OfferStatusValues)[number];
41
+ type OfferMetadata = {
42
+ issue: string;
43
+ };
44
+ type RouterOffer = Offer.Offer & {
45
+ status: OfferStatus;
46
+ metadata?: OfferMetadata;
47
+ };
48
+
49
+ type RouterOffer$1_OfferMetadata = OfferMetadata;
50
+ type RouterOffer$1_OfferStatus = OfferStatus;
51
+ declare const RouterOffer$1_OfferStatusValues: typeof OfferStatusValues;
52
+ type RouterOffer$1_RouterOffer = RouterOffer;
53
+ declare namespace RouterOffer$1 {
54
+ export { type RouterOffer$1_OfferMetadata as OfferMetadata, type RouterOffer$1_OfferStatus as OfferStatus, RouterOffer$1_OfferStatusValues as OfferStatusValues, type RouterOffer$1_RouterOffer as RouterOffer };
55
+ }
56
+
57
+ type GetParameters = {
58
+ /** Filter by multiple creator addresses (comma-separated) */
59
+ creators?: string[];
60
+ /** Filter by offer type: buy offers or sell offers */
61
+ side?: "buy" | "sell";
62
+ /** Filter by multiple blockchain networks (comma-separated chain IDs) */
63
+ chains?: number[];
64
+ /** Filter by multiple loan assets (comma-separated) */
65
+ loanTokens?: string[];
66
+ /** Filter by multiple statuses (comma-separated) */
67
+ status?: OfferStatus[];
68
+ /** Filter by multiple callback addresses (comma-separated) */
69
+ callbackAddresses?: string[];
70
+ /** Minimum amount of assets in the offer */
71
+ minAmount?: bigint;
72
+ /** Maximum amount of assets in the offer */
73
+ maxAmount?: bigint;
74
+ /** Minimum rate per asset (in wei) */
75
+ minRate?: bigint;
76
+ /** Maximum rate per asset (in wei) */
77
+ maxRate?: bigint;
78
+ /** Minimum maturity timestamp (Unix timestamp in seconds) */
79
+ minMaturity?: number;
80
+ /** Maximum maturity timestamp (Unix timestamp in seconds) */
81
+ maxMaturity?: number;
82
+ /** Minimum expiry timestamp (Unix timestamp in seconds) */
83
+ minExpiry?: number;
84
+ /** Maximum expiry timestamp (Unix timestamp in seconds) */
85
+ maxExpiry?: number;
86
+ /** Filter by multiple collateral assets (comma-separated) */
87
+ collateralAssets?: string[];
88
+ /** Filter by multiple rate oracles (comma-separated) */
89
+ collateralOracles?: string[];
90
+ /** Filter by collateral combinations */
91
+ collateralTuple?: Array<{
92
+ asset: string;
93
+ oracle?: string;
94
+ lltv?: number;
95
+ }>;
96
+ /** Minimum Loan-to-Value ratio (LLTV) for collateral (percentage as decimal) */
97
+ minLltv?: number;
98
+ /** Maximum Loan-to-Value ratio (LLTV) for collateral (percentage as decimal) */
99
+ maxLltv?: number;
100
+ /** Field to sort results by */
101
+ sortBy?: "rate" | "maturity" | "expiry" | "amount";
102
+ /** Sort direction: asc (ascending) or desc (descending, default) */
103
+ sortOrder?: "asc" | "desc";
104
+ /** Pagination cursor in base64url-encoded format */
105
+ cursor?: string;
106
+ /** Maximum number of offers to return. Defaults to 20. Maximum is 100. */
107
+ limit?: number;
108
+ };
109
+ type MatchParameters = {
110
+ /** The desired side of the match: 'buy' if you want to buy, 'sell' if you want to sell */
111
+ side: "buy" | "sell";
112
+ /** The blockchain network chain ID */
113
+ chainId: number;
114
+ /** Rate per asset (in wei) for matching offers */
115
+ rate?: bigint;
116
+ /** Collateral requirements */
117
+ collaterals?: Array<{
118
+ asset: string;
119
+ oracle: string;
120
+ lltv: bigint;
121
+ }>;
122
+ /** Exact maturity timestamp (Unix timestamp in seconds) */
123
+ maturity?: number;
124
+ /** Minimum maturity timestamp (Unix timestamp in seconds, inclusive) */
125
+ minMaturity?: number;
126
+ /** Maximum maturity timestamp (Unix timestamp in seconds, inclusive) */
127
+ maxMaturity?: number;
128
+ /** The loan asset address to match against */
129
+ loanToken?: string;
130
+ /** Filter by a specific offer creator address */
131
+ creator?: string;
132
+ /** Filter by multiple statuses (comma-separated) */
133
+ status?: OfferStatus[];
134
+ /** Pagination cursor in base64url-encoded format */
135
+ cursor?: string;
136
+ /** Maximum number of offers to return. Defaults to 20. Maximum is 100. */
137
+ limit?: number;
138
+ };
139
+ type RouterClientConfig = {
140
+ readonly url: URL;
141
+ readonly headers: Headers;
142
+ };
143
+ type Client = Compute<RouterClientConfig & {
144
+ get: (parameters: GetParameters) => Promise<{
145
+ cursor: string | null;
146
+ offers: RouterOffer[];
147
+ }>;
148
+ match: (parameters: MatchParameters) => Promise<{
149
+ cursor: string | null;
150
+ offers: RouterOffer[];
151
+ }>;
152
+ }>;
153
+ declare function connect(opts?: {
154
+ url?: string;
155
+ apiKey?: string;
156
+ }): Client;
157
+ declare namespace connect {
158
+ type ErrorType = InvalidUrlError;
159
+ }
160
+ /**
161
+ * Get offers from the router.
162
+ * @returns The offers with pagination cursor.
163
+ */
164
+ declare function get(config: RouterClientConfig, parameters: GetParameters): Promise<{
165
+ cursor: string | null;
166
+ offers: RouterOffer[];
167
+ }>;
168
+ declare namespace get {
169
+ type ErrorType = GetApiErrorType;
170
+ }
171
+ /**
172
+ * Match offers from the router.
173
+ * @returns The matched offers with pagination cursor.
174
+ */
175
+ declare function match(config: RouterClientConfig, parameters: MatchParameters): Promise<{
176
+ cursor: string | null;
177
+ offers: RouterOffer[];
178
+ }>;
179
+ declare namespace match {
180
+ type ErrorType = GetApiErrorType;
181
+ }
182
+ type GetApiErrorType = HttpGetOffersFailedError | HttpUnauthorizedError | HttpForbiddenError | HttpRateLimitError;
183
+ declare class InvalidUrlError extends Errors.BaseError {
184
+ name: string;
185
+ constructor(url: string);
186
+ }
187
+ declare class HttpUnauthorizedError extends Errors.BaseError {
188
+ name: string;
189
+ constructor();
190
+ }
191
+ declare class HttpForbiddenError extends Errors.BaseError {
192
+ name: string;
193
+ constructor();
194
+ }
195
+ declare class HttpRateLimitError extends Errors.BaseError {
196
+ name: string;
197
+ constructor();
198
+ }
199
+ declare class HttpGetOffersFailedError extends Errors.BaseError {
200
+ name: string;
201
+ constructor(message: string, { details }?: {
202
+ details?: string;
203
+ });
204
+ }
205
+
206
+ type index_Client = Client;
207
+ type index_GetParameters = GetParameters;
208
+ type index_HttpForbiddenError = HttpForbiddenError;
209
+ declare const index_HttpForbiddenError: typeof HttpForbiddenError;
210
+ type index_HttpGetOffersFailedError = HttpGetOffersFailedError;
211
+ declare const index_HttpGetOffersFailedError: typeof HttpGetOffersFailedError;
212
+ type index_HttpRateLimitError = HttpRateLimitError;
213
+ declare const index_HttpRateLimitError: typeof HttpRateLimitError;
214
+ type index_HttpUnauthorizedError = HttpUnauthorizedError;
215
+ declare const index_HttpUnauthorizedError: typeof HttpUnauthorizedError;
216
+ type index_InvalidUrlError = InvalidUrlError;
217
+ declare const index_InvalidUrlError: typeof InvalidUrlError;
218
+ type index_MatchParameters = MatchParameters;
219
+ type index_RouterClientConfig = RouterClientConfig;
220
+ declare const index_connect: typeof connect;
221
+ declare const index_get: typeof get;
222
+ declare const index_match: typeof match;
223
+ declare namespace index {
224
+ export { type index_Client as Client, type index_GetParameters as GetParameters, index_HttpForbiddenError as HttpForbiddenError, index_HttpGetOffersFailedError as HttpGetOffersFailedError, index_HttpRateLimitError as HttpRateLimitError, index_HttpUnauthorizedError as HttpUnauthorizedError, index_InvalidUrlError as InvalidUrlError, type index_MatchParameters as MatchParameters, type index_RouterClientConfig as RouterClientConfig, index_connect as connect, index_get as get, index_match as match };
225
+ }
226
+
227
+ declare const types: readonly ["offer_created", "offer_matched", "offer_validation"];
228
+ type Type = (typeof types)[number];
229
+ type EventPayload = {
230
+ offer_created: {
231
+ readonly offer: RouterOffer;
232
+ };
233
+ offer_matched: {
234
+ readonly offer: RouterOffer;
235
+ };
236
+ offer_validation: {
237
+ readonly offer: RouterOffer;
238
+ };
239
+ };
240
+ type BaseEvent<T extends Type> = {
241
+ readonly id: string;
242
+ readonly type: T;
243
+ };
244
+ type RouterEvent = Compute<{
245
+ [K in keyof EventPayload]: BaseEvent<K> & EventPayload[K];
246
+ }[keyof EventPayload]>;
247
+ /**
248
+ * Builds a deterministic event id based on the event type.
249
+ * For `offer_created` it returns `offer_created:${offer.hash.toLowerCase()}`.
250
+ * For `offer_matched` it returns `offer_matched:${offer.hash.toLowerCase()}`.
251
+ */
252
+ declare function buildId(event: Omit<RouterEvent, "id">): string;
253
+
254
+ type RouterEvent$1_RouterEvent = RouterEvent;
255
+ type RouterEvent$1_Type = Type;
256
+ declare const RouterEvent$1_buildId: typeof buildId;
257
+ declare const RouterEvent$1_types: typeof types;
258
+ declare namespace RouterEvent$1 {
259
+ export { type RouterEvent$1_RouterEvent as RouterEvent, type RouterEvent$1_Type as Type, RouterEvent$1_buildId as buildId, RouterEvent$1_types as types };
260
+ }
261
+
262
+ /**
263
+ * Splits an array into batches of a specified size.
264
+ * @param array The array to split.
265
+ * @param batchSize The size of each batch.
266
+ * @returns An iterator that yields each batch.
267
+ * @example
268
+ * ```typescript
269
+ * const array = [1, 2, 3, 4, 5];
270
+ * for (const batch of batch(array, 2)) {
271
+ * console.log(batch);
272
+ * }
273
+ * // Output:
274
+ * // [1, 2]
275
+ * // [3, 4]
276
+ * // [5]
277
+ * ```
278
+ */
279
+ declare function batch$1<T>(array: Array<T>, batchSize: number): Generator<T[], void, unknown>;
280
+
281
+ /**
282
+ * Polls a function at a specified interval.
283
+ * Inspired by https://github.com/wevm/viem/blob/845994d20275d08ff892018e237a4b599eeefb6a/src/utils/poll.ts
284
+ */
285
+ declare function poll<data>(fn: ({ unpoll }: {
286
+ unpoll: () => void;
287
+ }) => Promise<data | undefined>, { interval }: {
288
+ interval: number;
289
+ }): () => boolean;
290
+
291
+ declare function wait(time: number): Promise<unknown>;
292
+
293
+ /**
294
+ * A validation rule.
295
+ */
296
+ type Rule<T, Name extends string = string, Ctx = void> = {
297
+ kind: "single";
298
+ name: Name;
299
+ run: Single<T, Name, Ctx>;
300
+ } | {
301
+ kind: "batch";
302
+ name: Name;
303
+ run: Batch<T, Name, Ctx>;
304
+ };
305
+ type RuleNames<Rules extends readonly {
306
+ name: string;
307
+ }[]> = Rules[number]["name"];
308
+ /**
309
+ * A single item validation rule.
310
+ * @param item - The item to validate.
311
+ * @param ctx - The context of the validation.
312
+ * @returns The issue that was found. If the item is valid, this will be undefined.
313
+ */
314
+ type Single<T, RuleName extends string, Ctx = void> = (item: T, ctx: Ctx) => Omit<Issue<T, RuleName>, "ruleName" | "item"> | undefined | Promise<Omit<Issue<T, RuleName>, "ruleName" | "item"> | undefined>;
315
+ /**
316
+ * A batch item validation rule.
317
+ * @param items - The items to validate.
318
+ * @param ctx - The context of the validation.
319
+ * @returns A map of the items to the issue that was found.
320
+ */
321
+ type Batch<T, RuleName extends string, Ctx = void> = (items: T[], ctx: Ctx) => Map<number, Omit<Issue<T, RuleName>, "ruleName" | "item"> | undefined> | Promise<Map<number, Omit<Issue<T, RuleName>, "ruleName" | "item"> | undefined>>;
322
+ /**
323
+ * Create a validation rule iterating over a single item at a time.
324
+ * @param name - The name of the rule.
325
+ * @param run - The function that validates the rule.
326
+ * @returns The created rule.
327
+ */
328
+ declare function single<Name extends string, T, Ctx = void>(name: Name, run: Single<T, Name, Ctx>): Rule<T, Name, Ctx>;
329
+ /**
330
+ * Create a validation rule iterating over a batch of items at a time.
331
+ * @param name - The name of the rule.
332
+ * @param run - The function that validates the rule.
333
+ * @returns The created rule.
334
+ */
335
+ declare function batch<Name extends string, T, Ctx = void>(name: Name, run: Batch<T, Name, Ctx>): Rule<T, Name, Ctx>;
336
+ type MorphoContext = {
337
+ publicClients: Partial<Record<ChainName, PublicClient>>;
338
+ };
339
+ declare function morpho(parameters: {
340
+ whitelistedChains: Chain[];
341
+ }): (Rule<{
342
+ readonly offering: Address;
343
+ readonly assets: bigint;
344
+ readonly rate: bigint;
345
+ readonly maturity: number;
346
+ readonly expiry: number;
347
+ readonly nonce: bigint;
348
+ readonly buy: boolean;
349
+ readonly chainId: bigint;
350
+ readonly loanToken: Address;
351
+ readonly start: number;
352
+ readonly collaterals: readonly {
353
+ asset: Address;
354
+ oracle: Address;
355
+ lltv: bigint;
356
+ }[];
357
+ readonly callback: {
358
+ readonly address: Address;
359
+ readonly data: viem.Hex;
360
+ readonly gasLimit: bigint;
361
+ };
362
+ readonly hash: viem.Hex;
363
+ signature?: viem.Hex;
364
+ createdAt?: number;
365
+ }, "chain_id", MorphoContext> | Rule<{
366
+ readonly offering: Address;
367
+ readonly assets: bigint;
368
+ readonly rate: bigint;
369
+ readonly maturity: number;
370
+ readonly expiry: number;
371
+ readonly nonce: bigint;
372
+ readonly buy: boolean;
373
+ readonly chainId: bigint;
374
+ readonly loanToken: Address;
375
+ readonly start: number;
376
+ readonly collaterals: readonly {
377
+ asset: Address;
378
+ oracle: Address;
379
+ lltv: bigint;
380
+ }[];
381
+ readonly callback: {
382
+ readonly address: Address;
383
+ readonly data: viem.Hex;
384
+ readonly gasLimit: bigint;
385
+ };
386
+ readonly hash: viem.Hex;
387
+ signature?: viem.Hex;
388
+ createdAt?: number;
389
+ }, "loan_token", MorphoContext> | Rule<{
390
+ readonly offering: Address;
391
+ readonly assets: bigint;
392
+ readonly rate: bigint;
393
+ readonly maturity: number;
394
+ readonly expiry: number;
395
+ readonly nonce: bigint;
396
+ readonly buy: boolean;
397
+ readonly chainId: bigint;
398
+ readonly loanToken: Address;
399
+ readonly start: number;
400
+ readonly collaterals: readonly {
401
+ asset: Address;
402
+ oracle: Address;
403
+ lltv: bigint;
404
+ }[];
405
+ readonly callback: {
406
+ readonly address: Address;
407
+ readonly data: viem.Hex;
408
+ readonly gasLimit: bigint;
409
+ };
410
+ readonly hash: viem.Hex;
411
+ signature?: viem.Hex;
412
+ createdAt?: number;
413
+ }, "expiry", MorphoContext> | Rule<{
414
+ readonly offering: Address;
415
+ readonly assets: bigint;
416
+ readonly rate: bigint;
417
+ readonly maturity: number;
418
+ readonly expiry: number;
419
+ readonly nonce: bigint;
420
+ readonly buy: boolean;
421
+ readonly chainId: bigint;
422
+ readonly loanToken: Address;
423
+ readonly start: number;
424
+ readonly collaterals: readonly {
425
+ asset: Address;
426
+ oracle: Address;
427
+ lltv: bigint;
428
+ }[];
429
+ readonly callback: {
430
+ readonly address: Address;
431
+ readonly data: viem.Hex;
432
+ readonly gasLimit: bigint;
433
+ };
434
+ readonly hash: viem.Hex;
435
+ signature?: viem.Hex;
436
+ createdAt?: number;
437
+ }, "empty_callback", MorphoContext> | Rule<{
438
+ readonly offering: Address;
439
+ readonly assets: bigint;
440
+ readonly rate: bigint;
441
+ readonly maturity: number;
442
+ readonly expiry: number;
443
+ readonly nonce: bigint;
444
+ readonly buy: boolean;
445
+ readonly chainId: bigint;
446
+ readonly loanToken: Address;
447
+ readonly start: number;
448
+ readonly collaterals: readonly {
449
+ asset: Address;
450
+ oracle: Address;
451
+ lltv: bigint;
452
+ }[];
453
+ readonly callback: {
454
+ readonly address: Address;
455
+ readonly data: viem.Hex;
456
+ readonly gasLimit: bigint;
457
+ };
458
+ readonly hash: viem.Hex;
459
+ signature?: viem.Hex;
460
+ createdAt?: number;
461
+ }, "sell_offers_empty_callback", MorphoContext> | Rule<{
462
+ readonly offering: Address;
463
+ readonly assets: bigint;
464
+ readonly rate: bigint;
465
+ readonly maturity: number;
466
+ readonly expiry: number;
467
+ readonly nonce: bigint;
468
+ readonly buy: boolean;
469
+ readonly chainId: bigint;
470
+ readonly loanToken: Address;
471
+ readonly start: number;
472
+ readonly collaterals: readonly {
473
+ asset: Address;
474
+ oracle: Address;
475
+ lltv: bigint;
476
+ }[];
477
+ readonly callback: {
478
+ readonly address: Address;
479
+ readonly data: viem.Hex;
480
+ readonly gasLimit: bigint;
481
+ };
482
+ readonly hash: viem.Hex;
483
+ signature?: viem.Hex;
484
+ createdAt?: number;
485
+ }, "buy_offers_empty_callback", MorphoContext>)[];
486
+
487
+ type ValidationRule_Batch<T, RuleName extends string, Ctx = void> = Batch<T, RuleName, Ctx>;
488
+ type ValidationRule_MorphoContext = MorphoContext;
489
+ type ValidationRule_Rule<T, Name extends string = string, Ctx = void> = Rule<T, Name, Ctx>;
490
+ type ValidationRule_RuleNames<Rules extends readonly {
491
+ name: string;
492
+ }[]> = RuleNames<Rules>;
493
+ type ValidationRule_Single<T, RuleName extends string, Ctx = void> = Single<T, RuleName, Ctx>;
494
+ declare const ValidationRule_batch: typeof batch;
495
+ declare const ValidationRule_morpho: typeof morpho;
496
+ declare const ValidationRule_single: typeof single;
497
+ declare namespace ValidationRule {
498
+ export { type ValidationRule_Batch as Batch, type ValidationRule_MorphoContext as MorphoContext, type ValidationRule_Rule as Rule, type ValidationRule_RuleNames as RuleNames, type ValidationRule_Single as Single, ValidationRule_batch as batch, ValidationRule_morpho as morpho, ValidationRule_single as single };
499
+ }
500
+
501
+ /**
502
+ * A validation issue.
503
+ */
504
+ type Issue<T, RuleName extends string = string> = {
505
+ /** The name of the rule that caused the issue. */
506
+ ruleName: RuleName;
507
+ /** The message of the issue. */
508
+ message: string;
509
+ /** The item that was not valid. */
510
+ item: T;
511
+ };
512
+ /**
513
+ * The result of a validation.
514
+ */
515
+ type Result<T, RuleName extends string = string> = {
516
+ /** The items that were valid. */
517
+ valid: T[];
518
+ /** The reports of the failed validations. */
519
+ issues: Issue<T, RuleName>[];
520
+ };
521
+ declare function run<T, Name extends string, Rules extends readonly Rule<T, Name, void>[]>(parameters: {
522
+ items: T[];
523
+ rules: Rules;
524
+ chunkSize?: number;
525
+ }): Promise<Result<T, RuleNames<Rules>>>;
526
+ declare function run<T, Ctx, Name extends string, Rules extends readonly Rule<T, Name, Ctx>[]>(parameters: {
527
+ items: T[];
528
+ rules: Rules;
529
+ ctx: Ctx;
530
+ chunkSize?: number;
531
+ }): Promise<Result<T, RuleNames<Rules>>>;
532
+
533
+ type Validation_Issue<T, RuleName extends string = string> = Issue<T, RuleName>;
534
+ type Validation_Result<T, RuleName extends string = string> = Result<T, RuleName>;
535
+ declare const Validation_run: typeof run;
536
+ declare namespace Validation {
537
+ export { type Validation_Issue as Issue, type Validation_Result as Result, Validation_run as run };
538
+ }
539
+
540
+ export { Chain$1 as Chain, type Compute, index as Router, RouterEvent$1 as RouterEvent, RouterOffer$1 as RouterOffer, Validation, ValidationRule, batch$1 as batch, poll, wait };