Subaccount Autonomous Trading
To let an AI Agent open/close positions, adjust margin, manage TP/SL, and cancel orders without per-transaction wallet popups, OursMeme provides the on-chain SubaccountRouter channel.
1. Contract-Enforced Security Boundaries
- Zero Withdrawal Rights: Subaccounts can only submit Perp orders on behalf of the master wallet. The smart contract enforces
receiver == masterWallet, so all collateral and profits can only return to your master wallet. - Capped Actions & Expiry: Configure maximum allowed actions (e.g.,
100) and validity duration (default3days, max30days). Access automatically expires once either limit is reached. - Instant Revocation: Call
build_agent_revokefrom your master wallet to revoke permissions immediately (optionally resetting Perp Router USDC allowance to0).
⚠️ Important Note on
usdcBudget(Shared Router Allowance): SettingusdcBudgetinbuild_agent_authorizeconfigures an exact ERC-20approveallowance onPerp Router, which is shared with manual orders placed from the web UI. If you trade manually on the same wallet frequently, your manual orders may consume or overwrite that allowance. Consider using a dedicated wallet for automated agent strategies.
2. Recommended Workflow (Master Wallet + Subaccount Agent)
- Generate a dedicated local subaccount key (
chmod 0600, never printed to chat logs, never using your master key). - One-time master wallet authorization:
- Authorize visually on
/agent, or build steps withPOST /api/agent/tools/build_agent_authorizeand sign once with your master wallet.
- Authorize visually on
- Issue an API Key for the subaccount address:
- Call
GET /api/v1/agent/keys/challenge?wallet=<AGENT_ADDR>+POST /api/v1/agent/keyssigned by the subaccount key.
- Call
- Autonomous monitoring and execution:
- Call
get_account({ account: MASTER_WALLET })with the subaccount'sX-API-Key: the backend verifies on-chainSubaccountRouterauthorization and returns full positions and pendingorders(accessRole: "subaccount"). - Call
build_open_position,build_close_position,build_set_tp_sl, orbuild_cancel_orderswithaccount: MASTER_WALLETandagent: AGENT_ADDR, then sign and broadcast with the subaccount key.
- Call
3. Inspection & Revocation
- Check remaining actions & expiry:
POST /api/agent/tools/get_agent_access - Revoke immediately:
POST /api/agent/tools/build_agent_revoke(passclearUsdcAllowance: trueto also reset Router USDC allowance to0)