AMM Blueprint

The AMM Blueprint provides developers with a modular template for building AMM-based applications. It enables developers to implement custom market-making algorithms or use two built-in algorithms: the UniswapV2 Curve and the Constant Price Algorithm. Below is a detailed analysis of its code logic and features.

Core Features

1. Pool Management

  • Supports adding new pools and setting liquidity for them.

  • Each pool can use different market-making algorithms (e.g., UniswapV2, BigOrder).

  • The state of each pool is maintained independently within FFP.Pools.

2. Order Operations

  • Allows users to execute trades via pools (e.g., swap transactions).

  • Validates the input and output of orders.

  • Creates Notes (tickets) that match pool prices to facilitate orders.

3. Settlement Operations

  • Executes settlement requests from the FFP settlement center to finalize Notes.

  • Maintains the settlement status of orders (e.g., completed, expired, rejected).

  • Automatically handles expired or completed Notes.

4. Flexible Market-Making Strategies

  • Uses the AlgoToPool mapping table to assign different market-making algorithms to pools.

  • Developers can implement new market-making algorithms within this framework.

Detailed Code Analysis

1. Pool Management

Creating a Pool

chevron-rightcodehashtag

Logic:

  • Check if a pool with the same configuration already exists.

  • Specify the market-making algorithm to create the pool.

  • Add the pool to FFP.Pools.

Setting Pool Liquidity

chevron-rightcodehashtag

Logic:

  • Locate the specified pool and call its updateLiquidity method.

  • Notify the results after updating.

2. Order Operations

Creating Orders

chevron-rightcodehashtag

Logic:

  • Validate input parameters and the current pool state. The pool’s Executing status acts as a global lock to ensure Notes are settled sequentially.

  • Call pool.getAmountOut to calculate the trade’s output.

  • Use the FFP protocol to create a Note and store it in FFP.Notes.

Order Settlement Execution

chevron-rightcodehashtag

Triggered by the FFP settlement center for Note settlement operations.

Logic:

  • Validate the Note and pool state.

  • Use validateAmountOut to verify if the Note matches the AMM market-making price.

  • Transfer the assets specified in the Note to the FFP settlement center for settlement.

Settlement Completion Notification

chevron-rightcodehashtag

Listens for asset transfers or refunds (in case of settlement failure) from the FFP settlement center.

Logic:

  • Check if the Note exists.

  • Ensure the related pool exists.

  • For successful settlements, update the pool’s market-making liquidity.

  • Update relevant variables.

3. Query Interfaces

Query Pools: Returns information about all created pools.

chevron-rightcodehashtag

Query Notes: Retrieves a specific Note based on its MakeTx.

chevron-rightcodehashtag

Query Prices: Provides real-time exchange price information.

chevron-rightcodehashtag

Complete Code

chevron-rightamm.luahashtag
chevron-rightutils.luahashtag
chevron-right./algo/bigorder.luahashtag
chevron-right./algo/uniswapv2.luahashtag

Before starting development, copy the above code into your development environment as per the specified paths.

Summary

The AMM Blueprint uses a flexible architectural design to enable developers to easily implement and extend various AMM functionalities. It ensures high efficiency in transaction handling and liquidity management, making it a robust solution for building custom AMM applications.

Last updated