Querying Move Contract Data with Dubhe SDK
Overview
Dubhe SDK provides a powerful dynamic contract interaction system that automatically parses contract metadata to enable seamless querying and transaction execution. Key features include:- Dynamic Method Generation: By passing contract metadata to the Dubhe instance, it automatically generates type-safe query and transaction methods accessible via
dubhe.query.moduleName.functionNameanddubhe.tx.moduleName.functionName. - Automatic BCS Parsing: Built-in BCS (Binary Canonical Serialization) parser that automatically deserializes contract return values based on metadata type definitions.
- Type-Safe Interactions: Provides compile-time type checking for contract interactions when used with TypeScript.
- View Method: Simplified data deserialization through the
view()method, which automatically converts BCS-encoded return values into JavaScript native types.
Initial Setup
First, let’s set up the Dubhe client. There are two ways to initialize the metadata:Method 1: Dynamic Loading
Method 2: Pre-saved Metadata (Recommended)
Basic Types
Number Types (u8, u64, u128, u256)
Move Contract
Query Implementation
Boolean and Address Types
Move Contract
Query Implementation
Complex Types
Vector and String
Move Contract
Query Implementation
Tuple and Option Types
Move Contract
Query Implementation
Struct Types
Basic Struct
Move Contract
Query Implementation
Best Practices
Error Handling
Transaction Management
Important Notes
-
Type Support
- Basic types (u8, u64, u128, u256)
- Boolean and Address
- Vector and String
- Tuple and Option
- Custom structs
-
Current Limitations
- Enum types support is pending (waiting for PR #20480)
- Custom structs from external packages may require manual BCS type definition
- Some complex nested structures need manual handling
- Custom Type Definition When encountering unsupported types, you can define custom BCS types:
-
Error Handling
If you encounter an “Unsupported type” error, it means either:
- The struct is from an external package and needs manual type definition
- The type is not yet supported in the metadata
-
Best Practices
- Always check if your type is supported in the metadata
- For unsupported types, use manual BCS type definitions
- Keep track of enum support updates
- Test queries with sample data before production use