Skip to content

Commit

Permalink
Merge pull request #75 from drift-labs/arb-bot
Browse files Browse the repository at this point in the history
protoype bot
  • Loading branch information
NourAlharithi authored Oct 18, 2023
2 parents e69c49e + ecfe026 commit 45a341d
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 128 deletions.
29 changes: 19 additions & 10 deletions jitMaker.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ global:
endpoint:
# custom websocket endpoint to use (if null will be determined from rpc endpoint)
wsEndpoint:
resubTimeoutMs: 30000 # timeout for resubscribing to websocket
resubTimeoutMs: 10000 # timeout for resubscribing to websocket

# Private key to use to sign transactions.
# will load from KEEPER_PRIVATE_KEY env var if null
Expand Down Expand Up @@ -52,7 +52,7 @@ enabledBots:
# - liquidator

# Example maker bot that participates in JIT auction (caution: you will probably lose money)
- jitMaker
# - jitMaker

# Example maker bot that posts floating oracle orders
# - floatingMaker
Expand All @@ -63,14 +63,23 @@ enabledBots:
# settles negative PnLs for users (may want to run with runOnce: true)
# - userPnlSettler

# uncross the book to capture an arb
- uncrossArb

# below are bot configs
botConfigs:
jitMaker:
botId: "jitMaker"
# jitMaker:
# botId: "jitMaker"
# dryRun: false
# # below, ordering is important: match the subaccountIds to perpMarketindices.
# # e.g. to MM perp markets 0, 1 both on subaccount 0, then subaccounts=[0,0], perpMarketIndicies=[0,1]
# # to MM perp market 0 on subaccount 0 and perp market 1 on subaccount 1, then subaccounts=[0, 1], perpMarketIndicies=[0, 1]
# # also, make sure all subaccounts are loaded in the global config subaccounts above to avoid errors
# subaccounts: [0]
# perpMarketIndicies: [0]

uncrossArb:
botId: "uncrossArb"
dryRun: false
# below, ordering is important: match the subaccountIds to perpMarketindices.
# e.g. to MM perp markets 0, 1 both on subaccount 0, then subaccounts=[0,0], perpMarketIndicies=[0,1]
# to MM perp market 0 on subaccount 0 and perp market 1 on subaccount 1, then subaccounts=[0, 1], perpMarketIndicies=[0, 1]
# also, make sure all subaccounts are loaded in the global config subaccounts above to avoid errors
subaccounts: [0]
perpMarketIndicies: [0]
driftEnv: "mainnet-beta"

