DittoETH - fouzantanveer's results

A decentralized stablecoin protocol with an order book design for supercharged staking yield.

General Information

Platform: Code4rena

Start Date: 15/03/2024

Pot Size: $60,500 USDC

Total HM: 16

Participants: 43

Period: 21 days

Judge: hansfriese

Total Solo HM: 5

Id: 348

League: ETH

DittoETH

Findings Distribution

Researcher Performance

Rank: 32/43

Findings: 1

Award: $24.56

🌟 Selected for report: 0

🚀 Solo Findings: 0

Findings Information

Labels

analysis-advanced
grade-b
sufficient quality report
edited-by-warden
A-03

Awards

24.5635 USDC - $24.56

External Links

Approach taken in evaluating the DittoETH

In conducting the audit for the DittoETH project, I took a systematic approach to thoroughly evaluate both the conceptual framework and the implementation details of the protocol. Here’s a breakdown of my methodical audit process:

  1. Overview and Conceptual Understanding: Initially, I immersed myself in DittoETH's project overview on Code4rena. This step was fundamental in grasping DittoETH's innovative approach to decentralized leveraged short positions on Ethereum. The key takeaway was the protocol's unique mechanism for users to trade and liquidate positions in a trustless environment.

  2. Technical Architecture: I then progressed to understanding the technical architecture as documented on DittoETH's website. This provided clarity on the protocol's reliance on oracles for price determination, its governance structure, and the intricacies of its bridge credit system. This phase was pivotal in comprehending the operational dynamics and innovative features of DittoETH.

  3. Prior Audit Insights: The next step involved a deep dive into Codehawk's previous audit report, focusing on identified vulnerabilities such as potential oracle manipulation, front-running risks, and nuances within the bridge credit system. Analyzing these insights was critical in pinpointing areas for rigorous scrutiny and potential security vulnerabilities.

  4. In-depth Codebase Review: The crux of my audit was an exhaustive examination of the codebase, where I prioritized files based on their significance to the protocol's core functionality and inherent risk. Below is a detailed table of the top 12 files I reviewed, ranked by priority and the rationale behind their selection:

PriorityFile NameRationale
1LibOrders.solCentral to the protocol’s order matching logic; scrutinized for complex vulnerabilities.
2RedemptionFacet.solVital for the dUSD to ETH conversion process; critical for maintaining liquidity and user trust.
3BidOrdersFacet.solFacilitates bid creation and matching; essential for market functionality and engaging user experience.
4ShortOrdersFacet.solManages short order logic; crucial for leveraging features and overall protocol stability.
5PrimaryLiquidationFacet.solGoverns liquidation processes; imperative for market health and mitigating insolvency risks.
6LibOracle.solPrice feed accuracy impacts all operations; evaluated for timeliness and reliability.
7BridgeRouterFacet.solManages cross-protocol interactions; essential for LST deposit/withdrawal processes.
8ExitShortFacet.solEnables users to exit shorts; paramount for ensuring user confidence and liquidity.
9LibSRUtil.solSupplements short record functionalities; scrutinized for data accuracy and efficiency.
10LibBridgeRouter.solUnderpins bridge operations; essential for managing bridge credits and facilitating withdrawals.
11LibBytes.solUtilized for handling proposal data; critical for ensuring data integrity and protocol operations.
12UniswapOracleLibrary.solProvides fallback TWAP price feeds; essential for oracle reliability and price accuracy.
  1. Testing and Validation: My testing regimen encompassed a comprehensive suite of tests, including unit, fork, and gas optimization assessments, to validate the protocol's robustness and efficiency. This hands-on testing phase was crucial in uncovering unexpected issues and verifying the protocol's expected behavior under various scenarios.

This thorough approach, blending comprehensive theoretical research with detailed practical testing, enabled me to conduct an exhaustive audit of DittoETH. I aimed to identify areas for enhancement while affirming the validity of its novel mechanisms.

Conceptual Overview

