Token Pairs and Pricing
Auriswap, much like Uniswap V2, allows users to create liquidity pools for any two tokens, enabling decentralized swaps across various token pairs. The pricing of token pairs in Auriswap is dynamically determined by the ratio of token reserves within these liquidity pools, ensuring that prices shift based on market demand and supply.
Auriswap's architecture supports a wide range of token pairs on the Vanarchain blockchain, including ERC-20-like tokens as well as native tokens. One of the key innovations in Auriswap’s pricing model is the use of time-weighted average price (TWAP) oracles, which provide secure and accurate price information, resistant to manipulation.
Token Pairs and Their Creation
In Auriswap, a new token pair is created through the Factory Contract, which generates a Pair Contract for any two ERC-20-compatible tokens. These token pairs are crucial for enabling liquidity pools and allowing users to trade between the two assets.
Deterministic Pair Addresses
Auriswap uses the CREATE2 opcode to create deterministic pair addresses. This approach ensures that each token pair has a unique, predictable contract address, calculated off-chain based on the token pair’s addresses:
pairAddress = keccak256( CREATE2_Salt, Token_A_Address, Token_B_Address );
This ensures that every pair contract address is predictable without needing to query the blockchain, which improves efficiency and reduces potential issues with pair duplication.
Constant Product Pricing Formula
The price between two tokens in a pair is determined by the constant product formula, which is represented as:
x * y = k
where:
x
: Reserve of token Ay
: Reserve of token Bk
: A constant that maintains the pool's balance
This formula ensures that after every swap, the product of the token reserves remains constant. As users trade tokens, the ratio of reserves changes, dynamically adjusting the price based on market activity. This creates a price curve, where larger trades result in more significant price shifts (slippage).
Price Oracle and Manipulation Resistance
One of the challenges in decentralized finance (DeFi) is ensuring secure and accurate price discovery. A core innovation in Auriswap, inherited from Uniswap V2, is the integration of a time-weighted average price (TWAP) oracle, which helps mitigate the risks of price manipulation.
Spot Price vs. Oracle Price
The spot price for a token pair at any given time t is determined by the ratio of the reserves:
Where:
While the spot price is accurate at the current moment, it can be manipulated by front-running or sandwich attacks during the execution of a trade. To prevent this, Auriswap’s TWAP oracle takes the cumulative price over a given period, providing a more stable and manipulation-resistant pricing mechanism.
TWAP Oracle Mechanics
The time-weighted average price (TWAP) is calculated by accumulating the prices at the beginning of each block and weighting them by the time elapsed between updates. This ensures that the price reflects a historical average, reducing the impact of short-term price fluctuations or manipulation within a single block.
Formula for Accumulating Prices:
At each block i, the price accumulator is updated based on the spot price pip_ipi and the time elapsed since the last update. The accumulator value at time t is:
TWAP Calculation Between Two Time Points:
To compute the time-weighted average price between two timestamps t1t_1t1 and t2t_2t2, the formula is:
Where:
By using the TWAP, external contracts interacting with Auriswap can retrieve reliable price data for various use cases such as lending, borrowing, oracles, and derivatives, ensuring that they are less susceptible to manipulation.
Why TWAP is Resistant to Manipulation:
TWAP’s resistance to manipulation comes from the fact that it takes into account the average price over a period of time. This makes it significantly harder for attackers to manipulate prices at the end of a block because the entire time interval is weighted. Even if an attacker controls a block, the next block’s average price will mitigate the impact.
Additionally, the system tracks both the price of Token A in terms of Token B and Token B in terms of Token A. This is important because the harmonic mean of prices is not simply the reciprocal of the arithmetic mean. By tracking both directions, Auriswap provides reliable pricing for any pair configuration.
Handling Impermanent Loss in Token Pricing
When liquidity providers (LPs) contribute assets to a pool, they are exposed to impermanent loss, which occurs when the price of one or both of the pooled tokens changes relative to when the liquidity was first deposited.
The price of the tokens in the pool fluctuates based on trades, and LPs face a risk that their final withdrawal value could be lower than if they had simply held the tokens outside the pool. However, this is typically mitigated by the 0.30% trading fee that is earned from every trade, compensating for any impermanent loss over time.
Cross-Pair Arbitrage and Routing
Auriswap supports multi-hop routing through its Router Contract, allowing trades to be executed across multiple pools to achieve the most efficient price. Arbitrageurs are encouraged to keep the prices in sync across different pools by executing trades when price discrepancies arise between pairs, thereby keeping the market efficient.
Example of Price Discovery:
Suppose a pool contains 1000 units of Token A and 5000 units of Token B. The spot price of Token A relative to Token B would be:
Now, a trade occurs where a user swaps 100 units of Token A for Token B. After the trade, the new reserves are recalculated based on the constant product formula:
Given that the product remains constant, the new price will adjust according to the updated reserves, and the spot price for the next trade will reflect the new token ratio.
Last updated