@scallop-io/sui-scallop-sdk 0.46.36 → 0.46.38

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 (45) hide show
  1. package/dist/builders/loyaltyProgramBuilder.d.ts +12 -0
  2. package/dist/builders/referralBuilder.d.ts +1 -1
  3. package/dist/constants/testAddress.d.ts +2 -0
  4. package/dist/index.js +684 -172
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +674 -162
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/models/scallopQuery.d.ts +10 -3
  9. package/dist/models/scallopUtils.d.ts +1 -1
  10. package/dist/queries/index.d.ts +1 -0
  11. package/dist/queries/loyaltyProgramQuery.d.ts +10 -0
  12. package/dist/queries/vescaQuery.d.ts +8 -6
  13. package/dist/types/address.d.ts +6 -0
  14. package/dist/types/builder/index.d.ts +3 -1
  15. package/dist/types/builder/loyaltyProgram.d.ts +23 -0
  16. package/dist/types/builder/vesca.d.ts +16 -0
  17. package/dist/types/query/index.d.ts +1 -0
  18. package/dist/types/query/loyaltyProgram.d.ts +5 -0
  19. package/dist/types/query/vesca.d.ts +18 -0
  20. package/dist/utils/builder.d.ts +6 -5
  21. package/package.json +7 -6
  22. package/src/builders/index.ts +6 -1
  23. package/src/builders/loyaltyProgramBuilder.ts +115 -0
  24. package/src/builders/referralBuilder.ts +1 -1
  25. package/src/builders/vescaBuilder.ts +5 -1
  26. package/src/constants/testAddress.ts +383 -0
  27. package/src/models/scallopAddress.ts +12 -2
  28. package/src/models/scallopCache.ts +0 -1
  29. package/src/models/scallopQuery.ts +28 -16
  30. package/src/models/scallopUtils.ts +3 -3
  31. package/src/queries/borrowIncentiveQuery.ts +9 -8
  32. package/src/queries/coreQuery.ts +70 -66
  33. package/src/queries/index.ts +1 -0
  34. package/src/queries/loyaltyProgramQuery.ts +77 -0
  35. package/src/queries/portfolioQuery.ts +36 -28
  36. package/src/queries/vescaQuery.ts +70 -15
  37. package/src/types/address.ts +6 -0
  38. package/src/types/builder/index.ts +3 -0
  39. package/src/types/builder/loyaltyProgram.ts +35 -0
  40. package/src/types/builder/vesca.ts +16 -0
  41. package/src/types/query/index.ts +1 -0
  42. package/src/types/query/loyaltyProgram.ts +5 -0
  43. package/src/types/query/vesca.ts +21 -0
  44. package/src/utils/builder.ts +69 -53
  45. package/src/utils/query.ts +6 -5
@@ -22,16 +22,61 @@ export const requireSender = (txBlock: SuiKitTxBlock) => {
22
22
  return sender;
23
23
  };
24
24
 
