This project implements a Bitcoin Liquidity Protocol with advanced features such as dynamic liquidity pools, governance, yield farming, and flash loans. Built on Stacks, the protocol leverages smart contracts to facilitate decentralized exchanges (DEX), automated liquidity provision, and yield optimization.
- Liquidity Pools: Automated liquidity provision using two token pairs. Dynamic pricing based on reserve balances.
- Governance: Token holders can vote and stake to influence protocol changes.
- Yield Farming: Reward liquidity providers for staking their tokens in specific pools.
- Flash Loans: Borrow tokens from liquidity pools without upfront collateral as long as the loan is repaid within the same transaction.
- Multi-hop Swaps: Perform multiple token swaps across several pools in one transaction.
Defines the standard functions for handling fungible tokens (FT):
transfer
: Transfer tokens between accounts.get-balance
: Get the balance of an account.get-total-supply
: Retrieve the total token supply.
(define-trait ft-trait
(
(transfer (uint principal principal (optional (buff 34))) (response bool uint))
(get-balance (principal) (response uint uint))
(get-total-supply () (response uint uint))
)
)
Predefined error codes for handling common errors in the protocol:
(define-constant ERR-NOT-AUTHORIZED (err u1000))
(define-constant ERR-INSUFFICIENT-BALANCE (err u1001))
...
The protocol includes adjustable parameters for fees, liquidity, and price impact management.
(define-constant FEE-DENOMINATOR u10000) ;; Basis points for fees
(define-constant MAX-PRICE-IMPACT u200) ;; 2% max price impact
(define-constant FLASH-LOAN-FEE u10) ;; 0.1% flash loan fee
- Pools: Contains token reserves, fee rates, and pricing information for each liquidity pool.
- Liquidity Providers: Stores information about liquidity stakers, their shares, and unclaimed fees.
- Governance Stakes: Manages staked governance tokens and delegation power.
- Flash Loans: Tracks the status and repayment of flash loans.
- Yield Farms: Contains information about yield farming, including reward distribution and staked amounts.
Key internal logic for the protocol:
- Price Impact Check: Ensures that large swaps do not exceed the max price impact.
- Liquidity Shares Calculation: Determines the number of shares to mint for liquidity providers based on the current pool reserves.
- Farm Rewards Calculation: Distributes rewards to stakers based on their proportional stake in the pool.
(define-public (create-pool (token-x <ft-trait>) (token-y <ft-trait>) (initial-x uint) (initial-y uint))
Creates a new liquidity pool between two fungible tokens.
(define-public (add-liquidity (pool-id uint) (token-x <ft-trait>) (token-y <ft-trait>) (amount-x uint) (amount-y uint) (min-shares uint))
Allows users to deposit both tokens to the pool and mint liquidity shares.
(define-public (swap-exact-x-for-y (pool-id uint) (token-x <ft-trait>) (token-y <ft-trait>) (amount-x uint) (min-y uint))
Executes a swap of token X
for token Y
, ensuring minimum output is received.
(define-public (flash-swap (pool-id uint) (token-x <ft-trait>) (token-y <ft-trait>) (amount-x uint) (callback-contract principal))
Facilitates flash loans, allowing borrowers to take out a loan and execute a callback contract to handle the loan repayment.
- Staking: Users can stake governance tokens to earn voting power.
- Setting Governance Token: The protocol owner can set the token used for governance.
(define-public (stake-governance (token <ft-trait>) (amount uint) (lock-blocks uint))
-
Clone the repository:
git clone https://github.com/adeshola-code/bitcoin-liquidity-protocol.git
-
Deploy the contract using the Stacks CLI:
stacks-cli deploy ./contracts/bitquid-amm.clar
-
Interact with the contract using the Stacks API to create pools, add liquidity, and perform swaps.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
This protocol is in active development. Use at your own risk and perform adequate security reviews before using in production.