Narwhal Finance
WebsiteTwitter
  • πŸ‘‹Introduction to Narwhal
  • πŸ«‚Referrals
  • πŸ“šGlossary
  • πŸ¦„Using Narwhal
    • Connect Wallet
    • Alternative RPCs
    • Trading
      • Opening trades
      • Managing Trades
      • Closing Trades
    • Staking
      • NLP Pool
      • NAR & esNAR Staking
      • esNAR Vesting
      • Claiming Rewards
    • Brand Assets
    • Risks
  • 🎲Carnival
    • Carnival Games
    • Descriptions
    • Game Responsibly
  • βš™οΈPerpetual Trading
    • Overview
      • Lifecycle of a Trade
      • Keepers
    • Risk Management
    • Assets
    • Fees and Spread
  • πŸͺ™Tokenomics
    • Liquidity Pools
  • πŸ‘¨β€πŸ’»Devs
    • Contracts
    • Security
    • Bug Bounty Program
    • Setting up a Keeper
  • πŸ”—Links
    • Twitter
    • Website
Powered by GitBook
On this page
  1. Devs

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 }

PreviousBug Bounty Program

Last updated 1 year ago

πŸ‘¨β€πŸ’»