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