Introduction
AirSwap trading technology is fully decentralized, powered by smart contracts that enable counterparty discovery and atomic swaps. AirSwap protocols communicate prices off-chain and settle on-chain. Peers take the form of individuals trading manually or software trading in an automated way, in which case market makers run servers that implement the following protocols.
For information on finding counter-parties, see Discovery. For protocol method specifications, see Protocols.
Orders
AirSwap orders are mutually signed instructions to perform an atomic swap. Prior to execution, both the signer (party that creates and signs the order) and sender (party that sends the order to the EVM) must have approved the swap contract to transfer the specified tokens on their behalf.
Properties
An OrderERC20 has the following properties:
nonce
uint256
Unique per signer and usually sequential.
expiry
uint256
Expiry in seconds since 1 January 1970.
signerWallet
address
Wallet that sets and signs terms.
signerToken
address
Token that the signer transfers.
signerAmount
uint256
Amount that the signer transfers.
senderToken
address
Token that the sender transfers.
senderAmount
uint256
Amount that the sender transfers.
v
uint8
v value of the ECDSA signature.
r
bytes32
r value of the ECDSA signature.
s
bytes32
s value of the ECDSA signature.
Execution
Orders are passed to the SwapERC20 contract for execution, which emits a SwapERC20 event on success. The swapLight function is more efficient, whereas the swap function provides protocol fee rebates to staked AST holders. Either function can execute a properly signed order.
RequestForQuoteERC20
AirSwap RequestForQuoteERC20 is a client-server protocol used by market makers running servers from which clients request ERC20 orders via HTTP or WebSocket. In RFQ, the server is the signer (i.e. signerAmount, signerToken) and the client is the sender (i.e. senderAmount, senderToken).
Example
To find counterparties, see Discovery. With server URLs in hand, clients may call getSignerSideOrderERC20 or getSenderSideOrderERC20 as JSON-RPC requests on servers that support RequestForQuoteERC20.
Client Request
Requests can also be made using curl or wget for debugging.
Server Response
Client Settlement
With an OrderERC20 in hand, the client sends an Ethereum transaction to the SwapERC20 contract. The swapLight function is gas efficient, whereas the swap function provides protocol fee rebates to staked AST holders. Either function can settle a correctly signed OrderERC20. A successful swap emits a SwapERC20 event.
The server or client may subscribe to a filter for a SwapERC20 event with the order nonce to track fills.
LastLookERC20
AirSwap LastLookERC20 is used by servers to stream quotes to clients. Clients periodically send signed OrderERC20s to the server, which then has the "last look" and option to send it to the EVM for settlement. In last-look, the client is the signer (e.g. signerAmount, signerToken) and the server is the sender (e.g. senderAmount, senderToken).
Example
To find counterparties, see Discovery. With WebSocket server URLs in hand (i.e. the URL schema is wss), clients connect to each and calls methods as JSON-RPC over WebSocket.
WebSocket
Upon connection, the server calls setProtocols on the client.
The client may then subscribe to pricing updates.
The server then continuously updates the client with new pricing.
The client may send an OrderERC20 to the server to consider a swap.
With an OrderERC20 in hand, the server sends an Ethereum transaction to the SwapERC20 contract. The swapLight function is gas efficient, whereas the swap function provides protocol fee rebates to staked AST holders. Either function can settle a correctly signed OrderERC20. A successful swap emits a SwapERC20 event.
The client may subscribe to a filter for a SwapERC20 event with the nonce they provided to the server to track fills.
Pricing Formats
Server pricing can be communicated either by levels or a formula. All input and output values for pricing are in base units rather than atomic units. When generating orders, all values must be converted to atomic units.
Levels
A server may provide "levels" to determine its pricing for various tokens and amounts. Each level is a tuple of amount and price at that level. Amounts and minimums are all in baseToken. Each level indicates price "up to" the specified amount and therefore the last level is the maximum.
Examples
Client wants to swap 1000 USDT into WETH. Client looks up baseToken USDT and quoteToken WETH and uses the bid levels above. The first 100 would be multiplied by 0.00053 and second 900 would be multiplied by 0.00061 for a total of 0.602 WETH.
Client wants to swap 1 WETH into USDT. Client looks up baseToken WETH and quoteToken USDT and uses the bid levels above. The first 0.5 would be multiplied by 2000 and second 0.5 would be multiplied by 2010 for a total of 2005 USDT.
Client wants to swap WETH into 1000 USDT. Client looks up baseToken USDT and quoteToken WETH and uses the ask levels above. The first 100 would be multiplied by 0.00055 and second 900 would be multiplied by 0.00067 for a total of 0.658 WETH.
Client wants to swap USDT into 1 WETH. Client looks up baseToken WETH and quoteToken USDT and uses the ask levels above. The first 0.5 would be multiplied by 2001 and second 0.5 would be multiplied by 2015 for a total WETH amount of 2008 USDT.
Formula
The server can specify formulas to use for pricing. Each formula is an expression with operations including addition, subtraction, multiplication, and division, where x is provided by the client.
Examples
Client wants to swap 1000 USDT into WETH. Client looks up baseToken USDT and quoteToken WETH and uses the bid levels above. 1000 is multiplied by 0.00053 for a total of 0.53 WETH.
Client wants to swap 1 WETH into USDT. Client looks up baseToken WETH and quoteToken USDT and uses the bid levels above. 1 is multiplied by 2000 for a total of 2000 WETH.
Client wants to swap WETH into 1000 USDT. Client looks up baseToken USDT and quoteToken WETH and uses the ask levels above. 1000 is multiplied by 0.00055 for a total of 0.55 WETH.
Client wants to swap USDT into 1 WETH. Client looks up baseToken WETH and quoteToken USDT and uses the ask levels above. 1 is multiplied by 2001 for a total of 2001 WETH.
Signatures
AirSwap signatures are EIP712, which includes a domain separator to avoid replays across chains.
TypeScript. Swap signatures in TypeScript can be created using the @airswap/utils package.
Python. Swap signatures in Python can be created using the py_eth_sig_utils package.
Authorized Signers
Optional. One account may authorize another account to sign orders on its behalf. For example, a server might sign using an account that has been authorized by a contract wallet. To manage signer authorizations, use the following functions on the SwapERC20 contract.
EIP712
The following values are used for the EIP712Domain.
name
bytes32
SWAP_ERC20
version
bytes32
4
chainId
uint256
Ethereum Mainnet: 1, Goerli: 5
Last updated