@suilend/sdk 3.0.8 → 4.0.0

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 (71) hide show
  1. package/_generated/_dependencies/source/0x1/ascii/structs.d.ts +4 -4
  2. package/_generated/_dependencies/source/0x1/ascii/structs.js +42 -41
  3. package/_generated/_dependencies/source/0x1/option/structs.js +22 -26
  4. package/_generated/_dependencies/source/0x1/type-name/structs.d.ts +3 -3
  5. package/_generated/_dependencies/source/0x1/type-name/structs.js +21 -25
  6. package/_generated/_dependencies/source/0x2/bag/structs.d.ts +5 -5
  7. package/_generated/_dependencies/source/0x2/bag/structs.js +22 -25
  8. package/_generated/_dependencies/source/0x2/balance/structs.d.ts +4 -4
  9. package/_generated/_dependencies/source/0x2/balance/structs.js +42 -41
  10. package/_generated/_dependencies/source/0x2/object/structs.d.ts +5 -5
  11. package/_generated/_dependencies/source/0x2/object/structs.js +42 -41
  12. package/_generated/_dependencies/source/0x2/object-table/structs.d.ts +5 -5
  13. package/_generated/_dependencies/source/0x2/object-table/structs.js +22 -25
  14. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.d.ts +3 -3
  15. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.js +22 -25
  16. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.d.ts +9 -9
  17. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.js +24 -25
  18. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.d.ts +21 -21
  19. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.js +23 -25
  20. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.d.ts +2 -2
  21. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.js +21 -25
  22. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.d.ts +52 -52
  23. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.js +45 -41
  24. package/_generated/_framework/vector.js +9 -6
  25. package/_generated/suilend/cell/structs.d.ts +1 -1
  26. package/_generated/suilend/cell/structs.js +21 -25
  27. package/_generated/suilend/decimal/structs.d.ts +2 -2
  28. package/_generated/suilend/decimal/structs.js +21 -25
  29. package/_generated/suilend/lending-market/structs.d.ts +129 -129
  30. package/_generated/suilend/lending-market/structs.js +391 -265
  31. package/_generated/suilend/liquidity-mining/structs.d.ts +44 -44
  32. package/_generated/suilend/liquidity-mining/structs.js +122 -89
  33. package/_generated/suilend/obligation/structs.d.ts +98 -98
  34. package/_generated/suilend/obligation/structs.js +194 -121
  35. package/_generated/suilend/rate-limiter/structs.d.ts +12 -12
  36. package/_generated/suilend/rate-limiter/structs.js +46 -41
  37. package/_generated/suilend/reserve/structs.d.ts +128 -128
  38. package/_generated/suilend/reserve/structs.js +243 -153
  39. package/_generated/suilend/reserve-config/structs.d.ts +30 -30
  40. package/_generated/suilend/reserve-config/structs.js +60 -41
  41. package/api/events.js +5 -14
  42. package/client.js +267 -319
  43. package/lib/initialize.d.ts +2 -2
  44. package/lib/initialize.js +26 -31
  45. package/lib/liquidityMining.js +15 -19
  46. package/lib/pyth.js +6 -15
  47. package/lib/pythAdapter.js +96 -112
  48. package/lib/strategyOwnerCap.js +8 -17
  49. package/margin/margin/admin_cap.d.ts +2 -2
  50. package/margin/margin/deps/std/type_name.d.ts +1 -1
  51. package/margin/margin/deps/suilend/lending_market.d.ts +2 -2
  52. package/margin/margin/market.d.ts +11 -11
  53. package/margin/margin/market.js +22 -22
  54. package/margin/margin/permissions.js +14 -14
  55. package/margin/margin/position.d.ts +47 -47
  56. package/margin/margin/position.js +10 -10
  57. package/margin/margin/router.d.ts +5 -5
  58. package/margin/margin/router.js +16 -16
  59. package/margin/margin/version.d.ts +1 -1
  60. package/margin/utils/index.js +22 -36
  61. package/package.json +1 -1
  62. package/parsers/apiReserveAssetDataEvent.js +4 -1
  63. package/parsers/lendingMarket.js +3 -4
  64. package/parsers/reserve.js +4 -1
  65. package/strategies.js +188 -204
  66. package/swap/quote.d.ts +1 -1
  67. package/swap/quote.js +28 -39
  68. package/swap/transaction.js +17 -26
  69. package/utils/events.js +20 -32
  70. package/utils/obligation.js +111 -135
  71. package/utils/simulate.js +16 -24