DittoETH is a pioneering DeFi project designed to facilitate decentralized leveraged short positions on the Ethereum blockchain. From a user perspective, it stands as a bridge between traditional financial trading concepts and the burgeoning world of decentralized finance, allowing participants to engage with the market in ways previously confined to centralized platforms.

At the heart of DittoETH's user experience is the ability to create and manage short positions against various Ethereum-based assets without relying on a centralized authority. Users can effectively bet against the future price movements of these assets, offering a mechanism to hedge or speculate within a trustless environment. This functionality not only enhances the dynamism of the DeFi ecosystem but also contributes to its maturity by integrating complex financial instruments.

Interactions within the DittoETH ecosystem revolve around several core functionalities. Initially, users can deposit collateral (in ETH or ERC-20 tokens) to open short positions, which are represented as ShortRecords within the protocol. This process is facilitated through a user-friendly interface that abstracts the complexities of smart contract interactions, making it accessible to both seasoned traders and DeFi novices.

Once a position is open, DittoETH's automated matching system allows other users to take the opposite side of the trade. This bid-ask dynamic is crucial for maintaining liquidity and ensuring that positions can be entered and exited with minimal slippage. For users holding short positions, the project offers mechanisms to manage these positions actively, including adding collateral to avoid liquidation or closing the position to capture profits or limit losses.

Liquidation processes within DittoETH are designed with the health of the ecosystem in mind, ensuring that positions are only liquidated when necessary to maintain overall protocol stability. Users can participate in liquidation processes, providing an additional layer of interaction and the potential for profit.

Furthermore, DittoETH incorporates innovative features like bridge routing and redemption facets, allowing users to seamlessly convert between different token types and withdraw their collateral or profits in their preferred assets. This flexibility is a testament to the protocol's user-centric design, ensuring that participants have control over their assets and can interact with the ecosystem according to their individual preferences and risk tolerance.

In summary, DittoETH provides a comprehensive suite of tools for engaging with leveraged short positions within a decentralized setting. Its user interface and underlying functionalities cater to a wide range of financial strategies, from speculative trading to hedging against market movements. By offering a decentralized alternative to traditional short-selling mechanisms, DittoETH not only expands the capabilities of the DeFi sector but also aligns with the broader ethos of autonomy and trustlessness that underpins the cryptocurrency space.

Conceptual.png

Architecture

Diving into the technical intricacies of DittoETH's architecture reveals a harmonious blend of smart contracts, meticulously designed to ensure seamless operation, risk management, and a user-centric approach to DeFi trading and asset management. This detailed analysis underscores the logical underpinnings and the codebase that enable DittoETH's functionalities:

Core Facets and Their Functions

  • BidOrdersFacet & ShortOrdersFacet: These facets are pivotal for order creation and management, utilizing createBidOrder and createShortOrder functions respectively. The logic within these functions is designed to intake user orders, validate them against current market conditions and the platform's rules, and queue them for execution. These facets rely on LibOrders.sol for order matching logic, ensuring that order execution is both efficient and equitable.

  • PrimaryLiquidationFacet: Central to maintaining the platform's health, this facet uses liquidatePosition to identify and process undercollateralized positions. This is crucial for mitigating risk and ensuring the platform's solvency. It leverages LibSRUtil.sol for calculations related to short records (SR), assessing their health against market conditions.

  • BridgeRouterFacet: This facet underscores the platform's interoperability through deposit and withdraw functions, bridging assets across ecosystems. It incorporates LibBridgeRouter.sol for the underlying logic, managing the nuances of asset transfers and the unique credit mechanism, enhancing liquidity without compromising user experience.

  • ExitShortFacet: Offering users a pathway to exit their positions, this facet's exitShort function is intricately designed to allow position closures under various conditions. It utilizes LibSRUtil.sol for managing the intricacies of short position closures, ensuring that users can exit their positions in a streamlined manner.

  • RedemptionFacet: A distinctive feature, this facet allows the conversion of dUSD to ETH, facilitated by the proposeRedemption function. This feature is vital for asset management, tapping into the liquidity pools to ensure efficient processing of redemptions.

Specialized Libraries for Enhanced Functionality

  • LibOracle.sol: This library is integral for fetching and processing price data from Chainlink and Uniswap, ensuring the platform's trading mechanisms are based on accurate market information. It provides a solid foundation for functions like getOraclePrice, crucial for order execution and liquidation processes.

  • LibOrders.sol: The backbone of the platform's trading logic, this library handles order lifecycle management, including creation, execution, and cancellation. Its sellMatchAlgo and buyMatchAlgo functions are central to matching bids and asks, showcasing a deep integration of market dynamics within the platform's core.

Integrations and Dependencies

The strategic choice to integrate with established DeFi protocols like Chainlink and Uniswap for oracles and liquidity calculations speaks volumes about DittoETH's commitment to reliability and accuracy. These integrations are seamlessly woven into the platform's logic, ensuring that users can rely on up-to-date market data for their trading decisions.

Conclusion: A Symphony of Smart Contracts

DittoETH's architecture is a complex yet elegantly designed ecosystem of smart contracts, each serving a unique purpose yet collectively ensuring the platform's robustness, adaptability, and user-centricity. From facilitating seamless trading and risk management to ensuring interoperability and future-proofing through upgradability, the architecture stands as a testament to the thoughtful integration of DeFi functionalities. As the platform evolves, maintaining a balance between complexity and usability, security, and innovation will be paramount, underlining the importance of continuous engagement with the community and staying abreast of technological advancements in the DeFi space.

technical.png

Basic Interactions of DittoETH

Interactions.png

Bridging Fees Calculation

The DittoETH project employs a fascinating approach to calculate bridging fees based on the market premium or discount of rETH and stETH relative to ETH. This mechanism ensures that the fees are dynamically adjusted to the current market conditions, mitigating arbitrage risks and ensuring fairness in transactions.

Given our hypothetical values:

  • rETH TWAP: 1.01 (1% premium)
  • rETH Oracle: 1.00
  • stETH TWAP: 0.99 (1% discount)
  • stETH Oracle: 1.00

The calculated factor for rETH is 1.01, indicating a 1% premium over the oracle price. Conversely, the factor for stETH is 0.99, reflecting a 1% discount. This method provides an elegant way to adjust fees based on real-time market performance, ensuring the bridging mechanism remains balanced and fair.

Disbursement of Collateral

The disburseCollateral function is another critical mathematical operation in the project, addressing the adjustment of collateral based on yield rate differences. This operation is crucial for managing the risks and rewards associated with shorting operations.

For our example:

  • Initial Collateral: 100 dETH
  • Vault's Yield Rate: 5% (1.05)
  • Short's Yield Rate: 2% (1.02)

The yield to be disbursed as a result of the difference in yield rates is 3 dETH. This calculation ensures that the collateral is adjusted to reflect the accrued yield, thereby maintaining the integrity and fairness of the shorting mechanism. The use of yield rates in this manner allows for a dynamic adjustment to the short's collateral, ensuring that the system remains balanced even as market conditions change.

To analyze the mathematical logic behind the DittoETH project, particularly focusing on bridging fees and collateral disbursement, I delved into specific sections of the provided codebase and documentation. My approach involved isolating key formulas and calculations pivotal to these functionalities.

Firstly, for the bridging fees, I extracted the relevant parts of the code that calculate the market premium or discount of rETH and stETH relative to ETH. Using hypothetical values for rETH and stETH prices from both TWAP (Time-Weighted Average Price) and Oracle, I manually calculated the factors to understand how the project dynamically adjusts fees based on market conditions.

Similarly, for the disburseCollateral function, I identified the section of code responsible for adjusting collateral based on yield rate differences. By assigning hypothetical values to the initial collateral, vault's yield rate, and short's yield rate, I was able to manually compute the yield to be disbursed. This calculation was crucial for understanding how the project manages the risk and rewards associated with shorting operations.

