Zero-Capital Arbitrage Agent
The Zero-Capital Arbitrage Agent is an extension of the Basic Blueprint. It can carry out arbitrage operations within the FFP protocol by leveraging price differences; all without without requiring upfront capital costs.
Below is an example implementation using the wAR/wUSDC trading pair.
Target Scenario
The Agent listens for exchange orders of the wAR/wUSDC trading pair in the FFP protocol:
The Agent fetches pending wAR/wUSDC orders from the FFP settlement center in real time.
It compares the price of each order with the price from the wAR/wUSDC AMM Agent.
If there is a price difference, it performs arbitrage operations:
Order Example: A user places an order to exchange 1 wAR for 20 wUSDC.
AMM Price: The current price in the AMM Agent is 1 wAR = 21 wUSDC.
Arbitrage Process:
Request the AMM Agent to create a 21 wUSDC -> 1 wAR order in FFP.
Submit the user’s order as a counter-order to the AMM-created order in the FFP settlement center.
The settlement center collects 1 wAR from the user and 21 wUSDC from the AMM Agent.
It distributes 20 wUSDC to the user and 1 wAR to the AMM Agent, leaving a surplus of 1 wUSDC.
The surplus 1 wUSDC is sent to the Arbitrage Agent as a reward, since the agent initiated the settlement.
Arbitrage Agent Code Explanation
Code Structure
arbitrage.lua: Contains all execution logic for arbitrage.
agent.lua: Extends basic.lua with arbitrage-specific methods.
utils.lua: A direct copy from the Basic Blueprint.
Core Methods Explained
1. ArbitrageExecutor
The main arbitrage scheduler that orchestrates the process:
Executes arbitrage for wAR -> wUSDC orders first.
Then processes wUSDC -> wAR orders to cover both arbitrage directions.
2. arbitrageExec
Executes arbitrage for all orders in a given direction:
Takes two asset types as input parameters to determine the arbitrage direction.
Fetches order data (Notes) from FFP in batches.
Performs arbitrage for each Note.
3. getNotesFromSettle
A dedicated function to fetch Notes from the settlement process:
Filters Notes by asset type and status.
Retrieves the necessary Notes from the FFP settlement center.
4. perNoteArbitrageExecutor
Executes arbitrage logic for a single Note:
AMM Liquidity Check: Fetches liquidity information from the AMM Agent to ensure the pool is available and unlocked.
Price Comparison:
If the AMM price is lower than the Note’s target price, arbitrage is not feasible.
If the AMM price is higher, the agent requests the AMM Agent to create a counter-order.
Example:
A Note represents 1 wAR -> 20 wUSDC.
The AMM offers 21 wUSDC -> 1 wAR.
The agent requests the AMM to create this counter-order, resulting in a price difference of 1 wUSDC.
Arbitrage Settlement:
Calls the takeOrder method (implemented in the Basic Blueprint) to initiate settlement.
After settlement is complete, the agent automatically collects the arbitrage profit (the price difference).
5. calcAmmInputPrice
This method calculates the exchange price based on the constant product formula (UniswapV2 algorithm) and fee structure:
Fetches the reserveIn, reserveOut, and Fee parameters from the AMM pool.
Extracts the input amount (AmountIn) from the Note.
Computes the exchangeable AmountOut using AMM formulas.
Compares AmountOut with the target amount in the Note.
If AmountOut exceeds the target amount, arbitrage is feasible, and the operation proceeds.
Summary
The Lossless Arbitrage Agent provides a practical example of leveraging the Basic Blueprint to implement a custom, automated arbitrage strategy within the FFP protocol. By exploiting price differences across the ecosystem, developers can create efficient and profitable agents while integrating seamlessly into the FusionFi financial activities.
Last updated
Was this helpful?