@zofai/zo-sdk 0.2.8 → 0.2.10

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 (112) hide show
  1. package/dist/abstract/BaseAPI.cjs +7 -1
  2. package/dist/abstract/BaseAPI.cjs.map +1 -1
  3. package/dist/abstract/BaseAPI.d.cts +8 -7
  4. package/dist/abstract/BaseAPI.d.cts.map +1 -1
  5. package/dist/abstract/BaseAPI.d.mts +8 -7
  6. package/dist/abstract/BaseAPI.d.mts.map +1 -1
  7. package/dist/abstract/BaseAPI.mjs +7 -1
  8. package/dist/abstract/BaseAPI.mjs.map +1 -1
  9. package/dist/api.cjs +12 -12
  10. package/dist/api.cjs.map +1 -1
  11. package/dist/api.d.cts.map +1 -1
  12. package/dist/api.d.mts.map +1 -1
  13. package/dist/api.mjs +12 -12
  14. package/dist/api.mjs.map +1 -1
  15. package/dist/coins.cjs +74 -0
  16. package/dist/coins.cjs.map +1 -0
  17. package/dist/coins.d.cts +37 -0
  18. package/dist/coins.d.cts.map +1 -0
  19. package/dist/coins.d.mts +37 -0
  20. package/dist/coins.d.mts.map +1 -0
  21. package/dist/coins.mjs +67 -0
  22. package/dist/coins.mjs.map +1 -0
  23. package/dist/consts/deployments-usdz-mainnet.json +1 -1
  24. package/dist/implementations/SLPAPI.cjs +192 -100
  25. package/dist/implementations/SLPAPI.cjs.map +1 -1
  26. package/dist/implementations/SLPAPI.d.cts +23 -23
  27. package/dist/implementations/SLPAPI.d.cts.map +1 -1
  28. package/dist/implementations/SLPAPI.d.mts +23 -23
  29. package/dist/implementations/SLPAPI.d.mts.map +1 -1
  30. package/dist/implementations/SLPAPI.mjs +192 -100
  31. package/dist/implementations/SLPAPI.mjs.map +1 -1
  32. package/dist/implementations/SLPDataAPI.cjs +2 -2
  33. package/dist/implementations/SLPDataAPI.cjs.map +1 -1
  34. package/dist/implementations/SLPDataAPI.mjs +2 -2
  35. package/dist/implementations/SLPDataAPI.mjs.map +1 -1
  36. package/dist/implementations/USDZAPI.cjs +176 -92
  37. package/dist/implementations/USDZAPI.cjs.map +1 -1
  38. package/dist/implementations/USDZAPI.d.cts +21 -21
  39. package/dist/implementations/USDZAPI.d.cts.map +1 -1
  40. package/dist/implementations/USDZAPI.d.mts +21 -21
  41. package/dist/implementations/USDZAPI.d.mts.map +1 -1
  42. package/dist/implementations/USDZAPI.mjs +176 -92
  43. package/dist/implementations/USDZAPI.mjs.map +1 -1
  44. package/dist/implementations/ZBTCVCAPI.cjs +84 -46
  45. package/dist/implementations/ZBTCVCAPI.cjs.map +1 -1
  46. package/dist/implementations/ZBTCVCAPI.d.cts +11 -11
  47. package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -1
  48. package/dist/implementations/ZBTCVCAPI.d.mts +11 -11
  49. package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -1
  50. package/dist/implementations/ZBTCVCAPI.mjs +84 -46
  51. package/dist/implementations/ZBTCVCAPI.mjs.map +1 -1
  52. package/dist/implementations/ZLPAPI.cjs +192 -100
  53. package/dist/implementations/ZLPAPI.cjs.map +1 -1
  54. package/dist/implementations/ZLPAPI.d.cts +23 -23
  55. package/dist/implementations/ZLPAPI.d.cts.map +1 -1
  56. package/dist/implementations/ZLPAPI.d.mts +23 -23
  57. package/dist/implementations/ZLPAPI.d.mts.map +1 -1
  58. package/dist/implementations/ZLPAPI.mjs +192 -100
  59. package/dist/implementations/ZLPAPI.mjs.map +1 -1
  60. package/dist/index.cjs +1 -0
  61. package/dist/index.cjs.map +1 -1
  62. package/dist/index.d.cts +1 -0
  63. package/dist/index.d.cts.map +1 -1
  64. package/dist/index.d.mts +1 -0
  65. package/dist/index.d.mts.map +1 -1
  66. package/dist/index.mjs +1 -0
  67. package/dist/index.mjs.map +1 -1
  68. package/dist/interfaces/base.d.cts +25 -21
  69. package/dist/interfaces/base.d.cts.map +1 -1
  70. package/dist/interfaces/base.d.mts +25 -21
  71. package/dist/interfaces/base.d.mts.map +1 -1
  72. package/dist/interfaces/slp.d.cts +1 -1
  73. package/dist/interfaces/slp.d.cts.map +1 -1
  74. package/dist/interfaces/slp.d.mts +1 -1
  75. package/dist/interfaces/slp.d.mts.map +1 -1
  76. package/dist/interfaces/usdz.d.cts +1 -1
  77. package/dist/interfaces/usdz.d.cts.map +1 -1
  78. package/dist/interfaces/usdz.d.mts +1 -1
  79. package/dist/interfaces/usdz.d.mts.map +1 -1
  80. package/dist/interfaces/zbtcvc.d.cts +1 -1
  81. package/dist/interfaces/zbtcvc.d.cts.map +1 -1
  82. package/dist/interfaces/zbtcvc.d.mts +1 -1
  83. package/dist/interfaces/zbtcvc.d.mts.map +1 -1
  84. package/dist/interfaces/zlp.d.cts +1 -1
  85. package/dist/interfaces/zlp.d.cts.map +1 -1
  86. package/dist/interfaces/zlp.d.mts +1 -1
  87. package/dist/interfaces/zlp.d.mts.map +1 -1
  88. package/dist/oracle.cjs +27 -22
  89. package/dist/oracle.cjs.map +1 -1
  90. package/dist/oracle.d.cts +13 -1
  91. package/dist/oracle.d.cts.map +1 -1
  92. package/dist/oracle.d.mts +13 -1
  93. package/dist/oracle.d.mts.map +1 -1
  94. package/dist/oracle.mjs +27 -22
  95. package/dist/oracle.mjs.map +1 -1
  96. package/package.json +1 -1
  97. package/src/abstract/BaseAPI.ts +10 -8
  98. package/src/api.ts +17 -13
  99. package/src/coins.ts +115 -0
  100. package/src/consts/deployments-usdz-mainnet.json +1 -1
  101. package/src/implementations/SLPAPI.ts +201 -117
  102. package/src/implementations/SLPDataAPI.ts +2 -2
  103. package/src/implementations/USDZAPI.ts +184 -94
  104. package/src/implementations/ZBTCVCAPI.ts +98 -48
  105. package/src/implementations/ZLPAPI.ts +199 -102
  106. package/src/index.ts +1 -0
  107. package/src/interfaces/base.ts +13 -21
  108. package/src/interfaces/slp.ts +0 -1
  109. package/src/interfaces/usdz.ts +0 -1
  110. package/src/interfaces/zbtcvc.ts +0 -1
  111. package/src/interfaces/zlp.ts +0 -1
  112. package/src/oracle.ts +64 -28
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ export * from './interfaces'
24
24
  // Export legacy components for backward compatibility
