Milestone 1 - Risk Simulation Engine for Compound v3
Chainrisk <> Compound Finance - 3rd Major Update !
In this update, we simulate ‘Black Thursday’ on Compound V3 which will be used to showcase the power of the Chainrisk Risk Simulation Engine.
Disclaimer: These simulations shouldn’t be used for statistical inferences. We would be working on statistically rigorous models in the third milestone to recommend risk parameters.
Chainrisk’s SoW for 1st milestone :
- Onboarding Compound community onto the Chainrisk Risk Simulation Engine.
- Integration of an in-built block explorer, transaction tracer and visualisation library with the simulation engine.
- A robust simulation engine (protocol agnostic) that can stress-test any DeFi protocol through Js/Ts scripts of Agents, Scenarios, Observers, Assertions, and contracts and reduce the dependencies.
- Release a beta-testing version where Chainrisk Team works on specific simulations closely with Compound community and inculcates the feedback received.
Simulation Description:
The simulation will focus on a 53% price decline for WETH (over a period of 1000 blocks ) within the USDC market on the Ethereum chain. The USDC market in the Ethereum chain has a total value locked (TVL) of approximately $1 billion and a wrapped Ether (WETH) holds a significant share with the supply of roughly $440 million. ( As of May 9,2024 )
The Chainrisk Cloud platform specifically targets the Compound V3 Price Oracle, which relies on Chainlink’s Price Feed. By altering the return values of this oracle, the simulation mimics the price drop associated with a Black Thursday event for the WETH asset.
Prodecure:
We run on-chain Agent-based Scenario-based Simulations: They directly interact with the blockchain to create a realistic testing environment that mimics actual operations.
These simulations fork the blockchain at a specified block height, incorporating real data up to that point, including account balances & smart contract states.
Steps for running an Agent-based Simulation -
- Environment Creation: On-chain simulations create a fork of the blockchain to use real, up-to-date data.
This includes all the conditions of the blockchain up to a specific block height, making the simulation as close to real-world conditions as possible.
- Agent-Based Interactions: Agents are deployed within these simulations to mimic the behaviour of different network participants (like lenders, borrowers, & liquidators).
This allows the simulation to evaluate how these actors would behave under various conditions without risking real assets.
- Scenario Testing: Various scenarios can be tested, including extreme market conditions, oracle failures, and gas price spikes.
This helps in understanding the potential impacts of such events on the protocol’s stability & security.
- Analysis: The simulation can track how changes to the blockchain, initiated by user actions or smart contract executions, impact the system.
Our in-built block explorer and visualisation library helps in understanding potential vulnerabilities & areas for optimization.
Agent-based Simulation Setup for Black Thursday:
Agents -
- Liquidator Bot: Utilizes Uniswap flashloans to exploit under-collateralized loans by borrowing the underlying asset (ETH or wBTC), repaying the original loan, and profiting from the price discrepancy.
- Borrower/Supplier: Supplies collateral and takes a loan on the protocol using a specific collateral asset.
Scenario -
Base Token: USDC | Collateral Token: WETH
- Market Conditions: ETH price dropped in the USDC market by 53%.
- Market State:
- USDC Market Cap: $1B
- ETH Supply: $440M
Observers -
- Price of WETH: Monitors the real-time price fluctuations of WETH during the simulation.
- Reserve Balance:
Tracks the total amount of protocol reserves of the base asset before and after
each liquidation event. - Total Borrow: Tracks the total amount of debt (total borrow) of USDC market during the simulation.
- Utilization Rate: Tracks the current protocol utilization during the simulation.
- User Borrow Balance: Tracks the borrowed amount of a users before and after liquidation.
- Collateral Reserves of WETH: The total balance of protocol collateral reserves for WETH.
Assertions -
- After each block is mined, check if there is a drop in price of WETH.
Contracts -
- Oracle: Provides modified price feeds for assets involved (WETH, wBTC, USDC).
Inferences -
- This simulation will reveal the protocol’s resilience under extreme market conditions and the effectiveness of Compound V3 in mitigating risks associated with volatile asset prices.
Platform Overview -
Here’s a full demo of the simulations :-
Platform Screenshots -
This shows how to create an Agent-based simulation:
- Filling in the basic simulation metadata ( like, Name and Description )
- The duration of simulation ( lets say, of 1000 blocks )
- The block height at which we need to fork
- Select whether the transaction tracer is to be turned on/off ( for enabling funds flow, balance changes and internal transactions for the simulations )
- Select the right set of simulation Agents, Assertions, Scenarios, Observers and Contracts for Simulation configuration
All Simulations page:
- View the simulations created on the platform
- Timestamp of simulation
- Agents used in the simulation
- Simulation Length ( in blocks )
- Run, Edit & Delete the simulation
All Simulation Results page:
- View the simulations which have been run on the platform
- Timestamp of simulation
- Duration of simulation
- Status of simulation ( Completed | Running | Failed )
- Number of assertion cases: Passed & Failed
- Delete the simulation
Simulation → Simulation Details
- Has the basic simulation metadata - Name and Description
- Simulation Configuration: Agents, Assertions & Run History
Simulation Results → Basic Simulation Metadata of a simulation -
- State
- Timestamp
- Simulation Duration ( in blocks )
- Number of assertion cases: Passed & Failed
- Terminal - View Logs
- Jump into Block Explorer
Simulation Results Configurations →
Show the Agents, Assertions and Scenarios for the simulation
Terminal - Client Logs
Shows the main steps performed in the simulation along with the timestamps
Simulation Results Configurations - Agent Zoom in
You can read through the JS/TS scripts by simply clicking on the respective agent, scenario and assertion.
Simulation Results - Observer ( utilization )
This tracks the protocol utilisation and the sudden decrement in the value of the utilisation is marked by the liquidation event.
Simulation Results - Observer ( borrowBalance )
This shows the borrow balance of the 10 wallets we selected for the simulation. Based on their collateral ratio, they’ll be liquidated at different time periods as the price keeps on decreasing. When the summation of all the collateral deposits multiplied by liquidation factor is less than the total borrow value, the account is eligible for liquidation. After the liquidation event, the borrow balance plummets to zero as the position is liquidated.
Simulation Results - Observer ( price )
This shows the gradual drop in price of WETH. Total drop is of 53% across the simulation duration set by the user.
Simulation Results - Observer ( reserves )
Reserves = usdc_balance + totalBorrow - totalSupply
usdc_balance = the balance of USDC held by Compound i.e. ERC20(baseToken).balanceOf(address(this))
totalBorrow = the net amount of USDC owed to Compound from borrowers
totalSupply = the amount of USDC “owed” by Compound
Liquidators call the absorb() function - the amount of loan is paid from the reserve. Let’s suppose there is a loan of 100 USDC. When absorb() is called, 100 USDC is deducted from the reserve so we see a sudden downward spike. Then, buycollateral() is called, so assuming 5% liquidation discount ( 100 - 5 = 95 ) 95 is added to the reserve. So, overall there’s less reserves for the protocol.
Simulation Results - Observer ( totalBorrow )
This tracks the total borrow and the sudden decrement in the value of the borrow is marked by the liquidation event.
Simulation Results - Observer ( collateralReservesWETH )
When the absorb() function is called the collateral of the borrower is set to zero and is absorbed by the protocol. So, the collateral reserves of WETH shots up in this case. Now, after deducting the liquidation penalty, rest is used to cover the debt.
Block Explorer ( Latest Transactions ) shows the latest transaction details ( Transaction Hash, Status, the ‘FROM’ address, the ‘TO’ address, function called, gas used, gas limit, block number and timestamp )
Block Explorer - Transactions Overview
Block Explorer ( Latest Blocks ) shows the latest blocks details ( Block Number, Status, timestamp, number of transactions in the block, gas used, gas limit and block size )
Block Explorer - Block Details
Block Explorer - Block Transactions shows the transactions within a particular block
For Economic exploit simulations, we have integrated the transaction tracer. For demonstration purposes, we have simulated the Harvest Finance ( Compound V2 Fork ) Hack and the screenshots attached below are of the same.
Shows the Fund Flow b/w the addresses along with the order of transactions.
Shows the Internal Transactions
Shows the balance changes for the internal transactions b/w the addresses
Note : The first tranche of the grant has been released by Compound and can be verified at Safe{Wallet} – Transaction details
Milestone 2 to be completed soon ! Stay tuned as we prove our commitments to the economic security of Compound