Skip to content

Conversation

@itohowo-dot
Copy link
Owner

Overview

This PR introduces the initial implementation of StackSwaps, a decentralized Automated Market Maker (AMM) smart contract for the Stacks blockchain. The contract enables liquidity pool creation, token swapping, and yield farming functionality with comprehensive safety checks and governance features.

Key Changes

  • Implemented core AMM functionality with liquidity pool management
  • Added yield farming mechanism with configurable reward rates
  • Integrated SIP-010 fungible token standard compatibility
  • Implemented governance controls for contract management
  • Added comprehensive validation and security checks
  • Created detailed documentation and examples

Implementation Details

Core Components

  1. Liquidity Pool Management

    • Map structure for storing pool data (liquidity-pools)
    • Functions for pool creation, liquidity addition/removal
    • Constant product formula implementation (x * y = k)
  2. Token Swapping

    • Implemented token swap mechanism with 0.3% fee
    • Price impact calculations
    • Reserve updates with safety checks
  3. Yield Farming

    • Reward distribution system
    • Minimum liquidity requirements
    • Configurable reward rates
    • User position tracking
  4. Governance

    • Owner-controlled token allowlist
    • Adjustable reward rates
    • Access control mechanisms

Error Handling

Added comprehensive error constants:

  • ERR-INSUFFICIENT-FUNDS
  • ERR-INVALID-AMOUNT
  • ERR-POOL-NOT-EXISTS
  • ERR-UNAUTHORIZED
  • ERR-TRANSFER-FAILED
  • ERR-INVALID-TOKEN
  • ERR-INVALID-PAIR
  • ERR-ZERO-AMOUNT
  • ERR-MAX-AMOUNT-EXCEEDED
  • ERR-SAME-TOKEN

Storage Definitions

  • liquidity-pools: Stores pool data and reserves
  • user-liquidity: Tracks user positions
  • yield-rewards: Manages pending rewards
  • allowed-tokens: Maintains allowlist of supported tokens

Testing

The implementation has been tested using Clarinet with the following scenarios:

  • Pool creation and management
  • Token swapping with various amounts
  • Liquidity provision and removal
  • Yield farming rewards calculation
  • Governance function access control

Documentation

  • Added comprehensive README
  • Included detailed function documentation
  • Provided usage examples
  • Listed security considerations and known limitations

Deployment Prerequisites

  1. Stacks blockchain environment
  2. SIP-010 compatible tokens
  3. Clarinet testing framework

Security Considerations

  • Implemented input validation for all public functions
  • Added access control for governance operations
  • Included overflow protection
  • Added token validation checks
  • Implemented minimum liquidity requirements

Next Steps

  • Community review of core mechanisms
  • Additional test coverage for edge cases
  • Security audit
  • Frontend integration
  • Documentation review

Reviewers

Please pay special attention to:

  • Constant product formula implementation
  • Security validation checks
  • Reward calculation mechanism
  • Token transfer safety

- Implemented the basic structure of the StackSwaps AMM smart contract.
- Added traits for token functionality using SIP-010 trait.
- Defined error constants for common error scenarios:
  - ERR-INSUFFICIENT-FUNDS
  - ERR-INVALID-AMOUNT
  - ERR-POOL-NOT-EXISTS
  - ERR-UNAUTHORIZED
  - ERR-TRANSFER-FAILED