25
25
  export * from './api'
26
26
  export * from './bcs'
27
+ export * from './coins'
27
28
  export * from './consts'
28
29
  export * from './data'
29
30
  export * from './oracle'
@@ -9,6 +9,7 @@ import type { Transaction, TransactionObjectArgument } from '@mysten/sui/transac
9
9
  import type { PriceFeed } from '@pythnetwork/pyth-sui-js'
10
10
 
11
11
  import type { IConsts, LPToken, Network } from '../consts'
12
+ import type { IInitOracleTxbResult } from '../oracle'
12
13
 
13
14
  // Common data interfaces
14
15
  export interface IBaseMarketValuationInfo {
@@ -293,7 +294,12 @@ export interface IBaseDataAPI {
293
294
  getOraclePrice: (tokenId: string) => Promise<PriceFeed>
294
295
  getOraclePrices: (tokens: string[]) => Promise<PriceFeed[]>
295
296
  subOraclePrices: (tokens: string[], callback: (price: PriceFeed) => void) => Promise<() => void>
296
- initOracleTxb: (tokens: string[], tx?: Transaction) => Promise<Transaction>
297
+ initOracleTxb: (
298
+ tokens: string[],
299
+ tx?: Transaction,
300
+ sponsoredTx?: boolean,
301
+ sponsorOptions?: { sender: string, additionalSuiAmount?: bigint },
302
+ ) => Promise<IInitOracleTxbResult>
297
303
 
298
304
  // Utility methods
299
305
  validateCache: () => void
@@ -309,7 +315,6 @@ export interface IBaseAPI {
309
315
  referralAddress?: string,
310
316
  sender?: string,
311
317
  sponsoredTx?: boolean,
312
- suiCoinObjectsForPythUpdate?: string[],
313
318
  ) => Promise<Transaction>
314
319
 
315
320
  withdraw: (
@@ -318,7 +323,6 @@ export interface IBaseAPI {
318
323
  amount: number,
319
324
  minAmountOut?: number,
320
325
  sponsoredTx?: boolean,
321
- suiCoinObjectsForPythUpdate?: string[],
322
326
  ) => Promise<Transaction>
323
327
 
324
328
  swap: (
@@ -362,7 +366,6 @@ export interface IBaseAPI {
362
366
  referralAddress?: string,
363
367
  sender?: string,
364
368
  sponsoredTx?: boolean,
365
- suiCoinObjectsForPythUpdate?: string[],
366
369
  ) => Promise<Transaction>
367
370
 
368
371
  // Trading operations
@@ -384,7 +387,6 @@ export interface IBaseAPI {
384
387
  sender?: string,
385
388
  tx?: Transaction,
386
389
  sponsoredTx?: boolean,
387
- suiCoinObjectsForPythUpdate?: string[],
388
390
  ) => Promise<Transaction>
389
391
 
390
392
  decreasePosition: (
@@ -403,7 +405,7 @@ export interface IBaseAPI {
403
405
  relayerFee?: bigint,
404
406
  coinObjects?: string[],
405
407
  sponsoredTx?: boolean,
406
- suiCoinObjectsForPythUpdate?: string[],
408
+ sender?: string,
407
409
  ) => Promise<Transaction>
408
410
 
409
411
  decreaseMultiPositions: (
@@ -425,7 +427,7 @@ export interface IBaseAPI {
425
427
  }>,
426
428
  tx?: Transaction,
427
429
  sponsoredTx?: boolean,
428
- suiCoinObjectsForPythUpdate?: string[],
430
+ sender?: string,
429
431
  ) => Promise<Transaction>
430
432
 
431
433
  openPositionV2?: (
@@ -446,7 +448,6 @@ export interface IBaseAPI {
446
448
  referralAddress?: string,
447
449
  sender?: string,
448
450
  sponsoredTx?: boolean,
449
- suiCoinObjectsForPythUpdate?: string[],
450
451
  ) => Promise<Transaction>
451
452
 
452
453
  // Trading operations
@@ -468,7 +469,6 @@ export interface IBaseAPI {
468
469
  sender?: string,
469
470
  tx?: Transaction,
470
471
  sponsoredTx?: boolean,
471
- suiCoinObjectsForPythUpdate?: string[],
472
472
  ) => Promise<Transaction>
473
473
 
474
474
  decreasePositionV2?: (
@@ -487,7 +487,7 @@ export interface IBaseAPI {
487
487
  relayerFee?: bigint,
488
488
  coinObjects?: string[],
489
489
  sponsoredTx?: boolean,
490
- suiCoinObjectsForPythUpdate?: string[],
490
+ sender?: string,
491
491
  ) => Promise<Transaction>
492
492
 
493
493
  decreaseMultiPositionsV2?: (
@@ -509,7 +509,7 @@ export interface IBaseAPI {
509
509
  }>,
510
510
  tx?: Transaction,
511
511
  sponsoredTx?: boolean,
512
- suiCoinObjectsForPythUpdate?: string[],
512
+ sender?: string,
513
513
  feeObjects?: TransactionObjectArgument[],
514
514
  ) => Promise<Transaction>
515
515
 
@@ -521,7 +521,7 @@ export interface IBaseAPI {
521
521
  coinObjects: string[],
522
522
  long: boolean,
523
523
  sponsoredTx?: boolean,
524
- suiCoinObjectsForPythUpdate?: string[],
524
+ sender?: string,
525
525
  ) => Promise<Transaction>
526
526
 
527
527
  redeemFromPosition: (
@@ -531,7 +531,6 @@ export interface IBaseAPI {
531
531
  amount: number,
532
532
  long: boolean,
533
533
  sponsoredTx?: boolean,
534
- suiCoinObjectsForPythUpdate?: string[],
535
534
  ) => Promise<Transaction>
536
535
 
537
536
  cancelOrder: (
@@ -621,7 +620,6 @@ export interface IBaseAPI {
621
620
  referralAddress?: string,
622
621
  sender?: string,
623
622
  sponsoredTx?: boolean,
624
- suiCoinObjectsForPythUpdate?: string[],
625
623
  ) => Promise<Transaction>
626
624
 
627
625
  // S Card operations
@@ -646,7 +644,6 @@ export interface IBaseAPI {
646
644
  sender?: string,
647
645
  tx?: Transaction,
648
646
  sponsoredTx?: boolean,
649
- suiCoinObjectsForPythUpdate?: string[],
650
647
  ) => Promise<Transaction>
651
648
 
652
649
  decreasePositionWithSCard: (
@@ -668,7 +665,6 @@ export interface IBaseAPI {
668
665
  relayerFee?: bigint,
669
666
  coinObjects?: string[],
670
667
  sponsoredTx?: boolean,
671
- suiCoinObjectsForPythUpdate?: string[],
672
668
  ) => Promise<Transaction>
673
669
 
674
670
  decreaseMultiPositionsWithSCard: (
@@ -693,7 +689,6 @@ export interface IBaseAPI {
693
689
  scard: string,
694
690
  tx?: Transaction,
695
691
  sponsoredTx?: boolean,
696
- suiCoinObjectsForPythUpdate?: string[],
697
692
  ) => Promise<Transaction>
698
693
 
699
694
  openPositionWithSCardV2?: (
@@ -717,7 +712,6 @@ export interface IBaseAPI {
717
712
  referralAddress?: string,
718
713
  sender?: string,
719
714
  sponsoredTx?: boolean,
720
- suiCoinObjectsForPythUpdate?: string[],
721
715
  ) => Promise<Transaction>
722
716
 
723
717
  // S Card operations
@@ -742,7 +736,6 @@ export interface IBaseAPI {
742
736
  sender?: string,
743
737
  tx?: Transaction,
744
738
  sponsoredTx?: boolean,
745
- suiCoinObjectsForPythUpdate?: string[],
746
739
  ) => Promise<Transaction>
747
740
 
748
741
  decreasePositionWithSCardV2?: (
@@ -764,7 +757,6 @@ export interface IBaseAPI {
764
757
  relayerFee?: bigint,
765
758
  coinObjects?: string[],
766
759
  sponsoredTx?: boolean,
767
- suiCoinObjectsForPythUpdate?: string[],
768
760
  ) => Promise<Transaction>
769
761
 
770
762
  decreaseMultiPositionsWithSCardV2?: (
@@ -789,7 +781,7 @@ export interface IBaseAPI {
789
781
  scard: string,
790
782
  tx?: Transaction,
791
783
  sponsoredTx?: boolean,
792
- suiCoinObjectsForPythUpdate?: string[],
784
+ sender?: string,
793
785
  feeObjects?: TransactionObjectArgument[],
794
786
  ) => Promise<Transaction>
795
787
 
@@ -239,7 +239,6 @@ export interface ISLPAPI extends IBaseAPI {
239
239
  sender?: string,
240
240
  tx?: Transaction,
241
241
  sponsoredTx?: boolean,
242
- suiCoinObjectsForPythUpdate?: string[],
243
242
  ) => Promise<TransactionObjectArgument>
244
243
 
245
244
  withdrawLpCoinObject: (
@@ -152,7 +152,6 @@ export interface IUSDZAPI extends IBaseAPI {
152
152
  sender?: string,
153
153
  tx?: Transaction,
154
154
  sponsoredTx?: boolean,
155
- suiCoinObjectsForPythUpdate?: string[],
156
155
  ) => Promise<TransactionObjectArgument>
157
156
 
158
157
  withdrawLpCoinObject: (
@@ -105,7 +105,6 @@ export interface IZBTCVCAPI extends IBaseAPI {
105
105
  sender?: string,
106
106
  tx?: Transaction,
107
107
  sponsoredTx?: boolean,
108
- suiCoinObjectsForPythUpdate?: string[],
109
108
  ) => Promise<TransactionObjectArgument>
110
109
 
111
110
  stakeCoinObject: (
@@ -215,7 +215,6 @@ export interface IZLPAPI extends IBaseAPI {
215
215
  sender?: string,
216
216
  tx?: Transaction,
217
217
  sponsoredTx?: boolean,
218
- suiCoinObjectsForPythUpdate?: string[],
219
218
  ) => Promise<TransactionObjectArgument>
220
219
 
221
220
  stakeCoinObject: (
package/src/oracle.ts CHANGED
@@ -1,12 +1,26 @@
1
1
  import type { SuiClient } from '@mysten/sui/client'
2
+ import type { TransactionObjectArgument } from '@mysten/sui/transactions'
2
3
  import { Transaction } from '@mysten/sui/transactions'
3
4
  import type { PriceFeed } from '@pythnetwork/pyth-sui-js'
4
5
  import { SuiPriceServiceConnection, SuiPythClient } from '@pythnetwork/pyth-sui-js'
5
6
 
7
+ import { resolveSpendableSuiCoin } from './coins'
6
8
  import type { IConsts, Network } from './consts'
7
9
  import { getConsts, getPriceIdToPythFeeder, getPythFeederToId, getPythFeederToPriceId, getSharedConfig, LPToken } from './consts'
8
10
  import { createJsonRpcProvider } from './utils'
9
11
 
12
+ export interface IInitOracleSponsorOptions {
13
+ sender: string
14
+ /** SUI (MIST) needed beyond Pyth update fees, e.g. SUI collateral deposit + relayer fee. */
15
+ additionalSuiAmount?: bigint
16
+ }
17
+
18
+ export interface IInitOracleTxbResult {
19
+ tx: Transaction
20
+ /** Remaining SUI coin after Pyth fee splits; present when sponsored and SUI was resolved. */
21
+ suiCoinObject?: TransactionObjectArgument
22
+ }
23
+
10
24
  export class OracleAPI {
11
25
  network: Network
12
26
  consts: IConsts
@@ -90,39 +104,47 @@ export class OracleAPI {
90
104
  }
91
105
  }
92
106
 
93
- async initOracleTxb(
94
- tokens: string[],
95
- tx?: Transaction,
96
- sponsoredTx?: boolean,
97
- suiCoinObject?: any,
98
- ) {
99
- let tx_ = tx
100
- if (!tx_) {
101
- tx_ = new Transaction()
102
- }
103
- // Remove redundant tokens first
107
+ async getStalePriceFeedIds(tokens: string[]): Promise<string[]> {
104
108
  const uniqueTokens = Array.from(new Set(tokens))
105
-
106
- const connection = new SuiPriceServiceConnection(this.connectionURL, {
107
- priceFeedRequestConfig: {
108
- binary: true,
109
- },
110
- })
111
109
  let pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token])
112
- // remove dupe object ids. If any of the object ids are duplicates the call will fail
113
110
  pythObjectIds = [...new Set(pythObjectIds)].filter(Boolean)
114
- const needUpdateObjectIds = (await this.provider.multiGetObjects({
111
+
112
+ const objects = await this.provider.multiGetObjects({
115
113
  ids: pythObjectIds,
116
- options: {
117
- showContent: true,
118
- },
119
- })).map(pythObject => [
114
+ options: { showContent: true },
115
+ })
116
+
117
+ return objects.map(pythObject => [
120
118
  Number.parseInt((pythObject.data?.content as any).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
121
119
  pythObject.data?.objectId,
122
120
  ]).filter((x: any) => Math.abs(x[0]) >= 0).map(x => x[1] as string)
121
+ }
122
+
123
+ async initOracleTxb(
124
+ tokens: string[],
125
+ tx?: Transaction,
126
+ sponsoredTx?: boolean,
127
+ sponsorOptions?: IInitOracleSponsorOptions,
128
+ ): Promise<IInitOracleTxbResult> {
129
+ const tx_ = tx ?? new Transaction()
130
+
131
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
132
+ priceFeedRequestConfig: { binary: true },
133
+ })
134
+
135
+ const needUpdateObjectIds = await this.getStalePriceFeedIds(tokens)
123
136
 
124
137
  if (needUpdateObjectIds.length === 0) {
125
- return tx_
138
+ if (sponsoredTx && sponsorOptions && (sponsorOptions.additionalSuiAmount ?? 0n) > 0n) {
139
+ const suiCoinObject = await resolveSpendableSuiCoin(
140
+ tx_,
141
+ this.provider,
142
+ sponsorOptions.sender,
143
+ sponsorOptions.additionalSuiAmount!,
144
+ )
145
+ return { tx: tx_, suiCoinObject }
146
+ }
147
+ return { tx: tx_ }
126
148
  }
127
149
 
128
150
  const priceFeedIds = needUpdateObjectIds
@@ -133,18 +155,32 @@ export class OracleAPI {
133
155
  const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds)
134
156
 
135
157
  if (sponsoredTx) {
158
+ if (!sponsorOptions?.sender) {
159
+ throw new Error('sender is required for sponsored transactions that update Pyth price feeds')
160
+ }
161
+
136
162
  const baseUpdateFee = await this.getBaseUpdateFee()
163
+ const pythCost = BigInt(baseUpdateFee) * BigInt(priceFeedIds.length)
164
+ const totalSui = pythCost + (sponsorOptions.additionalSuiAmount ?? 0n)
165
+
166
+ const suiCoinObject = await resolveSpendableSuiCoin(
167
+ tx_,
168
+ this.provider,
169
+ sponsorOptions.sender,
170
+ totalSui,
171
+ )
172
+
137
173
  const pythUpdateCoins = tx_.splitCoins(
138
174
  suiCoinObject,
139
175
  priceFeedIds.map(() => tx_.pure.u64(baseUpdateFee)),
140
176
  )
141
177
  await this.client.updatePriceFeedsWithCoins(tx_, priceUpdateData, priceFeedIds, pythUpdateCoins)
142
- }
143
- else {
144
- await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds)
178
+
179
+ return { tx: tx_, suiCoinObject }
145
180
  }
146
181
 
147
- return tx_
182
+ await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds)
183
+ return { tx: tx_ }
148
184
  }
149
185
 
150
186
  async getBaseUpdateFee(): Promise<number> {