@suilend/sdk 2.0.3 → 3.0.1

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.
Files changed (73) hide show
  1. package/_generated/_dependencies/source/0x1/ascii/structs.d.ts +7 -11
  2. package/_generated/_dependencies/source/0x1/ascii/structs.js +6 -63
  3. package/_generated/_dependencies/source/0x1/option/structs.d.ts +3 -9
  4. package/_generated/_dependencies/source/0x1/option/structs.js +4 -42
  5. package/_generated/_dependencies/source/0x1/type-name/structs.d.ts +5 -7
  6. package/_generated/_dependencies/source/0x1/type-name/structs.js +3 -32
  7. package/_generated/_dependencies/source/0x2/bag/structs.d.ts +7 -9
  8. package/_generated/_dependencies/source/0x2/bag/structs.js +3 -31
  9. package/_generated/_dependencies/source/0x2/balance/structs.d.ts +7 -11
  10. package/_generated/_dependencies/source/0x2/balance/structs.js +7 -82
  11. package/_generated/_dependencies/source/0x2/object/structs.d.ts +8 -12
  12. package/_generated/_dependencies/source/0x2/object/structs.js +7 -61
  13. package/_generated/_dependencies/source/0x2/object-table/structs.d.ts +7 -9
  14. package/_generated/_dependencies/source/0x2/object-table/structs.js +4 -44
  15. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.d.ts +5 -7
  16. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.js +3 -31
  17. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.d.ts +11 -13
  18. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.js +3 -32
  19. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.d.ts +23 -25
  20. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.js +3 -32
  21. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.d.ts +4 -6
  22. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.js +3 -33
  23. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.d.ts +55 -59
  24. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.js +6 -64
  25. package/_generated/_framework/reified.d.ts +2 -4
  26. package/_generated/_framework/vector.d.ts +2 -12
  27. package/_generated/suilend/cell/structs.d.ts +4 -10
  28. package/_generated/suilend/cell/structs.js +4 -42
  29. package/_generated/suilend/decimal/structs.d.ts +4 -6
  30. package/_generated/suilend/decimal/structs.js +3 -32
  31. package/_generated/suilend/lending-market/structs.d.ts +152 -392
  32. package/_generated/suilend/lending-market/structs.js +50 -548
  33. package/_generated/suilend/liquidity-mining/structs.d.ts +50 -158
  34. package/_generated/suilend/liquidity-mining/structs.js +16 -169
  35. package/_generated/suilend/obligation/structs.d.ts +108 -148
  36. package/_generated/suilend/obligation/structs.js +23 -232
  37. package/_generated/suilend/rate-limiter/structs.d.ts +15 -19
  38. package/_generated/suilend/rate-limiter/structs.js +6 -64
  39. package/_generated/suilend/reserve/structs.d.ts +138 -362
  40. package/_generated/suilend/reserve/structs.js +29 -327
  41. package/_generated/suilend/reserve-config/structs.d.ts +33 -37
  42. package/_generated/suilend/reserve-config/structs.js +6 -65
  43. package/client.d.ts +8 -8
  44. package/client.js +55 -88
  45. package/lib/initialize.d.ts +29 -28
  46. package/lib/initialize.js +16 -12
  47. package/lib/liquidityMining.d.ts +3 -3
  48. package/lib/pythAdapter.d.ts +9 -0
  49. package/lib/pythAdapter.js +121 -0
  50. package/lib/strategyOwnerCap.d.ts +12 -7
  51. package/lib/strategyOwnerCap.js +159 -156
  52. package/margin/margin/admin_cap.d.ts +2 -2
  53. package/margin/margin/deps/std/type_name.d.ts +1 -1
  54. package/margin/margin/deps/sui/vec_set.d.ts +3 -5
  55. package/margin/margin/deps/suilend/lending_market.d.ts +2 -2
  56. package/margin/margin/market.d.ts +21 -38
  57. package/margin/margin/position.d.ts +47 -47
  58. package/margin/margin/router.d.ts +5 -5
  59. package/margin/margin/version.d.ts +1 -1
  60. package/package.json +1 -1
  61. package/parsers/lendingMarket.d.ts +6 -6
  62. package/parsers/obligation.d.ts +8 -8
  63. package/parsers/reserve.d.ts +8 -8
  64. package/strategies.d.ts +38 -21
  65. package/strategies.js +239 -79
  66. package/swap/quote.d.ts +1 -9
  67. package/swap/quote.js +1 -52
  68. package/swap/transaction.d.ts +2 -4
  69. package/swap/transaction.js +9 -25
  70. package/utils/events.d.ts +6 -5
  71. package/utils/events.js +11 -8
  72. package/utils/obligation.d.ts +6 -5
  73. package/utils/obligation.js +118 -117
