Smart Contract Infrastructure
Last updated
Last updated
Auriswap is powered by a robust architecture that utilizes two key contracts: the Factory Contract and the Router Contract. These contracts work in harmony to create and manage liquidity pools, execute trades, and ensure security and efficiency across the platform. As a fork of Uniswap V2, Auriswap retains the core mechanics of constant-product automated market making (AMM) while introducing optimizations that leverage the Vanarchain blockchain.
The Factory Contract is responsible for deploying new Pair Contracts, which represent liquidity pools for token pairs. Each pair contract is assigned a unique, deterministic address, which simplifies off-chain computations. This is achieved using the CREATE2 opcode, allowing users to predict a pair’s address without querying the blockchain.
createPair(tokenA, tokenB)
Creates a new pair contract for the two specified tokens. This is where liquidity providers will deposit their assets, and it ensures that no duplicate pair contracts exist.
Formula:
Each pair follows the constant product formula:
x * y = k
where:
x
: Reserve of token A
y
: Reserve of token B
k
: A constant that maintains the pool's balance
getPair(tokenA, tokenB)
Returns the address of the pair contract for a given token pair. If no pair exists, the function returns a null address.
setFeeTo(address)
Configures the recipient of the protocol fees. The fees are initially set to 0 but can be turned on later to direct a 0.05% protocol fee to a specific address.
setFeeToSetter(address)
Updates the address authorized to change the protocol fee recipient, ensuring future flexibility.
The Router Contract is the primary user interface for executing trades, adding liquidity, and removing liquidity on Auriswap. It interacts with pair contracts to perform swaps and manage liquidity, abstracting the complexity from the end-user.
swapExactTokensForTokens(amountIn, amountOutMin, path, to, deadline)
Allows users to swap an exact input amount of one token for another, ensuring that the output amount is no less than the minimum specified (amountOutMin
).
Fee Adjustment Formula:
x₁ = x₀ - 0.003 * xᵢₙ
where:
x₀
: is the reserve of the input token before the swap
x₁
: is the new reserve after the swap.
The invariant condition: (x₁ - 0.003 * xᵢₙ) * (y₁ - 0.003 * yᵢₙ) ≥ x₀ * y₀ ensures that the constant product formula remains satisfied.
This formula calculates the new value of x
by subtracting 0.003 times the input value xᵢₙ from the original value x₀
.
addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin, to, deadline)
removeLiquidity(tokenA, tokenB, liquidity, amountAMin, amountBMin, to, deadline)
Removes liquidity from the pool, redeeming the user’s share of the liquidity based on the pool's reserves.
getAmountsOut(amountIn, path)
Computes the expected output for a given input token and trade path, based on the reserves in the pair contracts.
All pair contracts on Auriswap are instantiated using the CREATE2 opcode, which allows for deterministic pair addresses. This ensures that the address of any pair can be calculated off-chain based on the token pair’s addresses, without querying the blockchain.
Address Calculation:
The address of the pair contract for any two tokens A and B can be computed using:
\text{pairAddress} = \text{keccak256}(\text{CREATE2_Salt}, \text{Token\_A\_Address}, \text{Token\_B\_Address})
This deterministic creation ensures transparency and efficiency in contract deployment.
To ensure efficiency in managing liquidity and prevent overflows, Auriswap enforces a maximum reserve balance of tokens for any liquidity pool. This limitation is sufficiently large to accommodate tokens with a supply of up to 1 quadrillion units (for 18-decimal tokens).
Overflow Protection:
If the balance of a token exceeds this limit, the skim()
function can be called to withdraw any excess tokens. This ensures that trades do not fail due to overflows in the reserve balances.
Adds liquidity to a pair, allowing the user to specify desired amounts for both tokens and ensuring the liquidity added does not fall below the minimum amounts. Liquidity Share Formula: When adding liquidity, the amount of liquidity tokens minted is proportional to the geometric mean of the token amounts deposited: This ensures fairness in liquidity distribution, regardless of the initial ratio of token deposits.