@pendle/sdk-boros 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,21 +3,30 @@ export type MarketConfig = {
|
|
|
3
3
|
marginFactor: FixedX18;
|
|
4
4
|
markRate: FixedX18;
|
|
5
5
|
minMarginIndexRate: FixedX18;
|
|
6
|
+
minMarginIndexDuration_s: number;
|
|
6
7
|
marketExpiry_s: number;
|
|
7
8
|
};
|
|
8
|
-
export type
|
|
9
|
-
|
|
9
|
+
export type LeverageConfig = {
|
|
10
|
+
leverage: number;
|
|
11
|
+
};
|
|
12
|
+
export type TradeInfo = {
|
|
10
13
|
orderRate: FixedX18;
|
|
11
14
|
isMarketOrder: boolean;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
};
|
|
16
|
+
export type GetOrderSizeByExactInitialMarginParams = MarketConfig & LeverageConfig & TradeInfo & {
|
|
17
|
+
initialMargin: bigint;
|
|
18
|
+
};
|
|
19
|
+
export type GetInitialMarginByOrderSizeParams = MarketConfig & LeverageConfig & TradeInfo & {
|
|
20
|
+
orderSize: bigint;
|
|
21
|
+
};
|
|
22
|
+
export type GetOrderValueParams = {
|
|
15
23
|
orderSize: bigint;
|
|
16
24
|
orderRate: FixedX18;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} & MarketConfig;
|
|
25
|
+
marketExpiry_s: number;
|
|
26
|
+
};
|
|
20
27
|
export declare class Market {
|
|
21
28
|
static getOrderSizeByExactInitialMargin(params: GetOrderSizeByExactInitialMarginParams): bigint;
|
|
22
29
|
static getInitialMarginByOrderSize(params: GetInitialMarginByOrderSizeParams): bigint;
|
|
30
|
+
private static getIMSuf;
|
|
31
|
+
static getOrderValue(params: GetOrderValueParams): bigint;
|
|
23
32
|
}
|
|
@@ -3,41 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Market = void 0;
|
|
4
4
|
const boros_offchain_math_1 = require("@pendle/boros-offchain-math");
|
|
5
5
|
const MARKET_ORDER_BUFFER_RATE = boros_offchain_math_1.FixedX18.fromNumber(0.1);
|
|
6
|
-
const
|
|
6
|
+
const BUFFERED_INITIAL_MARGIN_RATE_RATIO = boros_offchain_math_1.FixedX18.fromNumber(0.01);
|
|
7
7
|
const SECONDS_PER_YEARS = 3600 * 24 * 365;
|
|
8
8
|
class Market {
|
|
9
9
|
static getOrderSizeByExactInitialMargin(params) {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
: orderRate;
|
|
15
|
-
const sizeOrderRateRatio = [
|
|
16
|
-
marginFactor.mulDown(markRate),
|
|
17
|
-
marginFactor.mulDown(minMarginIndexRate),
|
|
18
|
-
orderRateWithBuffer.divDown(boros_offchain_math_1.FixedX18.fromNumber(leverage)),
|
|
19
|
-
].reduce((a, b) => (a.gt(b) ? a : b), boros_offchain_math_1.FixedX18.ZERO);
|
|
20
|
-
const sizeOrderRateRatioWithBuffer = sizeOrderRateRatio.mulDown(boros_offchain_math_1.FixedX18.ONE.add(SIZE_ORDER_RATE_RATIO));
|
|
21
|
-
const orderSize = boros_offchain_math_1.FixedX18.mulFraction(initialMargin, [
|
|
22
|
-
boros_offchain_math_1.FixedX18.ONE,
|
|
23
|
-
sizeOrderRateRatioWithBuffer.mulUp(boros_offchain_math_1.FixedX18.fromNumber(timeToMaturity_y)),
|
|
24
|
-
]);
|
|
10
|
+
const imSuf = Market.getIMSuf(params);
|
|
11
|
+
const orderSize = boros_offchain_math_1.FixedX18.fromRawValue(params.initialMargin)
|
|
12
|
+
.mulDown(imSuf)
|
|
13
|
+
.divDown(boros_offchain_math_1.FixedX18.ONE.add(BUFFERED_INITIAL_MARGIN_RATE_RATIO)).value;
|
|
25
14
|
return orderSize;
|
|
26
15
|
}
|
|
27
16
|
static getInitialMarginByOrderSize(params) {
|
|
28
|
-
const
|
|
17
|
+
const imSuf = Market.getIMSuf(params);
|
|
18
|
+
return boros_offchain_math_1.FixedX18.fromRawValue(params.orderSize)
|
|
19
|
+
.mulDown(imSuf)
|
|
20
|
+
.mulDown(boros_offchain_math_1.FixedX18.ONE.add(BUFFERED_INITIAL_MARGIN_RATE_RATIO)).value;
|
|
21
|
+
}
|
|
22
|
+
static getIMSuf(data) {
|
|
23
|
+
const { orderRate, leverage, marginFactor, markRate, minMarginIndexRate, marketExpiry_s, minMarginIndexDuration_s, isMarketOrder, } = data;
|
|
24
|
+
const timeToMaturity_y = (marketExpiry_s - Math.floor(Date.now() / 1000)) / SECONDS_PER_YEARS;
|
|
25
|
+
const minTime_y = minMarginIndexDuration_s / SECONDS_PER_YEARS;
|
|
26
|
+
const time = boros_offchain_math_1.FixedX18.fromNumber(Math.min(timeToMaturity_y, minTime_y));
|
|
27
|
+
const contractRate = markRate.gt(minMarginIndexRate) ? markRate : minMarginIndexRate;
|
|
28
|
+
const offchainRate = orderRate.gt(minMarginIndexRate) ? orderRate : minMarginIndexRate;
|
|
29
|
+
const contractSuf = contractRate.mulDown(time).mulDown(marginFactor);
|
|
30
|
+
const offchainSuf = offchainRate.mulDown(time).divDown(boros_offchain_math_1.FixedX18.fromNumber(leverage));
|
|
31
|
+
return contractSuf.gt(offchainSuf) ? contractSuf : offchainSuf;
|
|
32
|
+
}
|
|
33
|
+
static getOrderValue(params) {
|
|
34
|
+
const { orderSize, orderRate, marketExpiry_s } = params;
|
|
29
35
|
const timeToMaturity_y = (marketExpiry_s - Math.floor(Date.now() / 1000)) / SECONDS_PER_YEARS;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
marginFactor.mulDown(markRate),
|
|
33
|
-
marginFactor.mulDown(minMarginIndexRate),
|
|
34
|
-
orderRateWithBuffer.divDown(boros_offchain_math_1.FixedX18.fromNumber(leverage)),
|
|
35
|
-
].reduce((a, b) => (a.gt(b) ? a : b), boros_offchain_math_1.FixedX18.ZERO);
|
|
36
|
-
const initialMarginRateRatioWithBuffer = sizeOrderRateRatio.mulDown(boros_offchain_math_1.FixedX18.ONE.add(SIZE_ORDER_RATE_RATIO));
|
|
37
|
-
const initialMargin = boros_offchain_math_1.FixedX18.fromRawValue(orderSize)
|
|
38
|
-
.mulDown(initialMarginRateRatioWithBuffer)
|
|
36
|
+
const orderValue = boros_offchain_math_1.FixedX18.fromRawValue(orderSize)
|
|
37
|
+
.mulDown(orderRate)
|
|
39
38
|
.mulDown(boros_offchain_math_1.FixedX18.fromNumber(timeToMaturity_y)).value;
|
|
40
|
-
return
|
|
39
|
+
return orderValue;
|
|
41
40
|
}
|
|
42
41
|
}
|
|
43
42
|
exports.Market = Market;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"market.js","sourceRoot":"","sources":["../../../src/entities/market/market.ts"],"names":[],"mappings":";;;AAAA,qEAAuD;AAEvD,MAAM,wBAAwB,GAAG,8BAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;AAC1D,MAAM,
|
|
1
|
+
{"version":3,"file":"market.js","sourceRoot":"","sources":["../../../src/entities/market/market.ts"],"names":[],"mappings":";;;AAAA,qEAAuD;AAEvD,MAAM,wBAAwB,GAAG,8BAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;AAC1D,MAAM,kCAAkC,GAAG,8BAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;AACrE,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAE,GAAG,GAAG,CAAC;AAsC1C,MAAa,MAAM;IACjB,MAAM,CAAC,gCAAgC,CAAC,MAA8C;QACpF,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAGtC,MAAM,SAAS,GAAG,8BAAQ,CAAC,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC;aAC1D,OAAO,CAAC,KAAK,CAAC;aACd,OAAO,CAAC,8BAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC,CAAC,KAAK,CAAC;QAEvE,OA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