Policy Patterns
Common recipes and best practices for Matador policies.
This library contains copy-pasteable patterns for common use cases. Mix and match these snippets to build robust security policies for your smart accounts.
Financial Safety Rails
Spending Limit
Restrict the maximum amount of ETH or tokens that can be transferred in a single transaction.
import "abis/ERC20.json" as Token;
permission SpendLimit -> 1.0.0 {
parameters: {
token: Token,
maxAmount: uint256
}
when: {
// Check for ERC20 transfer
Token.transfer(amount: context.args.amount),
// Enforce limit
context.args.amount <= parameters.maxAmount
}
}Recurring Subscription
Allow a specific service provider to charge a fixed fee once per month.
permission Subscription -> 1.0.0 {
parameters: {
serviceProvider: address,
fee: uint256
}
when: {
all {
// Only the provider can initiate the charge
context.caller == parameters.serviceProvider,
// Fee must match exactly
context.value == parameters.fee,
// Limit to once every 30 days
ratelimit(30 days, 1, "subscription-charge")
}
}
}Security & Access Control
Whitelist
Restrict interactions to a known list of safe contracts (e.g., official Uniswap routers).
permission SafeInteractions -> 1.0.0 {
parameters: {
allowedTargets: address[]
}
when: {
// target must be in the list
context.target in parameters.allowedTargets
}
}Circuit Breaker
Block all transactions if an emergency flag is set on a DAO contract or Oracle.
import "abis/EmergencyBrake.json" as Brake;
permission EmergencyStop -> 1.0.0 {
parameters: {
brakeContract: Brake
}
when: {
// Only allow execution if the system is NOT paused
Brake.isPaused() == false
}
}DeFi Automation
Swap with Slippage Protection
Allow an automated bot to execute swaps, but enforce a minimum output amount based on an on-chain oracle.
import "abis/UniswapV3.json" as Uniswap;
import "abis/Chainlink.json" as Oracle;
permission SafeSwap -> 1.0.0 {
parameters: {
oracle: Oracle,
maxSlippageBps: uint256 // e.g. 50 for 0.5%
}
when: {
all {
// Must be a swap
Uniswap.exactInputSingle,
// Calculate min out: OraclePrice * (1 - slippage)
// Note: Simplistic math for illustration
context.args.params.amountOutMinimum >=
Oracle.latestAnswer() * (10000 - parameters.maxSlippageBps) / 10000
}
}
}Math Precision
Matador's core arithmetic opcodes operate on uint256. For complex fixed-point math or price conversions, it is recommended to use a Custom Module or a helper contract rather than implementing complex math directly in DSL.
Flash Loan Prevention
Prevent the account from being used as a flash loan borrower by ensuring the transaction origin matches the sender.
permission NoFlashLoan -> 1.0.0 {
when: {
context.origin == context.caller
}
}