Skip to content

Commit

Permalink
ts: sniper checks limit price
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 24, 2024
1 parent da06135 commit 7907285
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
9 changes: 4 additions & 5 deletions ts/sdk/src/jitter/baseJitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ export abstract class BaseJitter {
this.jitProxyClient = jitProxyClient;
this.userStatsMap =
userStatsMap ||
new UserStatsMap(this.driftClient, new BulkAccountLoader(
this.driftClient.connection,
'confirmed',
0
));
new UserStatsMap(
this.driftClient,
new BulkAccountLoader(this.driftClient.connection, 'confirmed', 0)
);
}

async subscribe(): Promise<void> {
Expand Down
31 changes: 17 additions & 14 deletions ts/sdk/src/jitter/jitterShotgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ export class JitterShotgun extends BaseJitter {

console.log(`Trying to fill ${orderSignature}`);
try {
const { txSig } = await this.jitProxyClient.jit({
takerKey,
takerStatsKey,
taker,
takerOrderId: order.orderId,
maxPosition: params.maxPosition,
minPosition: params.minPosition,
bid: params.bid,
ask: params.ask,
postOnly: null,
priceType: params.priceType,
referrerInfo,
subAccountId: params.subAccountId,
}, txParams);
const { txSig } = await this.jitProxyClient.jit(
{
takerKey,
takerStatsKey,
taker,
takerOrderId: order.orderId,
maxPosition: params.maxPosition,
minPosition: params.minPosition,
bid: params.bid,
ask: params.ask,
postOnly: null,
priceType: params.priceType,
referrerInfo,
subAccountId: params.subAccountId,
},
txParams
);

console.log(`Filled ${orderSignature} txSig ${txSig}`);
await sleep(10000);
Expand Down
64 changes: 44 additions & 20 deletions ts/sdk/src/jitter/jitterSniper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DriftClient,
getAuctionPrice,
getAuctionPriceForOracleOffsetAuction,
getLimitPrice,
getVariant,
isVariant,
OraclePriceData,
Expand Down Expand Up @@ -191,21 +192,24 @@ export class JitterSniper extends BaseJitter {
const txParams = {
computeUnits: this.computeUnits,
computeUnitsPrice: this.computeUnitsPrice,
}
const { txSig } = await this.jitProxyClient.jit({
takerKey,
takerStatsKey,
taker,
takerOrderId: order.orderId,
maxPosition: params.maxPosition,
minPosition: params.minPosition,
bid: params.bid,
ask: params.ask,
postOnly: null,
priceType: params.priceType,
referrerInfo,
subAccountId: params.subAccountId,
}, txParams);
};
const { txSig } = await this.jitProxyClient.jit(
{
takerKey,
takerStatsKey,
taker,
takerOrderId: order.orderId,
maxPosition: params.maxPosition,
minPosition: params.minPosition,
bid: params.bid,
ask: params.ask,
postOnly: null,
priceType: params.priceType,
referrerInfo,
subAccountId: params.subAccountId,
},
txParams
);

console.log(`Filled ${orderSignature} txSig ${txSig}`);
await sleep(3000);
Expand Down Expand Up @@ -306,6 +310,25 @@ export class JitterSniper extends BaseJitter {
slotsTilCross++;
}

// if it doesnt cross during auction, check if limit price crosses
if (!willCross) {
const slotAfterAuction = order.slot.toNumber() + order.auctionDuration;
const limitPrice = getLimitPrice(order, oraclePrice, slotAfterAuction);
if (!limitPrice) {
willCross = true;
slotsTilCross = order.auctionDuration + 1;
} else {
const limitPriceNum = convertToNumber(limitPrice, PRICE_PRECISION);
if (makerOrderDir === 'buy' || limitPriceNum <= bid) {
willCross = true;
slotsTilCross = order.auctionDuration + 1;
} else if (makerOrderDir === 'sell' || limitPriceNum >= ask) {
willCross = true;
slotsTilCross = order.auctionDuration + 1;
}
}
}

return {
slotsTilCross,
willCross,
Expand All @@ -323,12 +346,12 @@ export class JitterSniper extends BaseJitter {
order: Order,
initialDetails: AuctionAndOrderDetails
): Promise<{ slot: number; updatedDetails: AuctionAndOrderDetails }> {
const auctionEndSlot = order.auctionDuration + order.slot.toNumber();
let currentDetails: AuctionAndOrderDetails = initialDetails;
let willCross = initialDetails.willCross;
if (this.slotSubscriber.currentSlot > auctionEndSlot) {
if (this.slotSubscriber.currentSlot > targetSlot) {
const slot = willCross ? this.slotSubscriber.currentSlot : -1;
return new Promise((resolve) =>
resolve({ slot: -1, updatedDetails: currentDetails })
resolve({ slot, updatedDetails: currentDetails })
);
}

Expand All @@ -346,13 +369,14 @@ export class JitterSniper extends BaseJitter {

// Update target slot as the bid/ask and the oracle changes
const intervalId = setInterval(async () => {
if (this.slotSubscriber.currentSlot >= auctionEndSlot) {
if (this.slotSubscriber.currentSlot >= targetSlot) {
this.slotSubscriber.eventEmitter.removeListener(
'newSlot',
slotListener
);
clearInterval(intervalId);
resolve({ slot: -1, updatedDetails: currentDetails });
const slot = willCross ? this.slotSubscriber.currentSlot : -1;
resolve({ slot, updatedDetails: currentDetails });
}

currentDetails = this.getAuctionAndOrderDetails(order);
Expand Down

0 comments on commit 7907285

Please sign in to comment.