Cozy Finance recently developed and open-sourced Agent Buttercup: a flexible, Rust-based framework for conducting computationally-intensive, agent-based simulations in an EVM environment.
Agent-based simulation (ABS) is a computational approach for modeling the actions of agents in order to understand the behavior of a complex system.
ABS is used in industries ranging from self-driving cars to trading. In such applications, it is not easy to intuit how changes to a system may affect outcomes given the participation of heterogenous agents and the endogenous nature of the decisions they make. Further, experimental approaches such as A/B testing are usually infeasible because they take too long and are too costly. In these cases, ABS can deliver statistically rigorous results about a complex system by composing simple models of agents and their environments.
Agent Buttercup was originally developed as a way for our team to explore new pricing mechanisms within the Cozy V2 protocol. We found ABS to be an attractive approach to study DeFi markets, which are driven by a multitude of agents (LPs, traders, liquidators, etc.) optimizing different risk-reward functions. We want to open-source it in order to give DeFi researchers/engineers the tools they need to quantitatively:
Evaluate the effects of new protocol mechanisms on market outcomes
Optimize risk settings, model parameters and incentives
Stress-test the robustness of a protocol against adversarial economic attacks and market environments
The hope is that it enables faster innovation of new DeFi mechanisms and a better grasp on the existing DeFi mechanisms.
At a high-level, the core components of Agent Buttercup are as follows:
State, an object which keeps track of:
EVM state inside a https://github.com/bluealloy/revm instance
Any non-EVM state inside a separate World object
Agents, who in each step, decide what to do based on the current state and send transactions to update the EVM or World state.
Time Policy, which determines how block numbers/timestamps progress in each step.
Summary Generators, which compute summary statistics from state and write them to a file for analysis.
Manager, the core object responsible for coordinating the state, agents, time policy and summarizers. The manager effectively runs a loop. In each step:
Agents are processed in parallel across multiple threads and asked to send the manager any updates.
Updates are executed against the EVM and world state.
Update results (Did the EVM tx revert?, etc.) are temporarily cached in state.
Agents are given the ability to read state and update any local state they track.
Summary generators are run and statistics are written to the file.
The time policy is called and the EVM block number/timestamp are updated.
The use of ABS in DeFi is not new.
The DELV team built a Python-based ABS for their protocol on top of Apeworx. To deliver high-performance simulations and lean into the growing ecosystem of Rust-based Ethereum tooling, we decided to build on top of https://github.com/bluealloy/revm. Using revm directly allows us to avoid I/O overhead associated with local node implementations like Anvil, offer maximum customizability, and easily integrate with other revm-based infrastructure like https://github.com/paradigmxyz/reth and https://github.com/foundry-rs/foundry.
Agent Buttercup was originally inspired by the work of the Primitive Finance team on Arbiter. However, the two frameworks are quite different implementation-wise and we assume they will ultimately serve different use cases. The key differences are as follows:
Notion of time: The core of Agent Buttercup is a simulation loop, where the block timestamp/number moves forward in each step per a customizable time policy. In Arbiter, there is no built-in notion of time or steps. Time is crucial to the Cozy V2 protocol where market utilization can decay over time. Further, we found the use of steps natural in an ABS framework where agents may need to know they are reading the most up-to-date state before deciding next actions.
Notion of state: Agent Buttercup defines a fully customizable definition of state, including EVM state and non-EVM World state. In each step, agents are allowed to read and take actions which update the EVM and/or World. In Arbiter, there is only EVM state and agents cannot read full state. Instead, all agents run completely asynchronously and all actions result from responding to events they receive from each other via a message-passing approach.
Agent Buttercup is now fully open-sourced.
We encourage DeFi researchers/engineers to try it out and let us know what they think. Further, we are committed to improving the framework, making it faster, and integrating it with the rest of the open-source ecosystem. If you are interested in contributing, please get in touch. We welcome collaboration. In the near future, we would like to support forking a chain onto the revm instance and exploring ways to accelerate the simulation loop.