@scallop-io/sui-scallop-sdk 0.46.48 → 0.46.50

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.
@@ -51,7 +51,7 @@ export declare class ScallopBuilder {
51
51
  * @param sender - Sender address.
52
52
  * @return Take coin and left coin.
53
53
  */
54
- selectCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, assetCoinName: SupportAssetCoins, amount: number, sender: string): Promise<{
54
+ selectCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, assetCoinName: SupportAssetCoins, amount: number, sender?: string): Promise<{
55
55
  takeCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
56
56
  leftCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
57
57
  }>;
@@ -64,7 +64,7 @@ export declare class ScallopBuilder {
64
64
  * @param sender - Sender address.
65
65
  * @return Take coin and left coin.
66
66
  */
67
- selectMarketCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, marketCoinName: SupportMarketCoins, amount: number, sender: string): Promise<{
67
+ selectMarketCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, marketCoinName: SupportMarketCoins, amount: number, sender?: string): Promise<{
68
68
  takeCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
69
69
  leftCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
70
70
  totalAmount: number;
@@ -78,7 +78,7 @@ export declare class ScallopBuilder {
78
78
  * @param sender - Sender address.
79
79
  * @return Take coin and left coin.
80
80
  */
81
- selectSCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, sCoinName: SupportSCoin, amount: number, sender: string): Promise<{
81
+ selectSCoin(txBlock: ScallopTxBlock | SuiKitTxBlock, sCoinName: SupportSCoin, amount: number, sender?: string): Promise<{
82
82
  takeCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
83
83
  leftCoin: import("@scallop-io/sui-kit").TransactionObjectArgument;
84
84
  totalAmount: number;
@@ -4,7 +4,7 @@ import { ScallopAddress } from './scallopAddress';
4
4
  import { ScallopUtils } from './scallopUtils';
5
5
  import { ScallopIndexer } from './scallopIndexer';
6
6
  import { ScallopCache } from './scallopCache';
7
- import { SuiObjectData } from '@mysten/sui.js/src/client';
7
+ import { SuiObjectData } from '@mysten/sui.js/client';
8
8
  /**
9
9
  * @description
10
10
  * It provides methods for getting on-chain data from the Scallop contract.
@@ -24,6 +24,7 @@ export declare class ScallopQuery {
24
24
  utils: ScallopUtils;
25
25
  indexer: ScallopIndexer;
26
26
  cache: ScallopCache;
27
+ walletAddress: string;
27
28
  constructor(params: ScallopQueryParams, instance?: ScallopInstanceParams);
28
29
  /**
29
30
  * Request the scallop API to initialize data.
@@ -1,4 +1,4 @@
1
- import { SuiObjectData } from '@mysten/sui.js/src/client';
1
+ import { SuiObjectData } from '@mysten/sui.js/client';
2
2
  import { ScallopQuery } from 'src/models';
3
3
  import { LoyaltyProgramInfo } from 'src/types';
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { SuiObjectData } from '@mysten/sui.js/src/client';
1
+ import { SuiObjectData } from '@mysten/sui.js/client';
2
2
  import type { ScallopQuery } from '../models';
3
3
  import type { SupportAssetCoins } from '../types';
4
4
  /**
@@ -27,7 +27,9 @@ export type ScallopBuilderParams = ScallopParams & {
27
27
  walletAddress?: string;
28
28
  pythEndpoints?: string[];
29
29
  };
30
- export type ScallopQueryParams = ScallopParams;
30
+ export type ScallopQueryParams = ScallopParams & {
31
+ walletAddress?: string;
32
+ };
31
33
  export type ScallopUtilsParams = ScallopParams & {
32
34
  pythEndpoints?: string[];
33
35
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "0.46.48",
3
+ "version": "0.46.50",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -122,7 +122,7 @@ export class ScallopBuilder {
122
122
  txBlock: ScallopTxBlock | SuiKitTxBlock,
123
123
  assetCoinName: SupportAssetCoins,
124
124
  amount: number,
125
- sender: string
125
+ sender: string = this.walletAddress
126
126
  ) {
127
127
  const coinType = this.utils.parseCoinType(assetCoinName);
128
128
  const coins = await this.utils.selectCoins(amount, coinType, sender);
@@ -143,7 +143,7 @@ export class ScallopBuilder {
143
143
  txBlock: ScallopTxBlock | SuiKitTxBlock,
144
144
  marketCoinName: SupportMarketCoins,
145
145
  amount: number,
146
- sender: string
146
+ sender: string = this.walletAddress
147
147
  ) {
148
148
  const marketCoinType = this.utils.parseMarketCoinType(marketCoinName);
149
149
  const coins = await this.utils.selectCoins(amount, marketCoinType, sender);
@@ -171,7 +171,7 @@ export class ScallopBuilder {
171
171
  txBlock: ScallopTxBlock | SuiKitTxBlock,
172
172
  sCoinName: SupportSCoin,
173
173
  amount: number,
174
- sender: string
174
+ sender: string = this.walletAddress
175
175
  ) {
176
176
  const sCoinType = this.utils.parseSCoinType(sCoinName);
177
177
  const coins = await this.utils.selectCoins(amount, sCoinType, sender);
@@ -742,12 +742,12 @@ export class ScallopClient {
742
742
  const sender = walletAddress || this.walletAddress;
743
743
  txBlock.setSender(sender);
744
744
 
745
- const marketCoin = await txBlock.unstakeQuick(
745
+ const sCoin = await txBlock.unstakeQuick(
746
746
  amount,
747
747
  stakeMarketCoinName,
748
748
  stakeAccountId
749
749
  );
750
- txBlock.transferObjects([marketCoin], sender);
750
+ txBlock.transferObjects([sCoin], sender);
751
751
 
752
752
  if (sign) {
753
753
  return (await this.suiKit.signAndSendTxn(
@@ -793,13 +793,16 @@ export class ScallopClient {
793
793
  const stakeMarketCoin = await txBlock.unstakeQuick(
794
794
  amount,
795
795
  stakeMarketCoinName,
796
- stakeAccountId
796
+ stakeAccountId,
797
+ false
797
798
  );
798
799
  const stakeCoinName =
799
800
  this.utils.parseCoinName<SupportStakeCoins>(stakeMarketCoinName);
800
801
  if (stakeMarketCoin) {
801
802
  const coin = txBlock.withdraw(stakeMarketCoin, stakeCoinName);
802
803
  txBlock.transferObjects([coin], sender);
804
+ } else {
805
+ throw new Error(`No stake found for ${stakeMarketCoinName}`);
803
806
  }
804
807
 
805
808
  if (sign) {
@@ -52,12 +52,13 @@ import { ScallopUtils } from './scallopUtils';
52
52
  import { ScallopIndexer } from './scallopIndexer';
53
53
  import { ScallopCache } from './scallopCache';
54
54
  import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
55
- import { SuiObjectData } from '@mysten/sui.js/src/client';
55
+ import { SuiObjectData } from '@mysten/sui.js/client';
56
56
  import {
57
57
  getSCoinAmount,
58
58
  getSCoinAmounts,
59
59
  getSCoinTotalSupply,
60
60
  } from 'src/queries/sCoinQuery';
61
+ import { normalizeSuiAddress } from '@mysten/sui.js/utils';
61
62
 
62
63
  /**
63
64
  * @description
@@ -79,6 +80,7 @@ export class ScallopQuery {
79
80
  public utils: ScallopUtils;
80
81
  public indexer: ScallopIndexer;
81
82
  public cache: ScallopCache;
83
+ public walletAddress: string;
82
84
 
83
85
  public constructor(
84
86
  params: ScallopQueryParams,
@@ -106,6 +108,9 @@ export class ScallopQuery {
106
108
  query: this,
107
109
  });
108
110
  this.indexer = new ScallopIndexer(this.params, { cache: this.cache });
111
+ this.walletAddress = normalizeSuiAddress(
112
+ params?.walletAddress || this.suiKit.currentAddress()
113
+ );
109
114
  }
110
115
 
111
116
  /**
@@ -205,7 +210,7 @@ export class ScallopQuery {
205
210
  * @param ownerAddress - The owner address.
206
211
  * @return Obligations data.
207
212
  */
208
- public async getObligations(ownerAddress?: string) {
213
+ public async getObligations(ownerAddress: string = this.walletAddress) {
209
214
  return await getObligations(this, ownerAddress);
210
215
  }
211
216
 
@@ -228,7 +233,7 @@ export class ScallopQuery {
228
233
  */
229
234
  public async getCoinAmounts(
230
235
  assetCoinNames?: SupportAssetCoins[],
231
- ownerAddress?: string
236
+ ownerAddress: string = this.walletAddress
232
237
  ) {
233
238
  return await getCoinAmounts(this, assetCoinNames, ownerAddress);
234
239
  }
@@ -242,7 +247,7 @@ export class ScallopQuery {
242
247
  */
243
248
  public async getCoinAmount(
244
249
  assetCoinName: SupportAssetCoins,
245
- ownerAddress?: string
250
+ ownerAddress: string = this.walletAddress
246
251
  ) {
247
252
  return await getCoinAmount(this, assetCoinName, ownerAddress);
248
253
  }
@@ -256,7 +261,7 @@ export class ScallopQuery {
256
261
  */
257
262
  public async getMarketCoinAmounts(
258
263
  marketCoinNames?: SupportMarketCoins[],
259
- ownerAddress?: string
264
+ ownerAddress: string = this.walletAddress
260
265
  ) {
261
266
  return await getMarketCoinAmounts(this, marketCoinNames, ownerAddress);
262
267
  }
@@ -270,7 +275,7 @@ export class ScallopQuery {
270
275
  */
271
276
  public async getMarketCoinAmount(
272
277
  marketCoinName: SupportMarketCoins,
273
- ownerAddress?: string
278
+ ownerAddress: string = this.walletAddress
274
279
  ) {
275
280
  return await getMarketCoinAmount(this, marketCoinName, ownerAddress);
276
281
  }
@@ -331,7 +336,7 @@ export class ScallopQuery {
331
336
  * @param ownerAddress - The owner address.
332
337
  * @return All Stake accounts data.
333
338
  */
334
- public async getAllStakeAccounts(ownerAddress?: string) {
339
+ public async getAllStakeAccounts(ownerAddress: string = this.walletAddress) {
335
340
  return await getStakeAccounts(this, ownerAddress);
336
341
  }
337
342
 
@@ -344,7 +349,7 @@ export class ScallopQuery {
344
349
  */
345
350
  public async getStakeAccounts(
346
351
  stakeMarketCoinName: SupportStakeMarketCoins,
347
- ownerAddress?: string
352
+ ownerAddress: string = this.walletAddress
348
353
  ) {
349
354
  const allStakeAccount = await this.getAllStakeAccounts(ownerAddress);
350
355
  return allStakeAccount[stakeMarketCoinName] ?? [];
@@ -472,7 +477,7 @@ export class ScallopQuery {
472
477
  */
473
478
  public async getLendings(
474
479
  poolCoinNames?: SupportPoolCoins[],
475
- ownerAddress?: string,
480
+ ownerAddress: string = this.walletAddress,
476
481
  indexer: boolean = false
477
482
  ) {
478
483
  return await getLendings(this, poolCoinNames, ownerAddress, indexer);
@@ -488,7 +493,7 @@ export class ScallopQuery {
488
493
  */
489
494
  public async getLending(
490
495
  poolCoinName: SupportPoolCoins,
491
- ownerAddress?: string,
496
+ ownerAddress: string = this.walletAddress,
492
497
  indexer: boolean = false
493
498
  ) {
494
499
  return await getLending(this, poolCoinName, ownerAddress, indexer);
@@ -505,7 +510,7 @@ export class ScallopQuery {
505
510
  * @return All obligation accounts information.
506
511
  */
507
512
  public async getObligationAccounts(
508
- ownerAddress?: string,
513
+ ownerAddress: string = this.walletAddress,
509
514
  indexer: boolean = false
510
515
  ) {
511
516
  return await getObligationAccounts(this, ownerAddress, indexer);
@@ -524,7 +529,7 @@ export class ScallopQuery {
524
529
  */
525
530
  public async getObligationAccount(
526
531
  obligationId: string,
527
- ownerAddress?: string,
532
+ ownerAddress: string = this.walletAddress,
528
533
  indexer: boolean = false
529
534
  ) {
530
535
  return await getObligationAccount(
@@ -619,7 +624,7 @@ export class ScallopQuery {
619
624
  */
620
625
  public async getSCoinAmounts(
621
626
  sCoinNames?: SupportSCoin[],
622
- ownerAddress?: string
627
+ ownerAddress: string = this.walletAddress
623
628
  ) {
624
629
  return await getSCoinAmounts(this, sCoinNames, ownerAddress);
625
630
  }
@@ -633,7 +638,7 @@ export class ScallopQuery {
633
638
  */
634
639
  public async getSCoinAmount(
635
640
  sCoinName: SupportSCoin | SupportMarketCoins,
636
- ownerAddress?: string
641
+ ownerAddress: string = this.walletAddress
637
642
  ) {
638
643
  const parsedSCoinName = this.utils.parseSCoinName(sCoinName);
639
644
  return parsedSCoinName
@@ -1,4 +1,4 @@
1
- import { SuiObjectData } from '@mysten/sui.js/src/client';
1
+ import { SuiObjectData } from '@mysten/sui.js/client';
2
2
  import BigNumber from 'bignumber.js';
3
3
  import { ScallopQuery } from 'src/models';
4
4
  import { LoyaltyProgramInfo } from 'src/types';
@@ -1,4 +1,4 @@
1
- import { SuiObjectData } from '@mysten/sui.js/src/client';
1
+ import { SuiObjectData } from '@mysten/sui.js/client';
2
2
  import type { ScallopQuery } from '../models';
3
3
  import type { SupportAssetCoins } from '../types';
4
4
 
@@ -61,35 +61,53 @@ export const getPythPrices = async (
61
61
  query: ScallopQuery,
62
62
  assetCoinNames: SupportAssetCoins[]
63
63
  ) => {
64
- const seen: Record<string, boolean> = {};
65
- const pythFeedObjectIds = assetCoinNames
66
- .map((assetCoinName) => {
67
- const pythFeedObjectId = query.address.get(
64
+ const pythPriceFeedIds = assetCoinNames.reduce(
65
+ (prev, assetCoinName) => {
66
+ const pythPriceFeed = query.address.get(
68
67
  `core.coins.${assetCoinName}.oracle.pyth.feedObject`
69
68
  );
70
- if (seen[pythFeedObjectId]) return null;
69
+ if (!prev[pythPriceFeed]) {
70
+ prev[pythPriceFeed] = [assetCoinName];
71
+ } else {
72
+ prev[pythPriceFeed].push(assetCoinName);
73
+ }
74
+ return prev;
75
+ },
76
+ {} as Record<string, SupportAssetCoins[]>
77
+ );
71
78
 
72
- seen[pythFeedObjectId] = true;
73
- return pythFeedObjectId;
74
- })
75
- .filter((item) => !!item) as string[];
79
+ // Fetch multiple objects at once to save rpc calls
76
80
  const priceFeedObjects = await query.cache.queryGetObjects(
77
- pythFeedObjectIds,
78
- {
79
- showContent: true,
80
- }
81
+ Object.keys(pythPriceFeedIds),
82
+ { showContent: true }
83
+ );
84
+
85
+ const assetToPriceFeedMapping = priceFeedObjects.reduce(
86
+ (prev, priceFeedObject) => {
87
+ pythPriceFeedIds[priceFeedObject.objectId].forEach((assetCoinName) => {
88
+ prev[assetCoinName] = priceFeedObject;
89
+ });
90
+ return prev;
91
+ },
92
+ {} as Record<SupportAssetCoins, SuiObjectData>
81
93
  );
82
94
 
83
95
  return (
84
96
  await Promise.all(
85
- priceFeedObjects.map(async (priceFeedObject, idx) => ({
86
- coinName: assetCoinNames[idx],
87
- price: await getPythPrice(query, assetCoinNames[idx], priceFeedObject),
88
- }))
97
+ Object.entries(assetToPriceFeedMapping).map(
98
+ async ([assetCoinName, priceFeedObject]) => ({
99
+ coinName: assetCoinName,
100
+ price: await getPythPrice(
101
+ query,
102
+ assetCoinName as SupportAssetCoins,
103
+ priceFeedObject
104
+ ),
105
+ })
106
+ )
89
107
  )
90
108
  ).reduce(
91
109
  (prev, curr) => {
92
- prev[curr.coinName] = curr.price;
110
+ prev[curr.coinName as SupportAssetCoins] = curr.price;
93
111
  return prev;
94
112
  },
95
113
  {} as Record<SupportAssetCoins, number>
@@ -41,7 +41,9 @@ export type ScallopBuilderParams = ScallopParams & {
41
41
  pythEndpoints?: string[];
42
42
  };
43
43
 
44
- export type ScallopQueryParams = ScallopParams;
44
+ export type ScallopQueryParams = ScallopParams & {
45
+ walletAddress?: string;
46
+ };
45
47
 
46
48
  export type ScallopUtilsParams = ScallopParams & {
47
49
  pythEndpoints?: string[];
@@ -512,23 +512,25 @@ export const calculateBorrowIncentivePoolPointData = (
512
512
  .multipliedBy(rewardCoinPrice);
513
513
 
514
514
  const weightScale = BigNumber(1_000_000_000_000);
515
- const rewardRate = rewardValueForYear
516
- .multipliedBy(
517
- BigNumber(parsedBorrowIncentivePoolPointData.baseWeight).dividedBy(
518
- weightScale
515
+
516
+ const rewardRate =
517
+ rewardValueForYear
518
+ .multipliedBy(
519
+ BigNumber(parsedBorrowIncentivePoolPointData.baseWeight).dividedBy(
520
+ weightScale
521
+ )
519
522
  )
520
- )
521
- .dividedBy(weightedStakedValue)
522
- .isFinite()
523
- ? rewardValueForYear
524
- .multipliedBy(
525
- BigNumber(parsedBorrowIncentivePoolPointData.baseWeight).dividedBy(
526
- weightScale
523
+ .dividedBy(weightedStakedValue)
524
+ .isFinite() && parsedBorrowIncentivePoolPointData.points > 0
525
+ ? rewardValueForYear
526
+ .multipliedBy(
527
+ BigNumber(parsedBorrowIncentivePoolPointData.baseWeight).dividedBy(
528
+ weightScale
529
+ )
527
530
  )
528
- )
529
- .dividedBy(weightedStakedValue)
530
- .toNumber()
531
- : Infinity;
531
+ .dividedBy(weightedStakedValue)
532
+ .toNumber()
533
+ : Infinity;
532
534
 
533
535
  return {
534
536
  distributedPointPerSec: distributedPointPerSec.toNumber(),
package/dist/test.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/src/test.ts DELETED
@@ -1,19 +0,0 @@
1
- import { Scallop } from './models';
2
- import * as dotenv from 'dotenv';
3
- dotenv.config();
4
- const scallop = new Scallop({
5
- secretKey: process.env.SECRET_KEY as string,
6
- });
7
-
8
- const main = async () => {
9
- const query = await scallop.createScallopQuery();
10
-
11
- const res = await query.getVeScas(
12
- '0xd1a70cfe7332e994a950b9c570e93def4b6d2ec53b34ff5c0cc9946226a7cf3d'
13
- );
14
- console.dir(res, { depth: null });
15
- };
16
- main()
17
- .then()
18
- .catch(console.error)
19
- .finally(() => process.exit(0));