* No commit suggestions generated * No commit suggestions generated * No commit suggestions generated
4.2 KiB
Sonr Script Library
This directory contains modular helper scripts for Sonr blockchain operations. These helpers provide reusable functions for common tasks like environment setup, configuration management, key handling, and transaction operations.
Library Structure
env.sh - Environment Management
Centralizes default environment variables and provides utility functions for logging, validation, and system checks.
Key Functions:
init_env()- Initialize environment with required tools and cleanuplog_info(),log_warn(),log_error(),log_success()- Colored loggingrequire_cmd(),ensure_file(),ensure_binary()- Validation helpersis_docker()- Check if running in Docker container
Environment Variables:
DENOM=usnr- Default denominationCHAIN_BIN=snrd- Binary nameCHAIN_DIR=~/.sonr- Chain data directoryCHAIN_ID=sonrtest_1-1- Default chain ID
jq_patch.sh - JSON Operations
Provides safe JSON patching operations using jq with temporary files and validation.
Key Functions:
patch_json(file, jq_expr)- Apply jq expression to fileset_json_string(),set_json_number(),set_json_bool()- Type-safe settersjson_path_exists(),get_json_value()- Query helpersvalidate_json()- JSON validation
config.sh - Configuration Management
Handles TOML configuration files for Cosmos SDK nodes using crudini or sed fallbacks.
Key Functions:
configure_node()- Complete node configuration with ports and settingsenable_rpc(),enable_rest(),enable_grpc()- Enable servicesset_consensus_timeouts(),set_min_gas_prices()- Parameter settersset_toml_value()- Generic TOML value setter
keys.sh - Key Management
Provides functions for importing, managing, and using cryptographic keys.
Key Functions:
import_mnemonic()- Import key from mnemonic phraseensure_key(),get_key_address()- Key validation and retrievalfund_key(),delegate_to_validator()- Account operationswait_for_sync()- Node synchronization waiting
tx.sh - Transaction Operations
Handles blockchain transactions with error handling and retry logic.
Key Functions:
submit_tx()- Submit transaction with gas and error handlingquery_chain()- Query blockchain statesubmit_proposal(),vote_proposal()- Governance operationsstake_tokens(),get_balance()- Staking operations
genesis.sh - Genesis File Operations
Specialized functions for genesis file creation and modification.
Key Functions:
generate_vrf_key()- Generate VRF keypair for networkupdate_genesis_params()- Apply Sonr-specific genesis parametersadd_constitution()- Add constitution to governancevalidate_genesis()- Genesis file validation
Usage Examples
Basic Environment Setup
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/lib/env.sh"
source "${SCRIPT_DIR}/lib/config.sh"
init_env
ensure_chain_dir
configure_node "$CHAIN_DIR" --rpc-port 26657 --rest-port 1317
Key Management
#!/bin/bash
source "scripts/lib/keys.sh"
import_mnemonic "mykey" "word1 word2 word3..." "eth_secp256k1"
fund_key "mykey" "1000000usnr"
Genesis Creation
#!/bin/bash
source "scripts/lib/genesis.sh"
update_genesis_params
add_constitution
generate_vrf_key "$CHAIN_DIR"
Integration Guidelines
- Always source required libraries at the top of scripts
- Call
init_env()early to set up logging and validation - Use consistent error handling with the provided logging functions
- Validate inputs using
ensure_*functions before operations - Handle Docker vs local execution in wrapper functions
- Use descriptive log messages for user feedback
Error Handling
All functions use consistent error handling:
- Functions return 0 on success, 1 on failure
- Use
log_error()for user-visible errors - Use
log_warn()for non-fatal issues - Cleanup temporary files automatically via
trap
Docker Compatibility
All functions support both local and Docker execution modes:
- Use
is_docker()to detect environment - Use
run_binary()wrapper for command execution - Mount volumes correctly for Docker containers
- Handle TTY and interactive mode appropriately