Sui gRPC Examples
This directory contains comprehensive examples for all Sui gRPC services. Each service has its own directory with specific examples demonstrating all available methods.
Available Services
Query blockchain data and state information.
Methods:
GetServiceInfo - Get service information
GetObject - Get object by ID
BatchGetObjects - Get multiple objects
GetTransaction - Get transaction by digest
BatchGetTransactions - Get multiple transactions
GetCheckpoint - Get checkpoint by sequence number
GetEpoch - Get epoch information
Resolve and manage Sui names.
Methods:
LookupName - Resolve name to object
ReverseLookupName - Get name from object ID
Execute transactions on the Sui network.
Methods:
ExecuteTransaction - Submit and execute transactions
Subscribe to real-time blockchain events.
Methods:
SubscribeCheckpoints - Stream checkpoint updates
Access live blockchain data.
Methods:
GetObject - Get live object data
GetTransaction - Get live transaction data
QueryEvents - Query live events
Interact with Move packages and modules.
Methods:
GetPackage - Get package information
GetModule - Get module details
GetFunction - Get function information
GetStruct - Get struct definition
Verify signatures and cryptographic operations.
Methods:
VerifySignature - Verify message signatures
VerifyTransactionSignature - Verify transaction signatures
Quick Start
-
Update Configuration: Each example uses placeholder values. Update these in each main.go file:
target := "your-grpc-server:9000" // Your Sui gRPC endpoint
token := "your-api-token" // Your authentication token
-
Run Individual Examples:
# Run ledger service examples
go run grpc_examples/ledger_service/main.go
# Run name service examples
go run grpc_examples/name_service/main.go
# Run transaction execution examples
go run grpc_examples/transaction_execution_service/main.go
-
Build All Examples:
# Build all examples to check for compilation errors
go build ./grpc_examples/...
Configuration
All examples support the following configuration options:
opts := []grpcconn.GrpcConnOption{
grpcconn.WithTimeout(time.Second * 30), // Request timeout
grpcconn.WithRetryCount(3), // Retry attempts
grpcconn.WithDialOptions( // Custom gRPC options
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
),
}
Authentication
The examples use the NewSuiGrpcClientWithAuth function which automatically adds authentication headers to all requests. Supported authentication formats:
Authorization: Bearer {token}
x-api-key: {token}
x-token: {token}
Error Handling
All examples include proper error handling and demonstrate:
- Connection error recovery
- Request timeout handling
- Retry mechanisms
- Graceful failure modes
Real Data vs Examples
The examples use placeholder data for demonstration. In real usage:
- Object IDs: Use actual Sui object IDs from your network
- Transaction Digests: Use real transaction hashes
- Addresses: Use valid Sui addresses
- Signatures: Provide properly signed data
- Timestamps: Use appropriate time ranges
Streaming Services
The Subscription Service demonstrates gRPC streaming:
- Server Streaming: Continuous data flow from server
- Error Recovery: Automatic reconnection on stream errors
- Backpressure: Proper handling of high-volume streams
- Graceful Shutdown: Clean stream termination
Best Practices
- Connection Management: Reuse gRPC clients across requests
- Context Handling: Use appropriate timeouts and cancellation
- Error Recovery: Implement retry logic for transient failures
- Resource Cleanup: Always close clients and streams
- Rate Limiting: Respect service rate limits
- Field Masks: Use field masks to optimize response size
Troubleshooting
Connection Issues:
- Verify server address and port
- Check network connectivity
- Ensure gRPC service is enabled on the server
Authentication Issues:
- Verify API token is valid
- Check token format and headers
- Ensure proper TLS configuration
Request Failures:
- Validate request parameters
- Check field mask syntax
- Verify object IDs and addresses exist
Contributing
When adding new examples:
- Follow the existing structure and naming conventions
- Include comprehensive error handling
- Add meaningful comments and documentation
- Test with real Sui network data
- Update this README with new service information