To further validate these calculations and ensure accuracy, I leveraged Python as a quick and effective tool for running these calculations. By inputting the hypothetical values into a Python script, I was able to quickly compute the expected outcomes for both bridging fees and collateral disbursement. This process not only confirmed my manual calculations but also provided a clear and tangible understanding of the underlying mathematical logic within the DittoETH project.

This methodical approach, combining manual analysis with Python scripting, allowed me to thoroughly understand the mathematical mechanisms at play within DittoETH, ensuring a comprehensive analysis of how the project dynamically adjusts to market conditions and manages financial operations.

Calculations involved in Ditto

Certainly! Let's dive into some additional calculations related to the DittoETH project, focusing on two specific aspects: yield distribution and redemption fees. We'll break down the logic behind these calculations and discuss how they're pivotal to the project's operation.

1. Yield Distribution

In the DittoETH ecosystem, yield is generated from collateral held in vaults. This yield is then distributed to users based on their participation (e.g., those who have short positions). The calculation for distributing yield involves determining the difference in yield rates between the time a short position was opened and the current rate.

Formula:

Yield = Collateral * (CurrentVaultYieldRate - ShortPositionYieldRate)
  • Collateral: The amount of collateral locked by the user for the short position.
  • CurrentVaultYieldRate: The current yield rate offered by the vault.
  • ShortPositionYieldRate: The yield rate at the time the short position was initiated.

Example Calculation: Suppose a user has locked 100 ETH as collateral for a short position. At the time of opening the short, the yield rate was 0.05 (5%), and the current vault yield rate has increased to 0.07 (7%).

Yield = 100 ETH * (0.07 - 0.05) = 2 ETH

The user is entitled to an additional 2 ETH as yield on their collateral, demonstrating how active participation in the platform's financial mechanisms can generate returns.

2. Redemption Fees

Redemption fees are crucial for maintaining balance in the DittoETH system, especially during operations where dUSD is swapped for ETH. The fee is calculated based on a dynamic base rate that adjusts with market conditions and the total amount being redeemed.

Formula:

RedemptionFee = RedeemedAmount * RedemptionRate
  • RedeemedAmount: The total ETH value of dUSD being redeemed.
  • RedemptionRate: A dynamically adjusted rate based on market conditions, oracle prices, and internal thresholds.

Example Calculation: Let's assume a user wishes to redeem 50 ETH worth of dUSD at a time when the redemption rate is set at 1%.

RedemptionFee = 50 ETH * 0.01 = 0.5 ETH

The user would incur a fee of 0.5 ETH for this redemption, which is designed to mitigate potential exploitation and ensure fair usage of the system's liquidity.

Logical Context

Both of these examples underscore how DittoETH manages and incentivizes user interaction with its ecosystem:

  • Yield Distribution encourages users to actively participate by locking collateral, thereby enhancing liquidity and stabilizing the platform. The mechanism ensures that users are rewarded in line with the risks they take and the value they add.
  • Redemption Fees serve as a mechanism to prevent arbitrage exploits and excessive liquidity drain, ensuring the platform remains stable and sustainable even during volatile market conditions. It aligns user actions with the overall health of the ecosystem, discouraging actions that could destabilize the system.

In summary, these calculations are integral to DittoETH's design, balancing incentives with safeguards to cultivate a robust and engaging DeFi platform.

Bridge Router and LST (Liquidity Staking Tokens) Mechanism

The Bridge Router and Liquidity Staking Tokens (LST) mechanism in DittoETH's codebase are intricately designed to facilitate the deposit and withdrawal of various forms of staked liquidity assets, such as rETH and stETH, while ensuring fairness and efficiency. Here's a deeper dive into how these components function and interact based on an analysis of the provided codebase:

Bridge Router Facet

The Bridge Router Facet (BridgeRouterFacet.sol) acts as a bridge between DittoETH and external liquidity pools, enabling users to seamlessly deposit and withdraw their assets.

Key Functions and Logic:
  • deposit and depositEth: These functions handle the deposit of liquidity tokens (LST) and ETH, respectively. Under the hood, they interact with external bridge contracts, such as Rocket Pool and Lido, to facilitate the conversion of assets into their staked forms. The logic ensures that the correct bridge contract is called based on the type of asset being deposited.

  • withdraw and withdrawEth: Similarly, these functions manage the withdrawal process, converting staked liquidity tokens back into their base assets (ETH or dETH). The logic includes fee calculations to ensure that users receive the appropriate amount of assets upon withdrawal, considering any premiums or discounts associated with the staked tokens.

Liquidity Staking Tokens (LST) Mechanism

The LST mechanism enables users to stake their ETH in various liquidity protocols and use the resulting staked tokens as collateral within the DittoETH ecosystem.

Key Components and Logic:
  • Bridge Contracts Interaction: The LST mechanism heavily relies on external bridge contracts, which represent liquidity protocols like Rocket Pool and Lido. These contracts handle the conversion between ETH and staked tokens, ensuring interoperability with DittoETH.

  • Credit Mechanism: A critical aspect of the LST mechanism is the credit system, which ensures that users can only withdraw assets proportional to their deposits. This mechanism prevents users from exploiting arbitrage opportunities that may arise due to fluctuations in the value of staked tokens.

  • Fee Assessment: When users withdraw their assets, the protocol calculates any applicable fees based on the current value of the staked tokens relative to their base assets. This ensures that users are compensated fairly for their deposits and discourages manipulation of the system.

Conclusion

The Bridge Router and LST mechanism form the backbone of DittoETH's liquidity management infrastructure, enabling users to interact seamlessly with external liquidity protocols while maintaining fairness and efficiency. By leveraging external bridge contracts and implementing robust logic for deposit, withdrawal, credit allocation, and fee assessment, DittoETH ensures a secure and transparent experience for users participating in staking and short-selling activities within the protocol.

mechanism-1.png

Shorting Mechanism

The mechanism central to the DittoETH project involves the Shorting mechanism, which is intricately woven into the protocol's fabric through the ShortOrdersFacet contract. This system empowers users to take short positions on assets within the DittoETH ecosystem, effectively capitalizing on price declines or hedging against potential downside risks.

  1. Short Order Creation: Users initiate short positions by invoking functions within the ShortOrdersFacet contract, specifying crucial parameters such as the target asset, collateral amount, and other relevant details. These actions trigger the creation of short orders within the protocol's order book.

  2. Order Matching Algorithm: The ShortOrdersFacet contract incorporates a sophisticated matching algorithm responsible for pairing short orders with compatible long positions. This process ensures liquidity within the ecosystem, facilitating seamless shorting transactions by connecting users with opposing trading interests.

  3. Collateral Management: Upon initiating a short position, users are required to deposit collateral, which is held securely in escrow by the protocol. This collateral serves as a protective buffer against potential losses incurred if the short position moves unfavorably, safeguarding the financial integrity of the system.

  4. Liquidation Mechanism: In instances where the price movement of the shorted asset renders the position undercollateralized, the protocol may trigger a liquidation event. During liquidation, the system automatically sells a portion of the collateral to cover the outstanding debt, thereby preventing insolvency and maintaining overall system stability.

  5. Risk Mitigation Strategies: The ShortOrdersFacet contract implements robust risk management protocols to mitigate inherent risks associated with shorting activities. These strategies may include imposing minimum collateral requirements, dynamically adjusting collateral ratios based on market conditions, and implementing other safeguards to protect both users and the protocol from excessive risk exposure.

  6. Yield Generation Opportunities: Participants in the shorting mechanism may also benefit from yield generation opportunities on their collateral holdings. Similar to liquidity providers in other segments of the ecosystem, users engaging in shorting activities may earn yields on their deposited collateral, incentivizing participation and enhancing overall protocol liquidity.

By integrating the Shorting mechanism into its framework, DittoETH empowers users with advanced trading capabilities, risk management tools, and yield-generating opportunities, thereby fostering a vibrant and dynamic ecosystem conducive to both speculative trading and prudent risk management strategies.

shorting-mechannism.png

Redemption Mechanism

