Indexers are used by makers to signal their interest in trading and by takers to query for makers with mutual trading interest. Locators are strings used by takers to connect to makers. In the case of Servers, locators are URLs, and in the case of Delegates, locators are contract addresses.
Add the @airswap/protocols
package to your application.
$ yarn add @airswap/protocols
Import the Indexer client.
import { Indexer } from '@airswap/protocols'
Create a new Indexer
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 indexer = new Indexer()
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 indexer = new Indexer(chainIds.MAINNET, provider);
See a list of available providers on the ethers.js documentation.
Get a list of string
locators by token pair.
public async getLocators(signerToken: string,senderToken: string,protocol = protocols.SERVER,limit = 10,cursor = INDEX_HEAD)): Promise<LocatorResult>
Param | Type | Optionality | Description |
|
|
| Address of a token for the signer side of a trade. |
|
|
| Address of a token for the sender side of a trade. |
|
|
| Protocol to query e.g. |
|
|
| Maximum number of results to query. |
|
|
| Cursor from which to start the query. |
Example Query rinkeby Servers for those selling DAI for WETH. Locators are Server URLs.
const indexer = new Indexer()const { locators } = await indexer.getLocators(signerToken, senderToken)
Example Query rinkeby Delegates for those selling DAI for WETH. Locators are Delegate contract addresses.
import { protocols } from `@airswap/constants`const indexer = new Indexer();const { locators } = await indexer.getLocators(signerToken, senderToken, protocols.DELEGATE);
See Contract Deployments for the latest mainnet and rinkeby Indexer deployments.
An Indexer is a smart contract to discover trading parties by token pair. View the code on GitHub.
Create a new Indexer
contract.
constructor(address indexerStakingToken) public
Param | Type | Description |
|
| Address of the token required for staking. Must be a standard ERC20 contract. |
If none exists, deploy a new Index
contract for the given token pair and return its address. If the requested Index
already exists, the function just returns its address.
function createIndex(address signerToken,address senderToken,bytes2 protocol) external returns (address)
Param | Type | Description |
|
| Address of the token transferred from a signer in a trade. |
|
| Address of the token transferred from a sender in a trade. |
|
| Identifier for different locator versions ie http makers, delegates. |
When a new Index
is deployed, createIndex
emits a CreateIndex
event.
event CreateIndex(address signerToken,address senderToken,bytes2 protocol);
Add a token to the blacklist. Markets that include a blacklisted token cannot have intents to trade set on them and cannot have locators fetched for them.
function addTokenToBlacklist(address token) external onlyOwner
Param | Type | Description |
|
| Address of the token to blacklist. |
A successful addTokenToBlacklist
emits a AddTokenToBlacklist
event.
event AddTokenToBlacklist(address token);
Remove a token from the blacklist. Markets that include a blacklisted token cannot have intents to trade set on them and cannot have locators fetched for them.
function removeTokenFromBlacklist(address token) external onlyOwner
Param | Type | Description |
|
| The address of the token to remove. |
A successful removeTokenFromBlacklist
emits a RemoveTokenFromBlacklist
event.
event RemoveTokenFromBlacklist(address token);
Stake tokens to the Indexer and set an intent to trade. If the caller already has an intent on the specified Index, then the intent is updated to reflect the new stakingAmount
and locator
.
function setIntent(address signerToken,address senderToken,bytes2 protocol,uint256 stakingAmount,bytes32 locator) external
Param | Type | Description |
|
| Signer token of the Index being staked. |
|
| Sender token of the Index being staked. |
|
| Identifies protocol to communicate with locator. |
|
| Amount of stakingToken to stake. |
|
| Arbitrary data. Often an address in the first 20 bytes. |
A successful setIntent
emits a Stake
event. The underlying Index
emits an SetLocator
event.
event Stake(address indexed staker,address indexed signerToken,address indexed senderToken,bytes2 protocol,uint256 stakeAmount);
Revert Reason | Scenario |
| There is no index for the token pair. |
| The locator was not found on the whitelist. |
| One or both of the tokens are blacklisted. |
| The staking amount was not transferred. |
| Entry does not exist for the message sender. |
Unset an intent to trade and return staked tokens to the sender.
function unsetIntent(address signerToken,address senderToken,bytes2 protocol) external
Param | Type | Description |
|
| Signer token of the Index being unstaked. |
|
| Sender token of the Index being unstaked. |
|
| Identifies protocol to communicate with locator. |
A successful unsetIntent
emits an Unstake
event. The underlying Index
emits an UnsetLocator
event.
event Unstake(address indexed staker,address indexed signerToken,address indexed senderToken,bytes2 protocol,uint256 stakeAmount);
Revert Reason | Scenario |
| There is no Index for the token pair. |
| Entry does not exist for the message sender. |
Get a list of locators that have an intent to trade a token pair. Along with the locators, their corresponding staking scores are returned, and the address of the next cursor for pagination.
function getLocators(address signerToken,address senderToken,bytes2 protocol,address cursor,uint256 limit) external view returns (bytes32[] memory locators,uint256[] memory scores,address nextCursor) {
Param | Type | Description |
|
| Address of the token that the signer transfers. |
|
| Address of the token that the sender transfers. |
|
| Identifies protocol to communicate with locator. |
|
| Address of the user to start from in the list. |
|
| Maximum number of items to return. |
Get the amount staked for a specific user, token pair, and protocol.
function getStakedAmount(address user,address signerToken,address senderToken,bytes2 protocol) public view returns (uint256 stakedAmount) {
Param | Type | Description |
|
| The user whose stake amount is requested. |
|
| The signer token of the Index they've staked on. |
|
| The sender token of the Index they've staked on. |
|
| Identifies protocol to communicate with locator. |
An Index is a list of locators sorted by score. View the code on GitHub.
Entry
struct Entry {bytes32 locator;uint256 score;address prev;address next;}
constructor
Create a new Index
contract.
constructor() public
setLocator
Set an Locator on the Index.
function setLocator(address identifier,uint256 score,bytes32 locator) external onlyOwner
Param | Type | Description |
|
| Onchain address identifying the owner of a locator. |
|
| Score for the locator being set. |
|
| Locator. |
A successful setLocator
emits a SetLocator
event.
event SetLocator(address indexed identifier,uint256 score,bytes32 indexed locator);
Revert Reason | Scenario |
| This address already has an Entry on the Index. |
| Locator must not be empty to ensure list integrity. |
updateLocator
Updates an existing Locator on the Index.
function updateLocator(address identifier,uint256 score,bytes32 locator) external onlyOwner
Param | Type | Description |
|
| Onchain address identifying the owner of a locator. |
|
| Score for the locator being set. |
|
| Locator. |
A successful updateLocator
emits a SetLocator
event.
event SetLocator(address indexed identifier,uint256 score,bytes32 indexed locator);
Revert Reason | Scenario |
| This address does not has an Entry on the Index and thus cannot be updated. |
| Locator must not be empty to ensure list integrity. |
unsetLocator
Unset a Locator from the Index.
function unsetLocator(address identifier) external onlyOwner returns (bool) {
A successful unsetLocator
emits an UnsetLocator
event.
event UnsetLocator(address event identifier);
Param | Type | Description |
|
| Onchain address identifying the owner of a locator. |
Revert Reason | Scenario |
| This address does not have an Entry on the Index. |
getScore
Gets the score for a given identifier.
function getScore(address identifier) external view returns (uint256)
Param | Type | Description |
|
| Onchain address identifying the owner of a locator. |
getLocator
Gets the locator as bytes32 for a given identifier.
function getLocator(address identifier) external view returns (bytes32)
Param | Type | Description |
|
| Onchain address identifying the owner of a locator. |
getLocators
Get a Range of Locators.
function getLocators(address cursor,uint256 limit) external view returns (bytes32[] memory locators, uint256[] memory scores, address nextCursor) {
Param | Type | Description |
|
| Cursor to start with. |
|
| Maximum number of locators to return. |