Skip to content

Commit

Permalink
Merge pull request #94 from drift-labs/run-once-polling
Browse files Browse the repository at this point in the history
no rpc spam for run once
  • Loading branch information
NourAlharithi authored Dec 5, 2023
2 parents cbe45be + 368ef45 commit 01f4a3f
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 190 deletions.
2 changes: 0 additions & 2 deletions example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ global:

initUser: false # initialize user on startup
testLiveness: false # test liveness, by failing liveness test after 1 min
cancelOpenOrders: false # cancel open orders on startup
closeOpenPositions: false # close all open positions

# Force deposit this amount of USDC to collateral account, the program will
# end after the deposit transaction is sent
Expand Down
2 changes: 0 additions & 2 deletions jitMaker.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ global:

initUser: false # initialize user on startup
testLiveness: false # test liveness, by failing liveness test after 1 min
cancelOpenOrders: false # cancel open orders on startup
closeOpenPositions: false # close all open positions

# Force deposit this amount of USDC to collateral account, the program will
# end after the deposit transaction is sent
Expand Down
32 changes: 27 additions & 5 deletions lite.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ global:

initUser: false # initialize user on startup
testLiveness: false # test liveness, by failing liveness test after 1 min
cancelOpenOrders: false # cancel open orders on startup
closeOpenPositions: false # close all open positions

# Force deposit this amount of USDC to collateral account, the program will
# end after the deposit transaction is sent
Expand Down Expand Up @@ -45,7 +43,7 @@ global:
# Bot specific configs are below
enabledBots:
# Perp order filler bot
- fillerLite
# - fillerLite

# Spot order filler bot
# - spotFiller
Expand All @@ -68,6 +66,8 @@ enabledBots:
# settles negative PnLs for users (may want to run with runOnce: true)
# - userPnlSettler

- markTwapCrank

# below are bot configs
botConfigs:
fillerLite:
Expand All @@ -79,6 +79,28 @@ botConfigs:
# will revert a transaction during simulation if a fill fails, this will save on tx fees,
# and be friendlier for use with services like Jito.
# Default is true
revertOnFailure: true


markTwapCrank:
botId: "mark-twap-cranker"
dryRun: false
metricsPort: 9465
crankIntervalToMarketIndicies:
15000:
- 0
- 1
- 2
60000:
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
24 changes: 22 additions & 2 deletions src/bots/ifRevenueSettler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
SpotMarketAccount,
OraclePriceData,
ZERO,
DriftClientConfig,
BulkAccountLoader,
} from '@drift-labs/sdk';
import { Mutex } from 'async-mutex';

Expand Down Expand Up @@ -31,15 +33,33 @@ export class IFRevenueSettlerBot implements Bot {
private watchdogTimerMutex = new Mutex();
private watchdogTimerLastPatTime = Date.now();

constructor(driftClient: DriftClient, config: BaseBotConfig) {
constructor(driftClientConfigs: DriftClientConfig, config: BaseBotConfig) {
this.name = config.botId;
this.dryRun = config.dryRun;
this.runOnce = config.runOnce || false;
this.driftClient = driftClient;
const bulkAccountLoader = new BulkAccountLoader(
driftClientConfigs.connection,
driftClientConfigs.connection.commitment || 'processed',
0
);
this.driftClient = new DriftClient(
Object.assign({}, driftClientConfigs, {
accountSubscription: {
type: 'polling',
accountLoader: bulkAccountLoader,
},
})
);
}

public async init() {
logger.info(`${this.name} initing`);
await this.driftClient.subscribe();
if (!(await this.driftClient.getUser().exists())) {
throw new Error(
`User for ${this.driftClient.wallet.publicKey.toString()} does not exist`
);
}
}

public async reset() {
Expand Down
38 changes: 31 additions & 7 deletions src/bots/userIdleFlipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
PublicKey,
UserMap,
TxSigAndSlot,
DriftClientConfig,
BulkAccountLoader,
} from '@drift-labs/sdk';
import { Mutex } from 'async-mutex';

Expand Down Expand Up @@ -32,21 +34,43 @@ export class UserIdleFlipperBot implements Bot {
private watchdogTimerMutex = new Mutex();
private watchdogTimerLastPatTime = Date.now();

constructor(
driftClient: DriftClient,
config: BaseBotConfig,
userMap: UserMap
) {
constructor(driftClientConfigs: DriftClientConfig, config: BaseBotConfig) {
this.name = config.botId;
this.dryRun = config.dryRun;
this.runOnce = config.runOnce || false;
this.driftClient = driftClient;
this.userMap = userMap;
const bulkAccountLoader = new BulkAccountLoader(
driftClientConfigs.connection,
driftClientConfigs.connection.commitment || 'processed',
0
);
this.driftClient = new DriftClient(
Object.assign({}, driftClientConfigs, {
accountSubscription: {
type: 'polling',
accountLoader: bulkAccountLoader,
},
})
);
this.userMap = new UserMap(
this.driftClient,
{
type: 'polling',
accountLoader: bulkAccountLoader,
},
false
);
}

public async init() {
logger.info(`${this.name} initing`);

await this.driftClient.subscribe();
if (!(await this.driftClient.getUser().exists())) {
throw new Error(
`User for ${this.driftClient.wallet.publicKey.toString()} does not exist`
);
}
await this.userMap.subscribe();
this.lookupTableAccount =
await this.driftClient.fetchMarketLookupTableAccount();
}
Expand Down
40 changes: 32 additions & 8 deletions src/bots/userPnlSettler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
calculateNetUserPnl,
BASE_PRECISION,
QUOTE_SPOT_MARKET_INDEX,
DriftClientConfig,
BulkAccountLoader,
} from '@drift-labs/sdk';
import { Mutex } from 'async-mutex';

Expand Down Expand Up @@ -68,21 +70,43 @@ export class UserPnlSettlerBot implements Bot {
private watchdogTimerMutex = new Mutex();
private watchdogTimerLastPatTime = Date.now();

constructor(
driftClient: DriftClient,
config: BaseBotConfig,
userMap: UserMap
) {
constructor(driftClientConfigs: DriftClientConfig, config: BaseBotConfig) {
this.name = config.botId;
this.dryRun = config.dryRun;
this.runOnce = config.runOnce || false;
this.driftClient = driftClient;
this.userMap = userMap;

const bulkAccountLoader = new BulkAccountLoader(
driftClientConfigs.connection,
driftClientConfigs.connection.commitment || 'processed',
0
);
this.driftClient = new DriftClient(
Object.assign({}, driftClientConfigs, {
accountSubscription: {
type: 'polling',
accountLoader: bulkAccountLoader,
},
})
);
this.userMap = new UserMap(
this.driftClient,
{
type: 'polling',
accountLoader: bulkAccountLoader,
},
false
);
}

public async init() {
logger.info(`${this.name} initing`);

await this.driftClient.subscribe();
if (!(await this.driftClient.getUser().exists())) {
throw new Error(
`User for ${this.driftClient.wallet.publicKey.toString()} does not exist`
);
}
await this.userMap.subscribe();
this.lookupTableAccount =
await this.driftClient.fetchMarketLookupTableAccount();
}
Expand Down
Loading

0 comments on commit 01f4a3f

Please sign in to comment.