Opus - 0xepley's results

A cross margin credit protocol with autonomous monetary policy and dynamic risk parameters.

General Information

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

Opus

Findings Distribution

Researcher Performance

Rank: 26/28

Findings: 1

Award: $100.10

🌟 Selected for report: 0

🚀 Solo Findings: 0

Findings Information

Labels

analysis-advanced
grade-b
sufficient quality report
A-01

Awards

100.0972 USDC - $100.10

External Links

🛠️ Analysis - Opus

A cross margin credit protocol with autonomous monetary policy and dynamic risk parameters.

Summary

ListHeadDetails
a)Overview of the Opus ProjectSummary of the whole Protocol
b)Technical ArchitectureArchitecture of the smart contracts
c)The approach I would follow when reviewing the codeStages in my code review and analysis
d)Analysis of the code baseWhat is unique? How are the existing patterns used? "Solidity-metrics" was used
e)Test analysisTest scope of the project and quality of tests
f)Security Approach of the ProjectAudit approach of the Project
g)In-depth architecture assessment of business logicArchitecture of the Protocol
h)Codebase QualityOverall Code Quality of the Project
i)Other Audit Reports and Automated FindingsWhat are the previous Audit reports and their analysis
j)Contract FunctionalitiesFunctionality of different Contracts involved
k)Full representation of the project’s risk modelWhat are the risks associated with the project
l)Packages and Dependencies AnalysisDetails about the project Packages
m)New insights and learning of project from this auditThings learned from the project

a) Overview of the Opus 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.

Key Features and Functionalities:

  1. Collateral Management: Users can deposit various tokens (e.g., WBTC, ETH, wstETH) as collateral to participate in the platform's offerings.
  2. Liquidity Provision and Earning Rewards: It facilitates liquidity provision to various pools, allowing users to earn rewards based on their contributions.
  3. Loan and Credit Facilities: Offers mechanisms for users to take loans against their deposited collateral under certain conditions.
  4. Risk Management and Liquidation Protocols: Implements safeguards and protocols for managing the health of assets and positions, including liquidation mechanisms for undercollateralized positions.
  5. PID Controller for Dynamic Adjustments: Incorporates a PID controller for adaptive and dynamic adjustments within the system, enhancing stability and responsiveness.

b) Technical Architecture:

<br/>

Screenshot-from-2024-02-06-23-43-07.png

Opus architecture is built around a set of smart contracts, each serving specific roles:

<br/>
File NameCore FunctionalityTechnical CharacteristicsImportance and Management
shrine.cairoManages 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.cairoIntegrates 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.cairoDefines 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.cairoOutlines 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.cairoHandles 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.cairoAllocates 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.cairoOversees 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.cairoControls 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.cairoBalances 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.cairoManages 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.cairoProvides 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.cairoMonitors 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.

Domain Model of the Protocol

<br/>

Screenshot-from-2024-02-06-23-27-58.png

c) The approach I would follow when reviewing the code

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;

NumberStageDetailsInformation
1Compile and Run TestInstallationTest and installation structure is simple, cleanly designed
2Architecture ReviewOpusProvides a basic architectural teaching for General Architecture
3Test SuitsTestsIn this section, the scope and content of the tests of the project are analyzed.
4Manuel Code ReviewScope
5Using Solodit for common vulnerabilitiesSoloditUsing solodit to find common vulnerabilites related to NFT protocol
6InfographicFigmaTried to make Visual drawings to understand the hard-to-understand mechanisms
7Special focus on Areas of ConcernAreas of ConcernCode where I should focus more

d) Analysis of the code base

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.

Analysis of sloc of core contracts

Total : 13 files, 3990 codes, 1489 comments, 1115 blanks, all 6594 lines

Files

filenamefilenamecodecommentblanktotal
src/core/abbot.cairoCairo1605747264
src/core/absorber.cairoCairo6452661991,110
src/core/allocator.cairoCairo884834170
src/core/caretaker.cairoCairo20810262372
src/core/controller.cairoCairo2072858293
src/core/equalizer.cairoCairo1334842223
src/core/flash_mint.cairoCairo883532155
src/core/gate.cairoCairo1365840234
src/core/purger.cairoCairo37915096625
src/core/roles.cairoCairo221041262
src/core/seer.cairoCairo1684232242
src/core/sentinel.cairoCairo1895053292
src/core/shrine.cairoCairo1,3686053792,352

