@0xobelisk/sui-client 1.0.0 → 1.0.2

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/dubhe.ts CHANGED
@@ -584,6 +584,73 @@ export class Dubhe {
584
584
  let baseType = res[1];
585
585
  const value = Uint8Array.from(baseValue);
586
586
 
587
+ const storageValueMatch = baseType.match(
588
+ /^.*::storage_value::StorageValue<(.+)>$/
589
+ );
590
+ if (storageValueMatch) {
591
+ const innerType = storageValueMatch[1];
592
+ if (this.#object[innerType]) {
593
+ const storageValueBcs = bcs.struct('StorageValue', {
594
+ contents: bcs.vector(
595
+ bcs.struct('Entry', {
596
+ value: this.#object[innerType],
597
+ })
598
+ ),
599
+ });
600
+ returnValues.push(storageValueBcs.parse(value));
601
+ continue;
602
+ }
603
+ }
604
+
605
+ const storageMapMatch = baseType.match(
606
+ /^.*::storage_map::StorageMap<(.+)>$/
607
+ );
608
+ if (storageMapMatch) {
609
+ const innerType = storageMapMatch[1];
610
+ const [keyType, valueType] = innerType
611
+ .split(',')
612
+ .map((type) => type.trim());
613
+ if (this.#object[keyType] && this.#object[valueType]) {
614
+ const storageMapBcs = bcs.struct('StorageMap', {
615
+ contents: bcs.vector(
616
+ bcs.struct('Entry', {
617
+ key: this.#object[keyType],
618
+ value: this.#object[valueType],
619
+ })
620
+ ),
621
+ });
622
+ returnValues.push(storageMapBcs.parse(value));
623
+ continue;
624
+ }
625
+ }
626
+
627
+ const storageDoubleMapMatch = baseType.match(
628
+ /^.*::storage_double_map::StorageDoubleMap<(.+)>$/
629
+ );
630
+ if (storageDoubleMapMatch) {
631
+ const innerType = storageDoubleMapMatch[1];
632
+ const [key1, key2, valueType] = innerType
633
+ .split(',')
634
+ .map((type) => type.trim());
635
+ if (
636
+ this.#object[key1] &&
637
+ this.#object[key2] &&
638
+ this.#object[valueType]
639
+ ) {
640
+ const storageDoubleMapBcs = bcs.struct('StorageDoubleMap', {
641
+ contents: bcs.vector(
642
+ bcs.struct('Entry', {
643
+ key1: this.#object[key1],
644
+ key2: this.#object[key2],
645
+ value: this.#object[valueType],
646
+ })
647
+ ),
648
+ });
649
+ returnValues.push(storageDoubleMapBcs.parse(value));
650
+ continue;
651
+ }
652
+ }
653
+
587
654
  if (this.#object[baseType]) {
588
655
  returnValues.push(this.#object[baseType].parse(value));
589
656
  continue;
@@ -630,6 +697,113 @@ export class Dubhe {
630
697
  }
631
698
  }
632
699
 
700
+ async state({
701
+ schema,
702
+ struct,
703
+ objectId,
704
+ storageType,
705
+ params,
706
+ }: {
707
+ schema: string;
708
+ struct: string;
709
+ objectId: string;
710
+ storageType: string; // 'StorageValue<V>' | 'StorageMap<K, V>' | 'StorageDoubleMap<K1, K2, V>'
711
+ params: any[];
712
+ }) {
713
+ const tx = new Transaction();
714
+ const moduleName = `${schema}_schema`;
715
+ const functionName = `get_${struct}`;
716
+ const schemaObject = tx.object(objectId);
717
+ // Parse storage type
718
+ const storageValueMatch = storageType.match(/^StorageValue<(.+)>$/);
719
+ const storageMapMatch = storageType.match(/^StorageMap<(.+),\s*(.+)>$/);
720
+ const storageDoubleMapMatch = storageType.match(
721
+ /^StorageDoubleMap<(.+),\s*(.+),\s*(.+)>$/
722
+ );
723
+
724
+ let processedParams: (TransactionArgument | SerializedBcs<any>)[] = [
725
+ schemaObject,
726
+ ];
727
+
728
+ if (storageValueMatch) {
729
+ // StorageValue only needs the object ID
730
+ if (params.length > 0) {
731
+ console.warn(
732
+ 'StorageValue does not require additional parameters. Extra parameters will be ignored.'
733
+ );
734
+ }
735
+ } else if (storageMapMatch) {
736
+ // StorageMap needs one key
737
+ if (params.length !== 1) {
738
+ throw new Error('StorageMap requires exactly one key parameter');
739
+ }
740
+ const keyType = storageMapMatch[1].trim();
741
+ processedParams.push(this.#processKeyParameter(tx, keyType, params[0]));
742
+ } else if (storageDoubleMapMatch) {
743
+ // StorageDoubleMap needs two keys
744
+ if (params.length !== 2) {
745
+ throw new Error('StorageDoubleMap requires exactly two key parameters');
746
+ }
747
+ const key1Type = storageDoubleMapMatch[1].trim();
748
+ const key2Type = storageDoubleMapMatch[2].trim();
749
+ processedParams.push(this.#processKeyParameter(tx, key1Type, params[0]));
750
+ processedParams.push(this.#processKeyParameter(tx, key2Type, params[1]));
751
+ } else {
752
+ throw new Error(
753
+ `Invalid storage type: ${storageType}. Must be StorageValue<V>, StorageMap<K,V>, or StorageDoubleMap<K1,K2,V>`
754
+ );
755
+ }
756
+ const queryResponse = (await this.query[moduleName][functionName]({
757
+ tx,
758
+ params: processedParams,
759
+ })) as DevInspectResults;
760
+ return this.view(queryResponse);
761
+ }
762
+
763
+ #processKeyParameter(tx: Transaction, keyType: string, value: any) {
764
+ // Handle basic types
765
+ switch (keyType.toLowerCase()) {
766
+ case 'u8':
767
+ return tx.pure.u8(value);
768
+ case 'u16':
769
+ return tx.pure.u16(value);
770
+ case 'u32':
771
+ return tx.pure.u32(value);
772
+ case 'u64':
773
+ return tx.pure.u64(value);
774
+ case 'u128':
775
+ return tx.pure.u128(value);
776
+ case 'u256':
777
+ return tx.pure.u256(value);
778
+ case 'bool':
779
+ return tx.pure.bool(value);
780
+ case 'address':
781
+ return tx.pure.address(value);
782
+ default:
783
+ // Check if it's an object type
784
+ if (keyType.includes('::')) {
785
+ // Assuming it's an object ID if the type contains '::'
786
+ return tx.object(value);
787
+ }
788
+
789
+ // If we reach here, the key type is not supported
790
+ console.log(
791
+ '\n\x1b[41m\x1b[37m ERROR \x1b[0m \x1b[31mUnsupported Key Type\x1b[0m'
792
+ );
793
+ console.log('\x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m');
794
+ console.log(`\x1b[95m•\x1b[0m Type: \x1b[33m"${keyType}"\x1b[0m`);
795
+ console.log('\x1b[95m•\x1b[0m Supported Types:\x1b[0m');
796
+ console.log(' \x1b[36m◆\x1b[0m u8, u16, u32, u64, u128, u256');
797
+ console.log(' \x1b[36m◆\x1b[0m bool');
798
+ console.log(' \x1b[36m◆\x1b[0m address');
799
+ console.log(
800
+ ' \x1b[36m◆\x1b[0m object (format: package::module::type)'
801
+ );
802
+ console.log('\x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\n');
803
+ throw new Error(`Unsupported key type: ${keyType}`);
804
+ }
805
+ }
806
+
633
807
  /**
634
808
  * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
635
809
  * else: