Skip to content

Commit

Permalink
Merge pull request #291 from drift-labs/master
Browse files Browse the repository at this point in the history
partition markets
  • Loading branch information
NourAlharithi authored Nov 12, 2024
2 parents 1b88c2c + 023edd2 commit f4a708a
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 178 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "lib/index.js",
"license": "Apache-2.0",
"dependencies": {
"@drift-labs/jit-proxy": "0.11.35",
"@drift-labs/sdk": "2.100.0-beta.10",
"@drift-labs/jit-proxy": "0.11.36",
"@drift-labs/sdk": "2.100.0-beta.11",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",
Expand Down
17 changes: 15 additions & 2 deletions src/bots/pythCranker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,20 @@ export class PythCrankerBot implements Bot {
await this.driftClient.fetchMarketLookupTableAccount()
);

for (const marketConfig of PerpMarkets[this.globalConfig.driftEnv]) {
const perpMarketIndexes = this.driftClient
.getPerpMarketAccounts()
.map((market) => market.marketIndex);
const perpMarketConfigs = PerpMarkets[this.globalConfig.driftEnv].filter(
(market) => perpMarketIndexes.includes(market.marketIndex)
);
const spotMarketIndexes = this.driftClient
.getSpotMarketAccounts()
.map((market) => market.marketIndex);
const spotMarketConfigs = SpotMarkets[this.globalConfig.driftEnv].filter(
(market) => spotMarketIndexes.includes(market.marketIndex)
);

for (const marketConfig of perpMarketConfigs) {
const feedId = marketConfig.pythFeedId;
if (!feedId) {
logger.warn(`No pyth feed id for market ${marketConfig.symbol}`);
Expand Down Expand Up @@ -165,7 +178,7 @@ export class PythCrankerBot implements Bot {
});
}

for (const marketConfig of SpotMarkets[this.globalConfig.driftEnv]) {
for (const marketConfig of spotMarketConfigs) {
if (
this.feedIdsToCrank.findIndex(
(feedId) => feedId.baseSymbol === marketConfig.symbol
Expand Down
47 changes: 8 additions & 39 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import YAML from 'yaml';
import {
loadCommaDelimitToArray,
loadCommaDelimitToStringArray,
parsePositiveIntArray,
} from './utils';
import { OrderExecutionAlgoType } from './types';
import {
Expand All @@ -21,17 +22,6 @@ export type BaseBotConfig = {
runOnce?: boolean;
};

export type JitMakerConfig = BaseBotConfig & {
subaccounts?: Array<number>;
marketType: string;
/// @deprecated, use {@link JitMakerConfig.marketIndexes} and {@link JitMakerConfig.marketType}
perpMarketIndicies?: Array<number>;
marketIndexes?: Array<number>;
targetLeverage?: number;
aggressivenessBps?: number;
jitCULimit?: number;
};

export type UserPnlSettlerConfig = BaseBotConfig & {
/// perp market indexes to filter for settling pnl
perpMarketIndicies?: Array<number>;
Expand Down Expand Up @@ -137,14 +127,12 @@ export type BotConfigMap = {
trigger?: BaseBotConfig;
liquidator?: LiquidatorConfig;
floatingMaker?: BaseBotConfig;
jitMaker?: JitMakerConfig;
ifRevenueSettler?: BaseBotConfig;
fundingRateUpdater?: BaseBotConfig;
userPnlSettler?: UserPnlSettlerConfig;
userLpSettler?: BaseBotConfig;
userIdleFlipper?: BaseBotConfig;
markTwapCrank?: BaseBotConfig;
uncrossArb?: BaseBotConfig;
pythCranker?: PythCrankerBotConfig;
switchboardCranker?: SwitchboardCrankerBotConfig;
swiftTaker?: BaseBotConfig;
Expand All @@ -160,6 +148,10 @@ export interface GlobalConfig {
hermesEndpoint?: string;
numNonActiveOraclesToPush?: number;

// Optional to specify markets loaded by drift client
perpMarketsToLoad?: Array<number>;
spotMarketsToLoad?: Array<number>;

/// helius endpoint to use helius priority fee strategy
heliusEndpoint?: string;
/// additional rpc endpoints to send transactions to
Expand Down Expand Up @@ -232,6 +224,9 @@ const defaultConfig: Partial<Config> = {
debug: false,
subaccounts: [0],

perpMarketsToLoad: parsePositiveIntArray(process.env.PERP_MARKETS_TO_LOAD),
spotMarketsToLoad: parsePositiveIntArray(process.env.SPOT_MARKETS_TO_LOAD),

eventSubscriberPollingInterval: 5000,
bulkAccountLoaderPollingInterval: 5000,

Expand Down Expand Up @@ -455,21 +450,6 @@ export function loadConfigFromOpts(opts: any): Config {
runOnce: opts.runOnce ?? false,
};
}
if (opts.jitMaker) {
config.enabledBots.push('jitMaker');
config.botConfigs!.jitMaker = {
dryRun: opts.dryRun ?? false,
botId: process.env.BOT_ID ?? 'jitMaker',
metricsPort: 9464,
runOnce: opts.runOnce ?? false,
perpMarketIndicies: loadCommaDelimitToArray(opts.perpMarketIndicies) ?? [
0,
],
subaccounts: loadCommaDelimitToArray(opts.subaccounts) ?? [0],
marketType: opts.marketType ?? 'perp',
targetLeverage: 1,
};
}
if (opts.ifRevenueSettler) {
config.enabledBots.push('ifRevenueSettler');
config.botConfigs!.ifRevenueSettler = {
Expand Down Expand Up @@ -528,17 +508,6 @@ export function loadConfigFromOpts(opts: any): Config {
runOnce: opts.runOnce ?? false,
};
}

if (opts.uncrossArb) {
config.enabledBots.push('uncrossArb');
config.botConfigs!.uncrossArb = {
dryRun: opts.dryRun ?? false,
botId: process.env.BOT_ID ?? 'uncrossArb',
metricsPort: 9464,
runOnce: opts.runOnce ?? false,
};
}

return mergeDefaults(defaultConfig, config) as Config;
}

Expand Down
19 changes: 13 additions & 6 deletions src/experimental-bots/swift/takerExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@drift-labs/sdk';
import { RuntimeSpec } from 'src/metrics';
import * as axios from 'axios';
import { sleepMs } from '../../utils';

const CONFIRM_TIMEOUT = 30_000;

Expand All @@ -34,26 +35,32 @@ export class SwiftTaker {
}

async startInterval() {
const marketIndexes = [0, 1, 2, 3, 4, 5, 6];
this.interval = setInterval(async () => {
await sleepMs(Math.random() * 1000); // Randomize for different grafana metrics
const slot = await this.driftClient.connection.getSlot();
const direction =
Math.random() > 0.5 ? PositionDirection.LONG : PositionDirection.SHORT;
const oracleInfo = this.driftClient.getOracleDataForPerpMarket(0);

const highPrice = oracleInfo.price.muln(102).divn(100);
const lowPrice = oracleInfo.price.muln(101).divn(100);
const marketIndex =
marketIndexes[Math.floor(Math.random() * marketIndexes.length)];

const oracleInfo =
this.driftClient.getOracleDataForPerpMarket(marketIndex);
const highPrice = oracleInfo.price.muln(101).divn(100);
const lowPrice = oracleInfo.price;

const orderMessage = {
swiftOrderParams: getMarketOrderParams({
marketIndex: 0,
marketIndex,
marketType: MarketType.PERP,
direction,
baseAssetAmount: BASE_PRECISION,
auctionStartPrice: isVariant(direction, 'long')
? lowPrice
: highPrice,
auctionEndPrice: isVariant(direction, 'long') ? highPrice : lowPrice,
auctionDuration: 100,
auctionDuration: 30,
}),
subAccountId: 0,
stopLossOrderParams: null,
Expand All @@ -70,7 +77,7 @@ export class SwiftTaker {
const response = await axios.default.post(
'https://master.swift.drift.trade/orders',
{
market_index: 0,
market_index: marketIndex,
market_type: 'perp',
message: this.driftClient
.encodeSwiftOrderParamsMessage(orderMessage)
Expand Down
Loading

0 comments on commit f4a708a

Please sign in to comment.