Valantis Documentation
Valantis Website
  • Welcome to Valantis
  • Sovereign Pool
    • The Modules
      • Pool Manager
      • Liquidity Module
      • Swap Fee Module
      • Verifier Module
      • Oracle Module
      • Sovereign Vault
        • Rebase token support
      • Gauge
    • Interacting with Pools
      • Swap Parameters
      • Swap Steps
      • Multi Token Support
      • Deposit Liquidity
      • Withdraw Liquidity
      • Flash Loans
  • Hybrid Order Type (HOT)
    • Understanding HOT: A Graphical Overview
    • HOT API
      • HTTP request parameters
      • HTTP request response
      • Reasoning for Request structure
      • Solver Integration
      • Market Maker integration
    • HOT Smart Contracts
      • Interfaces
      • State variables and roles
      • AMM
      • HOT Swap
      • AMM Swap
      • Hybrid Order Type (HOT) struct parameters
      • Alternating Nonce Bitmap
      • Deposits
      • Withdrawals
      • Deployment Assumptions
    • Liquidity Manager Docs
      • Reference Oracle
      • Deposit
      • Withdraw
      • Signature
    • Swap
      • Swap Context
      • AMM Swap
      • HOT Swap
      • HOT Quote Parameters
        • Bitmap Nonce Instructions
    • Solver Docs
      • Solver Request
      • Simple HOT Swap Example
      • Partial Fill HOT Swap Example
    • Risks and Trust Assumptions
      • Roles
        • Sovereign Pool Manager
        • HOT Manager
        • HOT Signer
        • Liquidity Provider
      • Threats
        • Deposit Sandwich
        • Malicious Price Bound
        • Malicious Signer
        • Mispriced HOT Quote
  • Validly
    • Understanding Validly
    • Swap
    • Deposits and Withdrawals
    • Deployments
  • Stake Exchange (stHYPE AMM)
    • Swap
      • Instant Withdrawals (LST -> Native Token)
      • New Stake (Native Token -> LST)
    • LP Positions
      • LP Withdrawals Instant
      • LP Withdrawal Queued
    • Ratio Fee
    • Lending of Reserves
    • Smart contracts
      • STEXAMM.sol
      • StHYPEWithdrawalModule.sol
      • StexRatioSwapFeeModule.sol
      • DepositWrapper.sol
      • AaveLendingModule.sol
    • Risks and Trust Assumptions
    • Integration examples
  • Deploy and Build
  • Resources
    • Audits
    • Links
    • Get HYPE on HyperEVM
Powered by GitBook
On this page

Was this helpful?

  1. Validly

Swap

PreviousUnderstanding ValidlyNextDeposits and Withdrawals

Last updated 6 months ago

Was this helpful?

The entry-point for Validly swaps is the . The following diagram contains more information:

Below is a Solidity example of how to execute a swap against a Validly pool, from the perspective of a smart contract which calls it.

Solidity example:

// Address of Validly's Sovereign Pool to swap against
address pool = 0x123;
// Amount of input token to swap against the pool
uint256 amountIn = 123;
// Minimum amount of output token required after swap
// WARNING: Be sure to set this within safe and reasonable bounds in production!
uint256 amountOutMin = 1;

// token0 and token1 from `pool`
address token0 = ISovereignPool(pool).token0();
address token1 = ISovereignPool(pool).token1();

// Direction of the swap:
// tokenIn=token0 and tokenOut=token1
bool isZeroToOne = true;

// handle tokenIn approval to `pool`
// Assumes that OpenZeppelin's ERC20 SafeTransferLib is used
if (isZeroToOne) {
    token0.safeApprove(pool, amountIn);
} else {
    token1.safeApprove(pool, amountIn);
}

// See: https://github.com/ValantisLabs/valantis-core/blob/main/src/pools/structs/SovereignPoolStructs.sol#L36-L45
SovereignPoolSwapParams memory params;
// Populate inputs to pool's swap function
params.isSwapCallback = false;
params.isZeroToOne = isZeroToOne;
params.amountIn = amountIn;
params.amountOutMin = amountOutMin;
params.deadline = block.timestamp + 1;
params.recipient = address(this);
params.swapTokenOut = isZeroToOne ? address(token1) : address(token0);

// Executes swap against Validly's Sovereign Pool (`pool`)
(uint256 amountInUsed, uint256 amountOut) = ISovereignPool(pool).swap(params);
Sovereign Pool's swap function