@morpho-dev/router 0.1.10 → 0.1.11

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, Utils, Offer, Format } from '@morpho-dev/mempool';
1
+ import { Errors, LLTV, Offer, Utils, Format } from '@morpho-dev/mempool';
2
2
  export * from '@morpho-dev/mempool';
3
3
  import { parseUnits, encodeAbiParameters, maxUint256, formatUnits, decodeAbiParameters, erc20Abi } from 'viem';
4
4
  import { Base64 } from 'js-base64';
@@ -34,46 +34,95 @@ var WhitelistedCallbackAddresses = {
34
34
  "0x1111111111111111111111111111111111111111",
35
35
  "0x2222222222222222222222222222222222222222"
36
36
  // @TODO: update once deployed and add mapping per chain if needed
37
- ]
37
+ ].map((address) => address.toLowerCase())
38
38
  };
39
39
  function buildLiquidity(parameters) {
40
- const { type, user, contract, chainId, amount, index = 0, updatedAt = /* @__PURE__ */ new Date() } = parameters;
41
- if (type !== "buy_with_empty_callback" /* BuyWithEmptyCallback */)
42
- throw new Error(`CallbackType not implemented: ${type}`);
43
- const amountStr = amount.toString();
44
- const id = `${user}-${chainId.toString()}-${type}-${contract}`.toLowerCase();
45
- return {
46
- userPosition: {
47
- id,
48
- availableLiquidityQueueId: id,
49
- user: user.toLowerCase(),
50
- chainId,
51
- amount: amountStr,
52
- updatedAt
53
- },
54
- queues: [
55
- {
56
- queue: {
57
- queueId: id,
58
- availableLiquidityPoolId: id,
59
- index,
40
+ switch (parameters.type) {
41
+ case "buy_with_empty_callback" /* BuyWithEmptyCallback */: {
42
+ const { user, loanToken, chainId, amount, index = 0, updatedAt = /* @__PURE__ */ new Date() } = parameters;
43
+ const amountStr = amount.toString();
44
+ const id = `${user}-${chainId.toString()}-${parameters.type}-${loanToken}`.toLowerCase();
45
+ const poolId = `${user}-${chainId.toString()}-${loanToken}`.toLowerCase();
46
+ return {
47
+ userPosition: {
48
+ id,
49
+ availableLiquidityQueueId: id,
50
+ user: user.toLowerCase(),
51
+ chainId,
52
+ amount: amountStr,
60
53
  updatedAt
61
54
  },
62
- pool: {
55
+ queues: [
56
+ {
57
+ queue: {
58
+ queueId: id,
59
+ availableLiquidityPoolId: poolId,
60
+ index,
61
+ callbackAmount: "0",
62
+ updatedAt
63
+ },
64
+ pool: {
65
+ id: poolId,
66
+ amount: amountStr,
67
+ updatedAt
68
+ }
69
+ }
70
+ ]
71
+ };
72
+ }
73
+ case "sell_withdraw_from_wallet" /* SellWithdrawFromWallet */: {
74
+ const {
75
+ user,
76
+ termId,
77
+ chainId,
78
+ amount,
79
+ collaterals,
80
+ index = 0,
81
+ updatedAt = /* @__PURE__ */ new Date()
82
+ } = parameters;
83
+ const amountStr = amount.toString();
84
+ const id = `${user}-${chainId.toString()}-${parameters.type}-${termId}`.toLowerCase();
85
+ return {
86
+ userPosition: {
63
87
  id,
88
+ availableLiquidityQueueId: id,
89
+ user: user.toLowerCase(),
90
+ chainId,
64
91
  amount: amountStr,
65
92
  updatedAt
66
- }
67
- }
68
- ]
69
- };
93
+ },
94
+ queues: collaterals.map((collateral) => {
95
+ const poolId = `${user}-${chainId.toString()}-${collateral.collateralAddress}`.toLowerCase();
96
+ return {
97
+ queue: {
98
+ queueId: id,
99
+ availableLiquidityPoolId: poolId,
100
+ index,
101
+ callbackAmount: collateral.callbackAmount.toString(),
102
+ updatedAt
103
+ },
104
+ pool: {
105
+ id: poolId,
106
+ amount: collateral.balance.toString(),
107
+ updatedAt
108
+ }
109
+ };
110
+ })
111
+ };
112
+ }
113
+ default: {
114
+ throw new Error(`CallbackType not implemented`);
115
+ }
116
+ }
70
117
  }
71
118
  function getCallbackIdForOffer(offer) {
72
119
  if (offer.buy && offer.callback.data === "0x") {
73
- const type = "buy_with_empty_callback" /* BuyWithEmptyCallback */;
74
- const user = offer.offering;
75
- const loanToken = offer.loanToken;
76
- return `${user}-${offer.chainId.toString()}-${type}-${loanToken}`.toLowerCase();
120
+ return `${offer.offering}-${offer.chainId.toString()}-${"buy_with_empty_callback" /* BuyWithEmptyCallback */}-${offer.loanToken}`.toLowerCase();
121
+ }
122
+ if (!offer.buy && offer.callback.data !== "0x" && WhitelistedCallbackAddresses["sell_withdraw_from_wallet" /* SellWithdrawFromWallet */].includes(
123
+ offer.callback.address.toLowerCase()
124
+ )) {
125
+ return `${offer.offering}-${offer.chainId.toString()}-${"sell_withdraw_from_wallet" /* SellWithdrawFromWallet */}-${Offer.termId(offer)}`.toLowerCase();
77
126
  }
78
127
  return null;
79
128
  }
@@ -269,20 +318,20 @@ async function fetch2(parameters) {
269
318
  const map = await fetchBalancesAndAllowances({
270
319
  client,
271
320
  spender,
272
- pairs: pairs.map(({ user, contract }) => ({ user, token: contract })),
321
+ pairs: pairs.map(({ user, loanToken }) => ({ user, token: loanToken })),
273
322
  options
274
323
  });
275
324
  const out = [];
276
- for (const [user, perContract] of map) {
277
- for (const [contract, { balance, allowance }] of perContract) {
325
+ for (const [user, perLoanToken] of map) {
326
+ for (const [loanToken, { balance, allowance }] of perLoanToken) {
278
327
  const amount = balance < allowance ? balance : allowance;
279
328
  out.push(
280
329
  buildLiquidity({
281
330
  type,
282
331
  user,
283
- contract,
332
+ loanToken,
284
333
  chainId,
285
- amount: amount.toString(),
334
+ amount,
286
335
  index: 0
287
336
  })
288
337
  );