Setting up a Keeper

Setting up bots on Narwhal is permissionless, and anyone can set up a bot to facilitate executing trades and performing liquidations. In return, keepers are rewarded with liquidation rewards.

The guide below will provide example code for setting up a keeper in typescript, but this can be done in various languages. If there are any questions please open a ticket in our Discord channel.

Triggering limit/stop orders

/*
 * Fetch all orders
 */
const limitOrders = await TradingStorage.getOpenLimitOrders();

/*
 * iterate through all orders
 */
for (let i = 0; i < limitOrders.length; i++) {
  const order = limitOrders[i];
  ...
}
/*
 * Fetch pyth price for order’s asset
 */
// find ORDER_ASSET with order.pairIndex
// see https://pyth.network/developers/price-feed-ids
const pythIds = [
  PythPriceIds[ORDER_ASSET],
  PythPriceIds.USDT
];
const priceFeeds = await pythClient.getLatestPriceFeeds(pythIds);
const data = priceFeeds[0].getPriceNoOlderThan(60);
const price = exponential(data.price, data.expo);
/*
 * Check if order is ready to be executed
 */
const openLimitOrderType = await LimitOrdersStorage.openLimitOrderTypes(
  order.trader,
  order.pairIndex,
  order.index,
);
// enum OpenLimitOrderType { MARKET = 0, LIMIT = 1, STOP = 2 }

Last updated