swap
@example
• v1.0.0Decentralized exchange protocol with automated market maker functionality
Token Swap Protocol
A decentralized exchange (DEX) protocol implementing automated market maker (AMM) functionality for token swapping. This protocol enables users to exchange different assets through liquidity pools managed by the DEX.
Features
- Automated Market Making: Dynamic pricing based on pool state and asset ratios
- Pool State Management: Maintains liquidity pool data with pair balances
- Asset Flexibility: Supports swapping any asset types (AnyAsset)
- Fee Integration: Automatic transaction fee handling
- Change Management: Returns excess payment to buyers
How It Works
- Pool Setup: DEX maintains liquidity pools with two assets (pair_a, pair_b)
- Swap Request: Buyer specifies what they want (ask) and what they're offering (bid)
- Pool Update: Pool balances are adjusted based on the swap amounts
- Asset Exchange: Buyer receives the requested asset, pool gets the bid asset
- Change Return: Any excess payment is returned to the buyer
Transaction Structure
- Inputs: Pool UTxO from DEX, payment from Buyer
- Outputs: Updated pool state, assets to buyer
- Redeemer: SwapParams containing ask and bid values
Parameters
ask
: AnyAsset - The asset and amount the buyer wants to receivebid
: AnyAsset - The asset and amount the buyer is offering
Use Cases
- Token Trading: Exchange different cryptocurrencies or tokens
- Liquidity Provision: Enable trading pairs for various assets
- DeFi Integration: Foundation for more complex financial protocols
- Educational Examples: Learn AMM mechanics and pool management
This protocol demonstrates core DEX functionality and serves as a building block for decentralized trading systems.
swap
Published by
@example
Install
trix install example/swap
Publication datePublished 10 months ago