package/swap/quote.d.ts CHANGED
@@ -2,7 +2,7 @@ import { QuoteResponse as Bluefin7kQuote } from "@bluefin-exchange/bluefin7k-agg
2
2
  import { RouterDataV3 as CetusQuote, AggregatorClient as CetusSdk } from "@cetusprotocol/aggregator-sdk";
3
3
  import { AggregatorQuoter as FlowXAggregatorQuoter, GetRoutesResult as FlowXGetRoutesResult } from "@flowx-finance/sdk";
4
4
  import BigNumber from "bignumber.js";
5
- import { Token } from "@suilend/sui-fe";
5
+ import { Token } from "@suilend/sui-core";
6
6
  export declare enum QuoteProvider {
7
7
  CETUS = "cetus",
8
8
  BLUEFIN7K = "bluefin7k",
package/swap/quote.js CHANGED
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { getQuote as getBluefin7kQuoteOriginal, } from "@bluefin-exchange/bluefin7k-aggregator-sdk";
11
2
  import { normalizeStructTag } from "@mysten/sui/utils";
12
3
  import BigNumber from "bignumber.js";
@@ -29,13 +20,13 @@ const getPoolProviders = (standardizedQuote) => {
29
20
  ...route.path.reduce((acc2, p) => [...acc2, p.provider], []),
30
21
  ], [])));
31
22
  };