- Included detailed comments for title, summary, and description of the contract.
- Defined a map `liquidity-pools` to store information about liquidity pools.
- Each pool is identified by a pair of tokens (`token1` and `token2`).
- Stored data includes `total-liquidity`, `token1-reserve`, and `token2-reserve`.
- Defined a map `user-liquidity` to track individual user positions in liquidity pools.
- Each entry is identified by a user and a pair of tokens (`token1` and `token2`).
- Stored data includes `liquidity-shares` representing the user's share in the pool.
- Defined a map `yield-rewards` to track pending yield farming rewards for users.
- Each entry is identified by a user and a token.
- Stored data includes `pending-rewards` representing the rewards yet to be claimed by the user.
- Defined `REWARD-RATE-PER-BLOCK` constant to specify the reward rate per block.
- Defined `MIN-LIQUIDITY-FOR-REWARDS` constant to specify the minimum liquidity required to earn rewards.
- Added `create-pool` function to allow users to create new liquidity pools.
- Validates initial amounts for both tokens.
- Transfers initial liquidity from the sender to the contract.
- Creates a new entry in the `liquidity-pools` map with the initial reserves.
- Assigns initial liquidity shares to the sender in the `user-liquidity` map.
- Added `add-liquidity` function to allow users to add liquidity to existing pools.
- Validates input amounts and ensures they are optimal based on pool reserves.
- Transfers the specified amounts of tokens from the sender to the contract.
- Updates the pool reserves in the `liquidity-pools` map.
- Updates the user's liquidity shares in the `user-liquidity` map.
- Added `remove-liquidity` function to allow users to remove liquidity from existing pools.
- Validates the user's liquidity shares to ensure sufficient funds.
- Calculates the proportional amounts of tokens to withdraw based on the shares being removed.
- Transfers the calculated token amounts back to the user.
- Updates the pool reserves and total liquidity in the `liquidity-pools` map.
- Updates the user's liquidity shares in the `user-liquidity` map.
- Added `swap-tokens` function to allow users to swap tokens using the AMM.
- Validates the existence of the liquidity pool for the given token pair.
- Transfers the input tokens from the user to the contract.
- Calculates the output token amount considering a 0.3% fee.
- Transfers the calculated output tokens to the user.
- Updates the pool reserves in the `liquidity-pools` map.
- Added `claim-yield-rewards` function to allow users to claim their yield farming rewards.
- Validates the user's liquidity position and ensures it meets the minimum requirement for rewards.
- Calculates the reward amount based on the user's liquidity shares and the reward rate per block.
- Updates the `yield-rewards` map with the calculated pending rewards for the user.
- Added `set-reward-rate` function to allow the contract owner to adjust the reward rate.
- Validates that the caller is the contract owner.
- Updates the `reward-rate` variable with the new rate.
- Defined `contract-owner` variable to store the principal of the contract owner.
- Initialized `contract-owner` with the principal of the transaction sender.
- Defined `reward-rate` variable to store the current reward rate.
- Initialized `reward-rate` with the `REWARD-RATE-PER-BLOCK` constant.
…s tracking

- Implemented functions for managing liquidity pools:
  - `create-pool`: Allows users to create new liquidity pools.
  - `add-liquidity`: Allows users to add liquidity to existing pools.
  - `remove-liquidity`: Allows users to remove liquidity from pools.
  - `swap-tokens`: Allows users to swap tokens using the AMM.
- Implemented user rewards tracking:
  - `claim-yield-rewards`: Allows users to claim their yield farming rewards.
- Added governance function `set-reward-rate` to adjust the reward rate, restricted to the contract owner.
- Defined owner and reward rate variables:
  - `contract-owner`: Stores the principal of the contract owner.
  - `reward-rate`: Stores the current reward rate.
- Added validation checks to handle untrusted input:
  - `validate-amount`: Ensures amounts are within valid ranges.
  - `validate-token-pair`: Ensures token pairs are valid and distinct.
  - `is-valid-token`: Checks if a token is allowed.
…n branch

- Added an overview of the StackSwaps AMM smart contract.
- Detailed the features including liquidity pool management, token swapping, yield farming, and governance.
- Provided technical specifications including constants and error codes.
- Documented core functions for pool management, trading, yield farming, and governance.
- Included usage examples for creating a pool, adding liquidity, and performing a swap.
- Outlined security considerations such as validation checks, access control, and safety mechanisms.
- Listed deployment prerequisites and known limitations.
- Added contributing guidelines and license information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants