@drift-labs/sdk 2.131.0-beta.0 → 2.131.0-beta.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/VERSION +1 -1
- package/lib/browser/events/types.d.ts +1 -1
- package/lib/browser/math/insurance.d.ts +2 -1
- package/lib/browser/math/insurance.js +7 -6
- package/lib/browser/math/oracles.d.ts +2 -2
- package/lib/browser/math/oracles.js +32 -32
- package/lib/browser/orderSubscriber/OrderSubscriber.js +4 -3
- package/lib/node/events/types.d.ts +1 -1
- package/lib/node/math/insurance.d.ts +2 -1
- package/lib/node/math/insurance.d.ts.map +1 -1
- package/lib/node/math/insurance.js +7 -6
- package/lib/node/math/oracles.d.ts +2 -2
- package/lib/node/math/oracles.d.ts.map +1 -1
- package/lib/node/math/oracles.js +32 -32
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +4 -3
- package/package.json +1 -1
- package/src/events/types.ts +1 -1
- package/src/math/insurance.ts +2 -1
- package/src/math/oracles.ts +9 -7
- package/src/orderSubscriber/OrderSubscriber.ts +2 -1
- package/tests/bn/test.ts +2 -1
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.131.0-beta.
|
|
1
|
+
2.131.0-beta.1
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Commitment, PublicKey, TransactionSignature } from '@solana/web3.js';
|
|
3
|
-
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord, SpotInterestRecord, InsuranceFundStakeRecord, CurveRecord, SwapRecord, SpotMarketVaultDepositRecord, SignedMsgOrderRecord, DeleteUserRecord, FuelSweepRecord, FuelSeasonRecord, InsuranceFundSwapRecord, TransferProtocolIfSharesToRevenuePoolRecord } from '../
|
|
3
|
+
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord, SpotInterestRecord, InsuranceFundStakeRecord, CurveRecord, SwapRecord, SpotMarketVaultDepositRecord, SignedMsgOrderRecord, DeleteUserRecord, FuelSweepRecord, FuelSeasonRecord, InsuranceFundSwapRecord, TransferProtocolIfSharesToRevenuePoolRecord } from '../types';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
export type EventSubscriptionOptions = {
|
|
6
6
|
address?: PublicKey;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { BN
|
|
2
|
+
import { BN } from '@coral-xyz/anchor';
|
|
3
|
+
import { SpotMarketAccount } from '../types';
|
|
3
4
|
export declare function nextRevenuePoolSettleApr(spotMarket: SpotMarketAccount, vaultBalance: BN, // vault token amount
|
|
4
5
|
amount?: BN): number;
|
|
5
6
|
export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
|
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.unstakeSharesToAmountWithOpenRequest = exports.unstakeSharesToAmount = exports.stakeAmountToShares = exports.nextRevenuePoolSettleApr = void 0;
|
|
4
4
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const index_1 = require("../index");
|
|
6
5
|
const spotBalance_1 = require("../math/spotBalance");
|
|
6
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
7
|
+
const types_1 = require("../types");
|
|
7
8
|
function nextRevenuePoolSettleApr(spotMarket, vaultBalance, // vault token amount
|
|
8
9
|
amount // delta token amount
|
|
9
10
|
) {
|
|
10
|
-
const MAX_APR = new
|
|
11
|
+
const MAX_APR = new anchor_1.BN(10).mul(numericConstants_1.PERCENTAGE_PRECISION); // 1000% APR
|
|
11
12
|
// Conmputing the APR:
|
|
12
|
-
const revenuePoolBN = (0, spotBalance_1.getTokenAmount)(spotMarket.revenuePool.scaledBalance, spotMarket,
|
|
13
|
+
const revenuePoolBN = (0, spotBalance_1.getTokenAmount)(spotMarket.revenuePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
13
14
|
const payoutRatio = 0.1;
|
|
14
15
|
const ratioForStakers = spotMarket.insuranceFund.totalFactor > 0 &&
|
|
15
16
|
spotMarket.insuranceFund.userFactor > 0 &&
|
|
@@ -47,7 +48,7 @@ exports.stakeAmountToShares = stakeAmountToShares;
|
|
|
47
48
|
function unstakeSharesToAmount(nShares, totalIfShares, insuranceFundVaultBalance) {
|
|
48
49
|
let amount;
|
|
49
50
|
if (totalIfShares.gt(numericConstants_1.ZERO)) {
|
|
50
|
-
amount =
|
|
51
|
+
amount = anchor_1.BN.max(numericConstants_1.ZERO, nShares.mul(insuranceFundVaultBalance).div(totalIfShares));
|
|
51
52
|
}
|
|
52
53
|
else {
|
|
53
54
|
amount = numericConstants_1.ZERO;
|
|
@@ -58,7 +59,7 @@ exports.unstakeSharesToAmount = unstakeSharesToAmount;
|
|
|
58
59
|
function unstakeSharesToAmountWithOpenRequest(nShares, withdrawRequestShares, withdrawRequestAmount, totalIfShares, insuranceFundVaultBalance) {
|
|
59
60
|
let stakedAmount;
|
|
60
61
|
if (totalIfShares.gt(numericConstants_1.ZERO)) {
|
|
61
|
-
stakedAmount =
|
|
62
|
+
stakedAmount = anchor_1.BN.max(numericConstants_1.ZERO, nShares
|
|
62
63
|
.sub(withdrawRequestShares)
|
|
63
64
|
.mul(insuranceFundVaultBalance)
|
|
64
65
|
.div(totalIfShares));
|
|
@@ -66,7 +67,7 @@ function unstakeSharesToAmountWithOpenRequest(nShares, withdrawRequestShares, wi
|
|
|
66
67
|
else {
|
|
67
68
|
stakedAmount = numericConstants_1.ZERO;
|
|
68
69
|
}
|
|
69
|
-
const withdrawAmount =
|
|
70
|
+
const withdrawAmount = anchor_1.BN.min(withdrawRequestAmount, withdrawRequestShares.mul(insuranceFundVaultBalance).div(totalIfShares));
|
|
70
71
|
const amount = withdrawAmount.add(stakedAmount);
|
|
71
72
|
return amount;
|
|
72
73
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
-
import { AMM, OracleGuardRails } from '../types';
|
|
4
|
+
import { AMM, HistoricalOracleData, OracleGuardRails, OracleSource, PerpMarketAccount } from '../types';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
import { BN
|
|
6
|
+
import { BN } from '@coral-xyz/anchor';
|
|
7
7
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: Pick<OraclePriceData, 'price'>): [BN, BN];
|
|
8
8
|
export declare function getMaxConfidenceIntervalMultiplier(market: PerpMarketAccount): BN;
|
|
9
9
|
export declare function isOracleValid(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
|
|
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMultipleBetweenOracleSources = exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const index_1 = require("../index");
|
|
7
6
|
const assert_1 = require("../assert/assert");
|
|
7
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
8
8
|
function oraclePriceBands(market, oraclePriceData) {
|
|
9
9
|
const maxPercentDiff = market.marginRatioInitial - market.marginRatioMaintenance;
|
|
10
10
|
const offset = oraclePriceData.price
|
|
11
|
-
.mul(new
|
|
11
|
+
.mul(new anchor_1.BN(maxPercentDiff))
|
|
12
12
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
13
13
|
(0, assert_1.assert)(offset.gte(numericConstants_1.ZERO));
|
|
14
14
|
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
@@ -17,19 +17,19 @@ exports.oraclePriceBands = oraclePriceBands;
|
|
|
17
17
|
function getMaxConfidenceIntervalMultiplier(market) {
|
|
18
18
|
let maxConfidenceIntervalMultiplier;
|
|
19
19
|
if ((0, types_1.isVariant)(market.contractTier, 'a')) {
|
|
20
|
-
maxConfidenceIntervalMultiplier = new
|
|
20
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(1);
|
|
21
21
|
}
|
|
22
22
|
else if ((0, types_1.isVariant)(market.contractTier, 'b')) {
|
|
23
|
-
maxConfidenceIntervalMultiplier = new
|
|
23
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(1);
|
|
24
24
|
}
|
|
25
25
|
else if ((0, types_1.isVariant)(market.contractTier, 'c')) {
|
|
26
|
-
maxConfidenceIntervalMultiplier = new
|
|
26
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(2);
|
|
27
27
|
}
|
|
28
28
|
else if ((0, types_1.isVariant)(market.contractTier, 'speculative')) {
|
|
29
|
-
maxConfidenceIntervalMultiplier = new
|
|
29
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(10);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
|
-
maxConfidenceIntervalMultiplier = new
|
|
32
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(50);
|
|
33
33
|
}
|
|
34
34
|
return maxConfidenceIntervalMultiplier;
|
|
35
35
|
}
|
|
@@ -39,17 +39,17 @@ function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
|
39
39
|
const amm = market.amm;
|
|
40
40
|
const isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
41
41
|
const isOraclePriceTooVolatile = oraclePriceData.price
|
|
42
|
-
.div(
|
|
42
|
+
.div(anchor_1.BN.max(numericConstants_1.ONE, amm.historicalOracleData.lastOraclePriceTwap))
|
|
43
43
|
.gt(oracleGuardRails.validity.tooVolatileRatio) ||
|
|
44
44
|
amm.historicalOracleData.lastOraclePriceTwap
|
|
45
|
-
.div(
|
|
45
|
+
.div(anchor_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
|
|
46
46
|
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
47
47
|
const maxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier(market);
|
|
48
|
-
const isConfidenceTooLarge =
|
|
48
|
+
const isConfidenceTooLarge = anchor_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence)
|
|
49
49
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
50
50
|
.div(oraclePriceData.price)
|
|
51
51
|
.gt(oracleGuardRails.validity.confidenceIntervalMaxSize.mul(maxConfidenceIntervalMultiplier));
|
|
52
|
-
const oracleIsStale = new
|
|
52
|
+
const oracleIsStale = new anchor_1.BN(slot)
|
|
53
53
|
.sub(oraclePriceData.slot)
|
|
54
54
|
.gt(oracleGuardRails.validity.slotsBeforeStaleForAmm);
|
|
55
55
|
return !(!oraclePriceData.hasSufficientNumberOfDataPoints ||
|
|
@@ -61,7 +61,7 @@ function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
|
61
61
|
exports.isOracleValid = isOracleValid;
|
|
62
62
|
function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
63
63
|
const sinceLastUpdate = now.sub(amm.historicalOracleData.lastOraclePriceTwapTs);
|
|
64
|
-
const sinceStart =
|
|
64
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.FIVE_MINUTE.sub(sinceLastUpdate));
|
|
65
65
|
const oracleTwap5min = amm.historicalOracleData.lastOraclePriceTwap5Min
|
|
66
66
|
.mul(sinceStart)
|
|
67
67
|
.add(oraclePriceData.price)
|
|
@@ -69,7 +69,7 @@ function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
|
69
69
|
.div(sinceStart.add(sinceLastUpdate));
|
|
70
70
|
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
71
71
|
const oracleSpreadPct = oracleSpread.mul(numericConstants_1.PRICE_PRECISION).div(oracleTwap5min);
|
|
72
|
-
const maxDivergence =
|
|
72
|
+
const maxDivergence = anchor_1.BN.max(oracleGuardRails.priceDivergence.markOraclePercentDivergence, numericConstants_1.PERCENTAGE_PRECISION.div(new anchor_1.BN(10)));
|
|
73
73
|
const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);
|
|
74
74
|
return tooDivergent;
|
|
75
75
|
}
|
|
@@ -84,10 +84,10 @@ function calculateLiveOracleTwap(histOracleData, oraclePriceData, now, period) {
|
|
|
84
84
|
// period = amm.fundingPeriod;
|
|
85
85
|
oracleTwap = histOracleData.lastOraclePriceTwap;
|
|
86
86
|
}
|
|
87
|
-
const sinceLastUpdate =
|
|
88
|
-
const sinceStart =
|
|
89
|
-
const clampRange = oracleTwap.div(new
|
|
90
|
-
const clampedOraclePrice =
|
|
87
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ONE, now.sub(histOracleData.lastOraclePriceTwapTs));
|
|
88
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, period.sub(sinceLastUpdate));
|
|
89
|
+
const clampRange = oracleTwap.div(new anchor_1.BN(3));
|
|
90
|
+
const clampedOraclePrice = anchor_1.BN.min(oracleTwap.add(clampRange), anchor_1.BN.max(oraclePriceData.price, oracleTwap.sub(clampRange)));
|
|
91
91
|
const newOracleTwap = oracleTwap
|
|
92
92
|
.mul(sinceStart)
|
|
93
93
|
.add(clampedOraclePrice.mul(sinceLastUpdate))
|
|
@@ -96,27 +96,27 @@ function calculateLiveOracleTwap(histOracleData, oraclePriceData, now, period) {
|
|
|
96
96
|
}
|
|
97
97
|
exports.calculateLiveOracleTwap = calculateLiveOracleTwap;
|
|
98
98
|
function calculateLiveOracleStd(amm, oraclePriceData, now) {
|
|
99
|
-
const sinceLastUpdate =
|
|
100
|
-
const sinceStart =
|
|
99
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ONE, now.sub(amm.historicalOracleData.lastOraclePriceTwapTs));
|
|
100
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, amm.fundingPeriod.sub(sinceLastUpdate));
|
|
101
101
|
const liveOracleTwap = calculateLiveOracleTwap(amm.historicalOracleData, oraclePriceData, now, amm.fundingPeriod);
|
|
102
102
|
const liveOracleTwap5MIN = calculateLiveOracleTwap(amm.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE);
|
|
103
|
-
const priceDeltaVsTwap =
|
|
103
|
+
const priceDeltaVsTwap = anchor_1.BN.max(oraclePriceData.price.sub(liveOracleTwap).abs(), oraclePriceData.price.sub(liveOracleTwap5MIN).abs());
|
|
104
104
|
const oracleStd = priceDeltaVsTwap.add(amm.oracleStd.mul(sinceStart).div(sinceStart.add(sinceLastUpdate)));
|
|
105
105
|
return oracleStd;
|
|
106
106
|
}
|
|
107
107
|
exports.calculateLiveOracleStd = calculateLiveOracleStd;
|
|
108
108
|
function getNewOracleConfPct(amm, oraclePriceData, reservePrice, now) {
|
|
109
109
|
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
110
|
-
const sinceLastUpdate =
|
|
110
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ZERO, now.sub(amm.historicalOracleData.lastOraclePriceTwapTs));
|
|
111
111
|
let lowerBoundConfPct = amm.lastOracleConfPct;
|
|
112
112
|
if (sinceLastUpdate.gt(numericConstants_1.ZERO)) {
|
|
113
|
-
const lowerBoundConfDivisor =
|
|
113
|
+
const lowerBoundConfDivisor = anchor_1.BN.max(new anchor_1.BN(21).sub(sinceLastUpdate), new anchor_1.BN(5));
|
|
114
114
|
lowerBoundConfPct = amm.lastOracleConfPct.sub(amm.lastOracleConfPct.div(lowerBoundConfDivisor));
|
|
115
115
|
}
|
|
116
116
|
const confIntervalPct = confInterval
|
|
117
117
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
118
118
|
.div(reservePrice);
|
|
119
|
-
const confIntervalPctResult =
|
|
119
|
+
const confIntervalPctResult = anchor_1.BN.max(confIntervalPct, lowerBoundConfPct);
|
|
120
120
|
return confIntervalPctResult;
|
|
121
121
|
}
|
|
122
122
|
exports.getNewOracleConfPct = getNewOracleConfPct;
|
|
@@ -136,36 +136,36 @@ exports.trimVaaSignatures = trimVaaSignatures;
|
|
|
136
136
|
function getMultipleBetweenOracleSources(firstOracleSource, secondOracleSource) {
|
|
137
137
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
138
138
|
(0, types_1.isVariant)(secondOracleSource, 'pyth1MPull')) {
|
|
139
|
-
return { numerator: new
|
|
139
|
+
return { numerator: new anchor_1.BN(1000000), denominator: new anchor_1.BN(1) };
|
|
140
140
|
}
|
|
141
141
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
142
142
|
(0, types_1.isVariant)(secondOracleSource, 'pyth1KPull')) {
|
|
143
|
-
return { numerator: new
|
|
143
|
+
return { numerator: new anchor_1.BN(1000), denominator: new anchor_1.BN(1) };
|
|
144
144
|
}
|
|
145
145
|
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1MPull') &&
|
|
146
146
|
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
147
|
-
return { numerator: new
|
|
147
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000000) };
|
|
148
148
|
}
|
|
149
149
|
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1KPull') &&
|
|
150
150
|
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
151
|
-
return { numerator: new
|
|
151
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000) };
|
|
152
152
|
}
|
|
153
153
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer') &&
|
|
154
154
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer1M')) {
|
|
155
|
-
return { numerator: new
|
|
155
|
+
return { numerator: new anchor_1.BN(1000000), denominator: new anchor_1.BN(1) };
|
|
156
156
|
}
|
|
157
157
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer') &&
|
|
158
158
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer1K')) {
|
|
159
|
-
return { numerator: new
|
|
159
|
+
return { numerator: new anchor_1.BN(1000), denominator: new anchor_1.BN(1) };
|
|
160
160
|
}
|
|
161
161
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer1M') &&
|
|
162
162
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer')) {
|
|
163
|
-
return { numerator: new
|
|
163
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000000) };
|
|
164
164
|
}
|
|
165
165
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer1K') &&
|
|
166
166
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer')) {
|
|
167
|
-
return { numerator: new
|
|
167
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000) };
|
|
168
168
|
}
|
|
169
|
-
return { numerator: new
|
|
169
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1) };
|
|
170
170
|
}
|
|
171
171
|
exports.getMultipleBetweenOracleSources = getMultipleBetweenOracleSources;
|
|
@@ -8,11 +8,12 @@ const DLOB_1 = require("../dlob/DLOB");
|
|
|
8
8
|
const PollingSubscription_1 = require("./PollingSubscription");
|
|
9
9
|
const WebsocketSubscription_1 = require("./WebsocketSubscription");
|
|
10
10
|
const events_1 = require("events");
|
|
11
|
-
const index_1 = require("../index");
|
|
12
11
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
13
12
|
const user_1 = require("../decode/user");
|
|
14
13
|
const grpcSubscription_1 = require("./grpcSubscription");
|
|
15
14
|
const userStatus_1 = require("../math/userStatus");
|
|
15
|
+
const orders_1 = require("../math/orders");
|
|
16
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
16
17
|
class OrderSubscriber {
|
|
17
18
|
constructor(config) {
|
|
18
19
|
var _a, _b, _c, _d, _e;
|
|
@@ -163,8 +164,8 @@ class OrderSubscriber {
|
|
|
163
164
|
for (const order of userAccount.orders) {
|
|
164
165
|
let baseAssetAmount = order.baseAssetAmount;
|
|
165
166
|
if (order.reduceOnly) {
|
|
166
|
-
const existingBaseAmount = ((_a = userAccount.perpPositions.find((pos) => pos.marketIndex === order.marketIndex && pos.openOrders > 0)) === null || _a === void 0 ? void 0 : _a.baseAssetAmount) ||
|
|
167
|
-
baseAssetAmount = (0,
|
|
167
|
+
const existingBaseAmount = ((_a = userAccount.perpPositions.find((pos) => pos.marketIndex === order.marketIndex && pos.openOrders > 0)) === null || _a === void 0 ? void 0 : _a.baseAssetAmount) || numericConstants_1.ZERO;
|
|
168
|
+
baseAssetAmount = (0, orders_1.calculateOrderBaseAssetAmount)(order, existingBaseAmount);
|
|
168
169
|
}
|
|
169
170
|
dlob.insertOrder(order, key, slot, protectedMaker, baseAssetAmount);
|
|
170
171
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Commitment, PublicKey, TransactionSignature } from '@solana/web3.js';
|
|
3
|
-
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord, SpotInterestRecord, InsuranceFundStakeRecord, CurveRecord, SwapRecord, SpotMarketVaultDepositRecord, SignedMsgOrderRecord, DeleteUserRecord, FuelSweepRecord, FuelSeasonRecord, InsuranceFundSwapRecord, TransferProtocolIfSharesToRevenuePoolRecord } from '../
|
|
3
|
+
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord, SpotInterestRecord, InsuranceFundStakeRecord, CurveRecord, SwapRecord, SpotMarketVaultDepositRecord, SignedMsgOrderRecord, DeleteUserRecord, FuelSweepRecord, FuelSeasonRecord, InsuranceFundSwapRecord, TransferProtocolIfSharesToRevenuePoolRecord } from '../types';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
export type EventSubscriptionOptions = {
|
|
6
6
|
address?: PublicKey;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { BN
|
|
2
|
+
import { BN } from '@coral-xyz/anchor';
|
|
3
|
+
import { SpotMarketAccount } from '../types';
|
|
3
4
|
export declare function nextRevenuePoolSettleApr(spotMarket: SpotMarketAccount, vaultBalance: BN, // vault token amount
|
|
4
5
|
amount?: BN): number;
|
|
5
6
|
export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insurance.d.ts","sourceRoot":"","sources":["../../../src/math/insurance.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"insurance.d.ts","sourceRoot":"","sources":["../../../src/math/insurance.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AACvC,OAAO,EAAmB,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE9D,wBAAgB,wBAAwB,CACvC,UAAU,EAAE,iBAAiB,EAC7B,YAAY,EAAE,EAAE,EAAE,qBAAqB;AACvC,MAAM,CAAC,EAAE,EAAE,GACT,MAAM,CAuCR;AAED,wBAAgB,mBAAmB,CAClC,MAAM,EAAE,EAAE,EACV,aAAa,EAAE,EAAE,EACjB,yBAAyB,EAAE,EAAE,GAC3B,EAAE,CASJ;AAED,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,EAAE,EACX,aAAa,EAAE,EAAE,EACjB,yBAAyB,EAAE,EAAE,GAC3B,EAAE,CAYJ;AAED,wBAAgB,oCAAoC,CACnD,OAAO,EAAE,EAAE,EACX,qBAAqB,EAAE,EAAE,EACzB,qBAAqB,EAAE,EAAE,EACzB,aAAa,EAAE,EAAE,EACjB,yBAAyB,EAAE,EAAE,GAC3B,EAAE,CAqBJ"}
|
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.unstakeSharesToAmountWithOpenRequest = exports.unstakeSharesToAmount = exports.stakeAmountToShares = exports.nextRevenuePoolSettleApr = void 0;
|
|
4
4
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const index_1 = require("../index");
|
|
6
5
|
const spotBalance_1 = require("../math/spotBalance");
|
|
6
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
7
|
+
const types_1 = require("../types");
|
|
7
8
|
function nextRevenuePoolSettleApr(spotMarket, vaultBalance, // vault token amount
|
|
8
9
|
amount // delta token amount
|
|
9
10
|
) {
|
|
10
|
-
const MAX_APR = new
|
|
11
|
+
const MAX_APR = new anchor_1.BN(10).mul(numericConstants_1.PERCENTAGE_PRECISION); // 1000% APR
|
|
11
12
|
// Conmputing the APR:
|
|
12
|
-
const revenuePoolBN = (0, spotBalance_1.getTokenAmount)(spotMarket.revenuePool.scaledBalance, spotMarket,
|
|
13
|
+
const revenuePoolBN = (0, spotBalance_1.getTokenAmount)(spotMarket.revenuePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
13
14
|
const payoutRatio = 0.1;
|
|
14
15
|
const ratioForStakers = spotMarket.insuranceFund.totalFactor > 0 &&
|
|
15
16
|
spotMarket.insuranceFund.userFactor > 0 &&
|
|
@@ -47,7 +48,7 @@ exports.stakeAmountToShares = stakeAmountToShares;
|
|
|
47
48
|
function unstakeSharesToAmount(nShares, totalIfShares, insuranceFundVaultBalance) {
|
|
48
49
|
let amount;
|
|
49
50
|
if (totalIfShares.gt(numericConstants_1.ZERO)) {
|
|
50
|
-
amount =
|
|
51
|
+
amount = anchor_1.BN.max(numericConstants_1.ZERO, nShares.mul(insuranceFundVaultBalance).div(totalIfShares));
|
|
51
52
|
}
|
|
52
53
|
else {
|
|
53
54
|
amount = numericConstants_1.ZERO;
|
|
@@ -58,7 +59,7 @@ exports.unstakeSharesToAmount = unstakeSharesToAmount;
|
|
|
58
59
|
function unstakeSharesToAmountWithOpenRequest(nShares, withdrawRequestShares, withdrawRequestAmount, totalIfShares, insuranceFundVaultBalance) {
|
|
59
60
|
let stakedAmount;
|
|
60
61
|
if (totalIfShares.gt(numericConstants_1.ZERO)) {
|
|
61
|
-
stakedAmount =
|
|
62
|
+
stakedAmount = anchor_1.BN.max(numericConstants_1.ZERO, nShares
|
|
62
63
|
.sub(withdrawRequestShares)
|
|
63
64
|
.mul(insuranceFundVaultBalance)
|
|
64
65
|
.div(totalIfShares));
|
|
@@ -66,7 +67,7 @@ function unstakeSharesToAmountWithOpenRequest(nShares, withdrawRequestShares, wi
|
|
|
66
67
|
else {
|
|
67
68
|
stakedAmount = numericConstants_1.ZERO;
|
|
68
69
|
}
|
|
69
|
-
const withdrawAmount =
|
|
70
|
+
const withdrawAmount = anchor_1.BN.min(withdrawRequestAmount, withdrawRequestShares.mul(insuranceFundVaultBalance).div(totalIfShares));
|
|
70
71
|
const amount = withdrawAmount.add(stakedAmount);
|
|
71
72
|
return amount;
|
|
72
73
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
-
import { AMM, OracleGuardRails } from '../types';
|
|
4
|
+
import { AMM, HistoricalOracleData, OracleGuardRails, OracleSource, PerpMarketAccount } from '../types';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
import { BN
|
|
6
|
+
import { BN } from '@coral-xyz/anchor';
|
|
7
7
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: Pick<OraclePriceData, 'price'>): [BN, BN];
|
|
8
8
|
export declare function getMaxConfidenceIntervalMultiplier(market: PerpMarketAccount): BN;
|
|
9
9
|
export declare function isOracleValid(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oracles.d.ts","sourceRoot":"","sources":["../../../src/math/oracles.ts"],"names":[],"mappings":";;;AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"oracles.d.ts","sourceRoot":"","sources":["../../../src/math/oracles.ts"],"names":[],"mappings":";;;AAAA,OAAO,EACN,GAAG,EACH,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,iBAAiB,EAEjB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAWnD,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AAEvC,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,iBAAiB,EACzB,eAAe,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,CAAC,GAC7C,CAAC,EAAE,EAAE,EAAE,CAAC,CAUV;AAED,wBAAgB,kCAAkC,CACjD,MAAM,EAAE,iBAAiB,GACvB,EAAE,CAcJ;AAED,wBAAgB,aAAa,CAC5B,MAAM,EAAE,iBAAiB,EACzB,eAAe,EAAE,eAAe,EAChC,gBAAgB,EAAE,gBAAgB,EAClC,IAAI,EAAE,MAAM,GACV,OAAO,CAmCT;AAED,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,GAAG,EACR,eAAe,EAAE,eAAe,EAChC,gBAAgB,EAAE,gBAAgB,EAClC,GAAG,EAAE,EAAE,GACL,OAAO,CAsBT;AAED,wBAAgB,uBAAuB,CACtC,cAAc,EAAE,oBAAoB,EACpC,eAAe,EAAE,eAAe,EAChC,GAAG,EAAE,EAAE,EACP,MAAM,EAAE,EAAE,GACR,EAAE,CA6BJ;AAED,wBAAgB,sBAAsB,CACrC,GAAG,EAAE,GAAG,EACR,eAAe,EAAE,eAAe,EAChC,GAAG,EAAE,EAAE,GACL,EAAE,CA+BJ;AAED,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,GAAG,EACR,eAAe,EAAE,eAAe,EAChC,YAAY,EAAE,EAAE,EAChB,GAAG,EAAE,EAAE,GACL,EAAE,CAwBJ;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,SAAI,GAAG,MAAM,CAe5D;AAED,wBAAgB,+BAA+B,CAC9C,iBAAiB,EAAE,YAAY,EAC/B,kBAAkB,EAAE,YAAY,GAC9B;IAAE,SAAS,EAAE,EAAE,CAAC;IAAC,WAAW,EAAE,EAAE,CAAA;CAAE,CA0DpC"}
|
package/lib/node/math/oracles.js
CHANGED
|
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMultipleBetweenOracleSources = exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const index_1 = require("../index");
|
|
7
6
|
const assert_1 = require("../assert/assert");
|
|
7
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
8
8
|
function oraclePriceBands(market, oraclePriceData) {
|
|
9
9
|
const maxPercentDiff = market.marginRatioInitial - market.marginRatioMaintenance;
|
|
10
10
|
const offset = oraclePriceData.price
|
|
11
|
-
.mul(new
|
|
11
|
+
.mul(new anchor_1.BN(maxPercentDiff))
|
|
12
12
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
13
13
|
(0, assert_1.assert)(offset.gte(numericConstants_1.ZERO));
|
|
14
14
|
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
@@ -17,19 +17,19 @@ exports.oraclePriceBands = oraclePriceBands;
|
|
|
17
17
|
function getMaxConfidenceIntervalMultiplier(market) {
|
|
18
18
|
let maxConfidenceIntervalMultiplier;
|
|
19
19
|
if ((0, types_1.isVariant)(market.contractTier, 'a')) {
|
|
20
|
-
maxConfidenceIntervalMultiplier = new
|
|
20
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(1);
|
|
21
21
|
}
|
|
22
22
|
else if ((0, types_1.isVariant)(market.contractTier, 'b')) {
|
|
23
|
-
maxConfidenceIntervalMultiplier = new
|
|
23
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(1);
|
|
24
24
|
}
|
|
25
25
|
else if ((0, types_1.isVariant)(market.contractTier, 'c')) {
|
|
26
|
-
maxConfidenceIntervalMultiplier = new
|
|
26
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(2);
|
|
27
27
|
}
|
|
28
28
|
else if ((0, types_1.isVariant)(market.contractTier, 'speculative')) {
|
|
29
|
-
maxConfidenceIntervalMultiplier = new
|
|
29
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(10);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
|
-
maxConfidenceIntervalMultiplier = new
|
|
32
|
+
maxConfidenceIntervalMultiplier = new anchor_1.BN(50);
|
|
33
33
|
}
|
|
34
34
|
return maxConfidenceIntervalMultiplier;
|
|
35
35
|
}
|
|
@@ -39,17 +39,17 @@ function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
|
39
39
|
const amm = market.amm;
|
|
40
40
|
const isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
41
41
|
const isOraclePriceTooVolatile = oraclePriceData.price
|
|
42
|
-
.div(
|
|
42
|
+
.div(anchor_1.BN.max(numericConstants_1.ONE, amm.historicalOracleData.lastOraclePriceTwap))
|
|
43
43
|
.gt(oracleGuardRails.validity.tooVolatileRatio) ||
|
|
44
44
|
amm.historicalOracleData.lastOraclePriceTwap
|
|
45
|
-
.div(
|
|
45
|
+
.div(anchor_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
|
|
46
46
|
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
47
47
|
const maxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier(market);
|
|
48
|
-
const isConfidenceTooLarge =
|
|
48
|
+
const isConfidenceTooLarge = anchor_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence)
|
|
49
49
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
50
50
|
.div(oraclePriceData.price)
|
|
51
51
|
.gt(oracleGuardRails.validity.confidenceIntervalMaxSize.mul(maxConfidenceIntervalMultiplier));
|
|
52
|
-
const oracleIsStale = new
|
|
52
|
+
const oracleIsStale = new anchor_1.BN(slot)
|
|
53
53
|
.sub(oraclePriceData.slot)
|
|
54
54
|
.gt(oracleGuardRails.validity.slotsBeforeStaleForAmm);
|
|
55
55
|
return !(!oraclePriceData.hasSufficientNumberOfDataPoints ||
|
|
@@ -61,7 +61,7 @@ function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
|
61
61
|
exports.isOracleValid = isOracleValid;
|
|
62
62
|
function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
63
63
|
const sinceLastUpdate = now.sub(amm.historicalOracleData.lastOraclePriceTwapTs);
|
|
64
|
-
const sinceStart =
|
|
64
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.FIVE_MINUTE.sub(sinceLastUpdate));
|
|
65
65
|
const oracleTwap5min = amm.historicalOracleData.lastOraclePriceTwap5Min
|
|
66
66
|
.mul(sinceStart)
|
|
67
67
|
.add(oraclePriceData.price)
|
|
@@ -69,7 +69,7 @@ function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
|
69
69
|
.div(sinceStart.add(sinceLastUpdate));
|
|
70
70
|
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
71
71
|
const oracleSpreadPct = oracleSpread.mul(numericConstants_1.PRICE_PRECISION).div(oracleTwap5min);
|
|
72
|
-
const maxDivergence =
|
|
72
|
+
const maxDivergence = anchor_1.BN.max(oracleGuardRails.priceDivergence.markOraclePercentDivergence, numericConstants_1.PERCENTAGE_PRECISION.div(new anchor_1.BN(10)));
|
|
73
73
|
const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);
|
|
74
74
|
return tooDivergent;
|
|
75
75
|
}
|
|
@@ -84,10 +84,10 @@ function calculateLiveOracleTwap(histOracleData, oraclePriceData, now, period) {
|
|
|
84
84
|
// period = amm.fundingPeriod;
|
|
85
85
|
oracleTwap = histOracleData.lastOraclePriceTwap;
|
|
86
86
|
}
|
|
87
|
-
const sinceLastUpdate =
|
|
88
|
-
const sinceStart =
|
|
89
|
-
const clampRange = oracleTwap.div(new
|
|
90
|
-
const clampedOraclePrice =
|
|
87
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ONE, now.sub(histOracleData.lastOraclePriceTwapTs));
|
|
88
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, period.sub(sinceLastUpdate));
|
|
89
|
+
const clampRange = oracleTwap.div(new anchor_1.BN(3));
|
|
90
|
+
const clampedOraclePrice = anchor_1.BN.min(oracleTwap.add(clampRange), anchor_1.BN.max(oraclePriceData.price, oracleTwap.sub(clampRange)));
|
|
91
91
|
const newOracleTwap = oracleTwap
|
|
92
92
|
.mul(sinceStart)
|
|
93
93
|
.add(clampedOraclePrice.mul(sinceLastUpdate))
|
|
@@ -96,27 +96,27 @@ function calculateLiveOracleTwap(histOracleData, oraclePriceData, now, period) {
|
|
|
96
96
|
}
|
|
97
97
|
exports.calculateLiveOracleTwap = calculateLiveOracleTwap;
|
|
98
98
|
function calculateLiveOracleStd(amm, oraclePriceData, now) {
|
|
99
|
-
const sinceLastUpdate =
|
|
100
|
-
const sinceStart =
|
|
99
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ONE, now.sub(amm.historicalOracleData.lastOraclePriceTwapTs));
|
|
100
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, amm.fundingPeriod.sub(sinceLastUpdate));
|
|
101
101
|
const liveOracleTwap = calculateLiveOracleTwap(amm.historicalOracleData, oraclePriceData, now, amm.fundingPeriod);
|
|
102
102
|
const liveOracleTwap5MIN = calculateLiveOracleTwap(amm.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE);
|
|
103
|
-
const priceDeltaVsTwap =
|
|
103
|
+
const priceDeltaVsTwap = anchor_1.BN.max(oraclePriceData.price.sub(liveOracleTwap).abs(), oraclePriceData.price.sub(liveOracleTwap5MIN).abs());
|
|
104
104
|
const oracleStd = priceDeltaVsTwap.add(amm.oracleStd.mul(sinceStart).div(sinceStart.add(sinceLastUpdate)));
|
|
105
105
|
return oracleStd;
|
|
106
106
|
}
|
|
107
107
|
exports.calculateLiveOracleStd = calculateLiveOracleStd;
|
|
108
108
|
function getNewOracleConfPct(amm, oraclePriceData, reservePrice, now) {
|
|
109
109
|
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
110
|
-
const sinceLastUpdate =
|
|
110
|
+
const sinceLastUpdate = anchor_1.BN.max(numericConstants_1.ZERO, now.sub(amm.historicalOracleData.lastOraclePriceTwapTs));
|
|
111
111
|
let lowerBoundConfPct = amm.lastOracleConfPct;
|
|
112
112
|
if (sinceLastUpdate.gt(numericConstants_1.ZERO)) {
|
|
113
|
-
const lowerBoundConfDivisor =
|
|
113
|
+
const lowerBoundConfDivisor = anchor_1.BN.max(new anchor_1.BN(21).sub(sinceLastUpdate), new anchor_1.BN(5));
|
|
114
114
|
lowerBoundConfPct = amm.lastOracleConfPct.sub(amm.lastOracleConfPct.div(lowerBoundConfDivisor));
|
|
115
115
|
}
|
|
116
116
|
const confIntervalPct = confInterval
|
|
117
117
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
118
118
|
.div(reservePrice);
|
|
119
|
-
const confIntervalPctResult =
|
|
119
|
+
const confIntervalPctResult = anchor_1.BN.max(confIntervalPct, lowerBoundConfPct);
|
|
120
120
|
return confIntervalPctResult;
|
|
121
121
|
}
|
|
122
122
|
exports.getNewOracleConfPct = getNewOracleConfPct;
|
|
@@ -136,36 +136,36 @@ exports.trimVaaSignatures = trimVaaSignatures;
|
|
|
136
136
|
function getMultipleBetweenOracleSources(firstOracleSource, secondOracleSource) {
|
|
137
137
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
138
138
|
(0, types_1.isVariant)(secondOracleSource, 'pyth1MPull')) {
|
|
139
|
-
return { numerator: new
|
|
139
|
+
return { numerator: new anchor_1.BN(1000000), denominator: new anchor_1.BN(1) };
|
|
140
140
|
}
|
|
141
141
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
142
142
|
(0, types_1.isVariant)(secondOracleSource, 'pyth1KPull')) {
|
|
143
|
-
return { numerator: new
|
|
143
|
+
return { numerator: new anchor_1.BN(1000), denominator: new anchor_1.BN(1) };
|
|
144
144
|
}
|
|
145
145
|
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1MPull') &&
|
|
146
146
|
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
147
|
-
return { numerator: new
|
|
147
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000000) };
|
|
148
148
|
}
|
|
149
149
|
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1KPull') &&
|
|
150
150
|
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
151
|
-
return { numerator: new
|
|
151
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000) };
|
|
152
152
|
}
|
|
153
153
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer') &&
|
|
154
154
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer1M')) {
|
|
155
|
-
return { numerator: new
|
|
155
|
+
return { numerator: new anchor_1.BN(1000000), denominator: new anchor_1.BN(1) };
|
|
156
156
|
}
|
|
157
157
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer') &&
|
|
158
158
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer1K')) {
|
|
159
|
-
return { numerator: new
|
|
159
|
+
return { numerator: new anchor_1.BN(1000), denominator: new anchor_1.BN(1) };
|
|
160
160
|
}
|
|
161
161
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer1M') &&
|
|
162
162
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer')) {
|
|
163
|
-
return { numerator: new
|
|
163
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000000) };
|
|
164
164
|
}
|
|
165
165
|
if ((0, types_1.isVariant)(firstOracleSource, 'pythLazer1K') &&
|
|
166
166
|
(0, types_1.isVariant)(secondOracleSource, 'pythLazer')) {
|
|
167
|
-
return { numerator: new
|
|
167
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1000) };
|
|
168
168
|
}
|
|
169
|
-
return { numerator: new
|
|
169
|
+
return { numerator: new anchor_1.BN(1), denominator: new anchor_1.BN(1) };
|
|
170
170
|
}
|
|
171
171
|
exports.getMultipleBetweenOracleSources = getMultipleBetweenOracleSources;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OrderSubscriber.d.ts","sourceRoot":"","sources":["../../../src/orderSubscriber/OrderSubscriber.ts"],"names":[],"mappings":";;;AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAMvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAyB,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"OrderSubscriber.d.ts","sourceRoot":"","sources":["../../../src/orderSubscriber/OrderSubscriber.ts"],"names":[],"mappings":";;;AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAMvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAyB,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAKtD,qBAAa,eAAe;IAC3B,WAAW,EAAE,WAAW,CAAC;IACzB,aAAa;cAA2B,MAAM;qBAAe,WAAW;OAAM;IAC9E,YAAY,EAAE,mBAAmB,GAAG,qBAAqB,GAAG,gBAAgB,CAAC;IAC7E,UAAU,EAAE,UAAU,CAAC;IACvB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,qBAAqB,CAAC,CAAC;IAEtE,YAAY,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,oBAAoB,EAAE,MAAM,IAAI,CAAC;IAEjC,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,WAAW,CAAC;IACtD,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB,oBAAoB,CAAC,EAAE,OAAO,CAAC;gBAEnB,MAAM,EAAE,qBAAqB;IA6C5B,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA6D5B,oBAAoB,CACnB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,KAAK,GAAG,SAAS,GAAG,QAAQ,EACtC,IAAI,EAAE,MAAM,EAAE,GAAG,WAAW,GAAG,MAAM,EACrC,IAAI,EAAE,MAAM,GACV,IAAI;IAgFP;;;;OAIG;IACH,SAAS,CAAC,UAAU,CAAC,uBAAuB,CAAC,EAAE,qBAAqB,GAAG,IAAI;IAI9D,OAAO,CACnB,IAAI,EAAE,MAAM,EACZ,uBAAuB,CAAC,EAAE,qBAAqB,GAC7C,OAAO,CAAC,IAAI,CAAC;IAuBT,OAAO,IAAI,MAAM;IAIX,SAAS,CAAC,oBAAoB,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBzD,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAOrD,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAIzC"}
|
|
@@ -8,11 +8,12 @@ const DLOB_1 = require("../dlob/DLOB");
|
|
|
8
8
|
const PollingSubscription_1 = require("./PollingSubscription");
|
|
9
9
|
const WebsocketSubscription_1 = require("./WebsocketSubscription");
|
|
10
10
|
const events_1 = require("events");
|
|
11
|
-
const index_1 = require("../index");
|
|
12
11
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
13
12
|
const user_1 = require("../decode/user");
|
|
14
13
|
const grpcSubscription_1 = require("./grpcSubscription");
|
|
15
14
|
const userStatus_1 = require("../math/userStatus");
|
|
15
|
+
const orders_1 = require("../math/orders");
|
|
16
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
16
17
|
class OrderSubscriber {
|
|
17
18
|
constructor(config) {
|
|
18
19
|
var _a, _b, _c, _d, _e;
|
|
@@ -163,8 +164,8 @@ class OrderSubscriber {
|
|
|
163
164
|
for (const order of userAccount.orders) {
|
|
164
165
|
let baseAssetAmount = order.baseAssetAmount;
|
|
165
166
|
if (order.reduceOnly) {
|
|
166
|
-
const existingBaseAmount = ((_a = userAccount.perpPositions.find((pos) => pos.marketIndex === order.marketIndex && pos.openOrders > 0)) === null || _a === void 0 ? void 0 : _a.baseAssetAmount) ||
|
|
167
|
-
baseAssetAmount = (0,
|
|
167
|
+
const existingBaseAmount = ((_a = userAccount.perpPositions.find((pos) => pos.marketIndex === order.marketIndex && pos.openOrders > 0)) === null || _a === void 0 ? void 0 : _a.baseAssetAmount) || numericConstants_1.ZERO;
|
|
168
|
+
baseAssetAmount = (0, orders_1.calculateOrderBaseAssetAmount)(order, existingBaseAmount);
|
|
168
169
|
}
|
|
169
170
|
dlob.insertOrder(order, key, slot, protectedMaker, baseAssetAmount);
|
|
170
171
|
}
|
package/package.json
CHANGED
package/src/events/types.ts
CHANGED
package/src/math/insurance.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PERCENTAGE_PRECISION, ZERO } from '../constants/numericConstants';
|
|
2
|
-
import { BN, SpotMarketAccount, SpotBalanceType } from '../index';
|
|
3
2
|
import { getTokenAmount } from '../math/spotBalance';
|
|
3
|
+
import { BN } from '@coral-xyz/anchor';
|
|
4
|
+
import { SpotBalanceType, SpotMarketAccount } from '../types';
|
|
4
5
|
|
|
5
6
|
export function nextRevenuePoolSettleApr(
|
|
6
7
|
spotMarket: SpotMarketAccount,
|
package/src/math/oracles.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AMM,
|
|
3
|
+
HistoricalOracleData,
|
|
4
|
+
OracleGuardRails,
|
|
5
|
+
OracleSource,
|
|
6
|
+
PerpMarketAccount,
|
|
7
|
+
isVariant,
|
|
8
|
+
} from '../types';
|
|
2
9
|
import { OraclePriceData } from '../oracles/types';
|
|
3
10
|
import {
|
|
4
11
|
BID_ASK_SPREAD_PRECISION,
|
|
@@ -9,13 +16,8 @@ import {
|
|
|
9
16
|
FIVE_MINUTE,
|
|
10
17
|
PERCENTAGE_PRECISION,
|
|
11
18
|
} from '../constants/numericConstants';
|
|
12
|
-
import {
|
|
13
|
-
BN,
|
|
14
|
-
HistoricalOracleData,
|
|
15
|
-
OracleSource,
|
|
16
|
-
PerpMarketAccount,
|
|
17
|
-
} from '../index';
|
|
18
19
|
import { assert } from '../assert/assert';
|
|
20
|
+
import { BN } from '@coral-xyz/anchor';
|
|
19
21
|
|
|
20
22
|
export function oraclePriceBands(
|
|
21
23
|
market: PerpMarketAccount,
|
|
@@ -13,12 +13,13 @@ import { PollingSubscription } from './PollingSubscription';
|
|
|
13
13
|
import { WebsocketSubscription } from './WebsocketSubscription';
|
|
14
14
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
15
15
|
import { EventEmitter } from 'events';
|
|
16
|
-
import { calculateOrderBaseAssetAmount, ZERO } from '../index';
|
|
17
16
|
import { BN } from '@coral-xyz/anchor';
|
|
18
17
|
import { ProtectMakerParamsMap } from '../dlob/types';
|
|
19
18
|
import { decodeUser } from '../decode/user';
|
|
20
19
|
import { grpcSubscription } from './grpcSubscription';
|
|
21
20
|
import { isUserProtectedMaker } from '../math/userStatus';
|
|
21
|
+
import { calculateOrderBaseAssetAmount } from '../math/orders';
|
|
22
|
+
import { ZERO } from '../constants/numericConstants';
|
|
22
23
|
|
|
23
24
|
export class OrderSubscriber {
|
|
24
25
|
driftClient: DriftClient;
|
package/tests/bn/test.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { BN, numberToSafeBN } from '../../src/index';
|
|
2
1
|
import { expect } from 'chai';
|
|
3
2
|
import { BigNum } from '../../src/factory/bigNum';
|
|
4
3
|
import {
|
|
@@ -7,6 +6,8 @@ import {
|
|
|
7
6
|
BASE_PRECISION_EXP,
|
|
8
7
|
TEN_THOUSAND,
|
|
9
8
|
} from '../../src/constants/numericConstants';
|
|
9
|
+
import { BN } from '@coral-xyz/anchor';
|
|
10
|
+
import { numberToSafeBN } from '../../src/math/utils';
|
|
10
11
|
|
|
11
12
|
// if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
|
|
12
13
|
// import 'mocha';
|