assert
Assert that a contract view return satisfies a comparison, on-chain.
Syntax
Section titled “Syntax”assert <call> [operator] [expected] [message]Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
call |
expression |
A :: call expression, e.g. @token(WETH)::balanceOf(@me) |
[operator] |
string |
Comparison operator: ==, !=, >, <, >=, <=, ~= |
[expected] |
any |
Expected value the return is compared against |
[message] |
string |
Revert message when the assertion fails |
Options
Section titled “Options”| Name | Type | Description |
|---|---|---|
--delta |
number |
Allowed delta for the ~= (approximate) operator |
Examples
Section titled “Examples”load assertions
# Compare a view return against a value (named method; ABI fetched automatically)assertions:assert @token(WETH)::balanceOf(@me) >= @token.amount(WETH 10) "insufficient bal"
# Inline ABI when the return type must be explicit (no ABI lookup)assertions:assert @token(WETH)::{balanceOf(address)(uint256) @me} >= @token.amount(WETH 10) "insufficient bal"
# Select a tuple element with a destructure lens ($ marks the element)set $pool 0x44fA8E6f47987339850636F88629646662444217assertions:assert $pool::{getReserves()(uint112,uint112,uint32)}[_ $ _] >= 1000 "low reserve"
# Approximate comparison with an allowed deltaset $oracle 0x0102030405060708090a0b0c0d0e0f1011121314assertions:assert $oracle::{price()(uint256)} ~= 2000e8 --delta 50e8 "price out of range"
# Bare boolean assertion (asserts the return is true)set $gov 0xc0dbDcA66a0636236fAbe1B3C16B1bD4C84bB1E1assertions:assert $gov::{paused()(bool)}- The first argument must be a
::call expression so the (contract, calldata, return type) can be captured without being evaluated off-chain. - Inside a
batch, a failed assertion reverts the whole transaction. Run standalone, the assertion is evaluated as a read-onlyeth_call. - Operators map to the contract functions by return type:
uintsupports== != > < >= <= ~=;address/bool/bytes32support== !=. - Set
$assertions.addressto override the resolved contract (forks / testing).