Swap is a smart contract that trustlessly transfers tokens between parties.
Add the @airswap/protocols
package to your application.
$ yarn add @airswap/protocols
Import the Swap client.
import { Swap } from '@airswap/protocols'
Create a new Swap
client.
public constructor(chainId = chainIds.RINKEBY,walletOrProvider?: ethers.Wallet | ethers.providers.Provider)
Basic Example
Create a client for the Rinkeby Indexer using the default provider.
const swap = new Swap()
Custom Provider Example
Create a client for the Mainnet Indexer using an INFURA provider.
import { chainIds } from '@airswap/constants'const provider = new ethers.providers.InfuraProvider(...)const swap = new Swap(chainIds.MAINNET, provider);
See a list of available providers on the ethers.js documentation.
Perform a swap given an order.
public async swap(order: Order, wallet?: ethers.Wallet): Promise<string>
Param | Type | Optionality | Description |
|
|
| Order to swap tokens between |
|
|
| Wallet used to execute the transaction, otherwise uses wallet provided in the constructor |
Example Get an order from a local development Server and execute a swap.
// Create a new ethers walletconst wallet = new ethers.Wallet('...')// Construct a new Serverconst server = new Server('http://localhost:3000')// Get an order from the Serverconst order = await server.getSenderSideOrder(senderAmount,signerToken,senderToken,wallet.address,)// Swap the order on the Rinkeby swap contractconst hash = await new Swap().swap(order, wallet)
Swap is a trustless peer-to-peer trade settlement contract. View the code on GitHub.
Authorizations are for peers that trade on behalf of others. These peers are authorized by an individual to send or sign orders for them. Peers can be wallets (people or programs) or smart contracts.
Affiliates are third-parties compensated for their part in bringing together the two parties of a trade and can be other traders or software applications that connect traders on the network.
Create a new Swap
contract.
constructor() public
An atomic token swap between a signer and a sender.
function swap(Types.Order calldata order) external
Param | Type | Required | Description |
|
| required | Order struct as specified in Types. |
Preconditions |
✓ signerWallet must approve the Swap contract to transfer its signerToken. |
✓ senderWallet must approve the Swap contract to transfer its senderToken. |
A successful swap
emits a Swap
event.
event Swap(uint256 indexed nonce,uint256 timestamp,address indexed signerWallet,uint256 signerAmount,uint256 signerId,address signerToken,address indexed senderWallet,uint256 senderAmount,uint256 senderId,address senderToken,address affiliateWallet,uint256 affiliateAmount,uint256 affiliateId,address affiliateToken);
Revert Reason | Scenario |
| Order has been signed by an account that has not been authorized to sign it. |
| Signature provided does not match the Order provided. |
| Order has already been taken or cancelled by its |
| Order has an |
| Nonce provided is below the minimum value set. |
| Order has been sent by an account that has not been authorized to send it. |
| Order has the same signer and sender for the swap. |
| One of the token transfers in the swap failed. |
Provide an array of nonces
, unique by signer address, to mark one or more orders as cancelled.
Out of gas may occur in arrays of length > 400
function cancel(uint256[] memory nonces) external
Param | Type | Required | Description |
|
| required | Array of order nonces to cancel. |
A successful cancel
emits one Cancel
event per nonce.
event Cancel(uint256 indexed nonce,address indexed signerWallet);
Provide a minimum value to cancel all nonces below the value. This is not inclusive - the minimumNonce
value is still considered valid.
function cancelUpTo(uint256 minimumNonce) external
Param | Type | Required | Description |
|
| required | Lowest acceptable nonce value for a signer. |
A successful cancelUpTo
emits an CancelUpTo
event.
event CancelUpTo(uint256 indexed nonce,address indexed signerWallet);
Authorize another account or contract send orders on behalf of the caller.
function authorizeSender(address authorizedSender) external
Param | Type | Required | Description |
|
| required | Address to authorize for sending. |
A successful authorizeSender
emits an AuthorizeSender
event.
event AuthorizeSender(address indexed authorizerAddress,address indexed authorizedSender);
Revert Reason | Scenario |
| The |
Authorize another account or contract to:
Sign an order OR
Submit an order without a signature
on behalf of the function caller. This means the function caller is no longer required to sign such orders.
function authorizeSigner(address authorizedSigner) external
Param | Type | Required | Description |
|
| required | Address to authorize for signing. |
A successful authorizeSigner
emits an AuthorizeSigner
event.
event AuthorizeSigner(address indexed authorizerAddress,address indexed authorizedSigner);
Revert Reason | Scenario |
| The |
Revoke the sending authorization of a Delegate account or contract.
function revokeSender(address authorizedSender) external
Param | Type | Required | Description |
|
| required | Address from which to revoke sending authorization. |
A successful revokeSender
emits a RevokeSender
event.
event RevokeSender(address indexed authorizerAddress,address indexed revokedSender,);
Revoke the signing authorization of a Delegate account or contract.
function revokeSigner(address authorizedSigner) external
Param | Type | Required | Description |
|
| required | Address from which to revoke signing authorization. |
A successful revokeSigner
emits a RevokeSigner
event.
event RevokeSigner(address indexed authorizerAddress,address indexed revokedSigner,);