@reyaxyz/account-figures 0.2.0 → 0.3.0

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/index.d.ts CHANGED
@@ -10,3 +10,5 @@ export { accountFigures, computeAccountFigures, MARGIN_ASSUMPTIONS, positionRisk
10
10
  export type { AccountFigures, PositionFigures, PositionRisk } from './figures';
11
11
  export { formatPct, formatUsd } from './format';
12
12
  export type { Rounding } from './format';
13
+ export { accountRiskResponse } from './response';
14
+ export type { AccountRiskInputs, PositionValuation } from './response';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatUsd = exports.formatPct = exports.positionRisk = exports.MARGIN_ASSUMPTIONS = exports.computeAccountFigures = exports.accountFigures = exports.RUSD = exports.imMultiplierFrom = exports.buildMarginSnapshot = exports.roundTowardMark = exports.marginUsage = exports.liquidationPrice = exports.distanceToLiquidation = exports.SETTLEMENT_RESERVE_RATE = exports.SETTLEMENT_RESERVE = exports.roundingAllowance = exports.project = exports.LIQUIDATION_THRESHOLD = exports.INITIAL_MARGIN = exports.applyFill = exports.admit = exports.RUSD_DECIMALS = exports.AccountFiguresInputError = void 0;
3
+ exports.accountRiskResponse = exports.formatUsd = exports.formatPct = exports.positionRisk = exports.MARGIN_ASSUMPTIONS = exports.computeAccountFigures = exports.accountFigures = exports.RUSD = exports.imMultiplierFrom = exports.buildMarginSnapshot = exports.roundTowardMark = exports.marginUsage = exports.liquidationPrice = exports.distanceToLiquidation = exports.SETTLEMENT_RESERVE_RATE = exports.SETTLEMENT_RESERVE = exports.roundingAllowance = exports.project = exports.LIQUIDATION_THRESHOLD = exports.INITIAL_MARGIN = exports.applyFill = exports.admit = exports.RUSD_DECIMALS = exports.AccountFiguresInputError = void 0;
4
4
  var errors_1 = require("./errors");
5
5
  Object.defineProperty(exports, "AccountFiguresInputError", { enumerable: true, get: function () { return errors_1.AccountFiguresInputError; } });
6
6
  var decimal_1 = require("./decimal");
@@ -31,3 +31,5 @@ Object.defineProperty(exports, "positionRisk", { enumerable: true, get: function
31
31
  var format_1 = require("./format");
32
32
  Object.defineProperty(exports, "formatPct", { enumerable: true, get: function () { return format_1.formatPct; } });
33
33
  Object.defineProperty(exports, "formatUsd", { enumerable: true, get: function () { return format_1.formatUsd; } });
34
+ var response_1 = require("./response");
35
+ Object.defineProperty(exports, "accountRiskResponse", { enumerable: true, get: function () { return response_1.accountRiskResponse; } });
@@ -0,0 +1,47 @@
1
+ import { MarginInputs } from './snapshot';
2
+ /** Added to a position only when the complete account can be valued. */
3
+ export interface PositionValuation {
4
+ markPrice: string;
5
+ notional: string;
6
+ unrealizedPnl: string;
7
+ liquidationMarginRequirement: string;
8
+ liquidationPrice: string | null;
9
+ hasCorrelatedCollateral: boolean;
10
+ updatedAt: number;
11
+ pricesUpdatedAt: number | null;
12
+ }
13
+ export interface AccountRiskInputs extends MarginInputs {
14
+ marketSummaries: readonly (MarginInputs['marketSummaries'][number] & {
15
+ pricesUpdatedAt?: number;
16
+ })[];
17
+ assetOraclePrices?: readonly (NonNullable<MarginInputs['assetOraclePrices']>[number] & {
18
+ updatedAt: number;
19
+ })[];
20
+ }
21
+ /** Keep source freshness separate from the time at which this response is calculated. */
22
+ export declare function accountRiskResponse(inputs: AccountRiskInputs, updatedAt: number): {
23
+ summary: {
24
+ updatedAt: number;
25
+ pricesUpdatedAt: number | null;
26
+ accountId: number;
27
+ collateralValue: string;
28
+ unrealizedPnl: string;
29
+ marginBalance: string;
30
+ initialMarginRequirement: string;
31
+ liquidationMarginRequirement: string;
32
+ freeCollateral: string;
33
+ initialMarginRatio: string | null;
34
+ liquidationMarginRatio: string | null;
35
+ };
36
+ positions: {
37
+ updatedAt: number;
38
+ pricesUpdatedAt: number | null;
39
+ symbol: string;
40
+ markPrice: string;
41
+ notional: string;
42
+ unrealizedPnl: string;
43
+ liquidationMarginRequirement: string;
44
+ liquidationPrice: string | null;
45
+ hasCorrelatedCollateral: boolean;
46
+ }[];
47
+ };
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.accountRiskResponse = void 0;
7
+ const figures_1 = require("./figures");
8
+ const snapshot_1 = require("./snapshot");
9
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
10
+ /** Keep source freshness separate from the time at which this response is calculated. */
11
+ function accountRiskResponse(inputs, updatedAt) {
12
+ const figures = (0, figures_1.computeAccountFigures)(inputs);
13
+ const timestamps = [];
14
+ for (const position of figures.positions) {
15
+ const timestamp = inputs.marketSummaries.find((row) => row.symbol === position.symbol)?.pricesUpdatedAt;
16
+ if (!Number.isSafeInteger(timestamp) || timestamp <= 0) {
17
+ throw new Error(`Missing mark timestamp for ${position.symbol}`);
18
+ }
19
+ timestamps.push(timestamp);
20
+ }
21
+ for (const collateral of figures.collateral) {
22
+ if (collateral.asset === snapshot_1.RUSD || new bignumber_js_1.default(collateral.balance).isZero())
23
+ continue;
24
+ const timestamp = inputs.assetOraclePrices?.find((row) => row.asset === collateral.asset)?.updatedAt;
25
+ if (!Number.isSafeInteger(timestamp) || timestamp <= 0) {
26
+ throw new Error(`Missing collateral timestamp for ${collateral.asset}`);
27
+ }
28
+ timestamps.push(timestamp);
29
+ }
30
+ const freshness = {
31
+ updatedAt,
32
+ pricesUpdatedAt: timestamps.length ? Math.min(...timestamps) : null,
33
+ };
34
+ return {
35
+ summary: {
36
+ accountId: Number(inputs.accountId),
37
+ collateralValue: figures.realBalance,
38
+ unrealizedPnl: figures.unrealizedPnl,
39
+ marginBalance: figures.equity,
40
+ initialMarginRequirement: figures.imr,
41
+ liquidationMarginRequirement: figures.lmr,
42
+ freeCollateral: figures.availableMargin,
43
+ initialMarginRatio: ratio(figures.initialMarginUsage),
44
+ liquidationMarginRatio: ratio(figures.liquidationMarginUsage),
45
+ ...freshness,
46
+ },
47
+ positions: figures.positions
48
+ .map((position) => ({
49
+ symbol: position.symbol,
50
+ markPrice: position.markPrice,
51
+ notional: position.notional,
52
+ unrealizedPnl: position.unrealizedPnl,
53
+ liquidationMarginRequirement: position.lmr,
54
+ liquidationPrice: position.liquidationPrice,
55
+ hasCorrelatedCollateral: position.assumesFixedCollateral,
56
+ ...freshness,
57
+ }))
58
+ .sort((a, b) => a.symbol.localeCompare(b.symbol)),
59
+ };
60
+ }
61
+ exports.accountRiskResponse = accountRiskResponse;
62
+ const ratio = (value) => value === null
63
+ ? null
64
+ : new bignumber_js_1.default(value).decimalPlaces(18, bignumber_js_1.default.ROUND_UP).toFixed();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/account-figures",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Reya account figures from public V2 data: equity, margin requirements, margin usage and liquidation prices",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -38,8 +38,8 @@
38
38
  "bignumber.js": "9.1.2"
39
39
  },
40
40
  "devDependencies": {
41
- "@reyaxyz/api-v2-sdk": "0.315.0",
41
+ "@reyaxyz/api-v2-sdk": "0.316.0",
42
42
  "typescript": "5.3.3"
43
43
  },
44
- "gitHead": "1b24b3638169c934eae514c43fc6a91bdecf667f"
44
+ "gitHead": "c79e1c00f842259ddcb7c435ed799ea0b49e9939"
45
45
  }