Skip to content

Commit

Permalink
Merge pull request #289 from drift-labs/master
Browse files Browse the repository at this point in the history
sb crank
  • Loading branch information
NourAlharithi authored Nov 12, 2024
2 parents c84ee1f + 702aaf4 commit 1b88c2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 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.33",
"@drift-labs/sdk": "2.100.0-beta.8",
"@drift-labs/jit-proxy": "0.11.35",
"@drift-labs/sdk": "2.100.0-beta.10",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",
Expand Down
22 changes: 13 additions & 9 deletions src/bots/switchboardCranker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
BlockhashSubscriber,
DriftClient,
PriorityFeeSubscriber,
SlothashSubscriber,
TxSigAndSlot,
} from '@drift-labs/sdk';
import { SwitchboardOnDemandClient } from '@drift-labs/sdk';
import { BundleSender } from '../bundleSender';
import {
AddressLookupTableAccount,
Expand All @@ -28,11 +28,10 @@ const SIM_CU_ESTIMATE_MULTIPLIER = 1.5;
export class SwitchboardCrankerBot implements Bot {
public name: string;
public dryRun: boolean;
private intervalMs: number;
private oracleClient: SwitchboardOnDemandClient;
public defaultIntervalMs = 30_000;
public defaultIntervalMs: number;

private blockhashSubscriber: BlockhashSubscriber;
private slothashSubscriber: SlothashSubscriber;

constructor(
private globalConfig: GlobalConfig,
Expand All @@ -44,13 +43,16 @@ export class SwitchboardCrankerBot implements Bot {
) {
this.name = crankConfigs.botId;
this.dryRun = crankConfigs.dryRun;
this.intervalMs = crankConfigs.intervalMs;
this.defaultIntervalMs = crankConfigs.intervalMs || 10_000;
this.blockhashSubscriber = new BlockhashSubscriber({
connection: driftClient.connection,
});

this.oracleClient = new SwitchboardOnDemandClient(
this.driftClient.connection
this.slothashSubscriber = new SlothashSubscriber(
this.driftClient.connection,
{
commitment: 'processed',
}
);
}

Expand All @@ -60,6 +62,7 @@ export class SwitchboardCrankerBot implements Bot {
this.lookupTableAccounts.push(
await this.driftClient.fetchMarketLookupTableAccount()
);
await this.slothashSubscriber.subscribe();
}

async reset(): Promise<void> {
Expand All @@ -68,7 +71,7 @@ export class SwitchboardCrankerBot implements Bot {
await this.driftClient.unsubscribe();
}

async startIntervalLoop(intervalMs = this.intervalMs): Promise<void> {
async startIntervalLoop(intervalMs = this.defaultIntervalMs): Promise<void> {
logger.info(`Starting ${this.name} bot with interval ${intervalMs} ms`);
await sleepMs(5000);
await this.runCrankLoop();
Expand Down Expand Up @@ -117,7 +120,8 @@ export class SwitchboardCrankerBot implements Bot {
}
const pullIx =
await this.driftClient.getPostSwitchboardOnDemandUpdateAtomicIx(
pubkey
pubkey,
this.slothashSubscriber.currentSlothash
);
if (!pullIx) {
logger.error(`No pullIx for ${alias}`);
Expand Down
35 changes: 32 additions & 3 deletions src/experimental-bots/swift/takerExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import {
isVariant,
MarketType,
PositionDirection,
digestSignature,
} from '@drift-labs/sdk';
import { RuntimeSpec } from 'src/metrics';
import * as axios from 'axios';

const CONFIRM_TIMEOUT = 30_000;

export class SwiftTaker {
interval: NodeJS.Timeout | null = null;

Expand All @@ -35,7 +38,6 @@ export class SwiftTaker {
const slot = await this.driftClient.connection.getSlot();
const direction =
Math.random() > 0.5 ? PositionDirection.LONG : PositionDirection.SHORT;
console.log('Sending order in slot:', slot, Date.now());
const oracleInfo = this.driftClient.getOracleDataForPerpMarket(0);

const highPrice = oracleInfo.price.muln(102).divn(100);
Expand All @@ -51,7 +53,7 @@ export class SwiftTaker {
? lowPrice
: highPrice,
auctionEndPrice: isVariant(direction, 'long') ? highPrice : lowPrice,
auctionDuration: 15,
auctionDuration: 100,
}),
subAccountId: 0,
stopLossOrderParams: null,
Expand All @@ -60,6 +62,11 @@ export class SwiftTaker {
const signature =
this.driftClient.signSwiftOrderParamsMessage(orderMessage);

const hash = digestSignature(Uint8Array.from(signature));
console.log(
`Sending order in slot: ${slot}, time: ${Date.now()}, hash: ${hash}`
);

const response = await axios.default.post(
'https://master.swift.drift.trade/orders',
{
Expand All @@ -77,7 +84,29 @@ export class SwiftTaker {
},
}
);
console.log(response.data);
if (response.status !== 200) {
console.error('Failed to send order', response.data);
return;
}

const expireTime = Date.now() + CONFIRM_TIMEOUT;
while (Date.now() < expireTime) {
const response = await axios.default.get(
'https://master.swift.drift.trade/confirmation/hash-status?hash=' +
encodeURIComponent(hash),
{
validateStatus: (_status) => true,
}
);
if (response.status === 200) {
console.log('Confirmed hash ', hash);
return;
} else if (response.status >= 500) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 10000));
}
console.error('Failed to confirm hash: ', hash);
}, this.intervalMs);
}
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@
enabled "2.0.x"
kuler "^2.0.0"

"@drift-labs/[email protected].33":
version "0.11.33"
resolved "https://registry.yarnpkg.com/@drift-labs/jit-proxy/-/jit-proxy-0.11.33.tgz#002d7b1477b2aa495ea20eb9a00c474acb7b452c"
integrity sha512-KJUEvcvlYBYB8Y4XxFSkbC4awVxF0Lv/sgM8fWupPaihRrZF2t+CG+/FT2FbskUvcoo3iXAmkxlOIxEEOsGtUA==
"@drift-labs/[email protected].35":
version "0.11.35"
resolved "https://registry.yarnpkg.com/@drift-labs/jit-proxy/-/jit-proxy-0.11.35.tgz#24d900ca3d9a1c5aef7763b57f1c1034eb763cd4"
integrity sha512-+TcA2J3Pkm6u/t31JrUGdHsEgHYTfSfhrfeleMVdmc1WzQpAm6ZyaSL/e9IQTRF/mwcwZZvHA603pcxKUdzLRw==
dependencies:
"@coral-xyz/anchor" "^0.26.0"
"@drift-labs/sdk" "2.100.0-beta.8"
"@drift-labs/sdk" "2.100.0-beta.10"
"@solana/web3.js" "1.91.7"

"@drift-labs/[email protected].8":
version "2.100.0-beta.8"
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.100.0-beta.8.tgz#2733fd43e14feb7c214da346cabf43d9fe5c5b3a"
integrity sha512-+Yt0owk1PVO+3hFccb9I5jYzy1nkTp2vW/dhaehZIsy6nRCZXXpae545LVieAEn0F81rOIcKCA88SwiMN6hJHw==
"@drift-labs/[email protected].10":
version "2.100.0-beta.10"
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.100.0-beta.10.tgz#b7d867134af00ef1adde5512ff98342623438539"
integrity sha512-sCYcOCP4IXXK+i6+MdbORYTSq4cq1aFvwF4q6QjsKEDXvRXuP/6AJU/nU0Ijwzw7+CW2HlPZbXQTDwiQuwHYWQ==
dependencies:
"@coral-xyz/anchor" "0.28.0"
"@coral-xyz/anchor-30" "npm:@coral-xyz/[email protected]"
Expand Down

0 comments on commit 1b88c2c

Please sign in to comment.