25
+ export const checkVesca = (prevUnlockAtInMillisTimestamp?: number) => {
26
+ if (prevUnlockAtInMillisTimestamp === undefined) {
27
+ throw new Error('veSca not found');
28
+ }
29
+ };
30
+
31
+ export const checkVescaExpired = (prevUnlockAtInMillisTimestamp: number) => {
32
+ if (prevUnlockAtInMillisTimestamp <= new Date().getTime()) {
33
+ throw new Error('veSca is expired, use renewExpiredVeScaQuick instead');
34
+ }
35
+ };
36
+
37
+ export const checkExtendLockPeriod = (
38
+ lockPeriodInDays: number,
39
+ newUnlockAtInSecondTimestamp: number,
40
+ prevUnlockAtInMillisTimestamp?: number
41
+ ) => {
42
+ checkVesca(prevUnlockAtInMillisTimestamp);
43
+ checkVescaExpired(prevUnlockAtInMillisTimestamp!);
44
+ const prevUnlockAtInSecondTimestamp = Math.floor(
45
+ prevUnlockAtInMillisTimestamp! / 1000
46
+ );
47
+ if (lockPeriodInDays < 1) {
48
+ throw new Error('Minimum lock period is 1 day');
49
+ }
50
+
51
+ const availableLockPeriodInDays = Math.floor(
52
+ (newUnlockAtInSecondTimestamp - prevUnlockAtInSecondTimestamp) /
53
+ UNLOCK_ROUND_DURATION
54
+ );
55
+ console.log('availableLockPeriodInDays', availableLockPeriodInDays);
56
+ if (lockPeriodInDays > availableLockPeriodInDays) {
57
+ throw new Error(
58
+ `Cannot extend lock period by ${lockPeriodInDays} days, maximum lock period is ~4 years (${MAX_LOCK_ROUNDS} days), remaining lock period is ${
59
+ MAX_LOCK_ROUNDS - availableLockPeriodInDays
60
+ }`
61
+ );
62
+ }
63
+ };
64
+
25
65
  export const checkLockSca = (
26
- scaAmountOrCoin?: number | SuiObjectArg | undefined,
66
+ scaAmountOrCoin: number | SuiObjectArg | undefined,
27
67
  lockPeriodInDays?: number,
28
68
  newUnlockAtInSecondTimestamp?: number,
29
- prevUnlockAtInSecondTimestamp?: number
69
+ prevUnlockAtInMillisTimestamp?: number
30
70
  ) => {
71
+ const prevUnlockAtInSecondTimestamp = prevUnlockAtInMillisTimestamp
72
+ ? Math.floor(prevUnlockAtInMillisTimestamp / 1000)
73
+ : undefined;
31
74
  const isInitialLock = !prevUnlockAtInSecondTimestamp;
32
75
  const isLockExpired =
33
76
  !isInitialLock &&
34
77
  prevUnlockAtInSecondTimestamp * 1000 <= new Date().getTime();
78
+
79
+ // handle for initial lock / renewing expired veSca
35
80
  if (isInitialLock || isLockExpired) {
36
81
  if (scaAmountOrCoin !== undefined && lockPeriodInDays !== undefined) {
37
82
  if (lockPeriodInDays <= 0) {
@@ -61,7 +106,9 @@ export const checkLockSca = (
61
106
  );
62
107
  }
63
108
  } else {
64
- checkVesca(prevUnlockAtInSecondTimestamp);
109
+ // handle for extending lock period / top up / both
110
+ checkVesca(prevUnlockAtInMillisTimestamp);
111
+ checkVescaExpired(prevUnlockAtInMillisTimestamp!);
65
112
  if (
66
113
  typeof scaAmountOrCoin === 'number' &&
67
114
  scaAmountOrCoin < MIN_TOP_UP_AMOUNT
@@ -69,43 +116,12 @@ export const checkLockSca = (
69
116
  throw new Error('Minimum top up amount is 1 SCA');
70
117
  }
71
118
 
72
- if (!!newUnlockAtInSecondTimestamp && !!prevUnlockAtInSecondTimestamp) {
73
- const totalLockDuration =
74
- newUnlockAtInSecondTimestamp - prevUnlockAtInSecondTimestamp;
75
- if (totalLockDuration > MAX_LOCK_DURATION - UNLOCK_ROUND_DURATION) {
76
- throw new Error(
77
- `Maximum lock period is ~4 years (${MAX_LOCK_ROUNDS - 1} days)`
78
- );
79
- }
80
- }
81
- }
82
- };
83
-
84
- export const checkExtendLockPeriod = (
85
- lockPeriodInDays: number,
86
- newUnlockAtInSecondTimestamp: number,
87
- prevUnlockAtInSecondTimestamp?: number
88
- ) => {
89
- checkVesca(prevUnlockAtInSecondTimestamp);
90
-
91
- if (lockPeriodInDays <= 0) {
92
- throw new Error('Lock period must be greater than 0');
93
- }
94
-
95
- const isInitialLock = !prevUnlockAtInSecondTimestamp;
96
- const isLockExpired =
97
- !isInitialLock &&
98
- prevUnlockAtInSecondTimestamp * 1000 <= new Date().getTime();
99
- if (isLockExpired) {
100
- throw new Error('veSca is expired, use renewExpiredVeScaQuick instead');
101
- }
102
-
103
- if (prevUnlockAtInSecondTimestamp) {
104
- const totalLockDuration =
105
- newUnlockAtInSecondTimestamp - prevUnlockAtInSecondTimestamp!;
106
- if (totalLockDuration > MAX_LOCK_DURATION - UNLOCK_ROUND_DURATION) {
107
- throw new Error(
108
- `Maximum lock period is ~4 years (${MAX_LOCK_ROUNDS - 1} days)`
119
+ // for topup and extend lock period
120
+ if (newUnlockAtInSecondTimestamp && lockPeriodInDays) {
121
+ checkExtendLockPeriod(
122
+ lockPeriodInDays,
123
+ newUnlockAtInSecondTimestamp,
124
+ prevUnlockAtInMillisTimestamp
109
125
  );
110
126
  }
111
127
  }
@@ -113,18 +129,19 @@ export const checkExtendLockPeriod = (
113
129
 
114
130
  export const checkExtendLockAmount = (
115
131
  scaAmount: number,
116
- prevUnlockAtInSecondTimestamp?: number
132
+ prevUnlockAtInMillisTimestamp?: number
117
133
  ) => {
118
- checkVesca(prevUnlockAtInSecondTimestamp);
134
+ checkVesca(prevUnlockAtInMillisTimestamp);
135
+ checkVescaExpired(prevUnlockAtInMillisTimestamp!);
119
136
 
120
137
  if (scaAmount < MIN_TOP_UP_AMOUNT) {
121
138
  throw new Error('Minimum top up amount is 1 SCA');
122
139
  }
123
140
 
124
- const isInitialLock = !prevUnlockAtInSecondTimestamp;
141
+ const isInitialLock = !prevUnlockAtInMillisTimestamp;
125
142
  const isLockExpired =
126
- !isInitialLock &&
127
- prevUnlockAtInSecondTimestamp * 1000 <= new Date().getTime();
143
+ !isInitialLock && prevUnlockAtInMillisTimestamp <= new Date().getTime();
144
+
128
145
  if (isLockExpired) {
129
146
  throw new Error('veSca is expired, use renewExpiredVeScaQuick instead');
130
147
  }
@@ -133,9 +150,14 @@ export const checkExtendLockAmount = (
133
150
  export const checkRenewExpiredVeSca = (
134
151
  scaAmount: number,
135
152
  lockPeriodInDays: number,
136
- prevUnlockAtInSecondTimestamp?: number
153
+ prevUnlockAtInMillisTimestamp?: number
137
154
  ) => {
138
- checkVesca(prevUnlockAtInSecondTimestamp);
155
+ if (
156
+ !prevUnlockAtInMillisTimestamp ||
157
+ prevUnlockAtInMillisTimestamp > new Date().getTime()
158
+ ) {
159
+ throw new Error('Renew method can only be used for expired veSca');
160
+ }
139
161
 
140
162
  if (scaAmount < MIN_INITIAL_LOCK_AMOUNT) {
141
163
  throw new Error('Minimum lock amount for renewing expired vesca 10 SCA');
@@ -148,9 +170,3 @@ export const checkRenewExpiredVeSca = (
148
170
  );
149
171
  }
150
172
  };
151
-
152
- export const checkVesca = (prevUnlockAtInSecondTimestamp?: number) => {
153
- if (prevUnlockAtInSecondTimestamp === undefined) {
154
- throw new Error('veSca not found');
155
- }
156
- };
@@ -173,19 +173,20 @@ export const calculateMarketPoolData = (
173
173
  export const parseOriginMarketCollateralData = (
174
174
  originMarketCollateralData: OriginMarketCollateralData
175
175
  ): ParsedMarketCollateralData => {
176
+ const divisor = 2 ** 32;
176
177
  return {
177
178
  coinType: normalizeStructTag(originMarketCollateralData.type.name),
178
179
  collateralFactor:
179
- Number(originMarketCollateralData.collateralFactor.value) / 2 ** 32,
180
+ Number(originMarketCollateralData.collateralFactor.value) / divisor,
180
181
  liquidationFactor:
181
- Number(originMarketCollateralData.liquidationFactor.value) / 2 ** 32,
182
+ Number(originMarketCollateralData.liquidationFactor.value) / divisor,
182
183
  liquidationDiscount:
183
- Number(originMarketCollateralData.liquidationDiscount.value) / 2 ** 32,
184
+ Number(originMarketCollateralData.liquidationDiscount.value) / divisor,
184
185
  liquidationPanelty:
185
- Number(originMarketCollateralData.liquidationPanelty.value) / 2 ** 32,
186
+ Number(originMarketCollateralData.liquidationPanelty.value) / divisor,
186
187
  liquidationReserveFactor:
187
188
  Number(originMarketCollateralData.liquidationReserveFactor.value) /
188
- 2 ** 32,
189
+ divisor,
189
190
  maxCollateralAmount: Number(originMarketCollateralData.maxCollateralAmount),
190
191
  totalCollateralAmount: Number(
191
192
  originMarketCollateralData.totalCollateralAmount