CURRENTLY IN TESTNET — Should be updated when in mainnet

A secure and efficient token swapping mechanism needs to check for various conditions such as liquidity, slippage tolerance, and reserve availability.

The swap interaction consists of various processes. Let’s take a closer inspection at the mod.swap function within swap.lua which is responsible for swapping.

Pool Validation

The pool ID must mach the ID of the current process:

-- validate pool id (must be the same as this process)
if not pool.refundassert(
    message.Tags.Pool == ao.id,
    "Invalid pool id provided"
  ) then return end

Retrieve Pair

In order to retrieve the specific pair of tokens being traded from the pool:

local pair = pool.getPair()

Transfer Validation

The ID is validated as follows to check whether it is a valid Arweave address:

  -- validate transfer id
  local transferIDValid = arweave.Address:assert(transferID, nil, true)

  -- the transfer is not available here
  if not pool.refundassert(
    transferIDValid,
    "Invalid transfer id: " .. transferID
  ) then return end

We then validate the incoming token transfer based on its ID and sender. Here’s a quick refresher on error handling.

  -- validate if the provided transfer was received
  if not pool.refundassert(
    transfer ~= nil,
    "Could not find provided transfer"
  ) then return end
  ---@cast transfer TransferReceipt

  -- validate provided transfer's sender
  -- CHANGE THIS AS WELL
  if not pool.refundassert(
    transfer.sender == message.From,
    "Transfer owner is not the caller"
  ) then return end
  1. A transfer with the provided ID needs to be found.
  2. The owner of the transfer or “caller” should be calling the swap function.