Merge pull request #426 from drift-labs/master
indicative quotes v2 (#420)
This commit is contained in:
@@ -145,98 +145,101 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
|
|||||||
const mms = await this.indicativeQuotesRedisClient.smembers(
|
const mms = await this.indicativeQuotesRedisClient.smembers(
|
||||||
`market_mms_${marketType}_${marketArgs.marketIndex}`
|
`market_mms_${marketType}_${marketArgs.marketIndex}`
|
||||||
);
|
);
|
||||||
const mmQuotes = await Promise.all(
|
let mmQuotes: any = await Promise.all(
|
||||||
mms.map((mm) => {
|
mms.map((mm) => {
|
||||||
return this.indicativeQuotesRedisClient.get(
|
return this.indicativeQuotesRedisClient.get(
|
||||||
`mm_quotes_${marketType}_${marketArgs.marketIndex}_${mm}`
|
`mm_quotes_v2_${marketType}_${marketArgs.marketIndex}_${mm}`
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
mmQuotes = mmQuotes.filter((x) => !!x);
|
||||||
|
|
||||||
const nowMinus1000Ms = Date.now() - 1000;
|
const nowMinus1000Ms = Date.now() - 1000;
|
||||||
for (const quote of mmQuotes) {
|
for (const quotes of mmQuotes) {
|
||||||
try {
|
try {
|
||||||
if (Number(quote['ts']) > nowMinus1000Ms) {
|
if (Number(quotes['ts']) > nowMinus1000Ms) {
|
||||||
const indicativeBaseOrder: Order = {
|
for (const quote of quotes['quotes']) {
|
||||||
status: OrderStatus.OPEN,
|
const indicativeBaseOrder: Order = {
|
||||||
orderType: OrderType.LIMIT,
|
status: OrderStatus.OPEN,
|
||||||
orderId: 0,
|
orderType: OrderType.LIMIT,
|
||||||
slot: new BN(this.slotSource.getSlot()),
|
orderId: 0,
|
||||||
marketIndex: marketArgs.marketIndex,
|
slot: new BN(this.slotSource.getSlot()),
|
||||||
marketType: marketArgs.marketType,
|
marketIndex: marketArgs.marketIndex,
|
||||||
baseAssetAmount: ZERO,
|
marketType: marketArgs.marketType,
|
||||||
immediateOrCancel: false,
|
baseAssetAmount: ZERO,
|
||||||
direction: PositionDirection.LONG,
|
immediateOrCancel: false,
|
||||||
oraclePriceOffset: 0,
|
direction: PositionDirection.LONG,
|
||||||
maxTs: new BN(quote['ts'] + 1000),
|
oraclePriceOffset: 0,
|
||||||
reduceOnly: false,
|
maxTs: new BN(quote['ts'] + 1000),
|
||||||
triggerCondition: OrderTriggerCondition.ABOVE,
|
reduceOnly: false,
|
||||||
price: ZERO,
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
||||||
userOrderId: 0,
|
price: ZERO,
|
||||||
postOnly: true,
|
userOrderId: 0,
|
||||||
auctionDuration: 0,
|
postOnly: true,
|
||||||
auctionStartPrice: ZERO,
|
auctionDuration: 0,
|
||||||
auctionEndPrice: ZERO,
|
auctionStartPrice: ZERO,
|
||||||
// Rest are not necessary and set for type conforming
|
auctionEndPrice: ZERO,
|
||||||
existingPositionDirection: PositionDirection.LONG,
|
// Rest are not necessary and set for type conforming
|
||||||
triggerPrice: ZERO,
|
existingPositionDirection: PositionDirection.LONG,
|
||||||
baseAssetAmountFilled: ZERO,
|
triggerPrice: ZERO,
|
||||||
quoteAssetAmountFilled: ZERO,
|
baseAssetAmountFilled: ZERO,
|
||||||
quoteAssetAmount: ZERO,
|
quoteAssetAmountFilled: ZERO,
|
||||||
bitFlags: 0,
|
quoteAssetAmount: ZERO,
|
||||||
postedSlotTail: 0,
|
bitFlags: 0,
|
||||||
};
|
postedSlotTail: 0,
|
||||||
|
};
|
||||||
|
|
||||||
if (quote['bid_size'] && quote['bid_price'] != null) {
|
if (quote['bid_size'] && quote['bid_price'] != null) {
|
||||||
// Sanity check bid price and size
|
// Sanity check bid price and size
|
||||||
|
|
||||||
const indicativeBid: Order = Object.assign(
|
const indicativeBid: Order = Object.assign(
|
||||||
{},
|
{},
|
||||||
indicativeBaseOrder,
|
indicativeBaseOrder,
|
||||||
{
|
{
|
||||||
orderId: indicativeOrderId,
|
orderId: indicativeOrderId,
|
||||||
oraclePriceOffset: quote['is_oracle_offset']
|
oraclePriceOffset: quote['is_oracle_offset']
|
||||||
? quote['bid_price']
|
? quote['bid_price']
|
||||||
: 0,
|
: 0,
|
||||||
price: quote['is_oracle_offset']
|
price: quote['is_oracle_offset']
|
||||||
? 0
|
? 0
|
||||||
: new BN(quote['bid_price']),
|
: new BN(quote['bid_price']),
|
||||||
baseAssetAmount: new BN(quote['bid_size']),
|
baseAssetAmount: new BN(quote['bid_size']),
|
||||||
direction: PositionDirection.LONG,
|
direction: PositionDirection.LONG,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.dlob.insertOrder(
|
this.dlob.insertOrder(
|
||||||
indicativeBid,
|
indicativeBid,
|
||||||
INDICATIVE_QUOTES_PUBKEY,
|
INDICATIVE_QUOTES_PUBKEY,
|
||||||
this.slotSource.getSlot(),
|
this.slotSource.getSlot(),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
indicativeOrderId += 1;
|
indicativeOrderId += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (quote['ask_size'] && quote['ask_price'] != null) {
|
if (quote['ask_size'] && quote['ask_price'] != null) {
|
||||||
const indicativeAsk: Order = Object.assign(
|
const indicativeAsk: Order = Object.assign(
|
||||||
{},
|
{},
|
||||||
indicativeBaseOrder,
|
indicativeBaseOrder,
|
||||||
{
|
{
|
||||||
orderId: indicativeOrderId,
|
orderId: indicativeOrderId,
|
||||||
oraclePriceOffset: quote['is_oracle_offset']
|
oraclePriceOffset: quote['is_oracle_offset']
|
||||||
? quote['ask_price']
|
? quote['ask_price']
|
||||||
: 0,
|
: 0,
|
||||||
price: quote['is_oracle_offset']
|
price: quote['is_oracle_offset']
|
||||||
? 0
|
? 0
|
||||||
: new BN(quote['ask_price']),
|
: new BN(quote['ask_price']),
|
||||||
baseAssetAmount: new BN(quote['ask_size']),
|
baseAssetAmount: new BN(quote['ask_size']),
|
||||||
direction: PositionDirection.SHORT,
|
direction: PositionDirection.SHORT,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.dlob.insertOrder(
|
this.dlob.insertOrder(
|
||||||
indicativeAsk,
|
indicativeAsk,
|
||||||
INDICATIVE_QUOTES_PUBKEY,
|
INDICATIVE_QUOTES_PUBKEY,
|
||||||
this.slotSource.getSlot(),
|
this.slotSource.getSlot(),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
indicativeOrderId += 1;
|
indicativeOrderId += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
65
src/index.ts
65
src/index.ts
@@ -786,14 +786,20 @@ const main = async (): Promise<void> => {
|
|||||||
try {
|
try {
|
||||||
// Check origin validation
|
// Check origin validation
|
||||||
const origin = req.get('Origin') || req.get('Referer');
|
const origin = req.get('Origin') || req.get('Referer');
|
||||||
const allowedOrigins = ['https://app.drift.trade', 'https://beta.drift.trade'];
|
const allowedOrigins = [
|
||||||
|
'https://app.drift.trade',
|
||||||
const hasAuth = (
|
'https://beta.drift.trade',
|
||||||
(req.headers.Authorization || req.headers.authorization) ===
|
];
|
||||||
process.env.INTERNAL_SECRET
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!hasAuth && (!origin || !allowedOrigins.some(allowed => origin.startsWith(allowed)))) {
|
const hasAuth =
|
||||||
|
(req.headers.Authorization || req.headers.authorization) ===
|
||||||
|
process.env.INTERNAL_SECRET;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!hasAuth &&
|
||||||
|
(!origin ||
|
||||||
|
!allowedOrigins.some((allowed) => origin.startsWith(allowed)))
|
||||||
|
) {
|
||||||
res.status(403).json({ error: 'Forbidden: Invalid origin' });
|
res.status(403).json({ error: 'Forbidden: Invalid origin' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -815,23 +821,20 @@ const main = async (): Promise<void> => {
|
|||||||
channel: 'real_time',
|
channel: 'real_time',
|
||||||
jsonBinaryEncoding: 'hex',
|
jsonBinaryEncoding: 'hex',
|
||||||
};
|
};
|
||||||
|
|
||||||
const latestPriceRes = await fetch(
|
const latestPriceRes = await fetch(pythLazerEndpoint, {
|
||||||
pythLazerEndpoint,
|
method: 'POST',
|
||||||
{
|
headers: {
|
||||||
method: 'POST',
|
Authorization: `Bearer ${pythLazerDriftToken}`,
|
||||||
headers: {
|
},
|
||||||
Authorization: `Bearer ${pythLazerDriftToken}`,
|
body: JSON.stringify(latestLazerPricePayload),
|
||||||
},
|
}).then((res) => res.json());
|
||||||
body: JSON.stringify(latestLazerPricePayload),
|
|
||||||
}
|
|
||||||
).then((res) => res.json());
|
|
||||||
|
|
||||||
const data = latestPriceRes.solana.data;
|
const data = latestPriceRes.solana.data;
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
data
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -848,14 +851,20 @@ const main = async (): Promise<void> => {
|
|||||||
try {
|
try {
|
||||||
// Check origin validation
|
// Check origin validation
|
||||||
const origin = req.get('Origin') || req.get('Referer');
|
const origin = req.get('Origin') || req.get('Referer');
|
||||||
const allowedOrigins = ['https://app.drift.trade', 'https://beta.drift.trade'];
|
const allowedOrigins = [
|
||||||
|
'https://app.drift.trade',
|
||||||
const hasAuth = (
|
'https://beta.drift.trade',
|
||||||
|
];
|
||||||
|
|
||||||
|
const hasAuth =
|
||||||
(req.headers.Authorization || req.headers.authorization) ===
|
(req.headers.Authorization || req.headers.authorization) ===
|
||||||
process.env.INTERNAL_SECRET
|
process.env.INTERNAL_SECRET;
|
||||||
)
|
|
||||||
|
if (
|
||||||
if (!hasAuth && (!origin || !allowedOrigins.some(allowed => origin.startsWith(allowed)))) {
|
!hasAuth &&
|
||||||
|
(!origin ||
|
||||||
|
!allowedOrigins.some((allowed) => origin.startsWith(allowed)))
|
||||||
|
) {
|
||||||
res.status(403).json({ error: 'Forbidden: Invalid origin' });
|
res.status(403).json({ error: 'Forbidden: Invalid origin' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -881,7 +890,7 @@ const main = async (): Promise<void> => {
|
|||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
data
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user