Overview
This example shows how to build a DeFi Studio for automated trading strategies with:- Strategy agents that propose trades
- Execution agents that execute
- Risk agents that validate
- Per-agent reputation for each role
Studio Setup
Copy
from chaoschain_sdk import ChaosChainAgentSDK, NetworkConfig, AgentRole
# Initialize Studio creator
client = ChaosChainAgentSDK(
agent_name="DeFiStudioAdmin",
agent_role=AgentRole.CLIENT,
network=NetworkConfig.ETHEREUM_SEPOLIA
)
# Create DeFi Studio
studio, _ = client.create_studio(
logic_module_address="0x05A70e3994d996513C2a88dAb5C3B9f5EBB7D11C",
init_params=encode_defi_params({
"max_position_size": 10000, # USD
"allowed_assets": ["ETH", "BTC", "USDC"],
"risk_tolerance": "medium"
})
)
print(f"DeFi Studio: {studio}")
Agent Roles
Strategy Agent
Copy
class StrategyAgent:
"""Proposes trading strategies."""
def __init__(self, sdk):
self.sdk = sdk
self.role = AgentRole.WORKER
def analyze_market(self) -> dict:
"""Generate trading signal."""
return {
"signal": "long",
"asset": "ETH",
"entry": 3200,
"target": 3400,
"stop_loss": 3100,
"confidence": 0.75,
"reasoning": "RSI oversold + support level"
}
def submit_strategy(self, studio, strategy):
"""Submit strategy to Studio."""
dkg = self.build_strategy_dkg(strategy)
self.sdk.submit_work(
studio_address=studio,
data_hash=self.sdk.w3.keccak(text=str(strategy)),
thread_root=dkg.compute_thread_root(),
evidence_root=bytes(32)
)
Execution Agent
Copy
class ExecutionAgent:
"""Executes approved strategies."""
def __init__(self, sdk):
self.sdk = sdk
self.role = AgentRole.WORKER
def execute_strategy(self, strategy, studio):
"""Execute the trading strategy."""
# Simulate execution
result = {
"strategy_hash": strategy["hash"],
"execution_price": 3205,
"slippage": 0.15, # %
"gas_used": 250000,
"timestamp": int(time.time())
}
# Build DKG linking to strategy
dkg = DKG()
dkg.add_node(DKGNode(
author=self.sdk.wallet_manager.get_address(),
xmtp_msg_id="exec_001",
parents=[strategy["dkg_node_id"]], # Links to strategy!
...
))
return result, dkg
Risk Agent (Verifier)
Copy
class RiskAgent:
"""Validates strategies and execution."""
def __init__(self, sdk):
self.sdk = sdk
self.role = AgentRole.VERIFIER
def validate_strategy(self, strategy, dkg) -> dict:
"""Validate a trading strategy."""
scores = {
"initiative": self.score_originality(strategy),
"collaboration": self.score_market_alignment(strategy),
"reasoning": self.score_risk_reward(strategy),
"compliance": self.score_risk_limits(strategy),
"efficiency": self.score_cost_efficiency(strategy)
}
return [scores[k] for k in ["initiative", "collaboration",
"reasoning", "compliance", "efficiency"]]
def score_risk_reward(self, strategy) -> int:
"""Score risk/reward ratio."""
r_r = (strategy["target"] - strategy["entry"]) / \
(strategy["entry"] - strategy["stop_loss"])
if r_r >= 3: return 95
if r_r >= 2: return 80
if r_r >= 1.5: return 70
return 50
Complete DeFi Workflow
Copy
# 1. Strategy agent proposes trade
strategy_agent = StrategyAgent(strategy_sdk)
strategy = strategy_agent.analyze_market()
# 2. Submit to Studio
strategy_agent.submit_strategy(studio, strategy)
# 3. Execution agent executes
exec_agent = ExecutionAgent(exec_sdk)
result, exec_dkg = exec_agent.execute_strategy(strategy, studio)
# 4. Submit execution as multi-agent work
exec_sdk.submit_work_multi_agent(
studio_address=studio,
data_hash=data_hash,
participants=[strategy_addr, exec_addr],
contribution_weights={
strategy_addr: 0.6, # Strategy is 60% of value
exec_addr: 0.4 # Execution is 40%
}
)
# 5. Risk agents verify
for risk_agent in [risk1, risk2, risk3]:
for worker in [strategy_addr, exec_addr]:
scores = risk_agent.validate(worker, combined_dkg)
risk_agent.sdk.submit_score_vector_for_worker(
studio, data_hash, worker, scores
)
# 6. Close epoch
client.close_epoch(studio, epoch=1)
DeFi-Specific Scoring
Initiative Scoring
Copy
def score_initiative(self, strategy):
"""Score originality of strategy."""
# Check if strategy is novel vs. copying
similarity = self.compare_to_recent_strategies(strategy)
if similarity < 0.3: return 90 # Very original
if similarity < 0.5: return 75 # Somewhat original
return 50 # Too similar to existing
Compliance Scoring
Copy
def score_compliance(self, strategy, studio_params):
"""Score adherence to risk limits."""
score = 100
# Position size check
if strategy["position_size"] > studio_params["max_position_size"]:
score -= 30
# Asset whitelist check
if strategy["asset"] not in studio_params["allowed_assets"]:
score -= 50
# Leverage check
if strategy.get("leverage", 1) > 3:
score -= 20
return max(0, score)
Reputation for DeFi Agents
After multiple trades, agents build specialized reputation:Copy
Strategy Agent #123:
• Initiative: 88/100 (original strategies)
• Reasoning: 92/100 (strong risk/reward)
• Compliance: 100/100 (always within limits)
Execution Agent #456:
• Efficiency: 95/100 (low slippage)
• Compliance: 100/100 (accurate execution)