31Third Safe Rebalancer
This skill helps you monitor portfolio drift and prepare/execute policy-compliant rebalances on a Gnosis Safe through the 31Third ExecutorModule.
Best-practice usage:
- - Use one-step execution by default:
npm run cli -- rebalance-now. - Only use separated tools (
check_drift, plan_rebalance, execute_rebalance, etc.) if you explicitly understand each step and want manual control. - If unsure, run
help first (npm run cli -- help) and follow that guidance.
Prerequisites
Local Setup
CODEBLOCK0
Getting Started
If you have not deployed your policy stack yet, deploy it first:
Set required environment variables:
CODEBLOCK1
INLINECODE7 (31Third API key) can be requested via or by emailing dev@31third.com.
Wallet model and key handling:
- - Safe owner wallet: controls Safe ownership/governance operations. Never share this private key with the skill.
- Executor wallet: configured as executor on
ExecutorModule in the wizard. This private key is required by the skill for execute_rebalance. - The final step of the 31Third wizard provides an overview of all required environment variables. Use that as source of truth when configuring this skill.
What This Skill Does
- - Reads active on-chain policies from
ExecutorModule. - Computes current-vs-target drift (
check_drift). - Validates trades against Asset Universe + Slippage boundaries (
validate_trade). - Runs a configurable heartbeat monitor (
automation) and returns an alert payload when drift exceeds threshold. - Simulates and executes approved rebalance batches (
execute_rebalance) with pre-execution checkPoliciesVerbose validation and one retry on unknown execution failures. - Accepts SDK
plan_rebalance output directly for execution (txData + requiredAllowances) and decodes batch trade calldata internally. - Fast-fails execution if
scheduler != registry on the ExecutorModule, printing both addresses. - Generates SDK-based policy-aware trade plans (
plan_rebalance) using current Safe balances (bounded by Asset Universe when present) as baseEntries. - Provides one-command execution (
rebalance_now) for non-technical users: drift check -> SDK plan -> execution. - Returns setup and capability guidance (
help).
Execution Safety
Before execution, the agent provides a clear reason, for example:
The skill uses:
- - Viem
publicClient for all reads. - Viem
walletClient for execution.
Execution Contract (Important)
When using SDK/trading-api rebalancing output, execution must follow this exact pattern:
- 1. Build approvals from
requiredAllowances as (tokenAddress, neededAllowance). - Decode
txData as batchTrade(trades, config). - Re-encode
encodedTradeData as ABI tuple:
-
tuple(string,address,uint256,address,uint256,bytes,bytes)[]
-
tuple(bool,bool)
- 4. Run
checkPoliciesVerbose(tradesInput, configInput) before submitting. - Read
scheduler and registry from ExecutorModule. - Ensure the signing executor wallet address equals
registry (required by onlyRegistry). - Only execute immediate path (
executeTradeNow(approvals, encodedTradeData)) when scheduler == registry. - If
scheduler != registry, fail fast and show both addresses.
This is the required execution semantics for this skill and should not be changed to raw passthrough calldata execution.
CLI
Run the bundled CLI:
CODEBLOCK2
Read-only smoke preflight:
CODEBLOCK3
Notes
- - This skill is automation infrastructure, not investment advice.
- Validate behavior in test/staging before running in production.
31Third 安全再平衡器
该技能帮助您监控投资组合偏离度,并通过31Third的ExecutorModule在Gnosis Safe上准备/执行符合策略的再平衡操作。
最佳实践用法:
- - 默认使用一键执行:npm run cli -- rebalance-now
- 仅在明确理解每个步骤并希望手动控制时,才使用分离工具(checkdrift、planrebalance、execute_rebalance等)
- 如有疑问,先运行help(npm run cli -- help)并遵循相关指引
前提条件
本地设置
bash
npm install
npm run build
快速开始
如果您尚未部署策略堆栈,请先部署:
设置所需的环境变量:
bash
SAFE_ADDRESS=0xYourSafe
CHAIN_ID=8453
TOTAPIKEY=yourapikey
RPC_URL=https://mainnet.base.org
EXECUTORMODULEADDRESS=0xYourExecutorModule
EXECUTORWALLETPRIVATE_KEY=0x...
ORACLEMAXAGE_SECONDS=3600
HEARTBEATINTERVALSECONDS=21600
TOTAPIKEY(31Third API密钥)可通过或发送邮件至dev@31third.com申请。
钱包模型和密钥处理:
- - Safe所有者钱包:控制Safe所有权/治理操作。切勿将此私钥提供给该技能。
- 执行者钱包:在向导的ExecutorModule中配置为执行者。该技能在执行execute_rebalance时需要此私钥。
- 31Third向导的最后一步提供了所有必需环境变量的概览。配置此技能时,请以此作为真实信息来源。
该技能的功能
- - 从ExecutorModule读取链上活跃策略
- 计算当前与目标之间的偏离度(checkdrift)
- 根据资产宇宙+滑点边界验证交易(validatetrade)
- 运行可配置的心跳监控(automation),当偏离度超过阈值时返回警报负载
- 模拟并执行已批准的再平衡批次(executerebalance),包含执行前checkPoliciesVerbose验证,并在未知执行失败时进行一次重试
- 直接接受SDK planrebalance输出用于执行(txData + requiredAllowances),并在内部解码批量交易调用数据
- 如果ExecutorModule上的scheduler != registry,则快速失败执行,并打印两个地址
- 使用当前Safe余额(存在资产宇宙时受其约束)作为baseEntries,生成基于SDK的策略感知交易计划(planrebalance)
- 为非技术用户提供一键执行(rebalancenow):偏离度检查 -> SDK计划 -> 执行
- 返回设置和能力指引(help)
执行安全性
执行前,代理会提供明确原因,例如:
- - BTC当前为54.00%,目标为50.00%(偏离400个基点)。需要再平衡。
该技能使用:
- - Viem publicClient进行所有读取操作
- Viem walletClient进行执行操作
执行合约(重要)
使用SDK/交易API再平衡输出时,执行必须遵循以下精确模式:
- 1. 从requiredAllowances构建授权,格式为(tokenAddress, neededAllowance)
- 将txData解码为batchTrade(trades, config)
- 将encodedTradeData重新编码为ABI元组:
- tuple(string,address,uint256,address,uint256,bytes,bytes)[]
- tuple(bool,bool)
- 4. 在提交前运行checkPoliciesVerbose(tradesInput, configInput)
- 从ExecutorModule读取scheduler和registry
- 确保签名执行者钱包地址等于registry(onlyRegistry要求)
- 仅当scheduler == registry时执行即时路径(executeTradeNow(approvals, encodedTradeData))
- 如果scheduler != registry,快速失败并显示两个地址
这是该技能所需的执行语义,不应更改为原始透传调用数据执行。
CLI
运行捆绑的CLI:
bash
npm run cli -- help
npm run cli -- check-drift
npm run cli -- automation --last-heartbeat-ms 0
npm run cli -- plan-rebalance --signer 0xYourSigner --min-trade-value 100
npm run cli -- rebalance-now
npm run cli -- validate-trade --trade {from:0x...,to:0x...,fromAmount:1000000000000000000,minToReceiveBeforeFees:990000000000000000}
npm run cli -- execute-rebalance --trades [{exchangeName:0x,from:0x...,fromAmount:1000000000000000000,to:0x...,minToReceiveBeforeFees:990000000000000000,data:0x...,signature:0x...}] --approvals [{token:0x...,amount:1000000000000000000}]
npm run cli -- execute-rebalance --rebalancing {txData:0x...,requiredAllowances:[{token:{address:0x...},neededAllowance:1000000000000000000}]}
只读烟雾测试预检:
bash
npm run smoke -- --signer 0xYourSigner
npm run smoke -- --trades [...] --approvals [...]
注意事项
- - 该技能是自动化基础设施,不构成投资建议
- 在生产环境运行前,请在测试/预发布环境中验证行为