Policy Setup
Common policy patterns and configurations for different use cases.
Basic Spending Policy
Allows spending up to 1 ETH per transaction:
basic_policy.sol
Policy memory policy = Policy({
owner: msg.sender,
allowedTargets: [],
allowedSelectors: [],
maxValuePerTx: 1 ether,
maxAggregateValue: 0,
validAfter: 0,
validBefore: 0,
allowedChains: []
});Target-Restricted Policy
Only allows calls to specific contracts:
target_restricted_policy.sol
address[] memory targets = new address[](2);
targets[0] = uniswapRouter;
targets[1] = aavePool;
Policy memory policy = Policy({
owner: msg.sender,
allowedTargets: targets,
allowedSelectors: [],
maxValuePerTx: 5 ether,
maxAggregateValue: 0,
validAfter: 0,
validBefore: 0,
allowedChains: []
});Time-Limited Policy
Policy valid only for 30 days:
time_limited_policy.sol
Policy memory policy = Policy({
owner: msg.sender,
allowedTargets: [],
allowedSelectors: [],
maxValuePerTx: 1 ether,
maxAggregateValue: 0,
validAfter: block.timestamp,
validBefore: block.timestamp + 30 days,
allowedChains: []
});Chain-Specific Policy
Only valid on specific chains:
chain_specific_policy.sol
uint256[] memory chains = new uint256[](2);
chains[0] = 1; // Ethereum
chains[1] = 137; // Polygon
Policy memory policy = Policy({
owner: msg.sender,
allowedTargets: [],
allowedSelectors: [],
maxValuePerTx: 1 ether,
maxAggregateValue: 0,
validAfter: 0,
validBefore: 0,
allowedChains: chains
});Best Practices
1. Start Restrictive
Begin with restrictive policies and relax as needed. It's easier to add permissions than remove them.
2. Use Time Windows
Set validAfter and validBefore to limit exposure and reduce risk of stale policies.
3. Set Reasonable Caps
Set maxValuePerTx and maxAggregateValue to reasonable limits based on your use case.
4. Document Policies
Document the purpose and parameters of each policy for future reference.
5. Version Policies
Create new policyIds for policy updates rather than modifying existing ones.