A decentralized exchange (DEX) implementation on the Stacks blockchain using Clarity smart contracts. This DEX enables users to create liquidity pools, provide liquidity, execute token swaps, and manage liquidity positions.
This smart contract implements an automated market maker (AMM) decentralized exchange that follows the constant product formula (x * y = k). It supports SIP-010 compliant fungible tokens and includes essential features like liquidity provision, token swapping, and liquidity removal with built-in security measures.
- Liquidity Pool Creation: Create new trading pairs for SIP-010 compliant tokens
- Liquidity Management: Add and remove liquidity with slippage protection
- Token Swapping: Execute token swaps with configurable slippage tolerance
- Fee System: 0.3% fee on all trades (customizable)
- Deadline Protection: Transaction deadline enforcement to prevent stale trades
- Automatic Price Discovery: Based on constant product formula
- Provider Tracking: Complete liquidity provider position tracking
- Pools Map
{
token-x: principal,
token-y: principal,
liquidity-total: uint,
balance-x: uint,
balance-y: uint,
fee-rate: uint
}
- Liquidity Providers Map
{
pool-id: { token-x: principal, token-y: principal },
provider: principal,
liquidity-provided: uint,
rewards-claimed: uint
}
-
create-pool
- Creates a new liquidity pool for token pair
- Parameters: token-x, token-y, initial-x, initial-y
- Returns: (response bool uint)
-
add-liquidity
- Adds liquidity to an existing pool
- Parameters: token-x, token-y, amount-x, amount-y, min-liquidity, deadline
- Returns: (response uint uint)
-
remove-liquidity
- Removes liquidity from a pool
- Parameters: token-x, token-y, liquidity, min-amount-x, min-amount-y, deadline
- Returns: (response (tuple (amount-x uint) (amount-y uint)) uint)
swap-exact-tokens
- Executes a token swap with exact input amount
- Parameters: token-x, token-y, amount-in, min-amount-out, deadline
- Returns: (response uint uint)
-
get-pool-details
- Retrieves pool information
- Parameters: token-x, token-y
- Returns: (optional pool-data)
-
get-provider-liquidity
- Gets provider's liquidity position
- Parameters: token-x, token-y, provider
- Returns: (optional provider-data)
The contract includes comprehensive error codes:
Code | Description |
---|---|
1000 | Not authorized |
1001 | Insufficient balance |
1002 | Invalid pair |
1003 | Invalid amount |
1004 | Pool already exists |
1005 | Pool not found |
1006 | Slippage too high |
1007 | Deadline expired |
1008 | Zero liquidity |
- Stacks blockchain environment
- SIP-010 compliant tokens
- Clarity CLI tools
;; Create a new pool
(contract-call? .dex create-pool token-x token-y u1000000 u1000000)
;; Add liquidity
(contract-call? .dex add-liquidity token-x token-y u100000 u100000 u99000 u100)
;; Perform swap
(contract-call? .dex swap-exact-tokens token-x token-y u10000 u9900 u100)
- Slippage Protection: All trading functions include minimum output parameters
- Deadline Mechanism: Prevents stale transactions
- Balance Checks: Comprehensive balance verification before operations
- Authorization: Strict ownership and access controls
- Integer Overflow Protection: Built-in Clarity protection against overflow
- Liquidity Provider Safety: Minimum liquidity thresholds and proper tracking
- Contract Language: Clarity
- Token Standard: SIP-010
- Minimum Liquidity: Dynamic based on pool size
- Fee Structure: 0.3% trading fee
- Price Calculation: Constant product formula (x * y = k)
- Slippage Protection: Configurable minimum output amounts
- Transaction Deadline: Block height-based expiration
To run the test suite:
- Clone the repository
- Install dependencies
- Run test command:
clarinet console
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.