@levrbet/shared 0.1.132 → 0.2.1
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.
- package/dist/core/types/oracle/requests/market.request.types.d.ts +3 -16
- package/dist/core/types/oracle/requests/market.request.types.js +2 -8
- package/dist/core/types/oracle/requests/market.request.types.js.map +1 -1
- package/dist/core/utils/index.d.ts +1 -0
- package/dist/core/utils/index.js +1 -0
- package/dist/core/utils/index.js.map +1 -1
- package/dist/core/utils/position.utils.d.ts +18 -0
- package/dist/core/utils/position.utils.js +68 -0
- package/dist/core/utils/position.utils.js.map +1 -0
- package/package.json +1 -1
|
@@ -13,12 +13,11 @@ export declare const MarketRiskAllocationSchema: z.ZodObject<{
|
|
|
13
13
|
export declare const CreateMarketSchema: z.ZodObject<{
|
|
14
14
|
chainId: z.ZodNumber;
|
|
15
15
|
gameId: z.ZodNumber;
|
|
16
|
-
levrMarketId: z.ZodEnum<typeof LevrMarketId
|
|
16
|
+
levrMarketId: z.ZodArray<z.ZodEnum<typeof LevrMarketId>>;
|
|
17
17
|
levrMarketContract: z.ZodCustom<`0x${string}`, `0x${string}`>;
|
|
18
|
-
leveraged: z.ZodBoolean;
|
|
19
18
|
normalizationFactor: z.ZodNumber;
|
|
20
19
|
marketDetails: z.ZodObject<{}, z.core.$strip>;
|
|
21
|
-
marketRiskAllocation: z.ZodObject<{
|
|
20
|
+
marketRiskAllocation: z.ZodArray<z.ZodObject<{
|
|
22
21
|
asset: z.ZodString;
|
|
23
22
|
boundedLossA: z.ZodNumber;
|
|
24
23
|
boundedLossB: z.ZodNumber;
|
|
@@ -27,22 +26,10 @@ export declare const CreateMarketSchema: z.ZodObject<{
|
|
|
27
26
|
odds: z.ZodArray<z.ZodNumber>;
|
|
28
27
|
targetScore: z.ZodOptional<z.ZodNumber>;
|
|
29
28
|
sideGroup: z.ZodOptional<z.ZodNumber>;
|
|
30
|
-
}, z.core.$strip
|
|
29
|
+
}, z.core.$strip>>;
|
|
31
30
|
providers: z.ZodObject<{}, z.core.$strip>;
|
|
32
31
|
activeProvider: z.ZodObject<{}, z.core.$strip>;
|
|
33
|
-
marketType: z.ZodEnum<{
|
|
34
|
-
FullTimeWinner: "FullTimeWinner";
|
|
35
|
-
OverUnder: "OverUnder";
|
|
36
|
-
}>;
|
|
37
|
-
status: z.ZodEnum<{
|
|
38
|
-
Open: "Open";
|
|
39
|
-
Closed: "Closed";
|
|
40
|
-
Settled: "Settled";
|
|
41
|
-
Refunded: "Refunded";
|
|
42
|
-
}>;
|
|
43
32
|
levrGameObjectId: z.ZodString;
|
|
44
|
-
marketName: z.ZodString;
|
|
45
|
-
allowSideGroup: z.ZodOptional<z.ZodNumber>;
|
|
46
33
|
}, z.core.$strip>;
|
|
47
34
|
export type CreateMarketRequest = z.infer<typeof CreateMarketSchema>;
|
|
48
35
|
export declare const ProviderDetailsSchema: z.ZodObject<{
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ProviderDetailsArraySchema = exports.ProviderDetailsSchema = exports.CreateMarketSchema = exports.MarketRiskAllocationSchema = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const contracts_1 = require("../../../contracts");
|
|
6
|
-
const prisma_1 = require("../../../prisma");
|
|
7
6
|
const validators_1 = require("../../../validators");
|
|
8
7
|
exports.MarketRiskAllocationSchema = zod_1.z.object({
|
|
9
8
|
asset: zod_1.z.string(), // Address is a string alias
|
|
@@ -18,19 +17,14 @@ exports.MarketRiskAllocationSchema = zod_1.z.object({
|
|
|
18
17
|
exports.CreateMarketSchema = zod_1.z.object({
|
|
19
18
|
chainId: zod_1.z.number(),
|
|
20
19
|
gameId: zod_1.z.number(),
|
|
21
|
-
levrMarketId: zod_1.z.enum(contracts_1.LevrMarketId),
|
|
20
|
+
levrMarketId: zod_1.z.array(zod_1.z.enum(contracts_1.LevrMarketId)).min(1),
|
|
22
21
|
levrMarketContract: validators_1.addressSchema,
|
|
23
|
-
leveraged: zod_1.z.boolean(),
|
|
24
22
|
normalizationFactor: zod_1.z.number(),
|
|
25
23
|
marketDetails: zod_1.z.object({}), // details as needed
|
|
26
|
-
marketRiskAllocation: exports.MarketRiskAllocationSchema,
|
|
24
|
+
marketRiskAllocation: zod_1.z.array(exports.MarketRiskAllocationSchema).min(1),
|
|
27
25
|
providers: zod_1.z.object({}), // providers as needed
|
|
28
26
|
activeProvider: zod_1.z.object({}), // active provider as needed
|
|
29
|
-
marketType: zod_1.z.enum(prisma_1.MarketTypes),
|
|
30
|
-
status: zod_1.z.enum(prisma_1.MarketStatus),
|
|
31
27
|
levrGameObjectId: validators_1.objectIdSchema,
|
|
32
|
-
marketName: zod_1.z.string(),
|
|
33
|
-
allowSideGroup: zod_1.z.number().optional(), // Optional side group, using Bytes8Schema for validation
|
|
34
28
|
});
|
|
35
29
|
exports.ProviderDetailsSchema = zod_1.z.object({
|
|
36
30
|
fixtureId: zod_1.z.string(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"market.request.types.js","sourceRoot":"","sources":["../../../../../src/core/types/oracle/requests/market.request.types.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AACvB,kDAAiD;AACjD,
|
|
1
|
+
{"version":3,"file":"market.request.types.js","sourceRoot":"","sources":["../../../../../src/core/types/oracle/requests/market.request.types.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AACvB,kDAAiD;AACjD,oDAAmE;AAEtD,QAAA,0BAA0B,GAAG,OAAC,CAAC,MAAM,CAAC;IAC/C,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE,EAAE,4BAA4B;IAC/C,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;IACxB,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;IACxB,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;IACxB,OAAO,EAAE,OAAC,CAAC,MAAM,EAAE;IACnB,IAAI,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,C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Property(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./converters.utils"), exports);
|
|
18
18
|
__exportStar(require("./game.utils"), exports);
|
|
19
19
|
__exportStar(require("./misc.utils"), exports);
|
|
20
|
+
__exportStar(require("./position.utils"), exports);
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,+CAA4B;AAC5B,+CAA4B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,+CAA4B;AAC5B,+CAA4B;AAC5B,mDAAgC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface TraderPayoutResult {
|
|
2
|
+
/** The total payout of the trader */
|
|
3
|
+
payout: bigint;
|
|
4
|
+
/** The profit of the trader */
|
|
5
|
+
profit: bigint;
|
|
6
|
+
/** The amount of collateral lost by the trader */
|
|
7
|
+
collateralLost: bigint;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Calculates the trader's payout for a leveraged position.
|
|
11
|
+
* @param entryPrice The entry price of the position.
|
|
12
|
+
* @param currentPrice The current token price.
|
|
13
|
+
* @param eCollateral The effective collateral of the position.
|
|
14
|
+
* @param positionSize The effective size of the trader (position size - position fee).
|
|
15
|
+
* @param eLeverage The effective leverage of the position in 6 decimals precision.
|
|
16
|
+
* @returns Object containing payout, profit, and collateralLost
|
|
17
|
+
*/
|
|
18
|
+
export declare function calculateTraderPayout(entryPrice: bigint, currentPrice: bigint, eCollateral: bigint, positionSize: bigint, eLeverage: bigint): TraderPayoutResult;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateTraderPayout = calculateTraderPayout;
|
|
4
|
+
/**
|
|
5
|
+
* Constants
|
|
6
|
+
*/
|
|
7
|
+
const PRECISION_E6 = 1000000n;
|
|
8
|
+
/**
|
|
9
|
+
* Multiply two bigints and divide by a denominator with rounding down.
|
|
10
|
+
* Equivalent to Solidity's mulDiv.
|
|
11
|
+
*/
|
|
12
|
+
function mulDiv(a, b, denominator) {
|
|
13
|
+
if (denominator === 0n)
|
|
14
|
+
throw new Error("Division by zero");
|
|
15
|
+
return (a * b) / denominator;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Multiply two bigints and divide by a denominator with rounding up.
|
|
19
|
+
* Equivalent to Solidity's mulDivUp.
|
|
20
|
+
*/
|
|
21
|
+
function mulDivUp(a, b, denominator) {
|
|
22
|
+
if (denominator === 0n)
|
|
23
|
+
throw new Error("Division by zero");
|
|
24
|
+
const result = (a * b) / denominator;
|
|
25
|
+
const remainder = (a * b) % denominator;
|
|
26
|
+
return remainder > 0n ? result + 1n : result;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Calculates the trader's payout for a leveraged position.
|
|
30
|
+
* @param entryPrice The entry price of the position.
|
|
31
|
+
* @param currentPrice The current token price.
|
|
32
|
+
* @param eCollateral The effective collateral of the position.
|
|
33
|
+
* @param positionSize The effective size of the trader (position size - position fee).
|
|
34
|
+
* @param eLeverage The effective leverage of the position in 6 decimals precision.
|
|
35
|
+
* @returns Object containing payout, profit, and collateralLost
|
|
36
|
+
*/
|
|
37
|
+
function calculateTraderPayout(entryPrice, currentPrice, eCollateral, positionSize, eLeverage) {
|
|
38
|
+
// Early return if any parameter is zero
|
|
39
|
+
if (positionSize === 0n || eCollateral === 0n || eLeverage === 0n || entryPrice === 0n) {
|
|
40
|
+
return { payout: 0n, profit: 0n, collateralLost: 0n };
|
|
41
|
+
}
|
|
42
|
+
let payout = 0n;
|
|
43
|
+
let profit = 0n;
|
|
44
|
+
let collateralLost = 0n;
|
|
45
|
+
if (currentPrice >= entryPrice) {
|
|
46
|
+
// Profitable position, calculate profit directly
|
|
47
|
+
const priceDelta = currentPrice - entryPrice;
|
|
48
|
+
profit = mulDiv(positionSize, priceDelta, entryPrice);
|
|
49
|
+
payout = eCollateral + profit;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// Losing position, calculate loss
|
|
53
|
+
const priceDelta = entryPrice - currentPrice;
|
|
54
|
+
const rawLoss = mulDivUp(positionSize, priceDelta, entryPrice);
|
|
55
|
+
// Adjust collateral lost based on leverage
|
|
56
|
+
collateralLost = mulDivUp(rawLoss, PRECISION_E6, eLeverage);
|
|
57
|
+
// Cap collateral lost at effective collateral
|
|
58
|
+
if (collateralLost >= eCollateral) {
|
|
59
|
+
collateralLost = eCollateral;
|
|
60
|
+
payout = 0n;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
payout = eCollateral - collateralLost;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { payout, profit, collateralLost };
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=position.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"position.utils.js","sourceRoot":"","sources":["../../../src/core/utils/position.utils.ts"],"names":[],"mappings":";;AA2CA,sDAwCC;AAnFD;;GAEG;AACH,MAAM,YAAY,GAAG,QAAU,CAAA;AAE/B;;;GAGG;AACH,SAAS,MAAM,CAAC,CAAS,EAAE,CAAS,EAAE,WAAmB;IACrD,IAAI,WAAW,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;IAC3D,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAA;AAChC,CAAC;AAED;;;GAGG;AACH,SAAS,QAAQ,CAAC,CAAS,EAAE,CAAS,EAAE,WAAmB;IACvD,IAAI,WAAW,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;IAC3D,MAAM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