17 changes: 11 additions & 6 deletions src/bots/filler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
ReferrerInfo,
isOracleValid,
Expand Down Expand Up @@ -250,6 +251,7 @@ export class FillerBot implements Bot {
slotSubscriber: SlotSubscriber,
bulkAccountLoader: BulkAccountLoader | undefined,
driftClient: DriftClient,
userMap: UserMap | undefined,
eventSubscriber: EventSubscriber | undefined,
runtimeSpec: RuntimeSpec,
config: FillerConfig,
Expand Down Expand Up @@ -284,6 +286,7 @@ export class FillerBot implements Bot {
if (this.metricsPort) {
this.initializeMetrics();
}
this.userMap = userMap;

this.transactionVersion = config.transactionVersion ?? undefined;
logger.info(
Expand Down Expand Up @@ -522,12 +525,14 @@ export class FillerBot implements Bot {
public async init() {
logger.info(`${this.name} initing`);

this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
await this.userMap.subscribe();
if (!this.userMap) {
this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
await this.userMap.subscribe();
}

this.userStatsMap = new UserStatsMap(
this.driftClient,
Expand Down
1 change: 1 addition & 0 deletions src/bots/fillerLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class FillerLiteBot extends FillerBot {
undefined,
driftClient,
undefined,
undefined,
runtimeSpec,
config,
jitoSearcherClient,
Expand Down
8 changes: 2 additions & 6 deletions src/bots/jitMaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class JitMaker implements Bot {
constructor(
driftClient: DriftClient, // driftClient needs to have correct number of subaccounts listed
jitter: JitterSniper | JitterShotgun,
userMap: UserMap,
config: JitMakerConfig,
driftEnv: DriftEnv
) {
Expand All @@ -103,12 +104,8 @@ export class JitMaker implements Bot {
this.name = config.botId;
this.dryRun = config.dryRun;
this.driftEnv = driftEnv;
this.userMap = userMap;

this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
this.slotSubscriber = new SlotSubscriber(this.driftClient.connection);
this.dlobSubscriber = new DLOBSubscriber({
dlobSource: this.userMap,
Expand All @@ -125,7 +122,6 @@ export class JitMaker implements Bot {
logger.info(`${this.name} initing`);

// do stuff that takes some time
await this.userMap.subscribe();
await this.slotSubscriber.subscribe();
await this.dlobSubscriber.subscribe();

Expand Down
11 changes: 3 additions & 8 deletions src/bots/liquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class LiquidatorBot implements Bot {
private perpMarketToSubaccount: Map<number, number>;
private spotMarketToSubaccount: Map<number, number>;
private intervalIds: Array<NodeJS.Timer> = [];
private userMap?: UserMap;
private userMap: UserMap;
private deriskMutex = new Uint8Array(new SharedArrayBuffer(1));
private runtimeSpecs: RuntimeSpec;
private serumFulfillmentConfigMap: SerumFulfillmentConfigMap;
Expand All @@ -305,6 +305,7 @@ export class LiquidatorBot implements Bot {

constructor(
driftClient: DriftClient,
userMap: UserMap,
runtimeSpec: RuntimeSpec,
config: LiquidatorConfig,
defaultSubaccountId: number
Expand All @@ -317,6 +318,7 @@ export class LiquidatorBot implements Bot {
this.serumFulfillmentConfigMap = new SerumFulfillmentConfigMap(
this.driftClient
);
this.userMap = userMap;

this.metricsPort = config.metricsPort;
if (this.metricsPort) {
Expand Down Expand Up @@ -537,12 +539,6 @@ export class LiquidatorBot implements Bot {

public async init() {
logger.info(`${this.name} initing`);
this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
await this.userMap.subscribe();

const config = initialize({ env: this.runtimeSpecs.driftEnv as DriftEnv });
for (const spotMarketConfig of config.SPOT_MARKETS) {
Expand Down Expand Up @@ -1249,7 +1245,6 @@ export class LiquidatorBot implements Bot {
* attempts to close out any open positions on this account. It starts by cancelling any open orders
*/
private async derisk() {
console.log('wtfwtfwtf');
if (!this.acquireMutex()) {
return;
}
Expand Down
12 changes: 2 additions & 10 deletions src/bots/makerBidAskTwapCrank.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DLOB,
DriftClient,
DriftEnv,
UserMap,
SlotSubscriber,
MarketType,
Expand Down Expand Up @@ -72,7 +71,7 @@ export class MakerBidAskTwapCrank implements Bot {
constructor(
driftClient: DriftClient,
slotSubscriber: SlotSubscriber,
driftEnv: DriftEnv,
userMap: UserMap,
config: BaseBotConfig,
runOnce: boolean
) {
Expand All @@ -81,20 +80,13 @@ export class MakerBidAskTwapCrank implements Bot {
this.dryRun = config.dryRun;
this.runOnce = runOnce;
this.driftClient = driftClient;
this.userMap = userMap;
}

public async init() {
logger.info(`${this.name} initing, runOnce: ${this.runOnce}`);
this.lookupTableAccount =
await this.driftClient.fetchMarketLookupTableAccount();

// initialize userMap instance
this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
await this.userMap.subscribe();
}

public async reset() {
Expand Down
11 changes: 3 additions & 8 deletions src/bots/spotFiller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class SpotFillerBot implements Bot {

private dlobSubscriber?: DLOBSubscriber;

private userMap?: UserMap;
private userMap: UserMap;
private userStatsMap?: UserStatsMap;

private serumFulfillmentConfigMap: SerumFulfillmentConfigMap;
Expand Down Expand Up @@ -189,6 +189,7 @@ export class SpotFillerBot implements Bot {
slotSubscriber: SlotSubscriber,
bulkAccountLoader: BulkAccountLoader | undefined,
driftClient: DriftClient,
userMap: UserMap,
eventSubscriber: EventSubscriber,
runtimeSpec: RuntimeSpec,
config: FillerConfig
Expand All @@ -203,6 +204,7 @@ export class SpotFillerBot implements Bot {
this.slotSubscriber = slotSubscriber;
this.driftClient = driftClient;
this.eventSubscriber = eventSubscriber;
this.userMap = userMap;
this.bulkAccountLoader = bulkAccountLoader;
if (this.bulkAccountLoader) {
this.userStatsMapSubscriptionConfig = {
Expand Down Expand Up @@ -386,13 +388,6 @@ export class SpotFillerBot implements Bot {

const initPromises: Array<Promise<any>> = [];

this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
initPromises.push(this.userMap.subscribe());

this.userStatsMap = new UserStatsMap(
this.driftClient,
this.userStatsMapSubscriptionConfig
Expand Down
10 changes: 3 additions & 7 deletions src/bots/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TriggerBot implements Bot {
private triggeringNodes = new Map<string, number>();
private periodicTaskMutex = new Mutex();
private intervalIds: Array<NodeJS.Timer> = [];
private userMap?: UserMap;
private userMap: UserMap;

private priorityFeeCalculator: PriorityFeeCalculator;

Expand All @@ -76,12 +76,14 @@ export class TriggerBot implements Bot {
constructor(
driftClient: DriftClient,
slotSubscriber: SlotSubscriber,
userMap: UserMap,
runtimeSpec: RuntimeSpec,
config: BaseBotConfig
) {
this.name = config.botId;
this.dryRun = config.dryRun;
this.driftClient = driftClient;
this.userMap = userMap;
this.runtimeSpec = runtimeSpec;
this.slotSubscriber = slotSubscriber;

Expand Down Expand Up @@ -157,12 +159,6 @@ export class TriggerBot implements Bot {

public async init() {
logger.info(`${this.name} initing`);
this.userMap = new UserMap(
this.driftClient,
this.driftClient.userAccountSubscriptionConfig,
false
);
await this.userMap.subscribe();

this.dlobSubscriber = new DLOBSubscriber({
dlobSource: this.userMap,
Expand Down
Loading

0 comments on commit 45a341d

Please sign in to comment.