SwapFacility.sol

Overview

This Solidity smart contract provides a liquidity-swapping facility. It offers two main services: Swapping an underlying token to a specific token, and vice versa. Only whitelisted users can interact with these services.

The contract has logic for price oracle usage for both underlying and specific tokens. It also includes safeguards against stale Oracle data and extreme price situations.

Variables

  • underlyingToken: The address of the underlying token used in swap operations.

  • billyToken: The address of the Billy token used in swap operations.

  • underlyingTokenOracle: The address of the price oracle for the underlying token.

  • billyTokenOracle: The address of the price oracle for the Billy token.

  • underlyingScale: The scaling factor for the underlying token, calculated from the oracle and token decimals.

  • billyScale: The scaling factor for the Billy token, calculated from the oracle and token decimals.

  • MIN_STABLE_VALUE: The minimum stable value of the asset, denominated in the oracle's decimals.

  • MAX_BILLY_VALUE: The maximum fair value of the billy asset, denominated in the oracle's decimals.

  • whitelist: The contract that maintains the whitelist of addresses allowed to perform swaps.

  • spread: The spread applied to swaps in basis points.

  • pool: The address of the pool contract.

  • _stage: An internal state tracker for swap stages. There are five stages, each represented by a number from 0 to 4.

  • _swapAmount: An internal variable storing the total amount of tokens to be swapped in the current operation.

  • _totalAmount: An internal variable storing the total amount of out tokens produced from the swap operation.

Modifiers

  • onlyWhitelisted: This modifier ensures that only addresses present on the whitelist can perform certain operations.

Functions

  • constructor: Initializes the SwapFacility contract with the underlying token, Billy token, their oracles, a whitelist contract, the spread, pool, minimum stable value, and maximum billy value.

  • swap: This function is the public interface for performing a swap. It ensures that the swap is being performed with the correct tokens and that the sender is authorized to perform the operation. It also manages the internal swap stages and calls the internal _swap function to execute the swap.

  • _swap: This internal function executes the swap operation. It transfers the input tokens from the user to the contract, calculates the amount of output tokens based on the current Oracle prices, and transfers them from the contract to the user. It emits a Swap event upon completion.

Events

  • Swap: Emitted when a successful swap operation occurs. It includes the inToken and outToken addresses, inAmount, outAmount, and the user who performed the operation.

Errors

  • InvalidToken: Emitted when an invalid token is used in a swap operation.

  • NotPool: Emitted when the message sender is not the pool.

  • NotWhitelisted: Emitted when an unauthorized address attempts to perform a swap operation.

  • OracleAnswerNegative: Emitted when the oracle returns a negative price.

  • OracleStale: Emitted when the oracle price is older than the stale threshold.

  • InvalidSpread: Emitted when the spread parameter is greater than or equal to 100%.

  • ExtremePrice: Emitted when an extreme price situation occurs.

Last updated