Redemption Mechanism Technical Overview:

  1. Redemption Proposal Submission:

    • Users interact with the DittoETH protocol through smart contracts or a user interface to initiate redemption proposals.
    • The redemption proposal includes details such as the amount of DittoUSD tokens to be redeemed and any additional parameters required by the protocol.
  2. Proposal Verification and Validation:

    • Upon submission, the redemption proposal undergoes verification and validation checks within the DittoETH smart contracts.
    • The protocol verifies the user's account balance to ensure they have sufficient DittoUSD tokens available for redemption.
    • Additional validation checks may include verifying the user's identity, ensuring compliance with regulatory requirements, and assessing the proposal's adherence to protocol parameters.
  3. Execution of Redemption:

    • Once the redemption proposal is validated, it is submitted to the redemption execution mechanism for processing.
    • The execution mechanism interacts with relevant smart contracts and external systems to initiate the redemption process.
    • The DittoETH protocol executes the redemption by burning the specified amount of DittoUSD tokens from the user's account and transferring the corresponding amount of ETH to the user's designated wallet address.
  4. Dynamic Redemption Rate Adjustment:

    • The redemption rate, which determines the exchange ratio between DittoUSD tokens and ETH, may be adjusted dynamically based on various factors.
    • These factors may include market conditions, liquidity levels, volatility metrics, governance decisions, and protocol parameters.
    • The protocol's governance mechanism or automated algorithms may periodically update the redemption rate to maintain stability and align with the protocol's objectives.
  5. Event Logging and Audit Trails:

    • Throughout the redemption process, relevant events and transactions are logged on the Ethereum blockchain for transparency and auditability.
    • These event logs include details such as the redemption proposal ID, user's wallet address, redeemed token amount, ETH transferred, timestamp, and any other pertinent information.
    • Audit trails ensure that the redemption mechanism operates securely and in accordance with the protocol's rules and regulations.
  6. Stability and Liquidity Maintenance:

    • The Redemption Mechanism plays a crucial role in maintaining stability and liquidity within the DittoETH ecosystem.
    • By offering users a reliable method to convert DittoUSD tokens to ETH, the mechanism enhances liquidity, supports price stability, and fosters confidence in the protocol.
    • Efficient execution of redemption requests contributes to a healthy ecosystem by facilitating seamless asset conversion and mitigating market imbalances.

3.png

Potential Risks

The protocol's heavy reliance on oracles for price determination, notably evident in the LibOracle.sol and UniswapOracleLibrary.sol contracts, introduces significant technical risks that could be exploited by malicious actors. One potential risk is the susceptibility to Oracle Manipulation Attacks, whereby attackers could attempt to manipulate the price data provided by the Chainlink oracle, as well as Uniswap oracle, to deceive the protocol into executing trades or triggering liquidations at unfavorable rates. For instance, vulnerabilities within the functions responsible for interacting with the oracles, particularly in LibOracle.sol, could be exploited to feed inaccurate price data to the protocol, leading to erroneous trade executions or liquidations. Additionally, any flaws in the TWAP calculation process within UniswapOracleLibrary.sol could be leveraged by attackers to manipulate prices and deceive the protocol. Moreover, the dependency on oracle updates introduces the risk of Front-Running Opportunities, where attackers could exploit delays in oracle updates to anticipate price movements and gain an unfair advantage in executing trades. This risk is particularly relevant in contracts such as LibOracle.sol and UniswapOracleLibrary.sol, where price data is fetched and utilized for critical protocol operations. Furthermore, any delays or inconsistencies in the oracle updates, as handled within these contracts, could be exploited by attackers to manipulate prices or execute trades at favorable rates before the oracle updates reflect the true market conditions. Therefore, ensuring the robustness and security of the oracle systems, as well as implementing stringent validation mechanisms within the codebase, is imperative to mitigate these technical risks effectively.

Time spent:

17 hours

#0 - c4-pre-sort

2024-04-07T20:36:03Z

raymondfam marked the issue as sufficient quality report

#1 - c4-judge

2024-04-17T07:00:50Z

hansfriese marked the issue as grade-b

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