Swapping
Swapping is executed with two assets. Due to the computational complexity of the TWMM swap invariant (the formula defining the precise relationship between the input and output asset amounts), Arcanum handles swaps differently from traditional AMMs. Instead of calculating an output amount for a given input, the protocol "checks" a user-specified pair of amounts.
This means that when a user wants to execute a swap, they must provide both the exact input amount and the exact expected output amount. After validating these amounts against the swap invariant, the Arcanum pool will transfer the exact output amount to the user while charging the exact input amount.
In practice, this approach significantly reduces the on-chain computational load, which in turn lowers gas costs and simplifies the smart contract's complexity, making it more secure and trustworthy.

In the event of a rapid change in an asset's price or its deviation, a swap submitted with specific input and output amounts may no longer satisfy the invariant. If this occurs, the transaction will be reverted.

Rapidly changing market conditions can create situations with high price fluctuations. Traditional on-chain pools typically manage this risk through on-the-fly calculation of an output amount for a given input, combined with user-defined slippage tolerance.
Arcanum's pools provide an alternative approach: instead of providing a single input/output pair, a user can submit a range of possible values for either the input amount or the output amount. The smart contract will then check each corresponding pair against the invariant and execute the most favorable valid option for the user.
This range can be specified by providing a step value (for the input or output amount) and the total number of steps to check.

Swap execution flow
The swap execution flow requires a user to first determine the input and output amounts (or a range for one of them) for the desired token swap. These amounts, along with any required fresh data for pull-based oracles, are then sent to the smart contract.
The contract then checks the validity of the provided amounts against the swap invariant. This check relies on the pool's oracle prices. If the pool uses pull-based oracles, the contract will also validate that the provided oracle data is recent; otherwise, the transaction will be reverted.
If all conditions are met and a valid input/output pair is found within the specified range, the swap will be executed. Otherwise, the transaction will be reverted.

Swap estimation
Swap estimation can be performed using several methods:
On-chain view methods, potentially called via
delegatecall
.Backend computation, which involves solving the swap invariant for either the input or output amount.
Approximation, which involves using oracle price values and attempting to brute-force an acceptable estimate.
Each of these approaches involves its own pros and cons. It is highly dependent on the trader's infrastructure and strategy to decide which method to choose.
Liquidity capacity
There are several scenarios where a trade cannot be executed for liquidity-related reasons:
If the requested output amount of an asset is greater than the pool's total reserve of that asset.
When an asset's target weight is set to 0. In this case, its reserve in the pool can only be decreased (i.e., it can only be an output asset).
If the trade would cause the final absolute deviation of an asset to exceed its configured Deviation Limit (as described in the Fees section).
Last updated