Precursor is a behavior-first, pre-transaction DeFi defense system that detects predatory transaction sequences, simulates invariant impact before withdrawal, and blocks unsafe actions via an on-chain SecurityController gate.
Precursor monitors EVM event streams, detects high-confidence attack sequences such as deposit → borrow → oracle manipulation → phantom borrow, simulates the post-transaction state against collateralization invariants, and enforces a deterministic block decision before collateral can be withdrawn.
- Smart contracts in
packages/contracts - Shared types and schemas in
packages/shared - EVM ingestion and event decoding in
packages/evm - Behavioral sequence detection in
packages/behavior-engine - Attack path reconstruction in
packages/attack-analysis - Simulation and invariant evaluation in
packages/simulation - Policy gating in
packages/policy-engine - API and investigation UI in
apps/apiandapps/web
The system follows a fail-closed pattern:
- advisory engines explain and classify behavior
- deterministic policy logic validates the result
- on-chain contract logic enforces the final decision
Never use unverified heuristic output to move funds or sign transactions directly.
- Install Node.js and npm.
- Install Foundry (
forge,cast,anvil). - Copy
.env.exampleto a real env file and fill in deployment values. - Start a local chain for testing:
anvil -p 8555- Set the environment variables for the API and contract addresses.
npm install
npm run build
npm run verify
npm run dev:apiYou can also run the contract suite directly:
cd packages/contracts
forge testA minimal template is in .env.example.
Required production values include:
NODE_ENVPORTRPC_URLCHAIN_IDLENDING_POOLORACLECOLLATERALSECURITY_CONTROLLERPRECURSOR_API_KEY
The project’s release standard is:
npm run verifyThis runs:
- TypeScript typecheck
- Vitest unit and integration tests
- Foundry contract tests
- monorepo build
- Use the API only with a valid
X-Precursor-Keyheader. - Treat all contract addresses as deployment-critical values.
- Do not expose production secrets in source control or logs.
- If the simulation layer is unavailable, the policy gate fails closed.
apps/
api/
web/
docs/
packages/
attack-analysis/
behavior-engine/
contracts/
evm/
policy-engine/
shared/
simulation/