@morpho-dev/router 0.1.7 → 0.1.9

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