Comment-to-Source Ratio:

Core contracts: On average there are 4.69 code lines per comment (lower=better).

e) Test analysis

  1. Install Scarb v2.4.0 by running:
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v 2.4.0
  1. Install Starknet Foundry v0.13.1 by running:
curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh snfoundryup -v 0.13.1
  1. Run scarb test.

What did the project do differently? ;

    1. It can be said that the developers of the project did a quality job, there is a test structure consisting of tests with quality content. In particular, tests have been written successfully.
    1. Overall line coverage percentage provided by your tests : 90

What could they have done better?

    1. If we look at the test scope and content of the project with a systematic checklist, we can see which parts are good and which areas have room for improvement As a result of my analysis, those marked in green are the ones that the project has fully achieved. The remaining areas are the development areas of the project in terms of testing ;

test-cases.jpg

Ref:https://xin-xia.github.io/publication/icse194.pdf

nabeel-1.jpg

  • 2): It is recommended to increase the test coverage to 100% so make sure that each and every line is battle tested

f) Security Approach of the Project

Successful current security understanding of the project;

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:

  • Trail of Bits

What the project should add in the understanding of Security;

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.

sla.png

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

g) In-depth architecture assessment of business logic

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.

Shrine Contract: The Heart of OPUS

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:

  • Minting and Burning: Users can mint new synthetic assets by locking collateral in the Shrine Contract. Similarly, assets can be burned to release the underlying collateral, maintaining a stable and secure backing for each synthetic asset issued.
  • Interest Rate Adjustment: Employing an algorithmic approach, the Shrine Contract dynamically adjusts interest rates based on the total supply and demand within the protocol, optimizing economic incentives and ensuring system stability.
  • Collateral Management: The contract manages a diverse range of collateral types, adjusting collateralization ratios in real-time to reflect market conditions and mitigate risks associated with price volatility.
  • Liquidation Protocol: Incorporates a robust liquidation mechanism to safeguard against undercollateralized positions, ensuring the protocol's solvency and protecting users' interests.

Decentralized Autonomous Organization (DAO)

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:

  • Parameter Adjustments: The DAO has the authority to modify critical protocol parameters, such as collateralization ratios, interest rates, and supported collateral types, in response to evolving market dynamics.
  • Protocol Upgrades: Through collective decision-making, the community can introduce new features, optimize existing ones, and make strategic decisions to steer the protocol's development direction.
  • Reward Distribution: The DAO also oversees the distribution of rewards within the protocol, ensuring that incentives are aligned with the protocol's long-term health and sustainability.

Price Oracle Integration

The OPUS protocol relies on accurate and timely price data to manage synthetic assets effectively. It integrates with trusted price oracles to:

  • Provide Real-time Pricing: Ensures that all synthetic assets are minted, traded, and liquidated at fair market values.
  • Mitigate Oracle Risks: Implements multiple oracle sources and fallback mechanisms to protect against single points of failure and ensure data integrity.

Staking and Rewards Mechanism

To incentivize participation and secure the protocol, OPUS implements a staking and rewards mechanism:

  • Staking for Security: Users can stake tokens to participate in governance, secure the network, and contribute to the protocol's stability.
  • Dynamic Rewards Distribution: The protocol dynamically adjusts reward distributions based on participation levels and system needs, encouraging behaviors that enhance protocol health.

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.

h) Codebase Quality

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 CategoriesComments
Code Clarity and ReadabilityThe 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 FormattingThe codebase is well-structured and follows consistent formatting practices. Code is organized logically, which aids in readability and maintainability.
Modularity and OrganizationThe 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 ConsiderationsSecurity 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 OptimizationGas 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 CoverageComprehensive testing is evident with a high test coverage percentage (90%). Test cases encompass various scenarios, ensuring robustness and reliability.
External Dependencies and ImportsThe codebase does not rely on external dependencies or imports, reducing complexity and ensuring self-sufficiency.
Code CommentsWhile 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 CommentsDocumentation 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 RecoveryThe codebase handles errors gracefully, with well-structured error messages and recovery mechanisms.
Compliance with Best PracticesThe codebase aligns with blockchain best practices, including ERC-20 standards, role-based access control, and timelock mechanisms. It adheres to well-established coding patterns.

