-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release alpha #319
base: beta
Are you sure you want to change the base?
Release alpha #319
Changes from 4 commits
51d31a2
f71e556
808e858
3d4bf20
a43e373
f747654
79e5916
3510957
6759dbf
231945e
4e967cd
d61cee5
60248ce
a3b3617
f032024
ebee6bc
b10abf4
d3c7234
ce899d1
667a3a8
8bb0cc5
3e6f7b5
ba78b5f
114c9b3
a5976fe
d48470f
fe63ae3
0195bba
4991206
02f35e5
9eb671f
3957057
3357f19
de10932
c11d8e3
46a5f14
bdff536
9b098fa
a789721
850e420
6f7e5de
761a306
cf45c83
90b0406
25f9f7e
cd7ab72
9c66abf
44814e0
07f96bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
import { BigNumber } from 'bignumber.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
import * as pipmath from '#pipmath'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||||||||||
ROUNDING, | ||||||||||||||||||||||||||||||||||||||||||||||||||
absBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
arraySumBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
decimalToPip, | ||||||||||||||||||||||||||||||||||||||||||||||||||
divideBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
maxBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
minBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
multiplyPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
oneInPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
} from '#pipmath'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
import { OrderSide } from '#types/enums/request'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -9,6 +19,19 @@ import type { | |||||||||||||||||||||||||||||||||||||||||||||||||
OrderBookLevelL1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
OrderBookLevelL2, | ||||||||||||||||||||||||||||||||||||||||||||||||||
} from '#types/orderBook'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import type { IDEXMarket, IDEXPosition } from '#types/rest/endpoints/index'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
type LeverageParameters = Pick< | ||||||||||||||||||||||||||||||||||||||||||||||||||
IDEXMarket, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'maximumPositionSize' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'initialMarginFraction' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'maintenanceMarginFraction' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'basePositionSize' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'incrementalPositionSize' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'incrementalInitialMarginFraction' | ||||||||||||||||||||||||||||||||||||||||||||||||||
>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
export type LeverageParametersBigInt = Record<keyof LeverageParameters, bigint>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Price and Size values form the {@link OrderBookLevelL1} type | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -27,37 +50,274 @@ export const nullLevel: OrderBookLevelL2 = { | |||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Helper function to convert from quote to base quantities | ||||||||||||||||||||||||||||||||||||||||||||||||||
* see: {quantitiesAvailableFromPoolAtAskPrice} | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Determines the liquidity (expressed in the market's base asset) that can be | ||||||||||||||||||||||||||||||||||||||||||||||||||
* taken off the given order book (asks or bids) by spending the specified | ||||||||||||||||||||||||||||||||||||||||||||||||||
* fraction of a wallet's available collateral and taking into account the | ||||||||||||||||||||||||||||||||||||||||||||||||||
* margin requirement for the newly acquired position balance. | ||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Also returns the cost basis of the newly acquired position balance (i.e. the | ||||||||||||||||||||||||||||||||||||||||||||||||||
* quote quantity that is exchanged to acquire the position balance). | ||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Both values are signed (positive for buys, negative for sells). | ||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* The provided list of orders or price levels (asks or bids) is expected to be | ||||||||||||||||||||||||||||||||||||||||||||||||||
* sorted by best price (ascending for asks (lowest first), descending for bids | ||||||||||||||||||||||||||||||||||||||||||||||||||
* (highest first)). Multiple orders per price level are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
export function calculateGrossBaseValueOfBuyQuantities( | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseAssetQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteAssetQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
grossQuoteQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
): bigint { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseAssetQuantity - | ||||||||||||||||||||||||||||||||||||||||||||||||||
(baseAssetQuantity * quoteAssetQuantity) / | ||||||||||||||||||||||||||||||||||||||||||||||||||
(quoteAssetQuantity + grossQuoteQuantity) | ||||||||||||||||||||||||||||||||||||||||||||||||||
export function calculateBuySellPanelEstimate(args: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
/** All the wallet's open positions, including any in the current market */ | ||||||||||||||||||||||||||||||||||||||||||||||||||
allWalletPositions: IDEXPosition[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
/** Free collateral committed to open limit orders (unsigned) */ | ||||||||||||||||||||||||||||||||||||||||||||||||||
heldCollateral: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFractionOverride: bigint | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters: LeverageParameters; | ||||||||||||||||||||||||||||||||||||||||||||||||||
makerSideOrders: Iterable<PriceAndSize>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
market: Pick<IDEXMarket, 'market' | 'indexPrice'>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
/** Quote token balance (USDC) (signed) */ | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteBalance: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Floating point number between 0 and 1 that indicates the amount of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
* available collateral to be spent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
sliderFactor: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||
takerSide: OrderSide; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}): { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||||||||||||||||||
allWalletPositions, | ||||||||||||||||||||||||||||||||||||||||||||||||||
market, | ||||||||||||||||||||||||||||||||||||||||||||||||||
heldCollateral, | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFractionOverride, | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||
makerSideOrders, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteBalance, | ||||||||||||||||||||||||||||||||||||||||||||||||||
sliderFactor, | ||||||||||||||||||||||||||||||||||||||||||||||||||
takerSide, | ||||||||||||||||||||||||||||||||||||||||||||||||||
} = args; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Slider calculations | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
const accountValue = | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteBalance + calculateNotionalQuoteValueOfPositions(allWalletPositions); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const initialMarginRequirementOfAllPositions = arraySumBigInt( | ||||||||||||||||||||||||||||||||||||||||||||||||||
allWalletPositions.map((position) => | ||||||||||||||||||||||||||||||||||||||||||||||||||
decimalToPip(position.marginRequirement), | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const initialAvailableCollateral = | ||||||||||||||||||||||||||||||||||||||||||||||||||
accountValue - initialMarginRequirementOfAllPositions - heldCollateral; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if (initialAvailableCollateral <= BigInt(0) || sliderFactor === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: BigInt(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: BigInt(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (sliderFactor < 0 || sliderFactor > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
throw new Error( | ||||||||||||||||||||||||||||||||||||||||||||||||||
'sliderFactor must be a floating point number between 0 and 1', | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
const sliderFactorInPips = decimalToPip(sliderFactor.toString()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const remainingAvailableCollateral = | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialAvailableCollateral * (oneInPips - sliderFactorInPips); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Execute against order book | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
const indexPrice = decimalToPip(market.indexPrice); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const indexPrice2p = indexPrice * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const heldCollateral2p = heldCollateral * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const currentPosition = allWalletPositions.find( | ||||||||||||||||||||||||||||||||||||||||||||||||||
(position) => position.market === market.market, | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const otherPositions = allWalletPositions.filter( | ||||||||||||||||||||||||||||||||||||||||||||||||||
(position) => position.market !== market.market, | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const quoteValueOfOtherPositions = | ||||||||||||||||||||||||||||||||||||||||||||||||||
calculateNotionalQuoteValueOfPositions(otherPositions); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const quoteValueOfOtherPositions2p = quoteValueOfOtherPositions * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const initialMarginRequirementOfOtherPositions = arraySumBigInt( | ||||||||||||||||||||||||||||||||||||||||||||||||||
otherPositions.map((position) => decimalToPip(position.marginRequirement)), | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const initialMarginRequirementOfOtherPositions2p = | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginRequirementOfOtherPositions * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const leverageParametersBigInt = | ||||||||||||||||||||||||||||||||||||||||||||||||||
convertToLeverageParametersBigInt(leverageParameters); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
let additionalPositionQty = BigInt(0); // Signed | ||||||||||||||||||||||||||||||||||||||||||||||||||
let additionalPositionCostBasis = BigInt(0); // Signed | ||||||||||||||||||||||||||||||||||||||||||||||||||
let quoteBalance2p = quoteBalance * oneInPips; // Signed | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
for (const makerOrder of makerSideOrders) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (!doOrdersMatch(makerOrder, { side: takerSide })) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: additionalPositionQty, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: additionalPositionCostBasis, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
takerSide === 'buy' ? | ||||||||||||||||||||||||||||||||||||||||||||||||||
indexPrice >= makerOrder.price | ||||||||||||||||||||||||||||||||||||||||||||||||||
: indexPrice <= makerOrder.price | ||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: additionalPositionQty, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: additionalPositionCostBasis, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
const makerOrderPrice2p = makerOrder.price * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const positionBalance = | ||||||||||||||||||||||||||||||||||||||||||||||||||
(currentPosition ? decimalToPip(currentPosition.quantity) : BigInt(0)) + | ||||||||||||||||||||||||||||||||||||||||||||||||||
additionalPositionQty; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const quoteValueOfPosition2p = positionBalance * indexPrice; // Signed | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const initialMarginFraction = calculateInitialMarginFractionWithOverride({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFractionOverride, | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters: leverageParametersBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
positionBalance, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// Unsigned | ||||||||||||||||||||||||||||||||||||||||||||||||||
const initialMarginRequirementOfPosition2p = multiplyPips( | ||||||||||||||||||||||||||||||||||||||||||||||||||
absBigInt(quoteValueOfPosition2p), | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFraction, | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// Signed | ||||||||||||||||||||||||||||||||||||||||||||||||||
const maxTakerBaseQty = | ||||||||||||||||||||||||||||||||||||||||||||||||||
((-quoteBalance2p - | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteValueOfPosition2p - | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteValueOfOtherPositions2p + | ||||||||||||||||||||||||||||||||||||||||||||||||||
heldCollateral2p + | ||||||||||||||||||||||||||||||||||||||||||||||||||
remainingAvailableCollateral + | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginRequirementOfPosition2p + | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginRequirementOfOtherPositions2p) * | ||||||||||||||||||||||||||||||||||||||||||||||||||
oneInPips) / | ||||||||||||||||||||||||||||||||||||||||||||||||||
(indexPrice2p - | ||||||||||||||||||||||||||||||||||||||||||||||||||
makerOrderPrice2p + | ||||||||||||||||||||||||||||||||||||||||||||||||||
BigInt(takerSide === 'buy' ? -1 : 1) * | ||||||||||||||||||||||||||||||||||||||||||||||||||
indexPrice * | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFraction); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if (absBigInt(maxTakerBaseQty) < makerOrder.size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
additionalPositionQty += maxTakerBaseQty; | ||||||||||||||||||||||||||||||||||||||||||||||||||
additionalPositionCostBasis += multiplyPips( | ||||||||||||||||||||||||||||||||||||||||||||||||||
maxTakerBaseQty, | ||||||||||||||||||||||||||||||||||||||||||||||||||
makerOrder.price, | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: additionalPositionQty, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: additionalPositionCostBasis, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
const tradeBaseQty = makerOrder.size * BigInt(takerSide === 'buy' ? 1 : -1); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const tradeQuoteQty = multiplyPips(tradeBaseQty, makerOrder.price); | ||||||||||||||||||||||||||||||||||||||||||||||||||
additionalPositionQty += tradeBaseQty; | ||||||||||||||||||||||||||||||||||||||||||||||||||
additionalPositionCostBasis += tradeQuoteQty; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
quoteBalance2p -= tradeBaseQty * makerOrder.price; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: additionalPositionQty, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: additionalPositionCostBasis, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Helper function to convert from base to quote quantities | ||||||||||||||||||||||||||||||||||||||||||||||||||
* see: {quantitiesAvailableFromPoolAtBidPrice} | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @private | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
export function calculateGrossQuoteValueOfSellQuantities( | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseAssetQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteAssetQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
grossBaseQuantity: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
function calculateInitialMarginFraction( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters: LeverageParametersBigInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
positionBalance: bigint, | ||||||||||||||||||||||||||||||||||||||||||||||||||
): bigint { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const absPositionBalance = absBigInt(positionBalance); | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (absPositionBalance <= leverageParameters.basePositionSize) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return leverageParameters.initialMarginFraction; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteAssetQuantity - | ||||||||||||||||||||||||||||||||||||||||||||||||||
(baseAssetQuantity * quoteAssetQuantity) / | ||||||||||||||||||||||||||||||||||||||||||||||||||
(baseAssetQuantity + grossBaseQuantity) | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.initialMarginFraction + | ||||||||||||||||||||||||||||||||||||||||||||||||||
divideBigInt( | ||||||||||||||||||||||||||||||||||||||||||||||||||
absPositionBalance - leverageParameters.basePositionSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.incrementalPositionSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||
ROUNDING.RoundUp, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) * | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.incrementalInitialMarginFraction | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @private | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function calculateInitialMarginFractionWithOverride(args: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFractionOverride: bigint | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters: LeverageParametersBigInt; | ||||||||||||||||||||||||||||||||||||||||||||||||||
positionBalance: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}): bigint { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const { initialMarginFractionOverride, leverageParameters, positionBalance } = | ||||||||||||||||||||||||||||||||||||||||||||||||||
args; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return maxBigInt( | ||||||||||||||||||||||||||||||||||||||||||||||||||
calculateInitialMarginFraction(leverageParameters, positionBalance), | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFractionOverride ?? BigInt(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review the use of The use of |
||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @private | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function calculateNotionalQuoteValueOfPosition(position: IDEXPosition): bigint { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return multiplyPips( | ||||||||||||||||||||||||||||||||||||||||||||||||||
decimalToPip(position.quantity), | ||||||||||||||||||||||||||||||||||||||||||||||||||
decimalToPip(position.indexPrice), | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @private | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function calculateNotionalQuoteValueOfPositions( | ||||||||||||||||||||||||||||||||||||||||||||||||||
positions: IDEXPosition[], | ||||||||||||||||||||||||||||||||||||||||||||||||||
): bigint { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return arraySumBigInt(positions.map(calculateNotionalQuoteValueOfPosition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @private | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function convertToLeverageParametersBigInt( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters: LeverageParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||
): LeverageParametersBigInt { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
maximumPositionSize: decimalToPip(leverageParameters.maximumPositionSize), | ||||||||||||||||||||||||||||||||||||||||||||||||||
initialMarginFraction: decimalToPip( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.initialMarginFraction, | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
maintenanceMarginFraction: decimalToPip( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.maintenanceMarginFraction, | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
basePositionSize: decimalToPip(leverageParameters.basePositionSize), | ||||||||||||||||||||||||||||||||||||||||||||||||||
incrementalPositionSize: decimalToPip( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.incrementalPositionSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
incrementalInitialMarginFraction: decimalToPip( | ||||||||||||||||||||||||||||||||||||||||||||||||||
leverageParameters.incrementalInitialMarginFraction, | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The functions
Analysis chainEnsure that private functions like Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify unit tests for private functions in `quantities.ts`.
# Test: Search for unit tests covering these functions. Expect: Comprehensive test coverage.
rg --type python $'calculateInitialMarginFraction|convertToLeverageParametersBigInt'
Length of output: 116 Script: #!/bin/bash
# Description: Verify unit tests for private functions in `quantities.ts`.
# Test: Search for unit tests covering these functions. Expect: Comprehensive test coverage.
rg --type js 'calculateInitialMarginFraction|convertToLeverageParametersBigInt'
Length of output: 230 Script: #!/bin/bash
# Description: Debug ripgrep to understand why files are being skipped.
# Test: Run ripgrep with --debug to identify why no files are being searched.
rg --type js 'calculateInitialMarginFraction|convertToLeverageParametersBigInt' --debug
Length of output: 43689 Script: #!/bin/bash
# Description: Verify unit tests for private functions in `quantities.ts`.
# Test: Search for unit tests covering these functions. Expect: Comprehensive test coverage.
rg --type js 'calculateInitialMarginFraction|convertToLeverageParametersBigInt' --no-ignore
Length of output: 242 Script: #!/bin/bash
# Description: Verify unit tests for private functions in `quantities.ts`.
# Step 1: Find all test files.
test_files=$(fd --type f --extension js --extension ts --glob '*test*')
# Step 2: Search for the function calls within the test files.
for file in $test_files; do
echo "Searching in $file"
rg 'calculateInitialMarginFraction|convertToLeverageParametersBigInt' "$file"
done
Length of output: 1407 Script: #!/bin/bash
# Description: Verify usage of private functions in the entire codebase.
# Step 1: Search for the usage of the functions in the entire codebase.
rg 'calculateInitialMarginFraction|convertToLeverageParametersBigInt'
Length of output: 595
Comment on lines
+984
to
+1003
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize The function converts multiple parameters using |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Determines the liquidity available in the given order book (asks or bids) | ||||||||||||||||||||||||||||||||||||||||||||||||||
* for a given taker quantity (which may be expressed in base or quote asset) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -93,7 +353,7 @@ export function calculateGrossFillQuantities( | |||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const takerQuantity2p = takerOrder.quantity * pipmath.oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const takerQuantity2p = takerOrder.quantity * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
let filledBaseQty2p = BigInt(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||
let filledQuoteQty2p = BigInt(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -109,8 +369,8 @@ export function calculateGrossFillQuantities( | |||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: filledBaseQty2p / pipmath.oneInPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: filledQuoteQty2p / pipmath.oneInPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity: filledBaseQty2p / oneInPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity: filledQuoteQty2p / oneInPips, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -153,15 +413,15 @@ function determineTradeQuantities( | |||||||||||||||||||||||||||||||||||||||||||||||||
baseQuantity2p: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
quoteQuantity2p: bigint; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const makerQuantity2p = makerOrder.size * pipmath.oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const makerQuantity2p = makerOrder.size * oneInPips; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// Limit by base | ||||||||||||||||||||||||||||||||||||||||||||||||||
const fillBaseQty2p = | ||||||||||||||||||||||||||||||||||||||||||||||||||
isMaxTakerQuantityInQuote ? makerQuantity2p : ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
pipmath.minBigInt(maxTakerQuantity2p, makerQuantity2p) | ||||||||||||||||||||||||||||||||||||||||||||||||||
minBigInt(maxTakerQuantity2p, makerQuantity2p) | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const fillQuoteQty2p = pipmath.multiplyPips(fillBaseQty2p, makerOrder.price); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const fillQuoteQty2p = multiplyPips(fillBaseQty2p, makerOrder.price); | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1113
to
+1121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle division by zero in The function does not explicitly handle division by zero, which could potentially lead to runtime errors if such a situation arises. + if (maxTakerQuantity2p === BigInt(0)) {
+ throw new Error('Division by zero error');
+ } Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// Limit by quote | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (isMaxTakerQuantityInQuote && maxTakerQuantity2p < fillQuoteQty2p) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor
calculateBuySellPanelEstimate
to improve readability by breaking down complex logic into smaller, more manageable functions.