MTthoas / Dex

Dex website project
5 stars 1 forks source link

[BC] New Smart Contract Architecture (implementing access control) #68

Open Ronfflex opened 6 months ago

Ronfflex commented 6 months ago

https://docs.openzeppelin.com/contracts/5.x/access-control

  1. UserRegistry

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE, USER_ROLE.
    • Functions:
      • getRegisteredUserIds: Restricted to DEFAULT_ADMIN_ROLE, returns an array of user IDs of all registered users.
      • registerUser: Restricted to DEFAULT_ADMIN_ROLE, allows registering new users and assigning unique IDs.
      • isRegisteredUser: Checks if an address is a registered user.
      • getUserId: Returns the user ID for a given address.
  2. AssetManager (ex TokenManager)

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE, ASSET_MANAGER_ROLE.
    • Functions:
      • addAsset: Restricted to ASSET_MANAGER_ROLE, allows adding new assets to the platform.
      • removeAsset: Restricted to ASSET_MANAGER_ROLE, allows removing assets from the platform.
      • getAssetInfo: Allows querying information about a specific asset.
  3. Exchange

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE, USER_ROLE.
    • Functions:
      • swapTokens: Restricted to USER_ROLE, allows swapping tokens between users.
      • addLiquidity: Restricted to USER_ROLE, allows adding liquidity to a liquidity pool.
      • removeLiquidity: Restricted to USER_ROLE, allows removing liquidity from a liquidity pool.
  4. LiquidityPool

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE, LIQUIDITY_MANAGER_ROLE.
    • Functions:
      • createPool: Restricted to LIQUIDITY_MANAGER_ROLE, allows creating a new liquidity pool.
      • removePool: Restricted to LIQUIDITY_MANAGER_ROLE, allows removing a liquidity pool.
      • getPoolInfo: Allows querying information about a specific liquidity pool.
  5. StakingPool

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE, STAKING_MANAGER_ROLE.
    • Functions:
      • createStakingPool: Restricted to STAKING_MANAGER_ROLE, allows creating a new staking pool.
      • removeStakingPool: Restricted to STAKING_MANAGER_ROLE, allows removing a staking pool.
      • stakeTokens: Restricted to USER_ROLE, allows users to stake their tokens in a staking pool.
      • unstakeTokens: Restricted to USER_ROLE, allows users to unstake their tokens from a staking pool.
      • getStakingPoolInfo: Allows querying information about a specific staking pool.
  6. AdminControl

    • Inherits from AccessControl contract.
    • Defines roles: DEFAULT_ADMIN_ROLE.
    • Functions:
      • addAdmin: Restricted to DEFAULT_ADMIN_ROLE, allows adding new administrators.
      • removeAdmin: Restricted to DEFAULT_ADMIN_ROLE, allows removing administrators.
      • banUser: Restricted to DEFAULT_ADMIN_ROLE, allows banning a user from the platform.
      • unbanUser: Restricted to DEFAULT_ADMIN_ROLE, allows unbanning a user from the platform.
      • setFees: Restricted to DEFAULT_ADMIN_ROLE, allows setting fees for various platform operations.
      • withdrawFees: Restricted to DEFAULT_ADMIN_ROLE, allows withdrawing accumulated fees from the contract.
  7. AccessControl

    • This contract is inherited from the OpenZeppelin AccessControl contract.
    • Responsible for managing roles and permissions across the other contracts.
    • Provides functions like grantRole, revokeRole, and access control modifiers like onlyRole.

In this design, each contract inherits from the AccessControl contract and defines the necessary roles for its functionality. The AdminControl contract manages the DEFAULT_ADMIN_ROLE and provides functions for administrative tasks like adding/removing admins, banning users, and managing fees.

The other contracts define roles specific to their functionality, such as ASSET_MANAGER_ROLE, LIQUIDITY_MANAGER_ROLE, and STAKING_MANAGER_ROLE. These roles can be granted or revoked by the administrators, allowing for granular control over who can perform certain actions on the platform.

The UserRegistry contract manages user registration and assigns the USER_ROLE to registered users, allowing them to access user-specific functions like swapping tokens, adding liquidity, and staking tokens.

This design follows the principle of least privilege, where each role has the minimum required permissions to perform its intended actions. It also separates concerns by splitting functionality across multiple contracts, making the codebase more modular and easier to maintain.

Ronfflex commented 5 months ago

Changing some contracts and functions.

ArchitectureDEX