StackSwaps is a decentralized Automated Market Maker (AMM) smart contract built for the Stacks blockchain. It enables users to create liquidity pools, provide liquidity, perform token swaps, and participate in yield farming. The contract implements a Constant Product Market Maker formula (x * y = k) similar to Uniswap v2's core mechanics.
-
Liquidity Pool Management
- Create new liquidity pools
- Add liquidity to existing pools
- Remove liquidity from pools
- Automatic price discovery based on token reserves
-
Token Swapping
- Swap between any two supported tokens
- Constant product formula with 0.3% swap fee
- Automatic price impact calculation
-
Yield Farming
- Reward distribution for liquidity providers
- Configurable reward rates
- Minimum liquidity requirements for rewards
-
Governance
- Token allowlist management
- Adjustable reward rates
- Contract owner privileges
REWARD-RATE-PER-BLOCK: u10
MIN-LIQUIDITY-FOR-REWARDS: u100
MAX-TOKENS-PER-POOL: u2
MAX-REWARD-RATE: u1000000
MAX-UINT: u340282366920938463463374607431768211455
ERR-INSUFFICIENT-FUNDS (u1)
: Insufficient balance for operationERR-INVALID-AMOUNT (u2)
: Invalid amount specifiedERR-POOL-NOT-EXISTS (u3)
: Liquidity pool doesn't existERR-UNAUTHORIZED (u4)
: Unauthorized accessERR-TRANSFER-FAILED (u5)
: Token transfer failedERR-INVALID-TOKEN (u6)
: Invalid token addressERR-INVALID-PAIR (u7)
: Invalid token pairERR-ZERO-AMOUNT (u8)
: Zero amount specifiedERR-MAX-AMOUNT-EXCEEDED (u9)
: Maximum amount exceededERR-SAME-TOKEN (u10)
: Same token addresses provided
(define-public (create-pool (token1 <ft-trait>) (token2 <ft-trait>) (initial-amount1 uint) (initial-amount2 uint))
Creates a new liquidity pool with initial liquidity.
(define-public (add-liquidity (token1 <ft-trait>) (token2 <ft-trait>) (amount1 uint) (amount2 uint))
Adds liquidity to an existing pool.
(define-public (remove-liquidity (token1 <ft-trait>) (token2 <ft-trait>) (shares-to-remove uint))
Removes liquidity from a pool.
(define-public (swap-tokens (token-in <ft-trait>) (token-out <ft-trait>) (amount-in uint))
Executes a token swap using the AMM formula.
(define-public (claim-yield-rewards (token1 <ft-trait>) (token2 <ft-trait>))
Claims accumulated yield farming rewards.
(define-public (add-allowed-token (token principal))
Adds a token to the allowlist (owner only).
(define-public (set-reward-rate (new-rate uint))
Updates the yield farming reward rate (owner only).
(contract-call? .stackswaps-amm create-pool token-a token-b u1000000 u1000000)
(contract-call? .stackswaps-amm add-liquidity token-a token-b u100000 u100000)
(contract-call? .stackswaps-amm swap-tokens token-a token-b u10000)
-
Validation Checks
- All input amounts are validated
- Token pairs are verified against allowlist
- Overflow protection implemented
-
Access Control
- Owner-only functions for critical operations
- Protected governance functions
-
Safety Mechanisms
- Maximum limits on various parameters
- Minimum liquidity requirements
- Constant product formula maintains price stability
- Stacks blockchain environment
- Fungible tokens implementing the
ft-trait
- Contract owner address
- Fixed 0.3% swap fee
- Two tokens per pool maximum
- No flash loan protection
- Basic yield farming mechanism
To contribute to StackSwaps:
- Fork the repository
- Create a feature branch
- Submit a pull request with detailed description
- Ensure all tests pass
- Follow the coding style guidelines
This project is licensed under the MIT License. See the LICENSE file for details.