Overview
The Contract Service provides functions to interact with the Staxiq smart contract on the Stacks blockchain. It handles user profiles, risk preferences, and strategy anchoring with automatic network detection. Source:src/services/contractService.js
Contract Address: ST9ZZEP9M6VZ9YJA0P69H313CRPV0HQ1ZNPVS8NZContract Name:
staxiq-user-profile
Functions
saveRiskProfile
Save a user’s risk profile preference on-chain.string
required
Risk profile level:
"Conservative", "Balanced", or "Aggressive"string | null
Transaction ID if successful,
null if failed (fails gracefully without throwing)Example
Implementation Details
Risk levels are mapped to integers for on-chain storage:getUserProfile
Fetch a user’s complete on-chain profile.string
required
Stacks wallet address (mainnet or testnet)
object | null
Example
checkHasProfile
Check if a user has created an on-chain profile.string
required
Stacks wallet address to check
boolean
true if user has a profile, false otherwiseExample
anchorStrategy
Save a generated strategy to the blockchain for permanent record.string
required
Hash or identifier of the strategy (max 64 characters). Typically a SHA-256 hash of the strategy content.
string
required
Primary protocol name for this strategy (max 32 characters)
string | null
Transaction ID if successful,
null if failedExample
getStrategyCount
Get the total number of strategies anchored by a user.string
required
User’s Stacks wallet address
number
Number of strategies saved on-chain (returns 0 if none or error)
Example
Network Detection
All functions automatically detect the appropriate network:- Testnet
- Mainnet
Used when:
Explorer: https://explorer.hiro.so?chain=testnet
- Running on
localhost - Running on
127.0.0.1 - Development environment
Explorer: https://explorer.hiro.so?chain=testnet
Transaction Configuration
All write operations use these defaults:AnchorMode
Set to
AnchorMode.Any - transaction can be included in microblock or anchor blockPostConditionMode
Set to
PostConditionMode.Allow - allows transactions without explicit post-conditionsError Handling
All functions handle errors gracefully:- Write functions (
saveRiskProfile,anchorStrategy) returnnullon error - Read functions (
getUserProfile,checkHasProfile) return safe defaults - Count functions (
getStrategyCount) return0on error - Errors are logged to console with warnings
No exceptions are thrown - all errors result in safe fallback values.
Complete Example
Dependencies
Required packages:Best Practices
Check Before Write
Use
checkHasProfile before creating profiles to avoid duplicate transactionsHash Strategies
Always hash strategy content before anchoring to ensure data integrity
Handle Nulls
Check for
null returns and provide fallback UI for failed transactionsShow Transaction Links
Display explorer links so users can verify their on-chain data
Related Resources
- Authentication Guide - Connect wallets before calling contract functions
- Stacks API Service - Read blockchain data
- AI Service - Generate strategies to anchor