@@ -7,45 +7,37 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { fromBase64 } from "@mysten/sui/utils";
11
10
  import { BigNumber } from "bignumber.js";
12
11
  import pLimit from "p-limit";
13
12
  import { phantom } from "../_generated/_framework/reified";
14
13
  import { PACKAGE_ID } from "../_generated/suilend";
15
14
  import { Obligation } from "../_generated/suilend/obligation/structs";
16
- import * as parsed from "./events";
17
- export function fetchAllObligationsForMarketWithHandler(client, lendingMarketId, lendingMarketType, chunkHandler) {
15
+ export function fetchAllObligationsForMarketWithHandler(suiGrpcClient, lendingMarketId, lendingMarketType, chunkHandler) {
18
16
  return __awaiter(this, void 0, void 0, function* () {
19
- var _a;
20
17
  const limit = pLimit(30);
21
- const rawLendingMarket = yield client.getObject({
22
- id: lendingMarketId,
23
- options: {
24
- showType: true,
25
- showContent: true,
26
- showOwner: true,
27
- showBcs: true,
28
- },
18
+ const { object: rawLendingMarket } = yield suiGrpcClient.getObject({
19
+ objectId: lendingMarketId,
20
+ include: { json: true },
29
21
  });
30
- const obligationField = ((_a = rawLendingMarket.data) === null || _a === void 0 ? void 0 : _a.content).fields
31
- .obligations;
22
+ const obligationField = rawLendingMarket.json.fields.obligations;
32
23
  const obligationOwnerID = obligationField.fields.id.id;
33
24
  let hasNextPage = true;
34
25
  let cursor = null;
35
26
  const promises = [];
36
27
  while (hasNextPage) {
37
- const response = yield client.getDynamicFields({
28
+ const response = yield suiGrpcClient.listDynamicFields({
38
29
  parentId: obligationOwnerID,
39
30
  cursor: cursor,
40
31
  });
41
32
  hasNextPage = response.hasNextPage;
42
- cursor = response.nextCursor;
33
+ cursor = response.cursor;
43
34
  promises.push(limit(() => __awaiter(this, void 0, void 0, function* () {
44
- var _a;
45
- const obligationObjects = yield chunkedMultiGet(client, response.data.map((x) => x.objectId));
35
+ const obligationObjects = yield chunkedMultiGet(suiGrpcClient, response.dynamicFields.map((x) => x.fieldId));
46
36
  const obligations = [];
47
37
  for (const rawObligation of obligationObjects) {
48
- obligations.push(Obligation.fromBcs(phantom(lendingMarketType), fromBase64(((_a = rawObligation.data) === null || _a === void 0 ? void 0 : _a.bcs).bcsBytes)));
38
+ if (rawObligation instanceof Error)
39
+ continue;
40
+ obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
49
41
  }
50
42
  yield chunkHandler(obligations);
51
43
  })));
@@ -53,60 +45,50 @@ export function fetchAllObligationsForMarketWithHandler(client, lendingMarketId,
53
45
  yield Promise.all(promises);
54
46
  });
55
47
  }
56
- export function fetchAllObligationsForMarket(client, lendingMarketId, lendingMarketType) {
48
+ export function fetchAllObligationsForMarket(suiGrpcClient, lendingMarketId, lendingMarketType) {
57
49
  return __awaiter(this, void 0, void 0, function* () {
58
- var _a, _b;
59
- const rawLendingMarket = yield client.getObject({
60
- id: lendingMarketId,
61
- options: {
62
- showType: true,
63
- showContent: true,
64
- showOwner: true,
65
- showBcs: true,
66
- },
50
+ const { object: rawLendingMarket } = yield suiGrpcClient.getObject({
51
+ objectId: lendingMarketId,
52
+ include: { json: true },
67
53
  });
68
- const obligationField = ((_a = rawLendingMarket.data) === null || _a === void 0 ? void 0 : _a.content).fields
69
- .obligations;
54
+ const obligationField = rawLendingMarket.json.fields.obligations;
70
55
  const obligationOwnerID = obligationField.fields.id.id;
71
56
  let hasNextPage = true;
72
57
  let cursor = null;
73
58
  let fields = [];
74
59
  while (hasNextPage) {
75
- const response = yield client.getDynamicFields({
60
+ const response = yield suiGrpcClient.listDynamicFields({
76
61
  parentId: obligationOwnerID,
77
62
  cursor: cursor,
78
63
  });
79
- fields = fields.concat(response.data);
64
+ fields = fields.concat(response.dynamicFields);
80
65
  hasNextPage = response.hasNextPage;
81
- cursor = response.nextCursor;
66
+ cursor = response.cursor;
82
67
  }
83
- const obligationObjects = yield chunkedMultiGet(client, fields.map((x) => x.objectId));
68
+ const obligationObjects = yield chunkedMultiGet(suiGrpcClient, fields.map((x) => x.fieldId));
84
69
  const obligations = [];
85
70
  for (const rawObligation of obligationObjects) {
86
- obligations.push(Obligation.fromBcs(phantom(lendingMarketType), fromBase64(((_b = rawObligation.data) === null || _b === void 0 ? void 0 : _b.bcs).bcsBytes)));
71
+ if (rawObligation instanceof Error)
72
+ continue;
73
+ obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
87
74
  }
88
75
  return obligations;
89
76
  });
90
77
  }
91
- function chunkedMultiGet(client, objectIds) {
78
+ function chunkedMultiGet(suiGrpcClient, objectIds) {
92
79
  return __awaiter(this, void 0, void 0, function* () {
93
80
  const limit = pLimit(30);
94
81
  const results = [];
95
82
  const chunks = splitIntoChunks(objectIds, 50);
96
83
  const promises = [];
97
84
  for (const chunk of chunks) {
98
- promises.push(limit(() => client.multiGetObjects({
99
- ids: chunk,
100
- options: {
101
- showType: true,
102
- showContent: true,
103
- showOwner: true,
104
- showBcs: true,
105
- },
85
+ promises.push(limit(() => suiGrpcClient.getObjects({
86
+ objectIds: chunk,
87
+ include: { content: true },
106
88
  })));
107
89
  }
108
90
  for (const chunk of yield Promise.all(promises)) {
109
- for (const result of chunk) {
91
+ for (const result of chunk.objects) {
110
92
  results.push(result);
111
93
  }
112
94
  }
@@ -121,145 +103,164 @@ function splitIntoChunks(arr, chunkSize) {
121
103
  }
122
104
  return result;
123
105
  }
124
- export function getObligationHistoryPage(client, obligationId, maxQuantity, cursor) {
106
+ export function getObligationHistoryPage(suiGraphQLClient, obligationId, maxQuantity, cursor) {
125
107
  return __awaiter(this, void 0, void 0, function* () {
126
108
  var _a;
127
109
  if (maxQuantity > 50 || maxQuantity < 1) {
128
110
  throw Error("maxQuantity must be between 1 and 50");
129
111
  }
130
- const payload = yield client.queryTransactionBlocks({
131
- cursor: cursor,
132
- limit: maxQuantity,
133
- order: "descending",
134
- filter: {
135
- ChangedObject: obligationId,
136
- },
137
- options: {
138
- showEffects: true,
139
- showEvents: true,
112
+ const result = yield suiGraphQLClient.query({
113
+ query: `
114
+ query QueryObligationTransactions($objectId: SuiAddress!, $first: Int, $after: String) {
115
+ transactionBlocks(
116
+ first: $first
117
+ after: $after
118
+ filter: { changedObject: $objectId }
119
+ ) {
120
+ pageInfo {
121
+ hasNextPage
122
+ endCursor
123
+ }
124
+ nodes {
125
+ digest
126
+ effects {
127
+ timestamp
128
+ events(first: 50) {
129
+ nodes {
130
+ sendingModule {
131
+ package { address }
132
+ name
133
+ }
134
+ type { repr }
135
+ json
136
+ }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ `,
143
+ variables: {
144
+ objectId: obligationId,
145
+ first: maxQuantity,
146
+ after: cursor,
140
147
  },
141
148
  });
142
- let formattedEvents = [];
143
- for (const data of payload.data) {
144
- const events = ((_a = data.events) === null || _a === void 0 ? void 0 : _a.filter((e) => e.packageId === PACKAGE_ID)) || [];
145
- formattedEvents = formattedEvents.concat(formatEventsToHistory(obligationId, data, events));
149
+ const payload = (_a = result.data) === null || _a === void 0 ? void 0 : _a.transactionBlocks;
150
+ if (!payload) {
151
+ return { cursor: null, history: [] };
146
152
  }
147
- if (payload.hasNextPage) {
148
- return {
149
- cursor: payload.nextCursor,
150
- history: formattedEvents,
151
- };
153
+ let formattedEvents = [];
154
+ for (const txNode of payload.nodes) {
155
+ const events = txNode.effects.events.nodes.filter((e) => { var _a, _b; return ((_b = (_a = e.sendingModule) === null || _a === void 0 ? void 0 : _a.package) === null || _b === void 0 ? void 0 : _b.address) === PACKAGE_ID; });
156
+ formattedEvents = formattedEvents.concat(formatEventsToHistory(obligationId, txNode, events));
152
157
  }
153
158
  return {
154
- cursor: null,
159
+ cursor: payload.pageInfo.hasNextPage ? payload.pageInfo.endCursor : null,
155
160
  history: formattedEvents,
156
161
  };
157
162
  });
158
163
  }
159
- function formatEventsToHistory(obligationId, data, events) {
164
+ function formatEventsToHistory(obligationId, txNode, events) {
165
+ var _a;
160
166
  const formattedEvents = [];
161
- for (const [, event] of events.entries()) {
162
- const eventComponents = event.type.split("::");
163
- const eventType = eventComponents[eventComponents.length - 1];
167
+ const timestampMs = txNode.effects.timestamp
168
+ ? new Date(txNode.effects.timestamp).getTime()
169
+ : 0;
170
+ for (const event of events) {
171
+ const eventType = (_a = event.type.repr.split("::").pop()) !== null && _a !== void 0 ? _a : "";
172
+ const params = event.json;
164
173
  if (eventType === "DepositEvent") {
165
- const deposit = new parsed.DepositEvent(event);
166
- if (deposit.params().obligation_id !== obligationId) {
174
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
167
175
  continue;
168
176
  }
169
177
  // Find the corresponding mint event
170
178
  const matchingEvent = events.find((e) => {
171
- return (e.type.includes("MintEvent") &&
172
- new parsed.MintEvent(e).params().ctoken_amount ===
173
- deposit.params().ctoken_amount);
179
+ var _a;
180
+ return (e.type.repr.includes("MintEvent") &&
181
+ ((_a = e.json) === null || _a === void 0 ? void 0 : _a.ctoken_amount) === params.ctoken_amount);
174
182
  });
175
183
  if (!matchingEvent) {
176
184
  // TODO: Handle case with solo Mint or solo Deposit
177
185
  continue;
178
186
  }
179
- const mint = new parsed.MintEvent(matchingEvent);
180
187
  formattedEvents.push({
181
- reserveId: deposit.params().reserve_id,
182
- quantity: parseInt(mint.params().liquidity_amount),
188
+ reserveId: params.reserve_id,
189
+ quantity: parseInt(matchingEvent.json.liquidity_amount),
183
190
  action: "Deposit",
184
- timestampMs: parseInt(data.timestampMs, 10),
185
- digest: data.digest,
191
+ timestampMs,
192
+ digest: txNode.digest,
186
193
  });
187
194
  }
188
195
  else if (eventType === "WithdrawEvent") {
189
- const withdraw = new parsed.WithdrawEvent(event);
190
- if (withdraw.params().obligation_id !== obligationId) {
196
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
191
197
  continue;
192
198
  }
193
199
  // Find the corresponding mint event
194
200
  const matchingEvent = events.find((e) => {
195
- return (e.type.includes("RedeemEvent") &&
196
- new parsed.RedeemEvent(e).params().ctoken_amount ===
197
- withdraw.params().ctoken_amount);
201
+ var _a;
202
+ return (e.type.repr.includes("RedeemEvent") &&
203
+ ((_a = e.json) === null || _a === void 0 ? void 0 : _a.ctoken_amount) === params.ctoken_amount);
198
204
  });
199
205
  if (!matchingEvent) {
200
206
  continue;
201
207
  }
202
- const redeem = new parsed.RedeemEvent(matchingEvent);
203
208
  formattedEvents.push({
204
- reserveId: redeem.params().reserve_id,
205
- quantity: parseInt(redeem.params().liquidity_amount),
209
+ reserveId: matchingEvent.json.reserve_id,
210
+ quantity: parseInt(matchingEvent.json.liquidity_amount),
206
211
  action: "Withdraw",
207
- timestampMs: parseInt(data.timestampMs, 10),
208
- digest: data.digest,
212
+ timestampMs,
213
+ digest: txNode.digest,
209
214
  });
210
215
  }
211
216
  else if (eventType === "BorrowEvent") {
212
- const borrow = new parsed.BorrowEvent(event);
213
- if (borrow.params().obligation_id !== obligationId) {
217
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
214
218
  continue;
215
219
  }
216
220
  formattedEvents.push({
217
- reserveId: borrow.params().reserve_id,
218
- quantity: parseInt(borrow.params().liquidity_amount),
221
+ reserveId: params.reserve_id,
222
+ quantity: parseInt(params.liquidity_amount),
219
223
  action: "Borrow",
220
- timestampMs: parseInt(data.timestampMs, 10),
221
- digest: data.digest,
224
+ timestampMs,
225
+ digest: txNode.digest,
222
226
  });
223
227
  }
224
228
  else if (eventType === "RepayEvent") {
225
- const repay = new parsed.RepayEvent(event);
226
- if (repay.params().obligation_id !== obligationId) {
229
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
227
230
  continue;
228
231
  }
229
232
  formattedEvents.push({
230
- reserveId: repay.params().reserve_id,
231
- quantity: parseInt(repay.params().liquidity_amount),
233
+ reserveId: params.reserve_id,
234
+ quantity: parseInt(params.liquidity_amount),
232
235
  action: "Repay",
233
- timestampMs: parseInt(data.timestampMs, 10),
234
- digest: data.digest,
236
+ timestampMs,
237
+ digest: txNode.digest,
235
238
  });
236
239
  }
237
240
  else if (eventType === "LiquidateEvent") {
238
- const liquidate = new parsed.LiquidateEvent(event);
239
- if (liquidate.params().obligation_id !== obligationId) {
241
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
240
242
  continue;
241
243
  }
242
244
  formattedEvents.push({
243
- repayReserveId: liquidate.params().repay_reserve_id,
244
- repayQuantity: parseInt(liquidate.params().repay_amount),
245
- withdrawReserveId: liquidate.params().withdraw_reserve_id,
246
- withdrawQuantity: parseInt(liquidate.params().withdraw_amount),
245
+ repayReserveId: params.repay_reserve_id,
246
+ repayQuantity: parseInt(params.repay_amount),
247
+ withdrawReserveId: params.withdraw_reserve_id,
248
+ withdrawQuantity: parseInt(params.withdraw_amount),
247
249
  action: "Liquidation",
248
- timestampMs: parseInt(data.timestampMs, 10),
249
- digest: data.digest,
250
+ timestampMs,
251
+ digest: txNode.digest,
250
252
  });
251
253
  }
252
254
  else if (eventType === "ClaimRewardEvent") {
253
- const claimReward = new parsed.ClaimRewardEvent(event);
254
- if (claimReward.params().obligation_id !== obligationId) {
255
+ if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
255
256
  continue;
256
257
  }
257
258
  formattedEvents.push({
258
- reserveId: claimReward.params().reserve_id,
259
- quantity: parseInt(claimReward.params().liquidity_amount),
259
+ reserveId: params.reserve_id,
260
+ quantity: parseInt(params.liquidity_amount),
260
261
  action: "Claim Reward",
261
- timestampMs: parseInt(data.timestampMs, 10),
262
- digest: data.digest,
262
+ timestampMs,
263
+ digest: txNode.digest,
263
264
  });
264
265
  }
265
266
  }