Skip to content

Commit e6ad05b

Browse files
authored
Merge pull request #345 from drift-labs/master
mainnet
2 parents 870fb9c + 1a9e62d commit e6ad05b

File tree

9 files changed

+156
-120
lines changed

9 files changed

+156
-120
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
"main": "lib/index.js",
66
"license": "Apache-2.0",
77
"dependencies": {
8-
"@drift-labs/jit-proxy": "0.12.46",
9-
"@drift-labs/sdk": "2.107.0-beta.2",
8+
"@drift-labs/jit-proxy": "0.12.47",
9+
"@drift-labs/sdk": "2.107.0-beta.3",
1010
"@opentelemetry/api": "1.7.0",
1111
"@opentelemetry/auto-instrumentations-node": "0.31.2",
1212
"@opentelemetry/exporter-prometheus": "0.31.0",
1313
"@opentelemetry/sdk-node": "0.31.0",
1414
"@project-serum/anchor": "0.19.1-beta.1",
1515
"@project-serum/serum": "0.13.65",
1616
"@pythnetwork/price-service-client": "1.9.0",
17-
"@pythnetwork/pyth-lazer-sdk": "^0.1.1",
17+
"@pythnetwork/pyth-lazer-sdk": "0.2.0",
1818
"@solana/spl-token": "0.3.7",
1919
"@solana/web3.js": "1.92.3",
2020
"@types/bn.js": "5.1.5",

src/bots/liquidator.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -839,23 +839,12 @@ export class LiquidatorBot implements Bot {
839839

840840
private async jupiterSpotSwap(
841841
orderDirection: PositionDirection,
842-
spotMarketIndex: number,
842+
inMarketIndex: number,
843+
outMarketIndex: number,
843844
quote: QuoteResponse,
844845
slippageBps: number,
845846
subAccountId: number
846847
) {
847-
let outMarketIndex: number;
848-
let inMarketIndex: number;
849-
if (isVariant(orderDirection, 'long')) {
850-
// sell USDC, buy spotMarketIndex
851-
inMarketIndex = 0;
852-
outMarketIndex = spotMarketIndex;
853-
} else {
854-
// sell spotMarketIndex, buy USDC
855-
inMarketIndex = spotMarketIndex;
856-
outMarketIndex = 0;
857-
}
858-
859848
const swapIx = await this.driftClient.getJupiterSwapIxV6({
860849
jupiterClient: this.jupiterClient!,
861850
outMarketIndex,
@@ -893,7 +882,7 @@ export class LiquidatorBot implements Bot {
893882
logger.error(
894883
`Error trying to ${getVariant(
895884
orderDirection
896-
)} spot position for market ${spotMarketIndex} on jupiter, subaccount start ${subAccountId}, simError: ${JSON.stringify(
885+
)} on inMarketIndex ${inMarketIndex}, outMarketIndex ${outMarketIndex} on jupiter, subaccount start ${subAccountId}, simError: ${JSON.stringify(
897886
simResult.simError
898887
)}`
899888
);
@@ -905,9 +894,7 @@ export class LiquidatorBot implements Bot {
905894
);
906895

907896
logger.info(
908-
`closed spot position for market ${spotMarketIndex.toString()} on subaccount ${subAccountId}: ${
909-
resp.txSig
910-
} `
897+
`closed spot position inMarketIndex ${inMarketIndex}, outMarketIndex ${outMarketIndex} on subaccount ${subAccountId}: ${resp.txSig} `
911898
);
912899
}
913900
}
@@ -1234,7 +1221,10 @@ export class LiquidatorBot implements Bot {
12341221
orderDirection: PositionDirection,
12351222
baseAmountIn: BN,
12361223
slippageBps: number
1237-
): Promise<QuoteResponse | undefined> {
1224+
): Promise<
1225+
| { quote: QuoteResponse; inMarketIndex: number; outMarketIndex: number }
1226+
| undefined
1227+
> {
12381228
if (!this.jupiterClient) {
12391229
return undefined;
12401230
}
@@ -1347,7 +1337,11 @@ export class LiquidatorBot implements Bot {
13471337
);
13481338
return undefined;
13491339
} else {
1350-
return quote;
1340+
return {
1341+
quote,
1342+
inMarketIndex: inMarket.marketIndex,
1343+
outMarketIndex: outMarket.marketIndex,
1344+
};
13511345
}
13521346
} else {
13531347
// selling spotMarketIndex, want max out
@@ -1358,7 +1352,11 @@ export class LiquidatorBot implements Bot {
13581352
);
13591353
return undefined;
13601354
} else {
1361-
return quote;
1355+
return {
1356+
quote,
1357+
inMarketIndex: inMarket.marketIndex,
1358+
outMarketIndex: outMarket.marketIndex,
1359+
};
13621360
}
13631361
}
13641362
}
@@ -1610,8 +1608,9 @@ export class LiquidatorBot implements Bot {
16101608
} else {
16111609
await this.jupiterSpotSwap(
16121610
orderParams.direction,
1613-
position.marketIndex,
1614-
jupQuote,
1611+
jupQuote.inMarketIndex,
1612+
jupQuote.outMarketIndex,
1613+
jupQuote.quote,
16151614
slippageBps,
16161615
userAccount.subAccountId
16171616
);

src/bots/spotFiller.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import { selectMakers } from '../makerSelection';
9898
import { LRUCache } from 'lru-cache';
9999
import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes';
100100
import { PythPriceFeedSubscriber } from '../pythPriceFeedSubscriber';
101+
import { FallbackLiquiditySource } from '../experimental-bots/filler-common/types';
101102

102103
const THROTTLED_NODE_SIZE_TO_PRUNE = 10; // Size of throttled nodes to get to before pruning the map
103104
const FILL_ORDER_THROTTLE_BACKOFF = 1000; // the time to wait before trying to fill a throttled (error filling) node again
@@ -162,8 +163,6 @@ function getMakerNodeFromNodeToFill(
162163
return nodeToFill.makerNodes[0];
163164
}
164165

165-
export type FallbackLiquiditySource = 'phoenix' | 'openbook';
166-
167166
export type NodesToFillWithContext = {
168167
nodesToFill: NodeToFill[];
169168
fallbackAskSource?: FallbackLiquiditySource;
@@ -1760,6 +1759,7 @@ export class SpotFillerBot implements Bot {
17601759
'multiMakerSpotFill',
17611760
this.revertOnFailure ?? false,
17621761
false,
1762+
undefined,
17631763
spotPrecision
17641764
);
17651765

@@ -1965,7 +1965,7 @@ export class SpotFillerBot implements Bot {
19651965
)!;
19661966
const spotMarketPrecision = TEN.pow(new BN(spotMarket.decimals));
19671967

1968-
if (nodeToFill.makerNodes.length > 1) {
1968+
if (nodeToFill.makerNodes.length > 0) {
19691969
// do multi maker fills in a separate tx since they're larger
19701970
return this.tryFillMultiMakerSpotNode(
19711971
fillTxId,
@@ -1979,55 +1979,48 @@ export class SpotFillerBot implements Bot {
19791979
? fallbackBidSource
19801980
: fallbackAskSource;
19811981

1982-
const {
1983-
makerInfos,
1984-
takerUser,
1985-
takerUserPubKey,
1986-
takerUserSlot,
1987-
marketType,
1988-
} = await this.getNodeFillInfo(nodeToFill);
1982+
const { takerUser, takerUserPubKey, takerUserSlot, marketType } =
1983+
await this.getNodeFillInfo(nodeToFill);
19891984

19901985
if (!isVariant(marketType, 'spot')) {
19911986
throw new Error('expected spot market type');
19921987
}
19931988

1994-
const makerInfo = makerInfos.length > 0 ? makerInfos[0].data : undefined;
19951989
let fulfillmentConfig:
19961990
| PhoenixV1FulfillmentConfigAccount
19971991
| OpenbookV2FulfillmentConfigAccount
19981992
| undefined = undefined;
1999-
if (makerInfo === undefined) {
2000-
if (fallbackSource === 'phoenix') {
2001-
const cfg = this.phoenixFulfillmentConfigMap.get(
2002-
nodeToFill.node.order!.marketIndex
2003-
);
2004-
if (cfg && isVariant(cfg.status, 'enabled')) {
2005-
fulfillmentConfig = cfg;
2006-
}
2007-
} else if (fallbackSource === 'openbook') {
2008-
const cfg = this.openbookFulfillmentConfigMap.get(
2009-
nodeToFill.node.order!.marketIndex
2010-
);
2011-
if (cfg && isVariant(cfg.status, 'enabled')) {
2012-
fulfillmentConfig = cfg;
2013-
}
2014-
} else {
2015-
logger.error(
2016-
`makerInfo doesnt exist and unknown fallback source: ${fallbackSource} (fillTxId: ${fillTxId})`
2017-
);
1993+
if (fallbackSource === 'phoenix') {
1994+
const cfg = this.phoenixFulfillmentConfigMap.get(
1995+
nodeToFill.node.order!.marketIndex
1996+
);
1997+
if (cfg && isVariant(cfg.status, 'enabled')) {
1998+
fulfillmentConfig = cfg;
1999+
}
2000+
} else if (fallbackSource === 'openbook') {
2001+
const cfg = this.openbookFulfillmentConfigMap.get(
2002+
nodeToFill.node.order!.marketIndex
2003+
);
2004+
if (cfg && isVariant(cfg.status, 'enabled')) {
2005+
fulfillmentConfig = cfg;
20182006
}
2007+
} else {
2008+
logger.error(
2009+
`unknown fallback source: ${fallbackSource} (fillTxId: ${fillTxId})`
2010+
);
20192011
}
20202012

20212013
logMessageForNodeToFill(
20222014
nodeToFill,
20232015
takerUserPubKey,
20242016
takerUserSlot,
2025-
makerInfos,
2017+
[],
20262018
this.getMaxSlot(),
20272019
fillTxId,
20282020
'fillSpotNode',
20292021
this.revertOnFailure ?? false,
20302022
false,
2023+
fallbackSource,
20312024
spotMarketPrecision
20322025
);
20332026

@@ -2055,8 +2048,7 @@ export class SpotFillerBot implements Bot {
20552048
new PublicKey(takerUserPubKey),
20562049
takerUser,
20572050
nodeToFill.node.order,
2058-
fulfillmentConfig,
2059-
makerInfo
2051+
fulfillmentConfig
20602052
)
20612053
);
20622054

src/experimental-bots/filler-common/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export type SerializedDLOBNode = {
128128
isUserProtectedMaker: boolean;
129129
};
130130

131-
export type FallbackLiquiditySource = 'serum' | 'phoenix' | 'openbook';
131+
export type FallbackLiquiditySource = 'phoenix' | 'openbook';
132132
export type NodeToFillWithContext = NodeToFill & {
133133
fallbackAskSource?: FallbackLiquiditySource;
134134
fallbackBidSource?: FallbackLiquiditySource;

src/experimental-bots/filler/fillerMultithreaded.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ import { ChildProcess } from 'child_process';
111111
import { PythPriceFeedSubscriber } from 'src/pythPriceFeedSubscriber';
112112
import { PythLazerSubscriber } from '../../pythLazerSubscriber';
113113
import path from 'path';
114+
import { SignedSwiftOrderParams } from '@drift-labs/sdk/lib/node/swift/types';
114115

115116
const logPrefix = '[Filler]';
116117
export type MakerNodeMap = Map<string, DLOBNode[]>;
@@ -1768,10 +1769,16 @@ export class FillerMultithreaded {
17681769
const swiftOrderMessageParams = this.swiftOrderMessages.get(
17691770
nodeToFill.node.order!.orderId
17701771
);
1772+
const signedSwiftOrderParams: SignedSwiftOrderParams = {
1773+
orderParams: Buffer.from(swiftOrderMessageParams['order_message']),
1774+
signature: Buffer.from(
1775+
swiftOrderMessageParams['order_signature'],
1776+
'base64'
1777+
),
1778+
};
17711779
ixs.push(
17721780
...(await this.driftClient.getPlaceSwiftTakerPerpOrderIxs(
1773-
Buffer.from(swiftOrderMessageParams['order_message'], 'base64'),
1774-
Buffer.from(swiftOrderMessageParams['order_signature'], 'base64'),
1781+
signedSwiftOrderParams,
17751782
nodeToFill.node.order!.marketIndex,
17761783
{
17771784
taker: new PublicKey(takerUserPubKey),
@@ -2063,10 +2070,16 @@ export class FillerMultithreaded {
20632070
const swiftOrderMessageParams = this.swiftOrderMessages.get(
20642071
nodeToFill.node.order!.orderId
20652072
);
2073+
const signedSwiftOrderMessageParams: SignedSwiftOrderParams = {
2074+
orderParams: Buffer.from(swiftOrderMessageParams['order_message']),
2075+
signature: Buffer.from(
2076+
swiftOrderMessageParams['order_signature'],
2077+
'base64'
2078+
),
2079+
};
20662080
ixs.push(
20672081
...(await this.driftClient.getPlaceSwiftTakerPerpOrderIxs(
2068-
Buffer.from(swiftOrderMessageParams['order_message'], 'base64'),
2069-
Buffer.from(swiftOrderMessageParams['order_signature'], 'base64'),
2082+
signedSwiftOrderMessageParams,
20702083
nodeToFill.node.order!.marketIndex,
20712084
{
20722085
taker: new PublicKey(takerUserPubKey),

src/experimental-bots/spotFiller/spotFillerMultithreaded.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ export class SpotFillerMultithreaded {
907907
continue;
908908
}
909909
this.seenFillableOrders.add(getNodeToFillSignature(node));
910-
if (node.makerNodes.length > 1) {
910+
if (node.makerNodes.length > 0) {
911911
this.tryFillMultiMakerSpotNodes(node, !!buildForBundle);
912912
} else {
913913
this.tryFillSpotNode(node, !!buildForBundle);
@@ -985,6 +985,7 @@ export class SpotFillerMultithreaded {
985985
'multiMakerSpotFill',
986986
this.revertOnFailure ?? false,
987987
false,
988+
undefined,
988989
spotPrecision
989990
);
990991

@@ -1182,13 +1183,8 @@ export class SpotFillerMultithreaded {
11821183
)!;
11831184
const spotMarketPrecision = TEN.pow(new BN(spotMarket.decimals));
11841185

1185-
const {
1186-
makerInfos,
1187-
takerUser,
1188-
takerUserPubKey,
1189-
takerUserSlot,
1190-
marketType,
1191-
} = await this.getNodeFillInfo(nodeToFill);
1186+
const { takerUser, takerUserPubKey, takerUserSlot, marketType } =
1187+
await this.getNodeFillInfo(nodeToFill);
11921188

11931189
if (!isVariant(marketType, 'spot')) {
11941190
throw new Error('expected spot market type');
@@ -1198,43 +1194,41 @@ export class SpotFillerMultithreaded {
11981194
? nodeToFill.fallbackBidSource
11991195
: nodeToFill.fallbackAskSource;
12001196

1201-
const makerInfo = makerInfos.length > 0 ? makerInfos[0].data : undefined;
12021197
let fulfillmentConfig:
12031198
| PhoenixV1FulfillmentConfigAccount
12041199
| OpenbookV2FulfillmentConfigAccount
12051200
| undefined = undefined;
1206-
if (makerInfo === undefined) {
1207-
if (fallbackSource === 'phoenix') {
1208-
const cfg = this.phoenixFulfillmentConfigMap.get(
1209-
nodeToFill.node.order!.marketIndex
1210-
);
1211-
if (cfg && isVariant(cfg.status, 'enabled')) {
1212-
fulfillmentConfig = cfg;
1213-
}
1214-
} else if (fallbackSource === 'openbook') {
1215-
const cfg = this.openbookFulfillmentConfigMap.get(
1216-
nodeToFill.node.order!.marketIndex
1217-
);
1218-
if (cfg && isVariant(cfg.status, 'enabled')) {
1219-
fulfillmentConfig = cfg;
1220-
}
1221-
} else {
1222-
logger.error(
1223-
`makerInfo doesnt exist and unknown fallback source: ${fallbackSource} (fillTxId: ${fillTxId})`
1224-
);
1201+
if (fallbackSource === 'phoenix') {
1202+
const cfg = this.phoenixFulfillmentConfigMap.get(
1203+
nodeToFill.node.order!.marketIndex
1204+
);
1205+
if (cfg && isVariant(cfg.status, 'enabled')) {
1206+
fulfillmentConfig = cfg;
1207+
}
1208+
} else if (fallbackSource === 'openbook') {
1209+
const cfg = this.openbookFulfillmentConfigMap.get(
1210+
nodeToFill.node.order!.marketIndex
1211+
);
1212+
if (cfg && isVariant(cfg.status, 'enabled')) {
1213+
fulfillmentConfig = cfg;
12251214
}
1215+
} else {
1216+
logger.error(
1217+
`makerInfo doesnt exist and unknown fallback source: ${fallbackSource} (fillTxId: ${fillTxId})`
1218+
);
12261219
}
12271220

12281221
logMessageForNodeToFill(
12291222
nodeToFill,
12301223
takerUserPubKey,
12311224
takerUserSlot,
1232-
makerInfos,
1225+
[],
12331226
this.slotSubscriber.getSlot(),
12341227
fillTxId,
12351228
'fillSpotNode',
12361229
this.revertOnFailure ?? false,
12371230
false,
1231+
fallbackSource,
12381232
spotMarketPrecision
12391233
);
12401234

@@ -1267,7 +1261,7 @@ export class SpotFillerMultithreaded {
12671261
takerUser,
12681262
nodeToFill.node.order,
12691263
fulfillmentConfig,
1270-
makerInfo,
1264+
undefined,
12711265
undefined,
12721266
user.userAccountPublicKey
12731267
)

0 commit comments

Comments
 (0)