Skip to content

Commit ab88965

Browse files
authored
add additional end price buffer param (#147)
* add additional end price buffer param * apply additional buffer even if max slippage reached * add buffer to limitPrice in deriveMarketOrderParams
1 parent a285bca commit ab88965

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

common-ts/src/common-ui-utils/commonUiUtils.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ const getMarketAuctionParams = ({
349349
duration,
350350
auctionStartPriceOffset,
351351
auctionEndPriceOffset,
352+
additionalEndPriceBuffer,
352353
}: {
353354
direction: PositionDirection;
354355
startPriceFromSettings: BN;
@@ -357,13 +358,14 @@ const getMarketAuctionParams = ({
357358
duration: number;
358359
auctionStartPriceOffset: number;
359360
auctionEndPriceOffset: number;
361+
additionalEndPriceBuffer?: BN;
360362
}): AuctionParams => {
361363
let auctionStartPrice: BN;
362364
let auctionEndPrice: BN;
363365
let constrainedBySlippage: boolean;
364366

365367
const auctionEndPriceBuffer = BigNum.from(PRICE_PRECISION).scale(
366-
auctionEndPriceOffset * 100,
368+
Math.abs(auctionEndPriceOffset * 100),
367369
10000
368370
).val;
369371

@@ -389,6 +391,12 @@ const getMarketAuctionParams = ({
389391
// use BEST (limit price, auction end price) as end price
390392
auctionEndPrice = BN.min(limitPrice, auctionEndPrice);
391393

394+
// apply additional buffer if provided
395+
if (additionalEndPriceBuffer) {
396+
auctionEndPrice = auctionEndPrice.add(additionalEndPriceBuffer);
397+
constrainedBySlippage = limitPrice.lt(auctionEndPrice);
398+
}
399+
392400
auctionStartPrice = BN.min(auctionStartPrice, auctionEndPrice);
393401
} else {
394402
auctionStartPrice = startPriceFromSettings.add(auctionStartPriceBuffer);
@@ -407,6 +415,12 @@ const getMarketAuctionParams = ({
407415
// use BEST (limit price, auction end price) as end price
408416
auctionEndPrice = BN.max(limitPrice, auctionEndPrice);
409417

418+
// apply additional buffer if provided
419+
if (additionalEndPriceBuffer) {
420+
auctionEndPrice = auctionEndPrice.sub(additionalEndPriceBuffer);
421+
constrainedBySlippage = limitPrice.gt(auctionEndPrice);
422+
}
423+
410424
auctionStartPrice = BN.max(auctionStartPrice, auctionEndPrice);
411425
}
412426

@@ -445,6 +459,7 @@ const deriveMarketOrderParams = ({
445459
auctionPriceCaps,
446460
slippageTolerance,
447461
isOracleOrder,
462+
additionalEndPriceBuffer,
448463
}: {
449464
marketType: MarketType;
450465
marketIndex: number;
@@ -470,6 +485,7 @@ const deriveMarketOrderParams = ({
470485
auctionEndPriceOffsetFrom: TradeOffsetPrice;
471486
slippageTolerance: number;
472487
isOracleOrder?: boolean;
488+
additionalEndPriceBuffer?: BN;
473489
}): OptionalOrderParams & { constrainedBySlippage?: boolean } => {
474490
const priceObject = getPriceObject({
475491
oraclePrice,
@@ -481,12 +497,18 @@ const deriveMarketOrderParams = ({
481497
});
482498

483499
// max slippage price
484-
const limitPrice = getMarketOrderLimitPrice({
500+
let limitPrice = getMarketOrderLimitPrice({
485501
direction,
486502
baselinePrice: priceObject[auctionStartPriceOffsetFrom],
487503
slippageTolerance: allowInfSlippage ? undefined : slippageTolerance,
488504
});
489505

506+
if (additionalEndPriceBuffer) {
507+
limitPrice = isVariant(direction, 'long')
508+
? limitPrice.add(additionalEndPriceBuffer)
509+
: limitPrice.sub(additionalEndPriceBuffer);
510+
}
511+
490512
const auctionParams = getMarketAuctionParams({
491513
direction,
492514
startPriceFromSettings: priceObject[auctionStartPriceOffsetFrom],
@@ -495,6 +517,7 @@ const deriveMarketOrderParams = ({
495517
duration: auctionDuration,
496518
auctionStartPriceOffset: auctionStartPriceOffset,
497519
auctionEndPriceOffset: auctionEndPriceOffset,
520+
additionalEndPriceBuffer,
498521
});
499522

500523
let orderParams = getMarketOrderParams({

common-ts/tests/utils/orders.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,40 @@ describe('COMMON_UI_UTILS OrderParams Tests', () => {
377377
//@ts-ignore
378378
expect(result.constrainedBySlippage).to.equal(false);
379379
});
380+
381+
it('should correctly generate params for LONG non-oracle market order with auction end price NOT capped by slippage AND an additional offset', () => {
382+
const result = COMMON_UI_UTILS.deriveMarketOrderParams({
383+
marketType: MarketType.PERP,
384+
marketIndex: 0,
385+
direction: PositionDirection.LONG,
386+
maxLeverageSelected: false,
387+
maxLeverageOrderSize: new BN(1000).mul(BASE_PRECISION),
388+
baseAmount: new BN(1).mul(BASE_PRECISION),
389+
reduceOnly: false,
390+
allowInfSlippage: true,
391+
bestPrice: new BN(99).mul(PRICE_PRECISION),
392+
entryPrice: new BN(101).mul(PRICE_PRECISION),
393+
oraclePrice: new BN(104).mul(PRICE_PRECISION),
394+
worstPrice: new BN(108).mul(PRICE_PRECISION),
395+
markPrice: new BN(100).mul(PRICE_PRECISION),
396+
auctionDuration: 20,
397+
auctionStartPriceOffset: 0,
398+
auctionEndPriceOffset: 0,
399+
auctionStartPriceOffsetFrom: 'mark',
400+
auctionEndPriceOffsetFrom: 'worst',
401+
slippageTolerance: 50,
402+
isOracleOrder: false,
403+
additionalEndPriceBuffer: new BN(100),
404+
});
405+
406+
expect(ENUM_UTILS.toStr(result.orderType)).to.equal(
407+
ENUM_UTILS.toStr(OrderType.MARKET)
408+
);
409+
expect(result.auctionStartPrice?.toString()).to.equal('100000000');
410+
expect(result.auctionEndPrice?.toString()).to.equal('108000100');
411+
//@ts-ignore
412+
expect(result.constrainedBySlippage).to.equal(false);
413+
});
380414
});
381415
describe('getMarketOrderLimitPrice', () => {
382416
it('should use the correct price for a LONG with 5% max slippage', () => {

0 commit comments

Comments
 (0)