-
Notifications
You must be signed in to change notification settings - Fork 79
/
spotMarket.ts
39 lines (37 loc) · 1.04 KB
/
spotMarket.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { BN } from '@coral-xyz/anchor';
import {
isVariant,
MarginCategory,
SpotBalanceType,
SpotMarketAccount,
} from '../types';
import { calculateAssetWeight, calculateLiabilityWeight } from './spotBalance';
import { MARGIN_PRECISION } from '../constants/numericConstants';
export function castNumberToSpotPrecision(
value: number | BN,
spotMarket: SpotMarketAccount
): BN {
if (typeof value === 'number') {
return new BN(value * Math.pow(10, spotMarket.decimals));
} else {
return value.mul(new BN(Math.pow(10, spotMarket.decimals)));
}
}
export function calculateSpotMarketMarginRatio(
market: SpotMarketAccount,
marginCategory: MarginCategory,
size: BN,
balanceType: SpotBalanceType
): number {
if (isVariant(balanceType, 'deposit')) {
const assetWeight = calculateAssetWeight(size, market, marginCategory);
return MARGIN_PRECISION.sub(assetWeight).toNumber();
} else {
const liabilityWeight = calculateLiabilityWeight(
size,
market,
marginCategory
);
return liabilityWeight.sub(MARGIN_PRECISION).toNumber();
}
}