true or false.
Rules are executed in the order they appear in the policy. This matters when one rule affects data (like a tracker) used by another.
Every rule consists of:
- Name: The name of the rule
- Description: A description of the rule
- Condition expression: An expression that must evaluate to a boolean (
trueorfalse) - Effects when true: What happens if the condition expression evaluates to true
- Effects when false: What happens if the condition expression evaluates to false
- Calling function signature: Each rule is only applied when a specific calling function invokes the policy
Condition Expressions
Condition expressions are defined by a sequence of values, operators, and optional parentheses. These expressions evaluate totrue or false and control whether the positiveEffects or negativeEffects are triggered.
Supported operators include:
| Type | Operators |
|---|---|
| Arithmetic | +, -, *, / |
| Comparators | >, <, >=, <=, == |
| Logical | AND, OR, (, ) |
- Config data: static data set at configuration time
- Encoded values: data sent from the calling contract
- Trackers: on-chain values managed by the policy
- Foreign calls: values returned by external smart contract calls
Expressions follow standard operator precedence rules and can use parentheses for grouping.
Effects
Each rule must define at least one effect that takes place if the condition expression evaluates totrue or false. You can include multiple effects in either branch.
These can be:
- revert: revert the transaction
The revert message must be 32 bytes or less. If you attempt to call the
createPolicyfunction with a longer revert message you will receive aSizeOverflowError. - emit event: emit an on-chain event
- update tracker: update a tracker’s value
- call external contract: call a foreign contract
If the revert effect is triggered, no other effects will happen for that transaction.
Calling Function Signature
Each rule is evaluated only when called by a specific calling function signature. For example, let’s say we have a staking contract withstake(uint256 amount) and unstake(uint256 amount) functions. We want to create a policy that enforces that users cannot stake tokens after September 1st, and cannot unstake tokens until September 15th.
We would create two separate rules in the policy, and they would have different function signatures for the callingFunction property:
Notice that the
timestamp value used in the condition is not part of the function signature. This additional
variable must be passed via the modifier of the unstake function in the smart contract itself.Refer to the Passing Additional Values to the Rules Engine section of the linked guide for more info.Encoded Values
When a calling contract invokes the rules engine, it explicitly defines what data to send along with the call. This is theencodedValues field in the rule definition. This can include values from the calling function signature, and/or other values, like in the example above.