Skip to main content

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: ST9ZZEP9M6VZ9YJA0P69H313CRPV0HQ1ZNPVS8NZ
Contract 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
User profile data from blockchain, or null if not found
number
Risk level (1=Conservative, 2=Balanced, 3=Aggressive)
number
Total strategies anchored by this user
number
Block height of last profile update

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 otherwise

Example


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 failed

Example

Strategy hashes are truncated to 64 characters and protocol names to 32 characters to fit on-chain constraints.

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:
Used when:
  • Running on localhost
  • Running on 127.0.0.1
  • Development environment
Network: Stacks Testnet
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 block
PostConditionMode
Set to PostConditionMode.Allow - allows transactions without explicit post-conditions

Error Handling

All functions handle errors gracefully:
  • Write functions (saveRiskProfile, anchorStrategy) return null on error
  • Read functions (getUserProfile, checkHasProfile) return safe defaults
  • Count functions (getStrategyCount) return 0 on error
  • Errors are logged to console with warnings
No exceptions are thrown - all errors result in safe fallback values.

Complete Example

Dependencies

Required packages:
Imports:

Best Practices

Check Before Write

Use checkHasProfile before creating profiles to avoid duplicate transactions

Hash Strategies

Always hash strategy content before anchoring to ensure data integrity

Handle Nulls

Check for null returns and provide fallback UI for failed transactions

Show Transaction Links

Display explorer links so users can verify their on-chain data