Platform: Code4rena
Start Date: 09/01/2024
Pot Size: $100,000 USDC
Total HM: 13
Participants: 28
Period: 28 days
Judge: 0xsomeone
Total Solo HM: 8
Id: 319
League: ETH
Rank: 26/28
Findings: 1
Award: $100.10
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: aariiif
Also found by: 0xepley, LinKenji, Sathish9098, ZanyBonzy, catellatech, fouzantanveer, hassanshakeel13, hunter_w3b, invitedtea, yongskiws
100.0972 USDC - $100.10
A cross margin credit protocol with autonomous monetary policy and dynamic risk parameters.
List | Head | Details |
---|---|---|
a) | Overview of the Opus Project | Summary of the whole Protocol |
b) | Technical Architecture | Architecture of the smart contracts |
c) | The approach I would follow when reviewing the code | Stages in my code review and analysis |
d) | Analysis of the code base | What is unique? How are the existing patterns used? "Solidity-metrics" was used |
e) | Test analysis | Test scope of the project and quality of tests |
f) | Security Approach of the Project | Audit approach of the Project |
g) | In-depth architecture assessment of business logic | Architecture of the Protocol |
h) | Codebase Quality | Overall Code Quality of the Project |
i) | Other Audit Reports and Automated Findings | What are the previous Audit reports and their analysis |
j) | Contract Functionalities | Functionality of different Contracts involved |
k) | Full representation of the project’s risk model | What are the risks associated with the project |
l) | Packages and Dependencies Analysis | Details about the project Packages |
m) | New insights and learning of project from this audit | Things learned from the project |
The Opus project is a decentralized finance (DeFi) platform designed to offer users various financial services on the blockchain. It leverages smart contracts to enable activities such as collateralized lending, liquidity provision, and rewards distribution. Users can interact with the platform by depositing specific cryptocurrencies as collateral to participate in liquidity pools, earn rewards, or take out loans. The project focuses on security, scalability, and user experience, aiming to provide a comprehensive ecosystem for decentralized financial services.
Opus architecture is built around a set of smart contracts, each serving specific roles:
<br/>File Name | Core Functionality | Technical Characteristics | Importance and Management |
---|---|---|---|
shrine.cairo | Manages financial interactions, asset collateral, and debt within the Shrine ecosystem. | Defines complex financial operations, role-based access control, event emissions, and manages deposits, withdrawals, asset rates, and trove management. | Central to the ecosystem's operation, managing core financial mechanisms and user interactions. |
pragma.cairo | Integrates with the Pragma oracle for price data. | Implements oracle interaction for price fetching, validity checks, and manages yang-to-pair ID mappings. | Critical for ensuring accurate and up-to-date pricing information is used within the ecosystem. |
types.cairo | Defines data types and structures used across the contract system. | Includes enums, structs, and utility functions for data representation and manipulation. | Fundamental for the system's structure, ensuring consistent and efficient data handling. |
roles.cairo | Outlines roles and permissions for access control across different modules. | Specifies permissions for actions within the system, facilitating governance and operational security. | Key for managing access and actions within the contract, ensuring secure and authorized operations. |
absorber.cairo | Handles absorption of synthetic assets and distribution of rewards. | Manages the mechanics of absorbing excess assets, distributing rewards, and interaction with blessers for rewards allocation. | Important for maintaining the stability of the synthetic asset and incentivizing participants. |
allocator.cairo | Allocates resources within the ecosystem based on predefined rules. | Manages the distribution and allocation of resources among various components and users within the system. | Ensures efficient resource management and supports the ecosystem's economic balance. |
caretaker.cairo | Oversees the maintenance and administrative operations. | Manages administrative tasks such as system shutdowns, emergency interventions, and operational adjustments. | Essential for system oversight and managing critical operational aspects in emergency scenarios. |
controller.cairo | Controls operational parameters within the ecosystem. | Adjusts key operational parameters such as interest rates, thresholds, and system settings based on governance decisions. | Central to dynamic system management and adaptation to changing conditions. |
equalizer.cairo | Balances resource distribution and access within the ecosystem. | Manages equitable access to resources, adjusting allocations to ensure a balanced and fair ecosystem. | Supports fairness and equity in resource distribution, enhancing system sustainability. |
purger.cairo | Manages the purging of underperforming or risky assets. | Implements mechanisms for identifying and removing risky assets from the system to maintain stability. | Critical for risk management and protecting the ecosystem from volatility and failures. |
seer.cairo | Provides insights and analytics for system performance and metrics. | Gathers and analyzes data on system operations, offering insights for decision-making and strategic adjustments. | Supports informed decision-making and strategic planning through data analysis. |
sentinel.cairo | Monitors system health and triggers responses to operational anomalies. | Watches over system operations, triggering automated responses or alerts for anomalies or operational issues. | Essential for proactive system monitoring and response to ensure continuous stability. |
First, by examining the scope of the code, I determined my code review and analysis strategy. https://code4rena.com/audits/2024-01-opus#top
Accordingly, I would analyze and audit the subject in the following steps;
Number | Stage | Details | Information |
---|---|---|---|
1 | Compile and Run Test | Installation | Test and installation structure is simple, cleanly designed |
2 | Architecture Review | Opus | Provides a basic architectural teaching for General Architecture |
3 | Test Suits | Tests | In this section, the scope and content of the tests of the project are analyzed. |
4 | Manuel Code Review | Scope | |
5 | Using Solodit for common vulnerabilities | Solodit | Using solodit to find common vulnerabilites related to NFT protocol |
6 | Infographic | Figma | Tried to make Visual drawings to understand the hard-to-understand mechanisms |
7 | Special focus on Areas of Concern | Areas of Concern | Code where I should focus more |
The most important summary in analyzing the code base is the stacking of codes to be analyzed. In this way, many predictions can be made, including the difficulty levels of the contracts, which one is more important for the auditor, the features they contain that are important for security (payable functions, uses assembly, etc.), the audit cost of the project, and the time to be allocated to the audit; Uses Consensys Solidity Metrics
filename: This field indicates the language in which smart contracts are written
Code: This field indicates the number of actual lines of code in the smart contract.
Comment: This field indicates the number of lines in the smart contract.
Blank: This field indicates the number of Blank lines in the smart contract.
Total: This field indicates the number of Total lines (code + comment + blank) in the smart contract.
core
contractsTotal : 13 files, 3990 codes, 1489 comments, 1115 blanks, all 6594 lines
filename | filename | code | comment | blank | total |
---|---|---|---|---|---|
src/core/abbot.cairo | Cairo | 160 | 57 | 47 | 264 |
src/core/absorber.cairo | Cairo | 645 | 266 | 199 | 1,110 |
src/core/allocator.cairo | Cairo | 88 | 48 | 34 | 170 |
src/core/caretaker.cairo | Cairo | 208 | 102 | 62 | 372 |
src/core/controller.cairo | Cairo | 207 | 28 | 58 | 293 |
src/core/equalizer.cairo | Cairo | 133 | 48 | 42 | 223 |
src/core/flash_mint.cairo | Cairo | 88 | 35 | 32 | 155 |
src/core/gate.cairo | Cairo | 136 | 58 | 40 | 234 |
src/core/purger.cairo | Cairo | 379 | 150 | 96 | 625 |
src/core/roles.cairo | Cairo | 221 | 0 | 41 | 262 |
src/core/seer.cairo | Cairo | 168 | 42 | 32 | 242 |
src/core/sentinel.cairo | Cairo | 189 | 50 | 53 | 292 |
src/core/shrine.cairo | Cairo | 1,368 | 605 | 379 | 2,352 |
Core
contracts: On average there are 4.69 code lines per comment (lower=better).
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v 2.4.0
curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh snfoundryup -v 0.13.1
scarb test
.Ref:https://xin-xia.github.io/publication/icse194.pdf
1- The project has already underwent an audits(stated in the docs), this innovative assessments on Code4rena is the second audit, where multiple auditors are scrutinizing the code.
According to the Docs, which can be found here
The core contracts found in opus_contracts directory have been audited by:
1- By distributing the project to testnets, ensuring that the audits are carried out in onchain audit. (This will increase coverage)
2- Add On-Chain Monitoring System; If On-Chain Monitoring systems such as Forta are added to the project, its security will increase.
For example ; This bot tracks any DEFI transactions in which wrapping, unwrapping, swapping, depositing, or withdrawals occur over a threshold amount. If transactions occur with unusually high token amounts, the bot sends out an alert. https://app.forta.network/bot/0x7f9afc392329ed5a473bcf304565adf9c2588ba4bc060f7d215519005b8303e3
3- After the Code4rena audit is completed and the project is live, I recommend the audit process to continue, projects like immunefi do this. https://immunefi.com/
4- Emergency Action Plan In a high-level security approach, there should be a crisis handbook like the one below and the strategic members of the project should be trained on this subject and drills should be carried out. Naturally, this information and road plan will not be available to the public. https://docs.google.com/document/u/0/d/1DaAiuGFkMEMMiIuvqhePL5aDFGHJ9Ya6D04rdaldqC0/mobilebasic#h.27dmpkyp2k1z
5- I also recommend that you have an "Economic Audit" for projects based on such complex mathematics and economic models. An example Economic Audit is provided in the link below; Economic Audit with Three Sigma
6 - As the project team, you can consider applying the multi-stage audit model.
Read more about the MPA model; https://mpa.solodit.xyz/
7 - I recommend having a masterplan applied to project team members (This information is not included in the documents). All authorizations, including NPM passwords and authorizations, should be reserved only for current employees. I also recommend that a definitive security constitution project be found for employees to protect these passwords with rules such as 2FA. The LEDGER hack, which has made a big impact recently, is the best example in this regard;
https://twitter.com/Ledger/status/1735326240658100414?t=UAuzoir9uliXplerqP-Ing&s=19
The OPUS protocol's architecture is meticulously designed to serve as an advanced framework for synthetic asset management within the DeFi ecosystem, emphasizing modularity, security, and scalability. This architecture facilitates the creation, trading, and management of synthetic assets by leveraging blockchain technology to offer a decentralized, transparent, and efficient alternative to traditional financial instruments.
At the core of the OPUS protocol is the Shrine Contract, a sophisticated smart contract that manages the lifecycle of synthetic assets. It is responsible for:
Governance within the OPUS protocol is decentralized and democratized through the DAO, which allows token holders to propose, vote on, and implement changes to the protocol. This includes:
The OPUS protocol relies on accurate and timely price data to manage synthetic assets effectively. It integrates with trusted price oracles to:
To incentivize participation and secure the protocol, OPUS implements a staking and rewards mechanism:
In summary, the OPUS protocol's architecture is a testament to the power of combining DeFi innovations with traditional finance principles, offering a comprehensive platform for synthetic asset management. Its emphasis on decentralization, security, and user-centricity positions it as a significant player in the DeFi space, poised to address the challenges of modern finance with blockchain solutions.
Overall, I consider the quality of the Opus protocol codebase to be Good. The code appears to be mature and well-developed. We have noticed the implementation of various standards adhere to appropriately. Details are explained below:
Codebase Quality Categories | Comments |
---|---|
Code Clarity and Readability | The codebase demonstrates good clarity and readability. It follows consistent naming conventions, making it easier for developers to understand and maintain. Documentation is present, but additional comments could enhance understanding in complex areas. |
Code Structure and Formatting | The codebase is well-structured and follows consistent formatting practices. Code is organized logically, which aids in readability and maintainability. |
Modularity and Organization | The codebase is well-organized, with clear separation of concerns into different contracts and modules. Each contract/module has a distinct purpose, which aids in maintainability and upgrades. |
Security Considerations | Security considerations are a top priority in the codebase. The use of time-lock mechanisms, role-based access control, and carefully designed functions mitigates risks and vulnerabilities. Extensive testing and auditing further enhance security. |
Gas Efficiency and Optimization | Gas efficiency is a key focus, with gas optimization techniques applied where possible. For instance, flash minting is implemented efficiently to minimize gas costs. |
Testing and Test Coverage | Comprehensive testing is evident with a high test coverage percentage (90%). Test cases encompass various scenarios, ensuring robustness and reliability. |
External Dependencies and Imports | The codebase does not rely on external dependencies or imports, reducing complexity and ensuring self-sufficiency. |
Code Comments | While the codebase contains some comments, additional detailed comments should be added, especially in complex sections. These comments will significantly enhance code comprehension and serve as valuable documentation. |
Documentation and Comments | Documentation is present but could benefit from additional explanatory comments in certain complex sections. Overall, the codebase is adequately documented, making it accessible to developers and auditors. |
Error Handling and Recovery | The codebase handles errors gracefully, with well-structured error messages and recovery mechanisms. |
Compliance with Best Practices | The codebase aligns with blockchain best practices, including ERC-20 standards, role-based access control, and timelock mechanisms. It adheres to well-established coding patterns. |
Previous Audits
Although the security Report of previous Audit isn't public but we can see it the docs that the opus_contracts
already went an audit
Trail of Bits
The OPUS protocol encompasses a suite of smart contracts, each designed to fulfill specific roles within the ecosystem, focusing on synthetic asset management, governance, collateralization, and rewards distribution. Below, we delve into the key contracts, outlining their primary functionalities and technical characteristics:
Functionalities:
Technical Characteristics:
Functionalities:
Technical Characteristics:
Functionalities:
Functionalities:
Technical Characteristics:
Functionalities:
The OPUS protocol, like any decentralized finance (DeFi) platform, faces several categories of risk, including administrative, systemic, technical, and integration risks. Understanding and mitigating these risks are crucial for the security and efficiency of the protocol.
Centralization of Control: Even with decentralized governance structures like DAOs, the risk of admin abuse exists if a small number of participants control a majority of governance tokens. This could lead to unilateral decision-making, including unfavorable changes to protocol parameters or misallocation of funds.
Governance Manipulation: The potential for governance proposals to be manipulated by actors with large stakes or through social engineering attacks poses a risk to the protocol's integrity and direction.
Smart Contract Vulnerabilities: Bugs or logical errors in the smart contracts can lead to loss of funds, unauthorized access, or unintended behavior. Given the complexity of contracts like the Shrine, interest rate models, and oracle interactions, the attack surface is significant.
Scalability Concerns: As transaction volumes grow, the platform must scale without compromising performance or security.
Package | Usage |
---|---|
starknet | Project uses version 2.4.0 while the recommended version is latest stable version i.e: 2.5.3 |
snforge_std | Project uses version 0.13.1 while the recommended version is latest stable version i.e: 0.16.0 |
As an auditor having reviewed the OPUS protocol, the audit process has yielded several insights and learning experiences, emphasizing the complexity and sophistication of decentralized finance (DeFi) ecosystems. Here are the key takeaways:
The OPUS protocol showcases an intricate web of smart contracts interacting with each other, where the behavior of one contract can significantly impact others. For instance, the Shrine contract's role as the core for synthetic asset management is closely tied to the Interest Rate Model, Price Oracle, and Liquidation Manager, among others. Understanding these interactions is crucial for assessing the protocol's resilience, security, and efficiency.
The audit process highlighted the critical role of governance in the OPUS protocol, managed through the DAO. It showed how decentralized decision-making processes are embedded into the protocol's operations, from proposing adjustments to executing protocol updates. This underlines the necessity for auditors to examine governance mechanisms thoroughly, ensuring they are secure, fair, and resistant to manipulation.
The protocol's approach to managing risk, particularly through the Liquidation Manager and the Interest Rate Model, demonstrated the nuanced balance required to maintain system stability. Auditing these components provided insights into the challenges of designing DeFi systems that are resilient to market volatility and systemic risks.
Price Oracles serve as a backbone for the OPUS protocol, feeding necessary market data for various operations, including asset minting/burning and liquidation processes. The audit reinforced the importance of oracles in DeFi protocols and the need to ensure their reliability, security, and resistance to price manipulation or oracle failure.
The Staking Rewards contract and the subsequent distribution of incentives illustrate innovative approaches to encourage protocol participation and secure network operations. It was insightful to see how these mechanisms are designed to balance protocol liquidity, user engagement, and governance participation.
In conclusion, auditing the OPUS protocol was a comprehensive learning experience, showcasing the depth of innovation in DeFi while also highlighting the critical areas of focus for ensuring the security, stability, and sustainability of such protocols.
Note: I didn't tracked the time, the time I mentioned is just a number.
25 hours
#0 - c4-pre-sort
2024-02-07T17:14:01Z
bytes032 marked the issue as sufficient quality report
#1 - c4-judge
2024-02-26T17:56:30Z
alex-ppg marked the issue as grade-b
#2 - Nabeel-javaid
2024-02-28T19:25:52Z
can you please have another look at this report i don't know why its marked as grade b
#3 - alex-ppg
2024-02-29T16:47:18Z
Hey @Nabeel-javaid, thanks for raising your concerns. The Analysis report contains a lot of boilerplate code that we can observe has been posted in many C4 contests repetitively; the purpose of Analysis reports is to offer unique insights. As such, I retain my original grade.
#4 - Nabeel-javaid
2024-02-29T16:55:43Z
Hey @Nabeel-javaid, thanks for raising your concerns. The Analysis report contains a lot of boilerplate code that we can observe has been posted in many C4 contests repetitively; the purpose of Analysis reports is to offer unique insights. As such, I retain my original grade.
Yeah I can see that this template has been used alot of times but I've used it myself on alot of contests the template is very much similar but the data isn't.
It's a humble request to please have another look at the report