Skip to main content

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.functionName and dubhe.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

Using pre-saved metadata is recommended for better performance in production environments as it eliminates the need for network requests to fetch metadata.

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

  1. Type Support
    • Basic types (u8, u64, u128, u256)
    • Boolean and Address
    • Vector and String
    • Tuple and Option
    • Custom structs
  2. 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
  3. Custom Type Definition When encountering unsupported types, you can define custom BCS types:
  1. 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
  2. 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