@morpho-dev/router 0.1.7 → 0.1.8

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.
@@ -14,7 +14,7 @@ var __export = (target, all) => {
14
14
  };
15
15
  var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
16
16
 
17
- // src/Callback.ts
17
+ // src/core/Callback.ts
18
18
  var Callback_exports = {};
19
19
  __export(Callback_exports, {
20
20
  CallbackType: () => CallbackType,
@@ -67,7 +67,7 @@ function getCallbackIdForOffer(offer) {
67
67
  return null;
68
68
  }
69
69
 
70
- // src/Cursor.ts
70
+ // src/core/Cursor.ts
71
71
  var Cursor_exports = {};
72
72
  __export(Cursor_exports, {
73
73
  decode: () => decode,
@@ -153,20 +153,141 @@ function decode(token) {
153
153
  return decoded;
154
154
  }
155
155
 
156
- // src/core/router/Client.ts
157
- var Client_exports = {};
158
- __export(Client_exports, {
159
- HttpForbiddenError: () => HttpForbiddenError,
160
- HttpGetOffersFailedError: () => HttpGetOffersFailedError,
161
- HttpRateLimitError: () => HttpRateLimitError,
162
- HttpUnauthorizedError: () => HttpUnauthorizedError,
163
- InvalidUrlError: () => InvalidUrlError,
164
- connect: () => connect,
165
- get: () => get,
166
- match: () => match
156
+ // src/core/Liquidity.ts
157
+ var Liquidity_exports = {};
158
+ __export(Liquidity_exports, {
159
+ fetch: () => fetch2,
160
+ fetchBalancesAndAllowances: () => fetchBalancesAndAllowances,
161
+ serialize: () => serialize
167
162
  });
163
+ async function fetchBalancesAndAllowances(parameters) {
164
+ const { client, spender, pairs, options } = parameters;
165
+ if (pairs.length === 0) return /* @__PURE__ */ new Map();
166
+ const batchSize = Math.max(1, options?.batchSize ?? 5e3);
167
+ const retryAttempts = Math.max(1, options?.retryAttempts ?? 3);
168
+ const retryDelayMs = Math.max(0, options?.retryDelayMs ?? 50);
169
+ const blockNumber = options?.blockNumber ? BigInt(options.blockNumber) : void 0;
170
+ const out = /* @__PURE__ */ new Map();
171
+ for (const pairsBatch of mempool.Utils.batch(pairs, batchSize)) {
172
+ const balanceContracts = [];
173
+ const allowanceContracts = [];
174
+ for (const { user, token } of pairsBatch) {
175
+ balanceContracts.push({
176
+ address: token,
177
+ abi: viem.erc20Abi,
178
+ functionName: "balanceOf",
179
+ args: [user]
180
+ });
181
+ allowanceContracts.push({
182
+ address: token,
183
+ abi: viem.erc20Abi,
184
+ functionName: "allowance",
185
+ args: [user, spender]
186
+ });
187
+ }
188
+ const [balances, allowances] = await Promise.all([
189
+ mempool.Utils.retry(
190
+ () => client.multicall({
191
+ allowFailure: false,
192
+ contracts: balanceContracts,
193
+ ...blockNumber ? { blockNumber } : {}
194
+ }),
195
+ retryAttempts,
196
+ retryDelayMs
197
+ ),
198
+ mempool.Utils.retry(
199
+ () => client.multicall({
200
+ allowFailure: false,
201
+ contracts: allowanceContracts,
202
+ ...blockNumber ? { blockNumber } : {}
203
+ }),
204
+ retryAttempts,
205
+ retryDelayMs
206
+ )
207
+ ]);
208
+ for (let i = 0; i < pairsBatch.length; i++) {
209
+ const { user, token } = pairsBatch[i];
210
+ const balance = balances[i];
211
+ const allowance = allowances[i];
212
+ let perUser = out.get(user);
213
+ if (!perUser) {
214
+ perUser = /* @__PURE__ */ new Map();
215
+ out.set(user, perUser);
216
+ }
217
+ perUser.set(token, { balance, allowance });
218
+ }
219
+ }
220
+ return out;
221
+ }
222
+ async function fetch2(parameters) {
223
+ const { client, chainId, spender, type, pairs, options } = parameters;
224
+ if (type !== "buy_with_empty_callback" /* BuyWithEmptyCallback */)
225
+ throw new Error(`CallbackType not implemented: ${type}`);
226
+ const map = await fetchBalancesAndAllowances({
227
+ client,
228
+ spender,
229
+ pairs: pairs.map(({ user, contract }) => ({ user, token: contract })),
230
+ options
231
+ });
232
+ const out = [];
233
+ for (const [user, perContract] of map) {
234
+ for (const [contract, { balance, allowance }] of perContract) {
235
+ const amount = balance < allowance ? balance : allowance;
236
+ out.push(
237
+ buildLiquidity({
238
+ type,
239
+ user,
240
+ contract,
241
+ chainId,
242
+ amount: amount.toString(),
243
+ index: 0
244
+ })
245
+ );
246
+ }
247
+ }
248
+ return out;
249
+ }
250
+ function serialize(liquidity) {
251
+ const normalized = {
252
+ userPosition: {
253
+ id: liquidity.userPosition.id,
254
+ availableLiquidityQueueId: liquidity.userPosition.availableLiquidityQueueId,
255
+ user: liquidity.userPosition.user,
256
+ chainId: String(liquidity.userPosition.chainId),
257
+ amount: String(liquidity.userPosition.amount)
258
+ },
259
+ queues: liquidity.queues.map((queueWithPool) => ({
260
+ queue: {
261
+ queueId: queueWithPool.queue.queueId,
262
+ availableLiquidityPoolId: queueWithPool.queue.availableLiquidityPoolId,
263
+ index: queueWithPool.queue.index
264
+ },
265
+ pool: {
266
+ id: queueWithPool.pool.id,
267
+ amount: String(queueWithPool.pool.amount)
268
+ }
269
+ })).sort(
270
+ (left, right) => {
271
+ const leftQueueId = left.queue.queueId || "";
272
+ const rightQueueId = right.queue.queueId || "";
273
+ if (leftQueueId < rightQueueId) return -1;
274
+ if (leftQueueId > rightQueueId) return 1;
275
+ const leftPoolId = left.pool.id;
276
+ const rightPoolId = right.pool.id;
277
+ if (leftPoolId < rightPoolId) return -1;
278
+ if (leftPoolId > rightPoolId) return 1;
279
+ const leftIndex = left.queue.index;
280
+ const rightIndex = right.queue.index;
281
+ if (leftIndex < rightIndex) return -1;
282
+ if (leftIndex > rightIndex) return 1;
283
+ return 0;
284
+ }
285
+ )
286
+ };
287
+ return JSON.stringify(normalized);
288
+ }
168
289
 
169
- // src/RouterOffer.ts
290
+ // src/core/RouterOffer.ts
170
291
  var RouterOffer_exports = {};
171
292
  __export(RouterOffer_exports, {
172
293
  InvalidRouterOfferError: () => InvalidRouterOfferError,
@@ -246,7 +367,18 @@ var InvalidRouterOfferError = class extends mempool.Errors.BaseError {
246
367
  }
247
368
  };
248
369
 
249
- // src/core/apiSchema/requests.ts
370
+ // src/core/router/Client.ts
371
+ var Client_exports = {};
372
+ __export(Client_exports, {
373
+ HttpForbiddenError: () => HttpForbiddenError,
374
+ HttpGetOffersFailedError: () => HttpGetOffersFailedError,
375
+ HttpRateLimitError: () => HttpRateLimitError,
376
+ HttpUnauthorizedError: () => HttpUnauthorizedError,
377
+ InvalidUrlError: () => InvalidUrlError,
378
+ connect: () => connect,
379
+ get: () => get,
380
+ match: () => match
381
+ });
250
382
  var MAX_LIMIT = 100;
251
383
  var DEFAULT_LIMIT = 20;
252
384
  var MAX_LLTV = 100;
@@ -988,141 +1120,7 @@ var HttpGetOffersFailedError = class extends mempool.Errors.BaseError {
988
1120
  }
989
1121
  };
990
1122
 
991
- // src/Liquidity.ts
992
- var Liquidity_exports = {};
993
- __export(Liquidity_exports, {
994
- fetch: () => fetch2,
995
- fetchBalancesAndAllowances: () => fetchBalancesAndAllowances,
996
- serialize: () => serialize
997
- });
998
- async function fetchBalancesAndAllowances(parameters) {
999
- const { client, spender, pairs, options } = parameters;
1000
- if (pairs.length === 0) return /* @__PURE__ */ new Map();
1001
- const batchSize = Math.max(1, options?.batchSize ?? 5e3);
1002
- const retryAttempts = Math.max(1, options?.retryAttempts ?? 3);
1003
- const retryDelayMs = Math.max(0, options?.retryDelayMs ?? 50);
1004
- const blockNumber = options?.blockNumber ? BigInt(options.blockNumber) : void 0;
1005
- const out = /* @__PURE__ */ new Map();
1006
- for (const pairsBatch of mempool.Utils.batch(pairs, batchSize)) {
1007
- const balanceContracts = [];
1008
- const allowanceContracts = [];
1009
- for (const { user, token } of pairsBatch) {
1010
- balanceContracts.push({
1011
- address: token,
1012
- abi: viem.erc20Abi,
1013
- functionName: "balanceOf",
1014
- args: [user]
1015
- });
1016
- allowanceContracts.push({
1017
- address: token,
1018
- abi: viem.erc20Abi,
1019
- functionName: "allowance",
1020
- args: [user, spender]
1021
- });
1022
- }
1023
- const [balances, allowances] = await Promise.all([
1024
- mempool.Utils.retry(
1025
- () => client.multicall({
1026
- allowFailure: false,
1027
- contracts: balanceContracts,
1028
- ...blockNumber ? { blockNumber } : {}
1029
- }),
1030
- retryAttempts,
1031
- retryDelayMs
1032
- ),
1033
- mempool.Utils.retry(
1034
- () => client.multicall({
1035
- allowFailure: false,
1036
- contracts: allowanceContracts,
1037
- ...blockNumber ? { blockNumber } : {}
1038
- }),
1039
- retryAttempts,
1040
- retryDelayMs
1041
- )
1042
- ]);
1043
- for (let i = 0; i < pairsBatch.length; i++) {
1044
- const { user, token } = pairsBatch[i];
1045
- const balance = balances[i];
1046
- const allowance = allowances[i];
1047
- let perUser = out.get(user);
1048
- if (!perUser) {
1049
- perUser = /* @__PURE__ */ new Map();
1050
- out.set(user, perUser);
1051
- }
1052
- perUser.set(token, { balance, allowance });
1053
- }
1054
- }
1055
- return out;
1056
- }
1057
- async function fetch2(parameters) {
1058
- const { client, chainId, spender, type, pairs, options } = parameters;
1059
- if (type !== "buy_with_empty_callback" /* BuyWithEmptyCallback */)
1060
- throw new Error(`CallbackType not implemented: ${type}`);
1061
- const map = await fetchBalancesAndAllowances({
1062
- client,
1063
- spender,
1064
- pairs: pairs.map(({ user, contract }) => ({ user, token: contract })),
1065
- options
1066
- });
1067
- const out = [];
1068
- for (const [user, perContract] of map) {
1069
- for (const [contract, { balance, allowance }] of perContract) {
1070
- const amount = balance < allowance ? balance : allowance;
1071
- out.push(
1072
- buildLiquidity({
1073
- type,
1074
- user,
1075
- contract,
1076
- chainId,
1077
- amount: amount.toString(),
1078
- index: 0
1079
- })
1080
- );
1081
- }
1082
- }
1083
- return out;
1084
- }
1085
- function serialize(liquidity) {
1086
- const normalized = {
1087
- userPosition: {
1088
- id: liquidity.userPosition.id,
1089
- availableLiquidityQueueId: liquidity.userPosition.availableLiquidityQueueId,
1090
- user: liquidity.userPosition.user,
1091
- chainId: String(liquidity.userPosition.chainId),
1092
- amount: String(liquidity.userPosition.amount)
1093
- },
1094
- queues: liquidity.queues.map((queueWithPool) => ({
1095
- queue: {
1096
- queueId: queueWithPool.queue.queueId,
1097
- availableLiquidityPoolId: queueWithPool.queue.availableLiquidityPoolId,
1098
- index: queueWithPool.queue.index
1099
- },
1100
- pool: {
1101
- id: queueWithPool.pool.id,
1102
- amount: String(queueWithPool.pool.amount)
1103
- }
1104
- })).sort(
1105
- (left, right) => {
1106
- const leftQueueId = left.queue.queueId || "";
1107
- const rightQueueId = right.queue.queueId || "";
1108
- if (leftQueueId < rightQueueId) return -1;
1109
- if (leftQueueId > rightQueueId) return 1;
1110
- const leftPoolId = left.pool.id;
1111
- const rightPoolId = right.pool.id;
1112
- if (leftPoolId < rightPoolId) return -1;
1113
- if (leftPoolId > rightPoolId) return 1;
1114
- const leftIndex = left.queue.index;
1115
- const rightIndex = right.queue.index;
1116
- if (leftIndex < rightIndex) return -1;
1117
- if (leftIndex > rightIndex) return 1;
1118
- return 0;
1119
- }
1120
- )
1121
- };
1122
- return JSON.stringify(normalized);
1123
- }
1124
-
1125
- // src/Validation.ts
1123
+ // src/core/Validation.ts
1126
1124
  var Validation_exports = {};
1127
1125
  __export(Validation_exports, {
1128
1126
  run: () => run
@@ -1169,7 +1167,7 @@ async function run(parameters) {
1169
1167
  };
1170
1168
  }
1171
1169
 
1172
- // src/ValidationRule.ts
1170
+ // src/core/ValidationRule.ts
1173
1171
  var ValidationRule_exports = {};
1174
1172
  __export(ValidationRule_exports, {
1175
1173
  batch: () => batch,