zo-sdk 0.0.43 → 0.0.45

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.
package/src/data.ts CHANGED
@@ -156,21 +156,21 @@ export interface IPositionConfig {
156
156
  }
157
157
 
158
158
  export interface IHistory {
159
- owner: string;
160
- txid: string;
161
- id: string;
162
- created: number;
163
- eventName: string;
164
- indexToken: string;
165
- direction: string;
166
- collateralAmount: number;
167
- collateralPrice: number;
168
- indexPrice: number;
169
- pnl: number;
170
- positionId: string;
171
- volume: number;
172
- fee: number;
173
- network: string;
159
+ owner: string
160
+ txid: string
161
+ id: string
162
+ created: number
163
+ eventName: string
164
+ indexToken: string
165
+ direction: string
166
+ collateralAmount: number
167
+ collateralPrice: number
168
+ indexPrice: number
169
+ pnl: number
170
+ positionId: string
171
+ volume: number
172
+ fee: number
173
+ network: string
174
174
  }
175
175
 
176
176
  export interface IStaked {
@@ -199,16 +199,6 @@ export interface IStakePool {
199
199
  lockDuration: number
200
200
  }
201
201
 
202
- export interface IMarketValidationResult {
203
- handledVaults: Record<string, {
204
- weight: number;
205
- value: number;
206
- }>;
207
- totalWeight: number;
208
- totalVaultsValue: number;
209
- marketValue: number;
210
- }
211
-
212
202
  export class DataAPI extends OracleAPI {
213
203
  provider: SuiClient
214
204
  apiEndpoint: string
@@ -315,30 +305,6 @@ export class DataAPI extends OracleAPI {
315
305
  }
316
306
  }
317
307
 
318
- /**
319
- * Validates the market valuation by calling the validate_market_valuation Move function
320
- * @param tx Transaction object
321
- * @returns An object containing handled vaults, total weight, total vaults value, and market value
322
- */
323
- validateMarketValuation = (tx: Transaction) => {
324
- // First, create the valuations using existing methods
325
- const vaultsValuation = this.valuateVaults(tx);
326
- const symbolsValuation = this.valuateSymbols(tx);
327
-
328
- // Call the validate_market_valuation Move function
329
- const result = tx.moveCall({
330
- target: `${this.consts.zoCore.upgradedPackage}::market::validate_market_valuation`,
331
- typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`],
332
- arguments: [
333
- tx.object(this.consts.zoCore.market),
334
- vaultsValuation,
335
- symbolsValuation,
336
- ],
337
- });
338
-
339
- return result;
340
- }
341
-
342
308
  #parseMarketInfo(raw: any): IMarketInfo {
343
309
  const content = raw.data.content.fields
344
310
 
@@ -715,147 +681,106 @@ export class DataAPI extends OracleAPI {
715
681
  }
716
682
 
717
683
  public async getPositionCapInfoList(owner: string): Promise<IPositionCapInfo[]> {
718
- const positionCapInfoList: IPositionCapInfo[] = [];
719
- let cursor: string | undefined | null = undefined;
720
- let hasNextPage = true;
721
-
722
- while (hasNextPage) {
723
- const positionCaps = await this.provider.getOwnedObjects({
724
- owner,
725
- filter: {
726
- MoveModule: {
727
- package: this.consts.zoCore.package,
728
- module: 'market',
729
- },
730
- },
731
- options: {
732
- showType: true,
684
+ const positionCaps = await this.provider.getOwnedObjects({
685
+ owner,
686
+ filter: {
687
+ MoveModule: {
688
+ package: this.consts.zoCore.package,
689
+ module: 'market',
733
690
  },
734
- cursor,
735
- })
736
-
737
- for (const positionCap of positionCaps.data) {
738
- if (positionCap.data?.type?.includes('PositionCap')) {
739
- positionCapInfoList.push({
740
- positionCapId: positionCap.data.objectId,
741
- symbol0: positionCap.data.type.split('<')[1].split(',')[0].trim(),
742
- symbol1: positionCap.data.type.split('<')[1].split(',')[1].split(',')[0].trim(),
743
- long: positionCap.data.type.includes('LONG'),
744
- })
745
- }
691
+ },
692
+ options: {
693
+ showType: true,
694
+ },
695
+ })
696
+ const positionCapInfoList = []
697
+ for (const positionCap of positionCaps.data) {
698
+ if (positionCap.data?.type?.includes('PositionCap')) {
699
+ positionCapInfoList.push({
700
+ positionCapId: positionCap.data.objectId,
701
+ symbol0: positionCap.data.type.split('<')[1].split(',')[0].trim(),
702
+ symbol1: positionCap.data.type.split('<')[1].split(',')[1].split(',')[0].trim(),
703
+ long: positionCap.data.type.includes('LONG'),
704
+ })
746
705
  }
747
-
748
- // If we don't want to fetch all pages or there are no more pages, break the loop
749
- hasNextPage = positionCaps.hasNextPage
750
- cursor = positionCaps.nextCursor
751
706
  }
752
-
753
707
  return positionCapInfoList
754
708
  }
755
709
 
756
- public async getPositionInfoList(positionCapInfoList: IPositionCapInfo[], owner: string, batchSize: number = 10) {
710
+ public async getPositionInfoList(positionCapInfoList: IPositionCapInfo[], owner: string) {
757
711
  const positionInfoList: IPositionInfo[] = []
758
-
759
- // Process in batches of 10
760
- for (let i = 0; i < positionCapInfoList.length; i += batchSize) {
761
- const batch = positionCapInfoList.slice(i, i + batchSize)
762
-
763
- await Promise.all(batch.map(async (positionCapInfo) => {
764
- try {
765
- const positionRaw = await this.provider.getDynamicFieldObject({
766
- parentId: this.consts.zoCore.positionsParent,
767
- name: {
768
- type: `${this.consts.zoCore.package}::market::PositionName<${positionCapInfo.symbol0}, ${positionCapInfo.symbol1}, ${this.consts.zoCore.package}::market::${positionCapInfo.long ? 'LONG' : 'SHORT'}>`,
769
- value: {
770
- owner,
771
- id: positionCapInfo.positionCapId,
772
- },
712
+ await Promise.all(positionCapInfoList.map(async (positionCapInfo) => {
713
+ try {
714
+ const positionRaw = await this.provider.getDynamicFieldObject({
715
+ parentId: this.consts.zoCore.positionsParent,
716
+ name: {
717
+ type: `${this.consts.zoCore.package}::market::PositionName<${positionCapInfo.symbol0}, ${positionCapInfo.symbol1}, ${this.consts.zoCore.package}::market::${positionCapInfo.long ? 'LONG' : 'SHORT'}>`,
718
+ value: {
719
+ owner,
720
+ id: positionCapInfo.positionCapId,
773
721
  },
774
- })
775
- positionInfoList.push(await this.#parsePositionInfo(positionRaw, positionCapInfo.positionCapId))
776
- } catch (error) {
777
- // Position might have been deleted after force settlement
778
- console.warn(`Failed to parse position info for position cap ID ${positionCapInfo.positionCapId}: ${error}`)
779
- // Continue with next position without adding this one to the list
780
- }
781
- }))
782
- }
722
+ },
723
+ })
724
+ positionInfoList.push(await this.#parsePositionInfo(positionRaw, positionCapInfo.positionCapId))
725
+ }
726
+ catch (error) {
727
+ // Position might have been deleted after force settlement
728
+ console.warn(`Failed to parse position info for position cap ID ${positionCapInfo.positionCapId}: ${error}`)
729
+ // Continue with next position without adding this one to the list
730
+ }
731
+ }))
783
732
 
784
733
  return positionInfoList.sort((a, b) => a.openTimestamp > b.openTimestamp ? 1 : -1)
785
734
  }
786
735
 
787
- public async getOrderCapInfoList(owner: string): Promise<IOrderCapInfo[]> {
788
- const orderCapInfoList: IOrderCapInfo[] = [];
789
- let cursor: string | undefined | null = undefined;
790
- let hasNextPage = true;
791
-
792
- while (hasNextPage) {
793
- const orderCaps = await this.provider.getOwnedObjects({
794
- owner,
795
- filter: {
796
- MoveModule: {
797
- package: this.consts.zoCore.package,
798
- module: 'market',
799
- },
800
- },
801
- options: {
802
- showType: true,
803
- showContent: true,
736
+ public async getOrderCapInfoList(owner: string) {
737
+ const orderCaps = await this.provider.getOwnedObjects({
738
+ owner,
739
+ filter: {
740
+ MoveModule: {
741
+ package: this.consts.zoCore.package,
742
+ module: 'market',
804
743
  },
805
- cursor,
806
- })
807
-
808
- for (const orderCap of orderCaps.data) {
809
- if (orderCap.data?.type?.includes('OrderCap')) {
810
- orderCapInfoList.push({
811
- orderCapId: orderCap.data.objectId,
812
- symbol0: orderCap.data.type.split('<')[1].split(',')[0].trim(),
813
- symbol1: orderCap.data.type.split('<')[1].split(',')[1].split(',')[0].trim(),
814
- long: orderCap.data.type.includes('LONG'),
815
- positionId: (orderCap.data.content as any)?.fields?.position_id,
816
- })
817
- }
744
+ },
745
+ options: {
746
+ showType: true,
747
+ showContent: true,
748
+ },
749
+ })
750
+ const orderCapInfoList = []
751
+ for (const orderCap of orderCaps.data) {
752
+ if (orderCap.data?.type?.includes('OrderCap')) {
753
+ orderCapInfoList.push({
754
+ orderCapId: orderCap.data.objectId,
755
+ symbol0: orderCap.data.type.split('<')[1].split(',')[0].trim(),
756
+ symbol1: orderCap.data.type.split('<')[1].split(',')[1].split(',')[0].trim(),
757
+ long: orderCap.data.type.includes('LONG'),
758
+ positionId: (orderCap.data.content as any)?.fields?.position_id,
759
+ })
818
760
  }
819
-
820
- hasNextPage = orderCaps.hasNextPage;
821
- cursor = orderCaps.nextCursor;
822
761
  }
823
-
824
762
  return orderCapInfoList
825
763
  }
826
764
 
827
- public async getOrderInfoList(orderCapInfoList: IOrderCapInfo[], owner: string, batchSize: number = 10) {
765
+ public async getOrderInfoList(orderCapInfoList: IOrderCapInfo[], owner: string) {
828
766
  const orderInfoList: IOrderInfo[] = []
829
-
830
- // Process in batches of 10
831
- for (let i = 0; i < orderCapInfoList.length; i += batchSize) {
832
- const batch = orderCapInfoList.slice(i, i + batchSize)
833
-
834
- await Promise.all(batch.map(async (orderCapInfo) => {
835
- try {
836
- const orderRaw = await this.provider.getDynamicFieldObject({
837
- parentId: this.consts.zoCore.ordersParent,
838
- name: {
839
- // We have enforced collateral coin type to match with fee coin type so we can use symbol0 in the first slot and the last slot of the OrderName
840
- type: `${this.consts.zoCore.package}::market::OrderName<${orderCapInfo.symbol0}, ${orderCapInfo.symbol1}, ${this.consts.zoCore.package}::market::${orderCapInfo.long ? 'LONG' : 'SHORT'}, ${orderCapInfo.symbol0}>`,
841
- value: {
842
- owner,
843
- id: orderCapInfo.orderCapId,
844
- position_id: {
845
- vec: orderCapInfo.positionId ? [orderCapInfo.positionId] : [],
846
- },
847
- },
767
+ await Promise.all(orderCapInfoList.map(async (orderCapInfo) => {
768
+ const orderRaw = await this.provider.getDynamicFieldObject({
769
+ parentId: this.consts.zoCore.ordersParent,
770
+ name: {
771
+ // We have enforced collateral coin type to match with fee coin type so we can use symbol0 in the first slot and the last slot of the OrderName
772
+ type: `${this.consts.zoCore.package}::market::OrderName<${orderCapInfo.symbol0}, ${orderCapInfo.symbol1}, ${this.consts.zoCore.package}::market::${orderCapInfo.long ? 'LONG' : 'SHORT'}, ${orderCapInfo.symbol0}>`,
773
+ value: {
774
+ owner,
775
+ id: orderCapInfo.orderCapId,
776
+ position_id: {
777
+ vec: orderCapInfo.positionId ? [orderCapInfo.positionId] : [],
848
778
  },
849
- })
850
- orderInfoList.push(this.#parseOrderInfo(orderRaw, orderCapInfo.orderCapId))
851
- } catch (error) {
852
- // Order might have been deleted
853
- console.warn(`Failed to parse order info for order cap ID ${orderCapInfo.orderCapId}: ${error}`)
854
- // Continue with next order without adding this one to the list
855
- }
856
- }))
857
- }
858
-
779
+ },
780
+ },
781
+ })
782
+ orderInfoList.push(this.#parseOrderInfo(orderRaw, orderCapInfo.orderCapId))
783
+ }))
859
784
  return orderInfoList.sort((a, b) => a.createdAt > b.createdAt ? 1 : -1)
860
785
  }
861
786
 
@@ -928,16 +853,71 @@ export class DataAPI extends OracleAPI {
928
853
  return this.#calcReservingFeeRate(vaultInfo.reservingFeeModel, utilization, SECONDS_PER_EIGHT_HOUR)
929
854
  }
930
855
 
931
- getProxiedHistories = async (trader: string): Promise<IHistory[]> => {
932
- const url = `${this.apiEndpoint}/traderEvents?trader=${trader}`;
856
+ getProxiedHistories = async (trader: string, page?: number, limit?: number): Promise<IHistory[]> => {
857
+ const params = new URLSearchParams({ trader })
858
+ if (page !== undefined) {
859
+ params.append('page', page.toString())
860
+ }
861
+ if (limit !== undefined) {
862
+ params.append('limit', limit.toString())
863
+ }
864
+
865
+ const url = `${this.apiEndpoint}/traderEvents?${params}`
933
866
  const res = await fetch(url, {
934
867
  method: 'GET',
935
868
  headers: {
936
869
  'Content-Type': 'application/json',
937
870
  },
938
- });
939
- const historyInfoList = await res.json();
940
- return historyInfoList;
871
+ })
872
+ const response = await res.json() as any
873
+
874
+ // Handle both old format (array) and new format (object with data)
875
+ if (Array.isArray(response)) {
876
+ return response
877
+ }
878
+
879
+ return response.data?.histories || []
880
+ }
881
+
882
+ getProxiedHistoriesWithPagination = async (
883
+ trader: string,
884
+ page = 1,
885
+ limit = 20,
886
+ orderType?: string,
887
+ symbol?: string
888
+ ) => {
889
+ const params = new URLSearchParams({
890
+ trader,
891
+ page: page.toString(),
892
+ limit: limit.toString(),
893
+ })
894
+
895
+ // Add filter parameters if provided
896
+ if (orderType && orderType !== 'all') {
897
+ params.append('orderType', orderType)
898
+ }
899
+ if (symbol && symbol !== 'all') {
900
+ params.append('symbol', symbol)
901
+ }
902
+
903
+ const url = `${this.apiEndpoint}/traderEvents?${params}`
904
+ const res = await fetch(url, {
905
+ method: 'GET',
906
+ headers: {
907
+ 'Content-Type': 'application/json',
908
+ },
909
+ })
910
+ const response = await res.json() as any
911
+
912
+ return {
913
+ histories: response.data?.histories || [],
914
+ pagination: response.data?.pagination || {
915
+ total: 0,
916
+ page: 1,
917
+ limit: 20,
918
+ pages: 0,
919
+ },
920
+ }
941
921
  }
942
922
 
943
923
  getStaked = async (owner: string): Promise<IStaked> => {