Drift Protocol v2

โŒ˜K
๐Ÿ‘พWelcome to Drift Protocol
๐ŸงฎDrift DEX
๐Ÿ‘‹Getting Started
๐Ÿ“ˆPerpetual Futures
๐Ÿ“ŠSpot Margin Trading
๐ŸฆBorrow & Lend
๐Ÿ›๏ธStaking
๐ŸชMarket Makers
๐Ÿ”ฌTechnical Explanations
๐Ÿ“Accounting and Settlement
โž—Borrow Interest Rate
๐Ÿ“œDelisting Process
โ›ฒDrift AMM
๐ŸƒJust-In-Time (JIT) Auctions
๐Ÿ“šKeepers & Decentralised Orderbook
โ˜ ๏ธLiquidators
๐Ÿ’งLiquidity Providers (LPs)
๐Ÿ“‹Protocol Guard Rails
๐Ÿ“Risks
๐Ÿ–ฅ๏ธDeveloper Resources
๐Ÿ“”Program/Vault Addresses
โŒจ๏ธSDK Documentation
โŒจ๏ธTutorial: Bots
โš ๏ธTroubleshooting
๐Ÿ› ๏ธKeeper Bots
๐Ÿ› ๏ธTrading Bots
โŒจ๏ธHistorical Data (v1)
โŒจ๏ธAPI
๐Ÿ›ก๏ธSecurity
๐Ÿ›ก๏ธAudits
๐Ÿ›ก๏ธBug Bounty
โš–๏ธLegal and Regulations
๐Ÿ“Terms of Use
๐Ÿ“Disclaimer
๐Ÿ“Privacy Policy
๐Ÿ“Competition Terms and Conditions
๐Ÿ“šGlossary
Docs powered byย archbeeย 

Tutorial: Order Trigger Bot

7min

Introduction

Order Trigger Bots (Trigger Bots) are responsible for marking orders that satisfy the trigger condition, including:

  • Trigger Market Orders - Stop Market and Take Profit
  • Trigger Limit Orders - Stop Limit and Take Profit Limit

Trigger Bots receive a small compensation for each successfully marked order.

See ๏ปฟKeepers & Decentralised Orderbook for a technical explanation of how the decentralised orderbook (DLOB) and matching incentives work.

Trigger Bots are similar to ๏ปฟTutorial: Order Matching Bot in that they:

  • also maintain a local copy of the ๏ปฟKeepers & Decentralised Orderbook;
  • do not require the operator to manage collateral; and
  • receive a small reward for performing their duties.

Getting Started

The reference implementation of a Trigger Bot is available here.

Follow the instructions at ๏ปฟKeeper Bots to set the required environment variables and make sure a ClearingHouseUser is initialised.

Start the Trigger Bot:

Shell
|
yarn run dev:trigger
๏ปฟ

๏ปฟ

Technical Explanation

1. Get nodes from the DLOB that are ready to be triggered

๏ปฟ

The DLOB implementation includes a method for getting orders ready to be triggered:

TypeScript
|
const market = this.driftClient.getMarketAccounts()[0]; // get a MarketAccount

const oraclePriceData =
    this.driftClient.getOracleDataForMarket(marketIndex);

const nodesToTrigger = this.dlob.findNodesToTrigger(
    marketIndex,
    this.slotSubscriber.getSlot(),
    oraclePriceData.price
);
๏ปฟ

๏ปฟ

2. Call trigger_order on DriftClient

TypeScript
|
const user = this.userMap.get(nodeToTrigger.node.userAccount.toString());
const txSig = await this.driftClient.triggerOrder(
    nodeToTrigger.node.userAccount,
    user.getUserAccount(),
    nodeToTrigger.node.order
)
๏ปฟ

๏ปฟ

Updated 03 Mar 2023
Did this page help you?
Yes
No
PREVIOUS
Tutorial: Order Matching Bot
NEXT
Tutorial: Liquidation Bot
Docs powered byย archbeeย 
TABLE OF CONTENTS
Introduction
Getting Started
Technical Explanation
1. Get nodes from the DLOB that are ready to be triggered
2. Call trigger_order on DriftClient