i) Other Audit Reports and Automated Findings

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

Known issues and risks

  • The protocol relies on a trusted and honest admin with superuser privileges for all modules with access control at launch.
  • There is currently no fallback oracle. This is planned once more oracles are live on Starknet.
  • Interest is not accrued on redistributed debt until they have been attributed to a trove. This is intended as the alternative would be too computationally intensive.
  • Interest that have not been accrued at the time of shutdown will result in a permanent loss of debt surplus i.e. income. This is intended as the alternative to charge interest on all troves would be too expensive.

j) Contract Functionalities

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:

Shrine Contract

Functionalities:

  • Minting and Burning of Synthetic Assets: Enables users to create or destroy synthetic assets by depositing or withdrawing collateral, respectively.
  • Interest Rate Management: Dynamically adjusts interest rates based on the protocol's economic conditions to maintain stability and incentivize certain behaviors.
  • Collateral Management: Supports multiple types of collateral, managing their valuation and liquidation thresholds to ensure the protocol remains overcollateralized.
  • Liquidation Mechanism: Initiates liquidation processes for undercollateralized positions to protect the protocol's integrity and user assets.

Technical Characteristics:

  • Modular Design: Allows for easy updates and integration of new features or collateral types without disrupting the core functionalities.
  • Security Mechanisms: Implements checks and balances, such as reentrancy guards and oracle validation, to mitigate risks associated with smart contract vulnerabilities and price manipulation.
<br/>

Screenshot-from-2024-02-07-00-39-26.png

<br/>

DAO Governance Contract

Functionalities:

  • Proposal Submission and Voting: Facilitates the decentralized governance process, allowing token holders to submit proposals and vote on them.
  • Protocol Parameter Adjustment: Authorizes changes to critical protocol parameters, such as interest rates, collateral types, and liquidation thresholds.
  • Contract Upgrades: Manages the upgrade process for the protocol's smart contracts, ensuring that changes are made transparently and with community consensus.

Technical Characteristics:

  • Flexible Voting System: Supports various voting mechanisms, including simple majority and weighted voting, to accommodate different types of decisions.
  • Timelock Mechanism: Enforces a delay between proposal approval and execution, providing a window for community review and potential vetoing of contentious decisions.

Price Oracle Contract

Functionalities:

  • Price Feeding: Provides real-time price data for various assets, enabling accurate valuation of collateral and synthetic assets.

Staking and Rewards Contract

Functionalities:

  • Staking Collateral Tokens: Allows users to stake collateral tokens to participate in governance and earn rewards.
  • Rewards Distribution: Manages the distribution of protocol-generated rewards to stakers based on their contribution and stake size.

Technical Characteristics:

  • Flexible Reward Mechanisms: Adopts dynamic reward formulas that can adjust based on the performance and staking participation rates.
  • Governance Integration: Tightly integrates with the DAO governance contract to allow stakers to exercise their governance rights directly.

Liquidation Manager Contract

Functionalities:

  • Automated Liquidation Processes: Automatically triggers the liquidation of undercollateralized positions to safeguard the protocol's solvency.
  • Liquidation Auctions: Facilitates liquidation auctions, allowing participants to bid on collateral from liquidated positions.

k) Full representation of the project’s risk model

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.

Admin Abuse Risks

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.

Technical Risks

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.

l) Packages and Dependencies Analysis 📦

PackageUsage
starknetProject uses version 2.4.0 while the recommended version is latest stable version i.e: 2.5.3
snforge_stdProject uses version 0.13.1 while the recommended version is latest stable version i.e: 0.16.0

m) New insights and learning of project from this audit:

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:

1. Interconnectedness of Contracts

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.

2. Importance of Governance

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.

3. Complexity of Risk Management

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.

4. Oracles as Critical Infrastructure

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.

5. Innovative Use of Staking and Rewards

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.

Time spent:

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

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter