Skip to main content

Create a Trap

Monitor and analyze blockchain data.

Trap Anatomy

contract ExampleTrap {
struct CollectOutput {
// Monitored data points defined here
}

constructor() {
// Initialization logic defined here but without constructor arguments
}

function collect() external view returns (CollectOutput memory) {
// Data collection and monitoring logic defined here
return CollectOutput({});
}

function isValid(CollectOutput[] calldata dataPoints) external pure returns (bool) {
// Validation logic defined here
return true;
}
}

Constructor

The constructor is used to initalize the Trap with any necesseary data such as addresses of smart contracts to monitor. The constructor is called once when the Trap is deployed to a shadow fork without arguments.

Collect Function

The collect function is responsible for gathering data from the blockchain and returning it in a standardized format. This function is called by Operators on every new block and the output is stored off-chain.

info

The collect function is constrained to the view modifier because it is executed over a shadow fork of the evm state data and any state changes will not be persisted. State must only be viewed, not modified.

Collect Output

The CollectOutput struct is the standardized format for returning data from the collect function. This struct is defined by the developer and can contain any data points that are relevant to the Trap. The name of the struct can be anything but it must be exported and the collect function must return an instance of the struct.

IsValid Function

The isValid function is responsible for validating the data returned by the collect function. This function is called by the Operator on every new block and is used to determine whether or not the Trap response should be executed. The collect function is called first followed by the isValid function.

The isValid function takes an array of CollectOutput structs as an argument. The Operator will call the isValid function with the previous blocks of data returned by the collect function.

The outputs are ordered from oldest to newest. The last element in the array is the most recent block of data returned by the collect function. The first element in the array is the oldest block of data returned by the collect function.

The isValid function must return a bool value. If the value is true then the Trap response will not be executed. If the value is false then the Trap response will be executed by the Operators.

info

The isValid function is constrained to the pure modifier because the isValid function is not executed over a shadow fork. All relevant state is passed to the function as an argument.