-
Notifications
You must be signed in to change notification settings - Fork 0
Add StackSwaps Automated Market Maker (AMM) Implementation #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itohowo-dot
wants to merge
17
commits into
main
Choose a base branch
from
feature/add-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Implementation Details
Core Components
Liquidity Pool Management
liquidity-pools)Token Swapping
Yield Farming
Governance
Error Handling
Added comprehensive error constants:
ERR-INSUFFICIENT-FUNDSERR-INVALID-AMOUNTERR-POOL-NOT-EXISTSERR-UNAUTHORIZEDERR-TRANSFER-FAILEDERR-INVALID-TOKENERR-INVALID-PAIRERR-ZERO-AMOUNTERR-MAX-AMOUNT-EXCEEDEDERR-SAME-TOKENStorage Definitions
liquidity-pools: Stores pool data and reservesuser-liquidity: Tracks user positionsyield-rewards: Manages pending rewardsallowed-tokens: Maintains allowlist of supported tokensTesting
The implementation has been tested using Clarinet with the following scenarios:
Documentation
Deployment Prerequisites
Security Considerations
Next Steps
Reviewers
Please pay special attention to: