Skip to content

con_pairs Smart Contract Reference

The con_pairs contract is the core of the SNAKexchange protocol. It serves as both the factory for creating new liquidity pools and the operational hub for all existing pairs. It manages reserves, liquidity minting/burning, and executes the core logic of swaps.

Exported Methods

Below is a detailed description of the exported methods available in the con_pairs contract.

createPair(tokenA, tokenB)

Creates a new liquidity pool for a pair of tokens. This method is called by the con_dex_v2 router when liquidity is added to a non-existent pair.

  • tokenA: The contract address of the first token.

  • tokenB: The contract address of the second token.

  • Note: For this function to succeed, tokenA must be lexicographically smaller than tokenB, and a pair for these two tokens must not already exist.

pairFor(tokenA, tokenB)

Returns the unique numerical ID of the pair for tokenA and tokenB.

  • tokenA: The contract address of the first token.

  • tokenB: The contract address of the second token.

  • Note: The order of tokenA and tokenB does not matter; the function will handle it internally.

liqTransfer(pair, amount, to)

Transfers Liquidity Pool (LP) tokens from the caller to a specified address.

  • pair: The ID of the liquidity pool.

  • amount: The quantity of LP tokens to transfer.

  • to: The recipient's address.

liqApprove(pair, amount, to)

Approves a to address to spend a specific amount of the caller's LP tokens for a given pair.

  • pair: The ID of the liquidity pool.

  • amount: The quantity of LP tokens to approve.

  • to: The address being granted the allowance.

liqTransfer_from(pair, amount, to, main_account)

Transfers LP tokens on behalf of a main_account, using an allowance previously set by liqApprove.

  • pair: The ID of the liquidity pool.

  • amount: The quantity of LP tokens to transfer.

  • to: The recipient's address.

  • main_account: The address that owns the LP tokens and approved the caller to spend them.

sync2(pair)

Synchronizes the pair's internal balance records with the actual token balances held by the contract. This function is called after tokens have been transferred into the contract (e.g., before a mint or swap) to ensure the internal accounting is up-to-date.

  • pair: The ID of the pair to synchronize.

getReserves(pair)

Returns the reserves of a pair, which are the balances used for pricing calculations.

  • pair: The ID of the liquidity pool.

getSurplus(pair)

Returns the difference between the actual token balances held by the contract for a pair and its recorded reserves. This is useful for determining how many tokens have been sent to the contract before a mint or swap event.

  • pair: The ID of the liquidity pool.

mint(pair, to)

Mints new LP tokens. This function is called after liquidity has been added to a pair. It calculates the amount of LP tokens to create based on the amount of underlying tokens deposited.

  • pair: The ID of the pair to mint liquidity for.

  • to: The address that will receive the newly minted LP tokens.

burn(pair, to)

Burns LP tokens and sends the corresponding underlying tokens to a recipient. This function is called when a user removes their liquidity from a pool.

  • pair: The ID of the pair to burn liquidity from.

  • to: The address that will receive the withdrawn underlying tokens.

swap(pair, amount0Out, amount1Out, to)

The core swap function. It sends output tokens to a recipient (to) and then checks that the constant product formula (x * y = k) is satisfied after accounting for the tokens received.

  • pair: The ID of the pair executing the swap.

  • amount0Out: The amount of token0 to send out.

  • amount1Out: The amount of token1 to send out.

  • to: The address that will receive the output tokens.

swapToPair(pair, amount0Out, amount1Out, to)

A variant of the swap function used for multi-hop trades. Instead of sending the output tokens to a user's wallet, it sends them directly to another pair contract within con_pairs, which will then be the next step in the swap path.

  • pair: The ID of the pair executing the swap.

  • amount0Out: The amount of token0 to send out.

  • amount1Out: The amount of token1 to send out.

  • to: The numerical ID of the next pair in the swap path.