@reyaxyz/common 0.353.1 → 0.354.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/README.md +1 -1
- package/dist/incentives-api-types.js.map +1 -1
- package/dist/types/incentives-api-types.d.ts +16 -0
- package/dist/types/incentives-api-types.d.ts.map +1 -1
- package/dist/types/utils/compute-trade-pnl.d.ts +39 -0
- package/dist/types/utils/compute-trade-pnl.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/utils/compute-trade-pnl.js +60 -0
- package/dist/utils/compute-trade-pnl.js.map +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +2 -2
- package/src/incentives-api-types.ts +28 -0
- package/src/utils/compute-trade-pnl.ts +110 -0
- package/src/utils/index.ts +1 -0
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"incentives-api-types.js","sourceRoot":"/","sources":["incentives-api-types.ts"],"names":[],"mappings":"","sourcesContent":["import {\n AddressParam,\n ApiEndpoint,\n ApiInfiniteListEndpoint,\n ApiListEndpoint,\n PaginatedQueryParams,\n FailableResult,\n Stringified,\n} from './api-types';\nimport { Rank, RankTrading } from './types';\n\nexport type PointsBreakdown = {\n totalPoints: number;\n tradingPoints: number;\n stakingPoints: number;\n signalPoints: number;\n rank: number;\n};\n\nexport type IncentivesUserPointsData = {\n weekly: PointsBreakdown;\n total: PointsBreakdown;\n};\n\nexport type IncentivesUserRanks = {\n walletAddress: string;\n tradingRank: RankTrading;\n stakingRank: Rank;\n};\n\nexport type IncentivesUserBoosts = {\n walletAddress: string;\n tradingBoost: number;\n stakingBoost: number;\n loyaltyBoost: number;\n};\n\nexport type IncentivesUserSummaryData = {\n walletAddress: string;\n points: IncentivesUserPointsData;\n ranks: Omit<IncentivesUserRanks, 'walletAddress'>;\n boosts: Omit<IncentivesUserBoosts, 'walletAddress'>;\n};\n\nexport type IncentivesLeaderBoardEntry = {\n rank: number;\n walletAddress: string;\n totalPoints: number;\n tradingPoints: number;\n stakingPoints: number;\n signalPoints: number;\n isCurrentUser?: boolean;\n};\n\nexport type IncentivesRankingHistoryEntry = {\n week: number;\n createdAt: Date;\n tradingRank: RankTrading;\n stakingRank: Rank;\n tradingPoints: number;\n stakingPoints: number;\n};\n\nexport type IncentivesPointsHistoryEntry = {\n walletAddress: string;\n totalXpOG: number;\n totalXpOGRanked: number;\n liquidityReferralXpOG: number;\n liquidityXpOG: number;\n rankMultiplierOG: number;\n rankOG: Rank;\n stakingPointsOG: number;\n liquidityReferralXpSeason1: number;\n liquidityXpSeason1: number;\n liquidityXpSeason1Ranked: number;\n rankMultiplierSeason1: number;\n rankSeason1: Rank;\n stakingPointsSeason1: number;\n tradingReferralXpSeason1: number;\n tradingXpSeason1: number;\n tradingXpSeason1Ranked: number;\n tradingRankMultiplierSeason1: number;\n tradingRankSeason1: RankTrading;\n tradingPointsSeason1: number;\n liquidityReferralXpSeason2: number;\n liquidityXpSeason2: number;\n rankSeason2: Rank;\n stakingPointsSeason2: number;\n tradingReferralXpSeason2: number;\n tradingXpSeason2: number;\n tradingRankSeason2: RankTrading;\n tradingPointsSeason2: number;\n totalLiquidityXp: number;\n totalLiquidityReferralXp: number;\n totalTradingXp: number;\n totalTradingReferralXp: number;\n totalXp: number;\n totalPoints: number;\n};\n\nexport type LeaderBoardParams = { type: 'weekly' | 'total' };\n\nexport type TradingCompetitionRoundParams = { round: number };\n\nexport type TradingCompetitionUserSummaryData =\n | {\n status: 'out';\n }\n | {\n status: 'in';\n realizedPnl: number;\n position: number;\n volume: number;\n volumePosition: number;\n };\n\nexport type TradingCompetitionLeaderboardEntry = {\n walletAddress: string;\n realizedPnl: number;\n position: number;\n};\n\nexport type IncentivesApiSource = {\n 'incentives/points': ApiEndpoint<\n {\n total: number;\n weekly: number;\n },\n never\n >;\n\n 'incentives/leaderBoard/:type': ApiInfiniteListEndpoint<\n IncentivesLeaderBoardEntry,\n LeaderBoardParams,\n PaginatedQueryParams\n >;\n\n 'incentives/wallet/:address': ApiEndpoint<\n IncentivesUserSummaryData,\n AddressParam\n >;\n\n 'incentives/wallet/:address/referralsLeaderBoard/:type': ApiListEndpoint<\n IncentivesLeaderBoardEntry,\n AddressParam & LeaderBoardParams\n >;\n\n 'incentives/wallet/:address/leaderBoard/:type': ApiEndpoint<\n IncentivesLeaderBoardEntry,\n AddressParam & LeaderBoardParams\n >;\n\n 'incentives/wallet/:address/ranksHistory': ApiListEndpoint<\n IncentivesRankingHistoryEntry,\n AddressParam\n >;\n\n 'incentives/wallet/:address/signalPoints': ApiListEndpoint<\n {\n createdAt: Date;\n points: number;\n reason: string;\n },\n AddressParam\n >;\n\n 'incentives/wallet/:address/pointsHistory': ApiEndpoint<\n IncentivesPointsHistoryEntry,\n AddressParam\n >;\n\n 'incentives/tradingCompetition/:round': ApiEndpoint<\n {\n entries: TradingCompetitionLeaderboardEntry[];\n status: 'live' | 'ended';\n refreshedAt: number;\n },\n TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round': ApiEndpoint<\n TradingCompetitionUserSummaryData,\n AddressParam & TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round/registration': ApiEndpoint<\n { registered: boolean },\n AddressParam & TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round/claim': ApiEndpoint<\n FailableResult,\n AddressParam & TradingCompetitionRoundParams,\n { code: string }\n >;\n};\n\nexport type IncentivesApi = {\n [K in keyof IncentivesApiSource]: {\n params: IncentivesApiSource[K]['params'];\n query: IncentivesApiSource[K]['query'];\n response: Stringified<IncentivesApiSource[K]['response']>;\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"incentives-api-types.js","sourceRoot":"/","sources":["incentives-api-types.ts"],"names":[],"mappings":"","sourcesContent":["import {\n AddressParam,\n ApiEndpoint,\n ApiInfiniteListEndpoint,\n ApiListEndpoint,\n PaginatedQueryParams,\n FailableResult,\n Stringified,\n} from './api-types';\nimport { Rank, RankTrading } from './types';\n\nexport type PointsBreakdown = {\n totalPoints: number;\n tradingPoints: number;\n stakingPoints: number;\n signalPoints: number;\n rank: number;\n};\n\nexport type IncentivesUserPointsData = {\n weekly: PointsBreakdown;\n total: PointsBreakdown;\n};\n\nexport type IncentivesUserRanks = {\n walletAddress: string;\n tradingRank: RankTrading;\n stakingRank: Rank;\n};\n\nexport type IncentivesUserBoosts = {\n walletAddress: string;\n tradingBoost: number;\n stakingBoost: number;\n loyaltyBoost: number;\n};\n\nexport type IncentivesUserSummaryData = {\n walletAddress: string;\n points: IncentivesUserPointsData;\n ranks: Omit<IncentivesUserRanks, 'walletAddress'>;\n boosts: Omit<IncentivesUserBoosts, 'walletAddress'>;\n};\n\nexport type IncentivesLeaderBoardEntry = {\n rank: number;\n walletAddress: string;\n totalPoints: number;\n tradingPoints: number;\n stakingPoints: number;\n signalPoints: number;\n isCurrentUser?: boolean;\n};\n\nexport type IncentivesRankingHistoryEntry = {\n week: number;\n createdAt: Date;\n tradingRank: RankTrading;\n stakingRank: Rank;\n tradingPoints: number;\n stakingPoints: number;\n};\n\nexport type IncentivesPointsHistoryEntry = {\n walletAddress: string;\n totalXpOG: number;\n totalXpOGRanked: number;\n liquidityReferralXpOG: number;\n liquidityXpOG: number;\n rankMultiplierOG: number;\n rankOG: Rank;\n stakingPointsOG: number;\n liquidityReferralXpSeason1: number;\n liquidityXpSeason1: number;\n liquidityXpSeason1Ranked: number;\n rankMultiplierSeason1: number;\n rankSeason1: Rank;\n stakingPointsSeason1: number;\n tradingReferralXpSeason1: number;\n tradingXpSeason1: number;\n tradingXpSeason1Ranked: number;\n tradingRankMultiplierSeason1: number;\n tradingRankSeason1: RankTrading;\n tradingPointsSeason1: number;\n liquidityReferralXpSeason2: number;\n liquidityXpSeason2: number;\n rankSeason2: Rank;\n stakingPointsSeason2: number;\n tradingReferralXpSeason2: number;\n tradingXpSeason2: number;\n tradingRankSeason2: RankTrading;\n tradingPointsSeason2: number;\n totalLiquidityXp: number;\n totalLiquidityReferralXp: number;\n totalTradingXp: number;\n totalTradingReferralXp: number;\n totalXp: number;\n totalPoints: number;\n};\n\nexport type LeaderBoardParams = { type: 'weekly' | 'total' };\n\nexport type TradingCompetitionRoundParams = { round: number };\n\nexport type TradingCompetitionUserSummaryData =\n | {\n status: 'out';\n }\n | {\n status: 'in';\n realizedPnl: number;\n position: number;\n volume: number;\n volumePosition: number;\n };\n\nexport type TradingCompetitionLeaderboardEntry = {\n walletAddress: string;\n realizedPnl: number;\n position: number;\n};\n\nexport type IncentivesApiSource = {\n 'incentives/points': ApiEndpoint<\n {\n total: number;\n weekly: number;\n },\n never\n >;\n\n 'incentives/leaderBoard/:type': ApiInfiniteListEndpoint<\n IncentivesLeaderBoardEntry,\n LeaderBoardParams,\n PaginatedQueryParams\n >;\n\n 'incentives/wallet/:address': ApiEndpoint<\n IncentivesUserSummaryData,\n AddressParam\n >;\n\n 'incentives/wallet/:address/referralsLeaderBoard/:type': ApiListEndpoint<\n IncentivesLeaderBoardEntry,\n AddressParam & LeaderBoardParams\n >;\n\n 'incentives/wallet/:address/leaderBoard/:type': ApiEndpoint<\n IncentivesLeaderBoardEntry,\n AddressParam & LeaderBoardParams\n >;\n\n 'incentives/wallet/:address/ranksHistory': ApiListEndpoint<\n IncentivesRankingHistoryEntry,\n AddressParam\n >;\n\n 'incentives/wallet/:address/signalPoints': ApiListEndpoint<\n {\n createdAt: Date;\n points: number;\n reason: string;\n },\n AddressParam\n >;\n\n 'incentives/wallet/:address/pointsHistory': ApiEndpoint<\n IncentivesPointsHistoryEntry,\n AddressParam\n >;\n\n 'incentives/tradingCompetition/:round': ApiEndpoint<\n {\n entries: TradingCompetitionLeaderboardEntry[];\n status: 'live' | 'ended';\n refreshedAt: number;\n },\n TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round': ApiEndpoint<\n TradingCompetitionUserSummaryData,\n AddressParam & TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round/registration': ApiEndpoint<\n { registered: boolean },\n AddressParam & TradingCompetitionRoundParams\n >;\n\n 'incentives/wallet/:address/tradingCompetition/:round/claim': ApiEndpoint<\n FailableResult,\n AddressParam & TradingCompetitionRoundParams,\n { code: string }\n >;\n\n 'incentives/vip/wallet/:address': ApiEndpoint<\n {\n walletAddress: string;\n isVip: boolean;\n registered: boolean;\n hasInviteLink: boolean;\n registeredAt: number | null;\n },\n AddressParam\n >;\n\n 'incentives/vip/wallet/:address/nonce': ApiEndpoint<\n FailableResult<{\n nonce: string;\n message: string;\n expiresAt: number;\n }>,\n AddressParam\n >;\n\n 'incentives/vip/wallet/:address/register': ApiEndpoint<\n FailableResult<{\n inviteLink: string;\n alreadyRegistered: boolean;\n }>,\n AddressParam\n >;\n};\n\nexport type IncentivesApi = {\n [K in keyof IncentivesApiSource]: {\n params: IncentivesApiSource[K]['params'];\n query: IncentivesApiSource[K]['query'];\n response: Stringified<IncentivesApiSource[K]['response']>;\n };\n};\n"]}
|
|
@@ -129,6 +129,22 @@ export type IncentivesApiSource = {
|
|
|
129
129
|
'incentives/wallet/:address/tradingCompetition/:round/claim': ApiEndpoint<FailableResult, AddressParam & TradingCompetitionRoundParams, {
|
|
130
130
|
code: string;
|
|
131
131
|
}>;
|
|
132
|
+
'incentives/vip/wallet/:address': ApiEndpoint<{
|
|
133
|
+
walletAddress: string;
|
|
134
|
+
isVip: boolean;
|
|
135
|
+
registered: boolean;
|
|
136
|
+
hasInviteLink: boolean;
|
|
137
|
+
registeredAt: number | null;
|
|
138
|
+
}, AddressParam>;
|
|
139
|
+
'incentives/vip/wallet/:address/nonce': ApiEndpoint<FailableResult<{
|
|
140
|
+
nonce: string;
|
|
141
|
+
message: string;
|
|
142
|
+
expiresAt: number;
|
|
143
|
+
}>, AddressParam>;
|
|
144
|
+
'incentives/vip/wallet/:address/register': ApiEndpoint<FailableResult<{
|
|
145
|
+
inviteLink: string;
|
|
146
|
+
alreadyRegistered: boolean;
|
|
147
|
+
}>, AddressParam>;
|
|
132
148
|
};
|
|
133
149
|
export type IncentivesApi = {
|
|
134
150
|
[K in keyof IncentivesApiSource]: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"incentives-api-types.d.ts","sourceRoot":"/","sources":["incentives-api-types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,WAAW,EACX,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,cAAc,EACd,WAAW,EACZ,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE5C,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG;IACrC,MAAM,EAAE,eAAe,CAAC;IACxB,KAAK,EAAE,eAAe,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,WAAW,CAAC;IACzB,WAAW,EAAE,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;I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D,cAAc,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC,EACF,YAAY,CACb,CAAC;IAEF,yCAAyC,EAAE,WAAW,CACpD,cAAc,CAAC;QACb,UAAU,EAAE,MAAM,CAAC;QACnB,iBAAiB,EAAE,OAAO,CAAC;KAC5B,CAAC,EACF,YAAY,CACb,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;KACzB,CAAC,IAAI,MAAM,mBAAmB,GAAG;QAChC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QACzC,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QACvC,QAAQ,EAAE,WAAW,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;KAC3D;CACF,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type TradePnlInput = {
|
|
2
|
+
/** Position average entry price (position_last_price) — used for openingFee */
|
|
3
|
+
avgEntryPrice: number;
|
|
4
|
+
/** Executed base amount (signed — positive = buy, negative = sell) */
|
|
5
|
+
executedBase: number;
|
|
6
|
+
/** Execution price of this trade */
|
|
7
|
+
executionPrice: number;
|
|
8
|
+
/** Total fee for this execution (rUSD) */
|
|
9
|
+
fee: number;
|
|
10
|
+
/** Position base AFTER this execution */
|
|
11
|
+
postOrderBase: number;
|
|
12
|
+
/** Previous position base multiplier */
|
|
13
|
+
previousBaseMultiplier: number | undefined;
|
|
14
|
+
/** Previous position average entry funding value */
|
|
15
|
+
previousEntryFundingValue: number | undefined;
|
|
16
|
+
/** Previous position entry price (position_previous_last_price) */
|
|
17
|
+
previousEntryPrice: number | undefined;
|
|
18
|
+
/** Previous position funding value (market funding value at time of previous trade) */
|
|
19
|
+
previousFundingValue: number | undefined;
|
|
20
|
+
};
|
|
21
|
+
export type TradePnlResult = {
|
|
22
|
+
fundingPnl: string;
|
|
23
|
+
openingFee: string;
|
|
24
|
+
priceVariationPnl: string;
|
|
25
|
+
realizedPnl: string;
|
|
26
|
+
} | null;
|
|
27
|
+
/**
|
|
28
|
+
* Compute execution-level PnL for a perpetual trade.
|
|
29
|
+
*
|
|
30
|
+
* Returns null when PnL is not applicable (position-extending executions,
|
|
31
|
+
* missing previous state). Returns computed values only when a position is
|
|
32
|
+
* being unwound or flipped.
|
|
33
|
+
*
|
|
34
|
+
* All inputs should be pre-normalized to numbers (from Prisma Decimal, string, etc.).
|
|
35
|
+
* This function is the single source of truth for this computation — used by both
|
|
36
|
+
* the REST transformer (packages/api) and the WS transformer (packages/bun-socket).
|
|
37
|
+
*/
|
|
38
|
+
export declare function computeTradePnl(input: TradePnlInput): TradePnlResult;
|
|
39
|
+
//# sourceMappingURL=compute-trade-pnl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-trade-pnl.d.ts","sourceRoot":"/","sources":["utils/compute-trade-pnl.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,aAAa,GAAG;IAC1B,+EAA+E;IAC/E,aAAa,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,YAAY,EAAE,MAAM,CAAC;IACrB,oCAAoC;IACpC,cAAc,EAAE,MAAM,CAAC;IACvB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,yCAAyC;IACzC,aAAa,EAAE,MAAM,CAAC;IACtB,wCAAwC;IACxC,sBAAsB,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,oDAAoD;IACpD,yBAAyB,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9C,mEAAmE;IACnE,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,uFAAuF;IACvF,oBAAoB,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,MAAM,CAAC;CACrB,GAAG,IAAI,CAAC;AAET;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc,CAoEpE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeTradePnl = void 0;
|
|
4
|
+
var calculate_1 = require("./calculate");
|
|
5
|
+
/**
|
|
6
|
+
* Compute execution-level PnL for a perpetual trade.
|
|
7
|
+
*
|
|
8
|
+
* Returns null when PnL is not applicable (position-extending executions,
|
|
9
|
+
* missing previous state). Returns computed values only when a position is
|
|
10
|
+
* being unwound or flipped.
|
|
11
|
+
*
|
|
12
|
+
* All inputs should be pre-normalized to numbers (from Prisma Decimal, string, etc.).
|
|
13
|
+
* This function is the single source of truth for this computation — used by both
|
|
14
|
+
* the REST transformer (packages/api) and the WS transformer (packages/bun-socket).
|
|
15
|
+
*/
|
|
16
|
+
function computeTradePnl(input) {
|
|
17
|
+
var avgEntryPrice = input.avgEntryPrice, executedBase = input.executedBase, executionPrice = input.executionPrice, fee = input.fee, postOrderBase = input.postOrderBase, previousBaseMultiplier = input.previousBaseMultiplier, previousEntryFundingValue = input.previousEntryFundingValue, previousEntryPrice = input.previousEntryPrice, previousFundingValue = input.previousFundingValue;
|
|
18
|
+
if (executedBase === 0) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
var preOrderBase = postOrderBase - executedBase;
|
|
22
|
+
// Position-extending: PnL not applicable
|
|
23
|
+
if (preOrderBase === 0 ||
|
|
24
|
+
(preOrderBase > 0 && executedBase > 0) ||
|
|
25
|
+
(preOrderBase < 0 && executedBase < 0)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
// Missing previous state: can't compute PnL
|
|
29
|
+
if (previousEntryPrice == null ||
|
|
30
|
+
previousEntryFundingValue == null ||
|
|
31
|
+
previousFundingValue == null ||
|
|
32
|
+
previousBaseMultiplier == null) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
// For unwinding: adjustedBase = -executedBase; for flipping: adjustedBase = preOrderBase
|
|
36
|
+
var isUnwinding = (preOrderBase >= 0 && postOrderBase >= 0) ||
|
|
37
|
+
(preOrderBase <= 0 && postOrderBase <= 0);
|
|
38
|
+
var adjustedBase = isUnwinding ? -executedBase : preOrderBase;
|
|
39
|
+
var openingFee = executionPrice !== 0 ? fee * (avgEntryPrice / executionPrice) : 0;
|
|
40
|
+
var priceVariationPnl = (0, calculate_1.calculatePricePnl)({
|
|
41
|
+
positionAdjustedBase: adjustedBase,
|
|
42
|
+
positionAvgEntryPrice: previousEntryPrice,
|
|
43
|
+
price: executionPrice,
|
|
44
|
+
});
|
|
45
|
+
var fundingPnl = (0, calculate_1.calculateFundingPnl)({
|
|
46
|
+
marketBaseMultiplier: previousBaseMultiplier,
|
|
47
|
+
marketFundingValue: previousFundingValue,
|
|
48
|
+
positionAdjustedBase: adjustedBase,
|
|
49
|
+
positionAvgEntryFundingValue: previousEntryFundingValue,
|
|
50
|
+
});
|
|
51
|
+
var realizedPnl = priceVariationPnl + fundingPnl;
|
|
52
|
+
return {
|
|
53
|
+
fundingPnl: fundingPnl.toString(),
|
|
54
|
+
openingFee: openingFee.toString(),
|
|
55
|
+
priceVariationPnl: priceVariationPnl.toString(),
|
|
56
|
+
realizedPnl: realizedPnl.toString(),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.computeTradePnl = computeTradePnl;
|
|
60
|
+
//# sourceMappingURL=compute-trade-pnl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-trade-pnl.js","sourceRoot":"/","sources":["utils/compute-trade-pnl.ts"],"names":[],"mappings":";;;AAAA,yCAAqE;AA8BrE;;;;;;;;;;GAUG;AACH,SAAgB,eAAe,CAAC,KAAoB;IAEhD,IAAA,aAAa,GASX,KAAK,cATM,EACb,YAAY,GAQV,KAAK,aARK,EACZ,cAAc,GAOZ,KAAK,eAPO,EACd,GAAG,GAMD,KAAK,IANJ,EACH,aAAa,GAKX,KAAK,cALM,EACb,sBAAsB,GAIpB,KAAK,uBAJe,EACtB,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,oBAAoB;QACxC,oBAAoB,EAAE,YAAY;QAClC,4BAA4B,EAAE,yBAAyB;KACxD,CAAC,CAAC;IAEH,IAAM,WAAW,GAAG,iBAAiB,GAAG,UAAU,CAAC;IAEnD,OAAO;QACL,UAAU,EAAE,UAAU,CAAC,QAAQ,EAAE;QACjC,UAAU,EAAE,UAAU,CAAC,QAAQ,EAAE;QACjC,iBAAiB,EAAE,iBAAiB,CAAC,QAAQ,EAAE;QAC/C,WAAW,EAAE,WAAW,CAAC,QAAQ,EAAE;KACpC,CAAC;AACJ,CAAC;AApED,0CAoEC","sourcesContent":["import { calculateFundingPnl, calculatePricePnl } from './calculate';\n\nexport type TradePnlInput = {\n /** Position average entry price (position_last_price) — used for openingFee */\n avgEntryPrice: number;\n /** Executed base amount (signed — positive = buy, negative = sell) */\n executedBase: number;\n /** Execution price of this trade */\n executionPrice: number;\n /** Total fee for this execution (rUSD) */\n fee: number;\n /** Position base AFTER this execution */\n postOrderBase: number;\n /** Previous position base multiplier */\n previousBaseMultiplier: number | undefined;\n /** Previous position average entry funding value */\n previousEntryFundingValue: number | undefined;\n /** Previous position entry price (position_previous_last_price) */\n previousEntryPrice: number | undefined;\n /** Previous position funding value (market funding value at time of previous trade) */\n previousFundingValue: number | undefined;\n};\n\nexport type TradePnlResult = {\n fundingPnl: string;\n openingFee: string;\n priceVariationPnl: string;\n realizedPnl: string;\n} | null;\n\n/**\n * Compute execution-level PnL for a perpetual trade.\n *\n * Returns null when PnL is not applicable (position-extending executions,\n * missing previous state). Returns computed values only when a position is\n * being unwound or flipped.\n *\n * All inputs should be pre-normalized to numbers (from Prisma Decimal, string, etc.).\n * This function is the single source of truth for this computation — used by both\n * the REST transformer (packages/api) and the WS transformer (packages/bun-socket).\n */\nexport function computeTradePnl(input: TradePnlInput): TradePnlResult {\n const {\n avgEntryPrice,\n executedBase,\n executionPrice,\n fee,\n postOrderBase,\n previousBaseMultiplier,\n previousEntryFundingValue,\n previousEntryPrice,\n previousFundingValue,\n } = input;\n\n if (executedBase === 0) {\n return null;\n }\n\n const preOrderBase = postOrderBase - executedBase;\n\n // Position-extending: PnL not applicable\n if (\n preOrderBase === 0 ||\n (preOrderBase > 0 && executedBase > 0) ||\n (preOrderBase < 0 && executedBase < 0)\n ) {\n return null;\n }\n\n // Missing previous state: can't compute PnL\n if (\n previousEntryPrice == null ||\n previousEntryFundingValue == null ||\n previousFundingValue == null ||\n previousBaseMultiplier == null\n ) {\n return null;\n }\n\n // For unwinding: adjustedBase = -executedBase; for flipping: adjustedBase = preOrderBase\n const isUnwinding =\n (preOrderBase >= 0 && postOrderBase >= 0) ||\n (preOrderBase <= 0 && postOrderBase <= 0);\n const adjustedBase = isUnwinding ? -executedBase : preOrderBase;\n\n const openingFee =\n executionPrice !== 0 ? fee * (avgEntryPrice / executionPrice) : 0;\n\n const priceVariationPnl = calculatePricePnl({\n positionAdjustedBase: adjustedBase,\n positionAvgEntryPrice: previousEntryPrice,\n price: executionPrice,\n });\n\n const fundingPnl = calculateFundingPnl({\n marketBaseMultiplier: previousBaseMultiplier,\n marketFundingValue: previousFundingValue,\n positionAdjustedBase: adjustedBase,\n positionAvgEntryFundingValue: previousEntryFundingValue,\n });\n\n const realizedPnl = priceVariationPnl + fundingPnl;\n\n return {\n fundingPnl: fundingPnl.toString(),\n openingFee: openingFee.toString(),\n priceVariationPnl: priceVariationPnl.toString(),\n realizedPnl: realizedPnl.toString(),\n };\n}\n"]}
|
package/dist/utils/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./consts"), exports);
|
|
18
18
|
__exportStar(require("./points"), exports);
|
|
19
19
|
__exportStar(require("./calculate"), exports);
|
|
20
|
+
__exportStar(require("./compute-trade-pnl"), exports);
|
|
20
21
|
__exportStar(require("./struct"), exports);
|
|
21
22
|
__exportStar(require("./cooldown-interval"), exports);
|
|
22
23
|
__exportStar(require("./network"), exports);
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B;AAC5B,2CAAyB;AACzB,sDAAoC;AACpC,4CAA0B;AAC1B,2CAAyB;AACzB,0CAAwB;AACxB,8CAA4B;AAC5B,0CAAwB;AACxB,iDAA+B;AAC/B,wDAAsC;AACtC,8CAA4B;AAC5B,0CAAwB;AACxB,wDAAsC;AACtC,8CAA4B","sourcesContent":["export * from './consts';\nexport * from './points';\nexport * from './calculate';\nexport * from './struct';\nexport * from './cooldown-interval';\nexport * from './network';\nexport * from './number';\nexport * from './retry';\nexport * from './timestamp';\nexport * from './token';\nexport * from './task-wrapper';\nexport * from './whitelisted-wallets';\nexport * from './stringify';\nexport * from './delay';\nexport * from './transaction-receipt';\nexport * from './layerzero';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B;AAC5B,sDAAoC;AACpC,2CAAyB;AACzB,sDAAoC;AACpC,4CAA0B;AAC1B,2CAAyB;AACzB,0CAAwB;AACxB,8CAA4B;AAC5B,0CAAwB;AACxB,iDAA+B;AAC/B,wDAAsC;AACtC,8CAA4B;AAC5B,0CAAwB;AACxB,wDAAsC;AACtC,8CAA4B","sourcesContent":["export * from './consts';\nexport * from './points';\nexport * from './calculate';\nexport * from './compute-trade-pnl';\nexport * from './struct';\nexport * from './cooldown-interval';\nexport * from './network';\nexport * from './number';\nexport * from './retry';\nexport * from './timestamp';\nexport * from './token';\nexport * from './task-wrapper';\nexport * from './whitelisted-wallets';\nexport * from './stringify';\nexport * from './delay';\nexport * from './transaction-receipt';\nexport * from './layerzero';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.354.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
46
46
|
},
|
|
47
47
|
"packageManager": "pnpm@8.3.1",
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "84b500f29d92c10bd2015338ceebae1a2a24515b"
|
|
49
49
|
}
|
|
@@ -193,6 +193,34 @@ export type IncentivesApiSource = {
|
|
|
193
193
|
AddressParam & TradingCompetitionRoundParams,
|
|
194
194
|
{ code: string }
|
|
195
195
|
>;
|
|
196
|
+
|
|
197
|
+
'incentives/vip/wallet/:address': ApiEndpoint<
|
|
198
|
+
{
|
|
199
|
+
walletAddress: string;
|
|
200
|
+
isVip: boolean;
|
|
201
|
+
registered: boolean;
|
|
202
|
+
hasInviteLink: boolean;
|
|
203
|
+
registeredAt: number | null;
|
|
204
|
+
},
|
|
205
|
+
AddressParam
|
|
206
|
+
>;
|
|
207
|
+
|
|
208
|
+
'incentives/vip/wallet/:address/nonce': ApiEndpoint<
|
|
209
|
+
FailableResult<{
|
|
210
|
+
nonce: string;
|
|
211
|
+
message: string;
|
|
212
|
+
expiresAt: number;
|
|
213
|
+
}>,
|
|
214
|
+
AddressParam
|
|
215
|
+
>;
|
|
216
|
+
|
|
217
|
+
'incentives/vip/wallet/:address/register': ApiEndpoint<
|
|
218
|
+
FailableResult<{
|
|
219
|
+
inviteLink: string;
|
|
220
|
+
alreadyRegistered: boolean;
|
|
221
|
+
}>,
|
|
222
|
+
AddressParam
|
|
223
|
+
>;
|
|
196
224
|
};
|
|
197
225
|
|
|
198
226
|
export type IncentivesApi = {
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { calculateFundingPnl, calculatePricePnl } from './calculate';
|
|
2
|
+
|
|
3
|
+
export type TradePnlInput = {
|
|
4
|
+
/** Position average entry price (position_last_price) — used for openingFee */
|
|
5
|
+
avgEntryPrice: number;
|
|
6
|
+
/** Executed base amount (signed — positive = buy, negative = sell) */
|
|
7
|
+
executedBase: number;
|
|
8
|
+
/** Execution price of this trade */
|
|
9
|
+
executionPrice: number;
|
|
10
|
+
/** Total fee for this execution (rUSD) */
|
|
11
|
+
fee: number;
|
|
12
|
+
/** Position base AFTER this execution */
|
|
13
|
+
postOrderBase: number;
|
|
14
|
+
/** Previous position base multiplier */
|
|
15
|
+
previousBaseMultiplier: number | undefined;
|
|
16
|
+
/** Previous position average entry funding value */
|
|
17
|
+
previousEntryFundingValue: number | undefined;
|
|
18
|
+
/** Previous position entry price (position_previous_last_price) */
|
|
19
|
+
previousEntryPrice: number | undefined;
|
|
20
|
+
/** Previous position funding value (market funding value at time of previous trade) */
|
|
21
|
+
previousFundingValue: number | undefined;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type TradePnlResult = {
|
|
25
|
+
fundingPnl: string;
|
|
26
|
+
openingFee: string;
|
|
27
|
+
priceVariationPnl: string;
|
|
28
|
+
realizedPnl: string;
|
|
29
|
+
} | null;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Compute execution-level PnL for a perpetual trade.
|
|
33
|
+
*
|
|
34
|
+
* Returns null when PnL is not applicable (position-extending executions,
|
|
35
|
+
* missing previous state). Returns computed values only when a position is
|
|
36
|
+
* being unwound or flipped.
|
|
37
|
+
*
|
|
38
|
+
* All inputs should be pre-normalized to numbers (from Prisma Decimal, string, etc.).
|
|
39
|
+
* This function is the single source of truth for this computation — used by both
|
|
40
|
+
* the REST transformer (packages/api) and the WS transformer (packages/bun-socket).
|
|
41
|
+
*/
|
|
42
|
+
export function computeTradePnl(input: TradePnlInput): TradePnlResult {
|
|
43
|
+
const {
|
|
44
|
+
avgEntryPrice,
|
|
45
|
+
executedBase,
|
|
46
|
+
executionPrice,
|
|
47
|
+
fee,
|
|
48
|
+
postOrderBase,
|
|
49
|
+
previousBaseMultiplier,
|
|
50
|
+
previousEntryFundingValue,
|
|
51
|
+
previousEntryPrice,
|
|
52
|
+
previousFundingValue,
|
|
53
|
+
} = input;
|
|
54
|
+
|
|
55
|
+
if (executedBase === 0) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const preOrderBase = postOrderBase - executedBase;
|
|
60
|
+
|
|
61
|
+
// Position-extending: PnL not applicable
|
|
62
|
+
if (
|
|
63
|
+
preOrderBase === 0 ||
|
|
64
|
+
(preOrderBase > 0 && executedBase > 0) ||
|
|
65
|
+
(preOrderBase < 0 && executedBase < 0)
|
|
66
|
+
) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Missing previous state: can't compute PnL
|
|
71
|
+
if (
|
|
72
|
+
previousEntryPrice == null ||
|
|
73
|
+
previousEntryFundingValue == null ||
|
|
74
|
+
previousFundingValue == null ||
|
|
75
|
+
previousBaseMultiplier == null
|
|
76
|
+
) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// For unwinding: adjustedBase = -executedBase; for flipping: adjustedBase = preOrderBase
|
|
81
|
+
const isUnwinding =
|
|
82
|
+
(preOrderBase >= 0 && postOrderBase >= 0) ||
|
|
83
|
+
(preOrderBase <= 0 && postOrderBase <= 0);
|
|
84
|
+
const adjustedBase = isUnwinding ? -executedBase : preOrderBase;
|
|
85
|
+
|
|
86
|
+
const openingFee =
|
|
87
|
+
executionPrice !== 0 ? fee * (avgEntryPrice / executionPrice) : 0;
|
|
88
|
+
|
|
89
|
+
const priceVariationPnl = calculatePricePnl({
|
|
90
|
+
positionAdjustedBase: adjustedBase,
|
|
91
|
+
positionAvgEntryPrice: previousEntryPrice,
|
|
92
|
+
price: executionPrice,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const fundingPnl = calculateFundingPnl({
|
|
96
|
+
marketBaseMultiplier: previousBaseMultiplier,
|
|
97
|
+
marketFundingValue: previousFundingValue,
|
|
98
|
+
positionAdjustedBase: adjustedBase,
|
|
99
|
+
positionAvgEntryFundingValue: previousEntryFundingValue,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const realizedPnl = priceVariationPnl + fundingPnl;
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
fundingPnl: fundingPnl.toString(),
|
|
106
|
+
openingFee: openingFee.toString(),
|
|
107
|
+
priceVariationPnl: priceVariationPnl.toString(),
|
|
108
|
+
realizedPnl: realizedPnl.toString(),
|
|
109
|
+
};
|
|
110
|
+
}
|