Solidity · a treasury contract opens a mandate
// Owner = msg.sender of create(). A multisig or contract becomes the owner by calling create() itself.
interface IMandateFactory {
function create() external payable returns (address account);
}
struct PoolKey { address currency0; address currency1; uint24 fee; int24 tickSpacing; address hooks; }
struct Limits { uint64 maxTradeUsd; uint64 maxDailyUsd; uint16 maxSlippageBps; uint16 maxDrawdownBps; uint32 cooldown; uint40 expiry; }
interface IMandateAccount {
function setAgent(address agent) external;
function setToken(address token, bool allowed, uint8 kind, uint32 asset) external; // kind 1 = $1 stable, 2 = Troy Oracle asset
function setPool(PoolKey calldata key, bool allowed, bool unpricedOk, uint64 maxUnpricedUsd) external;
function setLimits(Limits calldata l) external;
function deposit(address token, uint256 amount) external payable;
function multicall(bytes[] calldata calls) external;
}
contract Treasury {
address constant FACTORY = 0x6f043507729D617c78235dE20Bd4812b44be5E46;
address constant USDC = 0xb47c4C8F73F009CC0C0d3f92B388d13E293F0B65;
function openMandate(address bot) external returns (address acct) { // guard it with your own access control
acct = IMandateFactory(FACTORY).create(); // this contract owns the account
PoolKey memory hypeUsdc = PoolKey(address(0), USDC, 3000, 60, address(0));
bytes[] memory c = new bytes[](5);
c[0] = abi.encodeCall(IMandateAccount.setAgent, (bot));
c[1] = abi.encodeCall(IMandateAccount.setToken, (address(0), true, 2, 159)); // HYPE, Troy Oracle
c[2] = abi.encodeCall(IMandateAccount.setToken, (USDC, true, 1, 0)); // USDC at $1
c[3] = abi.encodeCall(IMandateAccount.setPool, (hypeUsdc, true, false, 0));
c[4] = abi.encodeCall(IMandateAccount.setLimits, (Limits(100e6, 500e6, 100, 1500, 30, uint40(block.timestamp + 30 days))));
IMandateAccount(acct).multicall(c);
IMandateAccount(acct).deposit{value: 1 ether}(address(0), 1 ether); // fund it: only the owner can deposit or withdraw
}
}
cast · the agent trades
# the agent: price the trade, simulate it from the agent address, then send. It trades the account's funds, never its own.
POOL="(0x0000000000000000000000000000000000000000,0xb47c4C8F73F009CC0C0d3f92B388d13E293F0B65,3000,60,0x0000000000000000000000000000000000000000)"
RPC=https://rpc.elysium.kinetiq.xyz
cast call $ACCOUNT "fairOut(address,address,uint256)(uint256)" \
0x0000000000000000000000000000000000000000 0xb47c4C8F73F009CC0C0d3f92B388d13E293F0B65 1ether --rpc-url $RPC
# minOut a little inside fairOut x (1 - maxSlippage); a refused trade reverts with the rule's name
cast call $ACCOUNT "swap((address,address,uint24,int24,address),bool,uint256,uint256,uint256)(uint256)" \
$POOL true 1ether $MIN_OUT $(($(date +%s)+120)) --from $AGENT --rpc-url $RPC # simulate
cast send $ACCOUNT "swap((address,address,uint24,int24,address),bool,uint256,uint256,uint256)" \
$POOL true 1ether $MIN_OUT $(($(date +%s)+120)) --account agent --rpc-url $RPC # sell 1 HYPE for USDC