32
- const getAggQuoteWrapper = (provider, getAggQuote, timeoutMs) => __awaiter(void 0, void 0, void 0, function* () {
23
+ const getAggQuoteWrapper = async (provider, getAggQuote, timeoutMs) => {
33
24
  console.log(`[getAggQuoteWrapper] getting ${provider} quote`);
34
25
  try {
35
26
  const timeoutPromise = new Promise((resolve) => {
36
27
  setTimeout(() => resolve(null), timeoutMs);
37
28
  });
38
- const standardizedQuote = yield Promise.race([
29
+ const standardizedQuote = await Promise.race([
39
30
  getAggQuote(),
40
31
  timeoutPromise,
41
32
  ]);
@@ -48,9 +39,9 @@ const getAggQuoteWrapper = (provider, getAggQuote, timeoutMs) => __awaiter(void
48
39
  console.error(err);
49
40
  return null;
50
41
  }
51
- });
52
- const getCetusQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
53
- const quote = yield sdk.findRouters({
42
+ };
43
+ const getCetusQuote = async (sdk, tokenIn, tokenOut, amountIn) => {
44
+ const quote = await sdk.findRouters({
54
45
  from: tokenIn.coinType,
55
46
  target: tokenOut.coinType,
56
47
  amount: new BN(amountIn),
@@ -117,10 +108,9 @@ const getCetusQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, vo
117
108
  quote,
118
109
  };
119
110
  return standardizedQuote;
120
- });
121
- const getBluefin7kQuote = (tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
122
- var _a;
123
- const quote = yield getBluefin7kQuoteOriginal({
111
+ };
112
+ const getBluefin7kQuote = async (tokenIn, tokenOut, amountIn) => {
113
+ const quote = await getBluefin7kQuoteOriginal({
124
114
  tokenIn: tokenIn.coinType,
125
115
  tokenOut: tokenOut.coinType,
126
116
  amountIn,
@@ -136,7 +126,7 @@ const getBluefin7kQuote = (tokenIn, tokenOut, amountIn) => __awaiter(void 0, voi
136
126
  coinType: tokenOut.coinType,
137
127
  amount: new BigNumber(quote.returnAmountWithDecimal).div(10 ** tokenOut.decimals),
138
128
  },
139
- routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
129
+ routes: (quote.routes ?? []).map((route, routeIndex) => ({
140
130
  percent: new BigNumber(route.tokenInAmount)
141
131
  .div(quote.swapAmount)
142
132
  .times(100),
@@ -162,10 +152,9 @@ const getBluefin7kQuote = (tokenIn, tokenOut, amountIn) => __awaiter(void 0, voi
162
152
  quote,
163
153
  };
164
154
  return standardizedQuote;
165
- });
166
- const getFlowXQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
167
- var _a;
168
- const quote = yield sdk.getRoutes({
155
+ };
156
+ const getFlowXQuote = async (sdk, tokenIn, tokenOut, amountIn) => {
157
+ const quote = await sdk.getRoutes({
169
158
  tokenIn: tokenIn.coinType,
170
159
  tokenOut: tokenOut.coinType,
171
160
  amountIn: amountIn,
@@ -181,7 +170,7 @@ const getFlowXQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, vo
181
170
  coinType: tokenOut.coinType,
182
171
  amount: new BigNumber(quote.amountOut.toString()).div(10 ** tokenOut.decimals),
183
172
  },
184
- routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
173
+ routes: (quote.routes ?? []).map((route, routeIndex) => ({
185
174
  percent: new BigNumber(route.amountIn.toString())
186
175
  .div(quote.amountIn.toString())
187
176
  .times(100),
@@ -203,36 +192,36 @@ const getFlowXQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, vo
203
192
  quote,
204
193
  };
205
194
  return standardizedQuote;
206
- });
207
- export const getAggQuotes = (sdkMap, activeProviders, onGetAggQuote, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
195
+ };
196
+ export const getAggQuotes = async (sdkMap, activeProviders, onGetAggQuote, tokenIn, tokenOut, amountIn) => {
208
197
  const timeoutMs = 15000;
209
198
  // Get quotes in parallel
210
199
  // Cetus
211
200
  if (activeProviders.includes(QuoteProvider.CETUS)) {
212
- (() => __awaiter(void 0, void 0, void 0, function* () {
213
- const standardizedQuote = yield getAggQuoteWrapper(QuoteProvider.CETUS, () => getCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn), timeoutMs);
201
+ (async () => {
202
+ const standardizedQuote = await getAggQuoteWrapper(QuoteProvider.CETUS, () => getCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn), timeoutMs);
214
203
  onGetAggQuote(standardizedQuote);
215
- }))();
204
+ })();
216
205
  }
217
206
  // Bluefin7k
218
207
  if (activeProviders.includes(QuoteProvider.BLUEFIN7K)) {
219
- (() => __awaiter(void 0, void 0, void 0, function* () {
220
- const standardizedQuote = yield getAggQuoteWrapper(QuoteProvider.BLUEFIN7K, () => getBluefin7kQuote(tokenIn, tokenOut, amountIn), timeoutMs);
208
+ (async () => {
209
+ const standardizedQuote = await getAggQuoteWrapper(QuoteProvider.BLUEFIN7K, () => getBluefin7kQuote(tokenIn, tokenOut, amountIn), timeoutMs);
221
210
  onGetAggQuote(standardizedQuote);
222
- }))();
211
+ })();
223
212
  }
224
213
  // FlowX
225
214
  if (activeProviders.includes(QuoteProvider.FLOWX)) {
226
- (() => __awaiter(void 0, void 0, void 0, function* () {
227
- const standardizedQuote = yield getAggQuoteWrapper(QuoteProvider.FLOWX, () => getFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn), timeoutMs);
215
+ (async () => {
216
+ const standardizedQuote = await getAggQuoteWrapper(QuoteProvider.FLOWX, () => getFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn), timeoutMs);
228
217
  onGetAggQuote(standardizedQuote);
229
- }))();
218
+ })();
230
219
  }
231
- });
232
- export const getAggSortedQuotesAll = (sdkMap, activeProviders, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
220
+ };
221
+ export const getAggSortedQuotesAll = async (sdkMap, activeProviders, tokenIn, tokenOut, amountIn) => {
233
222
  const timeoutMs = 1500;
234
223
  // Get quotes in parallel
235
- const quotes = yield Promise.all([
224
+ const quotes = await Promise.all([
236
225
  activeProviders.includes(QuoteProvider.CETUS)
237
226
  ? getAggQuoteWrapper(QuoteProvider.CETUS, () => getCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn), timeoutMs)
238
227
  : null,
@@ -247,4 +236,4 @@ export const getAggSortedQuotesAll = (sdkMap, activeProviders, tokenIn, tokenOut
247
236
  .slice()
248
237
  .sort((a, b) => +b.out.amount.minus(a.out.amount));
249
238
  return sortedQuotes;
250
- });
239
+ };
@@ -1,20 +1,11 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { BluefinXTx, buildTx as buildBluefin7kTransaction, } from "@bluefin-exchange/bluefin7k-aggregator-sdk";
11
2
  import { Coin as FlowXCoin, Commission as FlowXCommission, CommissionType as FlowXCommissionType, TradeBuilder as FlowXTradeBuilder, } from "@flowx-finance/sdk";
12
- import { getSpendableCoin } from "@suilend/sui-fe";
3
+ import { getSpendableCoin } from "@suilend/sui-core";
13
4
  import { QuoteProvider } from "./quote.js";
14
- const getSwapTransactionWrapper = (provider, getSwapTransaction) => __awaiter(void 0, void 0, void 0, function* () {
5
+ const getSwapTransactionWrapper = async (provider, getSwapTransaction) => {
15
6
  console.log(`[getSwapTransactionWrapper] fetching transaction for ${provider} quote`);
16
7
  try {
17
- const res = yield getSwapTransaction();
8
+ const res = await getSwapTransaction();
18
9
  console.log(`[getSwapTransactionWrapper] fetched transaction for ${provider} quote`);
19
10
  return res;
20
11
  }
@@ -22,14 +13,14 @@ const getSwapTransactionWrapper = (provider, getSwapTransaction) => __awaiter(vo
22
13
  console.error(err);
23
14
  throw err;
24
15
  }
25
- });
26
- export const getSwapTransaction = (suiGrpcClient, address, quote, slippagePercent, sdkMap, partnerIdMap, transaction, coinIn) => __awaiter(void 0, void 0, void 0, function* () {
16
+ };
17
+ export const getSwapTransaction = async (suiGrpcClient, address, quote, slippagePercent, sdkMap, partnerIdMap, transaction, coinIn) => {
27
18
  if (quote.provider === QuoteProvider.CETUS) {
28
- return getSwapTransactionWrapper(QuoteProvider.CETUS, () => __awaiter(void 0, void 0, void 0, function* () {
19
+ return getSwapTransactionWrapper(QuoteProvider.CETUS, async () => {
29
20
  if (!coinIn) {
30
- coinIn = yield getSpendableCoin(suiGrpcClient, address, quote.in.coinType, quote.quote.amountIn.toString(), transaction);
21
+ coinIn = await getSpendableCoin(suiGrpcClient, address, quote.in.coinType, quote.quote.amountIn.toString(), transaction);
31
22
  }
32
- const coinOut = yield sdkMap[QuoteProvider.CETUS].routerSwap({
23
+ const coinOut = await sdkMap[QuoteProvider.CETUS].routerSwap({
33
24
  router: quote.quote,
34
25
  inputCoin: coinIn,
35
26
  slippage: slippagePercent / 100,
@@ -40,11 +31,11 @@ export const getSwapTransaction = (suiGrpcClient, address, quote, slippagePercen
40
31
  transaction: transaction,
41
32
  coinOut: coinOut,
42
33
  };
43
- }));
34
+ });
44
35
  }
45
36
  else if (quote.provider === QuoteProvider.BLUEFIN7K) {
46
- return getSwapTransactionWrapper(QuoteProvider.BLUEFIN7K, () => __awaiter(void 0, void 0, void 0, function* () {
47
- const { tx: transaction2, coinOut } = yield buildBluefin7kTransaction({
37
+ return getSwapTransactionWrapper(QuoteProvider.BLUEFIN7K, async () => {
38
+ const { tx: transaction2, coinOut } = await buildBluefin7kTransaction({
48
39
  quoteResponse: quote.quote,
49
40
  accountAddress: address,
50
41
  slippage: slippagePercent / 100,
@@ -67,25 +58,25 @@ export const getSwapTransaction = (suiGrpcClient, address, quote, slippagePercen
67
58
  coinOut: coinOut,
68
59
  };
69
60
  }
70
- }));
61
+ });
71
62
  }
72
63
  else if (quote.provider === QuoteProvider.FLOWX) {
73
- return getSwapTransactionWrapper(QuoteProvider.FLOWX, () => __awaiter(void 0, void 0, void 0, function* () {
64
+ return getSwapTransactionWrapper(QuoteProvider.FLOWX, async () => {
74
65
  if (!coinIn) {
75
- coinIn = yield getSpendableCoin(suiGrpcClient, address, quote.in.coinType, quote.quote.amountIn.toString(), transaction);
66
+ coinIn = await getSpendableCoin(suiGrpcClient, address, quote.in.coinType, quote.quote.amountIn.toString(), transaction);
76
67
  }
77
68
  const trade = new FlowXTradeBuilder("mainnet", quote.quote.routes)
78
69
  .slippage((slippagePercent / 100) * 1e6)
79
70
  .commission(new FlowXCommission(partnerIdMap[QuoteProvider.FLOWX], new FlowXCoin(quote.out.coinType), FlowXCommissionType.PERCENTAGE, 0, false))
80
71
  .build();
81
- const coinOut = yield trade.swap({
72
+ const coinOut = await trade.swap({
82
73
  coinIn: coinIn,
83
74
  client: suiGrpcClient,
84
75
  tx: transaction,
85
76
  });
86
77
  return { transaction, coinOut: coinOut };
87
- }));
78
+ });
88
79
  }
89
80
  else
90
81
  throw new Error("Unknown quote type");
91
- });
82
+ };
package/utils/events.js CHANGED
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  export var SuilendEventType;
11
2
  (function (SuilendEventType) {
12
3
  SuilendEventType["InterestUpdateEvent"] = "InterestUpdateEvent";
@@ -27,6 +18,7 @@ export var SuilendTransactionModule;
27
18
  SuilendTransactionModule["Reserve"] = "reserve";
28
19
  })(SuilendTransactionModule || (SuilendTransactionModule = {}));
29
20
  class TypedParamsSuiEvent {
21
+ event;
30
22
  constructor(event) {
31
23
  this.event = event;
32
24
  }
@@ -61,30 +53,26 @@ export class ClaimRewardEvent extends TypedParamsSuiEvent {
61
53
  }
62
54
  export class ObligationDataEvent extends TypedParamsSuiEvent {
63
55
  }
64
- export function getEvents(suiGrpcClient, digest) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- const result = yield suiGrpcClient.getTransaction({
67
- digest,
68
- include: { events: true },
69
- });
70
- const tx = result.$kind === "Transaction"
71
- ? result.Transaction
72
- : result.FailedTransaction;
73
- const events = [];
74
- for (const event of (tx === null || tx === void 0 ? void 0 : tx.events) || []) {
75
- events.push(new GenericSuilendEvent(event));
76
- }
77
- return events;
56
+ export async function getEvents(suiGrpcClient, digest) {
57
+ const result = await suiGrpcClient.getTransaction({
58
+ digest,
59
+ include: { events: true },
78
60
  });
61
+ const tx = result.$kind === "Transaction"
62
+ ? result.Transaction
63
+ : result.FailedTransaction;
64
+ const events = [];
65
+ for (const event of tx?.events || []) {
66
+ events.push(new GenericSuilendEvent(event));
67
+ }
68
+ return events;
79
69
  }
80
- export function getRedeemEvent(suiGrpcClient, digest) {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- const events = yield getEvents(suiGrpcClient, digest);
83
- for (const event of events) {
84
- if (event.isType(SuilendTransactionModule.LendingMarket, SuilendEventType.RedeemEvent)) {
85
- return event;
86
- }
70
+ export async function getRedeemEvent(suiGrpcClient, digest) {
71
+ const events = await getEvents(suiGrpcClient, digest);
72
+ for (const event of events) {
73
+ if (event.isType(SuilendTransactionModule.LendingMarket, SuilendEventType.RedeemEvent)) {
74
+ return event;
87
75
  }
88
- return null;
89
- });
76
+ }
77
+ return null;
90
78
  }
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { normalizeSuiAddress } from "@mysten/sui/utils";
11
2
  import { BigNumber } from "bignumber.js";
12
3
  import pLimit from "p-limit";
@@ -14,90 +5,84 @@ import { phantom } from "../_generated/_framework/reified.js";
14
5
  import { PACKAGE_ID } from "../_generated/suilend/index.js";
15
6
  import { Obligation } from "../_generated/suilend/obligation/structs.js";
16
7
  const NORMALIZED_PACKAGE_ID = normalizeSuiAddress(PACKAGE_ID);
17
- export function fetchAllObligationsForMarketWithHandler(suiGrpcClient, lendingMarketId, lendingMarketType, chunkHandler) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- const limit = pLimit(30);
20
- const { object: rawLendingMarket } = yield suiGrpcClient.getObject({
21
- objectId: lendingMarketId,
22
- include: { json: true },
23
- });
24
- const obligationField = rawLendingMarket.json.obligations;
25
- const obligationOwnerID = obligationField.id;
26
- let hasNextPage = true;
27
- let cursor = null;
28
- const promises = [];
29
- while (hasNextPage) {
30
- const response = yield suiGrpcClient.listDynamicFields({
31
- parentId: obligationOwnerID,
32
- cursor: cursor,
33
- });
34
- hasNextPage = response.hasNextPage;
35
- cursor = response.cursor;
36
- promises.push(limit(() => __awaiter(this, void 0, void 0, function* () {
37
- const obligationObjects = yield chunkedMultiGet(suiGrpcClient, response.dynamicFields
38
- .filter((x) => x.$kind === "DynamicObject")
39
- .map((x) => x.childId));
40
- const obligations = [];
41
- for (const rawObligation of obligationObjects) {
42
- if (rawObligation instanceof Error)
43
- continue;
44
- obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
45
- }
46
- yield chunkHandler(obligations);
47
- })));
48
- }
49
- yield Promise.all(promises);
8
+ export async function fetchAllObligationsForMarketWithHandler(suiGrpcClient, lendingMarketId, lendingMarketType, chunkHandler) {
9
+ const limit = pLimit(30);
10
+ const { object: rawLendingMarket } = await suiGrpcClient.getObject({
11
+ objectId: lendingMarketId,
12
+ include: { json: true },
50
13
  });
51
- }
52
- export function fetchAllObligationsForMarket(suiGrpcClient, lendingMarketId, lendingMarketType) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const { object: rawLendingMarket } = yield suiGrpcClient.getObject({
55
- objectId: lendingMarketId,
56
- include: { json: true },
14
+ const obligationField = rawLendingMarket.json.obligations;
15
+ const obligationOwnerID = obligationField.id;
16
+ let hasNextPage = true;
17
+ let cursor = null;
18
+ const promises = [];
19
+ while (hasNextPage) {
20
+ const response = await suiGrpcClient.listDynamicFields({
21
+ parentId: obligationOwnerID,
22
+ cursor: cursor,
57
23
  });
58
- const obligationField = rawLendingMarket.json.obligations;
59
- const obligationOwnerID = obligationField.id;
60
- let hasNextPage = true;
61
- let cursor = null;
62
- let fields = [];
63
- while (hasNextPage) {
64
- const response = yield suiGrpcClient.listDynamicFields({
65
- parentId: obligationOwnerID,
66
- cursor: cursor,
67
- });
68
- fields = fields.concat(response.dynamicFields);
69
- hasNextPage = response.hasNextPage;
70
- cursor = response.cursor;
71
- }
72
- const obligationObjects = yield chunkedMultiGet(suiGrpcClient, fields.filter((x) => x.$kind === "DynamicObject").map((x) => x.childId));
73
- const obligations = [];
74
- for (const rawObligation of obligationObjects) {
75
- if (rawObligation instanceof Error)
76
- continue;
77
- obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
78
- }
79
- return obligations;
24
+ hasNextPage = response.hasNextPage;
25
+ cursor = response.cursor;
26
+ promises.push(limit(async () => {
27
+ const obligationObjects = await chunkedMultiGet(suiGrpcClient, response.dynamicFields
28
+ .filter((x) => x.$kind === "DynamicObject")
29
+ .map((x) => x.childId));
30
+ const obligations = [];
31
+ for (const rawObligation of obligationObjects) {
32
+ if (rawObligation instanceof Error)
33
+ continue;
34
+ obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
35
+ }
36
+ await chunkHandler(obligations);
37
+ }));
38
+ }
39
+ await Promise.all(promises);
40
+ }
41
+ export async function fetchAllObligationsForMarket(suiGrpcClient, lendingMarketId, lendingMarketType) {
42
+ const { object: rawLendingMarket } = await suiGrpcClient.getObject({
43
+ objectId: lendingMarketId,
44
+ include: { json: true },
80
45
  });
46
+ const obligationField = rawLendingMarket.json.obligations;
47
+ const obligationOwnerID = obligationField.id;
48
+ let hasNextPage = true;
49
+ let cursor = null;
50
+ let fields = [];
51
+ while (hasNextPage) {
52
+ const response = await suiGrpcClient.listDynamicFields({
53
+ parentId: obligationOwnerID,
54
+ cursor: cursor,
55
+ });
56
+ fields = fields.concat(response.dynamicFields);
57
+ hasNextPage = response.hasNextPage;
58
+ cursor = response.cursor;
59
+ }
60
+ const obligationObjects = await chunkedMultiGet(suiGrpcClient, fields.filter((x) => x.$kind === "DynamicObject").map((x) => x.childId));
61
+ const obligations = [];
62
+ for (const rawObligation of obligationObjects) {
63
+ if (rawObligation instanceof Error)
64
+ continue;
65
+ obligations.push(Obligation.fromBcs(phantom(lendingMarketType), rawObligation.content));
66
+ }
67
+ return obligations;
81
68
  }
82
- function chunkedMultiGet(suiGrpcClient, objectIds) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- const limit = pLimit(30);
85
- const results = [];
86
- const chunks = splitIntoChunks(objectIds, 50);
87
- const promises = [];
88
- for (const chunk of chunks) {
89
- promises.push(limit(() => suiGrpcClient.getObjects({
90
- objectIds: chunk,
91
- include: { content: true },
92
- })));
93
- }
94
- for (const chunk of yield Promise.all(promises)) {
95
- for (const result of chunk.objects) {
96
- results.push(result);
97
- }
69
+ async function chunkedMultiGet(suiGrpcClient, objectIds) {
70
+ const limit = pLimit(30);
71
+ const results = [];
72
+ const chunks = splitIntoChunks(objectIds, 50);
73
+ const promises = [];
74
+ for (const chunk of chunks) {
75
+ promises.push(limit(() => suiGrpcClient.getObjects({
76
+ objectIds: chunk,
77
+ include: { content: true },
78
+ })));
79
+ }
80
+ for (const chunk of await Promise.all(promises)) {
81
+ for (const result of chunk.objects) {
82
+ results.push(result);
98
83
  }
99
- return results;
100
- });
84
+ }
85
+ return results;
101
86
  }
102
87
  function splitIntoChunks(arr, chunkSize) {
103
88
  const result = [];
@@ -107,14 +92,12 @@ function splitIntoChunks(arr, chunkSize) {
107
92
  }
108
93
  return result;
109
94
  }
110
- export function getObligationHistoryPage(suiGraphQLClient, obligationId, maxQuantity, cursor) {
111
- return __awaiter(this, void 0, void 0, function* () {
112
- var _a;
113
- if (maxQuantity > 50 || maxQuantity < 1) {
114
- throw Error("maxQuantity must be between 1 and 50");
115
- }
116
- const result = yield suiGraphQLClient.query({
117
- query: `
95
+ export async function getObligationHistoryPage(suiGraphQLClient, obligationId, maxQuantity, cursor) {
96
+ if (maxQuantity > 50 || maxQuantity < 1) {
97
+ throw Error("maxQuantity must be between 1 and 50");
98
+ }
99
+ const result = await suiGraphQLClient.query({
100
+ query: `
118
101
  query QueryObligationTransactions($objectId: SuiAddress!, $first: Int, $after: String) {
119
102
  transactions(
120
103
  first: $first
@@ -146,33 +129,28 @@ export function getObligationHistoryPage(suiGraphQLClient, obligationId, maxQuan
146
129
  }
147
130
  }
148
131
  `,
149
- variables: {
150
- objectId: obligationId,
151
- first: maxQuantity,
152
- after: cursor,
153
- },
154
- });
155
- const payload = (_a = result.data) === null || _a === void 0 ? void 0 : _a.transactions;
156
- if (!payload) {
157
- return { cursor: null, history: [] };
158
- }
159
- let formattedEvents = [];
160
- for (const txNode of payload.nodes) {
161
- const events = txNode.effects.events.nodes.filter((e) => {
162
- var _a, _b;
163
- return ((_a = e.contents) === null || _a === void 0 ? void 0 : _a.type.repr.startsWith(`${PACKAGE_ID}::`)) ||
164
- ((_b = e.contents) === null || _b === void 0 ? void 0 : _b.type.repr.startsWith(`${NORMALIZED_PACKAGE_ID}::`));
165
- });
166
- formattedEvents = formattedEvents.concat(formatEventsToHistory(obligationId, txNode, events));
167
- }
168
- return {
169
- cursor: payload.pageInfo.hasNextPage ? payload.pageInfo.endCursor : null,
170
- history: formattedEvents,
171
- };
132
+ variables: {
133
+ objectId: obligationId,
134
+ first: maxQuantity,
135
+ after: cursor,
136
+ },
172
137
  });
138
+ const payload = result.data?.transactions;
139
+ if (!payload) {
140
+ return { cursor: null, history: [] };
141
+ }
142
+ let formattedEvents = [];
143
+ for (const txNode of payload.nodes) {
144
+ const events = txNode.effects.events.nodes.filter((e) => e.contents?.type.repr.startsWith(`${PACKAGE_ID}::`) ||
145
+ e.contents?.type.repr.startsWith(`${NORMALIZED_PACKAGE_ID}::`));
146
+ formattedEvents = formattedEvents.concat(formatEventsToHistory(obligationId, txNode, events));
147
+ }
148
+ return {
149
+ cursor: payload.pageInfo.hasNextPage ? payload.pageInfo.endCursor : null,
150
+ history: formattedEvents,
151
+ };
173
152
  }
174
153
  function formatEventsToHistory(obligationId, txNode, events) {
175
- var _a;
176
154
  const formattedEvents = [];
177
155
  const timestampMs = txNode.effects.timestamp
178
156
  ? new Date(txNode.effects.timestamp).getTime()
@@ -180,18 +158,17 @@ function formatEventsToHistory(obligationId, txNode, events) {
180
158
  for (const event of events) {
181
159
  if (!event.contents)
182
160
  continue;
183
- const eventType = (_a = event.contents.type.repr.split("::").pop()) !== null && _a !== void 0 ? _a : "";
161
+ const eventType = event.contents.type.repr.split("::").pop() ?? "";
184
162
  const params = event.contents.json;
185
163
  if (eventType === "DepositEvent") {
186
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
164
+ if (params?.obligation_id !== obligationId) {
187
165
  continue;
188
166
  }
189
167
  const matchingEvent = events.find((e) => {
190
- var _a, _b, _c;
191
- return (((_a = e.contents) === null || _a === void 0 ? void 0 : _a.type.repr.includes("MintEvent")) &&
192
- ((_c = (_b = e.contents) === null || _b === void 0 ? void 0 : _b.json) === null || _c === void 0 ? void 0 : _c.ctoken_amount) === params.ctoken_amount);
168
+ return (e.contents?.type.repr.includes("MintEvent") &&
169
+ e.contents?.json?.ctoken_amount === params.ctoken_amount);
193
170
  });
194
- if (!(matchingEvent === null || matchingEvent === void 0 ? void 0 : matchingEvent.contents)) {
171
+ if (!matchingEvent?.contents) {
195
172
  continue;
196
173
  }
197
174
  formattedEvents.push({
@@ -203,15 +180,14 @@ function formatEventsToHistory(obligationId, txNode, events) {
203
180
  });
204
181
  }
205
182
  else if (eventType === "WithdrawEvent") {
206
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
183
+ if (params?.obligation_id !== obligationId) {
207
184
  continue;
208
185
  }
209
186
  const matchingEvent = events.find((e) => {
210
- var _a, _b, _c;
211
- return (((_a = e.contents) === null || _a === void 0 ? void 0 : _a.type.repr.includes("RedeemEvent")) &&
212
- ((_c = (_b = e.contents) === null || _b === void 0 ? void 0 : _b.json) === null || _c === void 0 ? void 0 : _c.ctoken_amount) === params.ctoken_amount);
187
+ return (e.contents?.type.repr.includes("RedeemEvent") &&
188
+ e.contents?.json?.ctoken_amount === params.ctoken_amount);
213
189
  });
214
- if (!(matchingEvent === null || matchingEvent === void 0 ? void 0 : matchingEvent.contents)) {
190
+ if (!matchingEvent?.contents) {
215
191
  continue;
216
192
  }
217
193
  formattedEvents.push({
@@ -223,7 +199,7 @@ function formatEventsToHistory(obligationId, txNode, events) {
223
199
  });
224
200
  }
225
201
  else if (eventType === "BorrowEvent") {
226
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
202
+ if (params?.obligation_id !== obligationId) {
227
203
  continue;
228
204
  }
229
205
  formattedEvents.push({
@@ -235,7 +211,7 @@ function formatEventsToHistory(obligationId, txNode, events) {
235
211
  });
236
212
  }
237
213
  else if (eventType === "RepayEvent") {
238
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
214
+ if (params?.obligation_id !== obligationId) {
239
215
  continue;
240
216
  }
241
217
  formattedEvents.push({
@@ -247,7 +223,7 @@ function formatEventsToHistory(obligationId, txNode, events) {
247
223
  });
248
224
  }
249
225
  else if (eventType === "LiquidateEvent") {
250
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
226
+ if (params?.obligation_id !== obligationId) {
251
227
  continue;
252
228
  }
253
229
  formattedEvents.push({
@@ -261,7 +237,7 @@ function formatEventsToHistory(obligationId, txNode, events) {
261
237
  });
262
238
  }
263
239
  else if (eventType === "ClaimRewardEvent") {
264
- if ((params === null || params === void 0 ? void 0 : params.obligation_id) !== obligationId) {
240
+ if (params?.obligation_id !== obligationId) {
265
241
  continue;
266
242
  }
267
243
  formattedEvents.push({