Code Monkey home page Code Monkey logo

decentralized-order-protocol's Introduction

Contract Analysis: ConditionalOrder & MockChainlinkAggregator

Overview

The provided code includes two Solidity contracts:

  1. ConditionalOrder: Enables users to place orders based on certain conditions. If these conditions are met, the orders are executed.
  2. MockChainlinkAggregator: A mock version of Chainlink's AggregatorV3Interface. It's primarily used for testing to simulate Chainlink price feeds.

ConditionalOrder Contract

Design Decisions:

  1. Mixins: This contract incorporates OpenZeppelin libraries (Pausable, Ownable, and ReentrancyGuard) which offer common functionalities like access control, pausability, and reentrancy attack protection.
  2. Order Logic: It supports users setting orders based on conditions. Hence, a user can indicate a combination of conditions; once these are met, the order gets executed.
  3. Price Feed Integration: The contract uses Chainlink's oracle service for fetching real-world data, especially price information. A mock version is also available for testing.

Architecture:

Data Structures:

  • OrderType: Enumerates if the order is a Buy or Sell.
  • ConditionType: Enumerates the type of condition - Time-Based, Event-Based, or Price-Based.
  • Logic: Enum for checking multiple conditions using AND or OR logic.
  • Condition: A struct containing a condition type and its value.
  • Order: A struct that describes an order with all its associated data.

State Variables:

  • priceFeed: An instance of Chainlink price feed.
  • mockFeed: An instance of the mock price feed.
  • isUsingMock: A boolean to ascertain if the contract uses real or mock data.
  • orderCount: Maintains the count of orders.
  • orders: Maps from order ID to the Order structure.

Constructor:

  • It initializes the contract in two modes: real Chainlink data or mock data for testing.

Key Functions:

  • createOrder(): Used by users to create orders.
  • getOrder(): Retrieves details of a specific order.
  • switchToMock() & switchToReal(): Enables the owner to toggle between real and mock price data.
  • getLatestPrice(): Fetches the latest price from either of the feeds.
  • executeOrder(): Executes a given order if conditions are met.
  • checkCondition(): A utility to verify if a given condition is met.
  • performTrade(): Simulates trade execution.
  • cancelOrder(): Enables users to retract their orders.

Security:

  • Access Control: The onlyOwner modifier ensures only the contract owner can toggle between real and mock data.
  • Reentrancy Protection: The nonReentrant modifier safeguards against reentrancy attacks, especially crucial during order execution.
  • Pausing Mechanism: The whenNotPaused modifier ensures functions can't be invoked when the contract is paused, offering an emergency stop mechanism.

MockChainlinkAggregator Contract

Design Decisions:

  1. Mocking Chainlink: Given the widespread use of Chainlink's oracles, testing in a local or simulation environment necessitates mock versions. This contract imitates Chainlink's behavior.

Architecture:

Data Structures:

  • price: A mock price value.
  • Other attributes to mimic the Chainlink aggregator, such as decimals, version, and description.

Key Functions:

  • decimals(), description(), version(): Overrides for Chainlink's interface.
  • latestRoundData(), getRoundData(): Simulates Chainlink's latestRoundData and getRoundData functions.
  • setPrice(): Enables updating the mock price.

Usefulness:

  • Critical for testing, it emulates Chainlink's behavior, enabling a controlled test environment without incurring real-world oracle expenses.

Final Thoughts:

The architecture is comprehensible and modular, utilizing both custom logic and trusted libraries to guarantee functionality and security. Potential enhancements could involve real-time integration with a DEX or DeFi platform to truly execute trades and refine the event-based conditions.

How to Setup

To get started with the contracts and run tests, follow the instructions below:

Compiling the Contracts

To compile all the contracts, use the command: forge build

Testing the Contracts

To test all the contracts, use the command: forge Test

Test Specific Contract

To test specific contract, use the command: forge test -vvvv --match-contract <ExampleTestName>

Example : forge test -vvvv --match-contract TimeBasedOrderTest

decentralized-order-protocol's People

Contributors

mouazzamaanalog avatar

Watchers

Mouazzama  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.