@drift-labs/sdk 2.131.0-beta.0 → 2.131.0-beta.2
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/accounts/grpcAccountSubscriber.d.ts +0 -1
- package/lib/browser/accounts/grpcAccountSubscriber.js +2 -18
- package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +0 -1
- package/lib/browser/accounts/grpcProgramAccountSubscriber.js +2 -18
- 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/accounts/grpcAccountSubscriber.d.ts +0 -1
- package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcAccountSubscriber.js +2 -18
- package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +0 -1
- package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcProgramAccountSubscriber.js +2 -18
- 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/accounts/grpcAccountSubscriber.ts +2 -28
- package/src/accounts/grpcProgramAccountSubscriber.ts +2 -28
- 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.2
|
|
@@ -9,7 +9,6 @@ export declare class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber
|
|
|
9
9
|
private stream;
|
|
10
10
|
private commitmentLevel;
|
|
11
11
|
listenerId?: number;
|
|
12
|
-
private enableReconnect;
|
|
13
12
|
private constructor();
|
|
14
13
|
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts): Promise<grpcAccountSubscriber<U>>;
|
|
15
14
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
@@ -29,11 +29,10 @@ const Buffer = __importStar(require("buffer"));
|
|
|
29
29
|
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
30
30
|
const grpc_1 = require("../isomorphic/grpc");
|
|
31
31
|
class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccountSubscriber {
|
|
32
|
-
constructor(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts
|
|
32
|
+
constructor(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
33
33
|
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
34
34
|
this.client = client;
|
|
35
35
|
this.commitmentLevel = commitmentLevel;
|
|
36
|
-
this.enableReconnect = enableReconnect;
|
|
37
36
|
}
|
|
38
37
|
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
39
38
|
var _a, _b;
|
|
@@ -41,7 +40,7 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
41
40
|
const commitmentLevel =
|
|
42
41
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
43
42
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : CommitmentLevel.CONFIRMED;
|
|
44
|
-
return new grpcAccountSubscriber(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts
|
|
43
|
+
return new grpcAccountSubscriber(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
45
44
|
}
|
|
46
45
|
async subscribe(onChange) {
|
|
47
46
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
@@ -71,19 +70,6 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
71
70
|
entry: {},
|
|
72
71
|
transactionsStatus: {},
|
|
73
72
|
};
|
|
74
|
-
if (this.enableReconnect) {
|
|
75
|
-
this.stream.on('error', (error) => {
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
if (error.code === 1) {
|
|
78
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
79
|
-
console.error('GRPC (grpcAccountSubscriber) Cancelled on client caught:', error);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
console.error('GRPC (grpcAccountSubscriber) unexpected error caught:', error);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
73
|
this.stream.on('data', (chunk) => {
|
|
88
74
|
var _a;
|
|
89
75
|
if (!chunk.account) {
|
|
@@ -160,8 +146,6 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
160
146
|
reject(err);
|
|
161
147
|
}
|
|
162
148
|
});
|
|
163
|
-
this.stream.cancel();
|
|
164
|
-
this.stream.destroy();
|
|
165
149
|
}).catch((reason) => {
|
|
166
150
|
console.error(reason);
|
|
167
151
|
throw reason;
|
|
@@ -9,7 +9,6 @@ export declare class grpcProgramAccountSubscriber<T> extends WebSocketProgramAcc
|
|
|
9
9
|
private stream;
|
|
10
10
|
private commitmentLevel;
|
|
11
11
|
listenerId?: number;
|
|
12
|
-
private enableReconnect;
|
|
13
12
|
private constructor();
|
|
14
13
|
static create<U>(grpcConfigs: GrpcConfigs, subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => U, options?: {
|
|
15
14
|
filters: MemcmpFilter[];
|
|
@@ -35,11 +35,10 @@ const grpc_1 = require("../isomorphic/grpc");
|
|
|
35
35
|
class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
|
|
36
36
|
constructor(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
37
37
|
filters: [],
|
|
38
|
-
}, resubOpts
|
|
38
|
+
}, resubOpts) {
|
|
39
39
|
super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
|
|
40
40
|
this.client = client;
|
|
41
41
|
this.commitmentLevel = commitmentLevel;
|
|
42
|
-
this.enableReconnect = enableReconnect;
|
|
43
42
|
}
|
|
44
43
|
static async create(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
45
44
|
filters: [],
|
|
@@ -49,7 +48,7 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
49
48
|
const commitmentLevel =
|
|
50
49
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
51
50
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : CommitmentLevel.CONFIRMED;
|
|
52
|
-
return new grpcProgramAccountSubscriber(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts
|
|
51
|
+
return new grpcProgramAccountSubscriber(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
|
|
53
52
|
}
|
|
54
53
|
async subscribe(onChange) {
|
|
55
54
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
@@ -84,19 +83,6 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
84
83
|
entry: {},
|
|
85
84
|
transactionsStatus: {},
|
|
86
85
|
};
|
|
87
|
-
if (this.enableReconnect) {
|
|
88
|
-
this.stream.on('error', (error) => {
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
if (error.code === 1) {
|
|
91
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
92
|
-
console.error('GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:', error);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
console.error('GRPC (grpcProgramAccountSubscriber) unexpected error caught:', error);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
86
|
this.stream.on('data', (chunk) => {
|
|
101
87
|
var _a;
|
|
102
88
|
if (!chunk.account) {
|
|
@@ -179,8 +165,6 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
179
165
|
reject(err);
|
|
180
166
|
}
|
|
181
167
|
});
|
|
182
|
-
this.stream.cancel();
|
|
183
|
-
this.stream.destroy();
|
|
184
168
|
}).catch((reason) => {
|
|
185
169
|
console.error(reason);
|
|
186
170
|
throw reason;
|
|
@@ -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
|
}
|
|
@@ -9,7 +9,6 @@ export declare class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber
|
|
|
9
9
|
private stream;
|
|
10
10
|
private commitmentLevel;
|
|
11
11
|
listenerId?: number;
|
|
12
|
-
private enableReconnect;
|
|
13
12
|
private constructor();
|
|
14
13
|
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts): Promise<grpcAccountSubscriber<U>>;
|
|
15
14
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAU1E,qBAAa,qBAAqB,CAAC,CAAC,CAAE,SAAQ,0BAA0B,CAAC,CAAC,CAAC;IAC1E,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"grpcAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAU1E,qBAAa,qBAAqB,CAAC,CAAC,CAAE,SAAQ,0BAA0B,CAAC,CAAC,CAAC;IAC1E,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IAE3B,OAAO;WAca,MAAM,CAAC,CAAC,EAC3B,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,SAAS,EAC3B,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC,EACpC,SAAS,CAAC,EAAE,SAAS,GACnB,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAqBrB,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkFrD,WAAW,CAAC,OAAO,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CAsC1D"}
|
|
@@ -29,11 +29,10 @@ const Buffer = __importStar(require("buffer"));
|
|
|
29
29
|
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
30
30
|
const grpc_1 = require("../isomorphic/grpc");
|
|
31
31
|
class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccountSubscriber {
|
|
32
|
-
constructor(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts
|
|
32
|
+
constructor(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
33
33
|
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
34
34
|
this.client = client;
|
|
35
35
|
this.commitmentLevel = commitmentLevel;
|
|
36
|
-
this.enableReconnect = enableReconnect;
|
|
37
36
|
}
|
|
38
37
|
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
39
38
|
var _a, _b;
|
|
@@ -41,7 +40,7 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
41
40
|
const commitmentLevel =
|
|
42
41
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
43
42
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : CommitmentLevel.CONFIRMED;
|
|
44
|
-
return new grpcAccountSubscriber(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts
|
|
43
|
+
return new grpcAccountSubscriber(client, commitmentLevel, accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
45
44
|
}
|
|
46
45
|
async subscribe(onChange) {
|
|
47
46
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
@@ -71,19 +70,6 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
71
70
|
entry: {},
|
|
72
71
|
transactionsStatus: {},
|
|
73
72
|
};
|
|
74
|
-
if (this.enableReconnect) {
|
|
75
|
-
this.stream.on('error', (error) => {
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
if (error.code === 1) {
|
|
78
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
79
|
-
console.error('GRPC (grpcAccountSubscriber) Cancelled on client caught:', error);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
console.error('GRPC (grpcAccountSubscriber) unexpected error caught:', error);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
73
|
this.stream.on('data', (chunk) => {
|
|
88
74
|
var _a;
|
|
89
75
|
if (!chunk.account) {
|
|
@@ -160,8 +146,6 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
160
146
|
reject(err);
|
|
161
147
|
}
|
|
162
148
|
});
|
|
163
|
-
this.stream.cancel();
|
|
164
|
-
this.stream.destroy();
|
|
165
149
|
}).catch((reason) => {
|
|
166
150
|
console.error(reason);
|
|
167
151
|
throw reason;
|
|
@@ -9,7 +9,6 @@ export declare class grpcProgramAccountSubscriber<T> extends WebSocketProgramAcc
|
|
|
9
9
|
private stream;
|
|
10
10
|
private commitmentLevel;
|
|
11
11
|
listenerId?: number;
|
|
12
|
-
private enableReconnect;
|
|
13
12
|
private constructor();
|
|
14
13
|
static create<U>(grpcConfigs: GrpcConfigs, subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => U, options?: {
|
|
15
14
|
filters: MemcmpFilter[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcProgramAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcProgramAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEnE,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AAUxF,qBAAa,4BAA4B,CACxC,CAAC,CACA,SAAQ,iCAAiC,CAAC,CAAC,CAAC;IAC7C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"grpcProgramAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcProgramAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEnE,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AAUxF,qBAAa,4BAA4B,CACxC,CAAC,CACA,SAAQ,iCAAiC,CAAC,CAAC,CAAC;IAC7C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IAE3B,OAAO;WAwBa,MAAM,CAAC,CAAC,EAC3B,WAAW,EAAE,WAAW,EACxB,gBAAgB,EAAE,MAAM,EACxB,oBAAoB,EAAE,MAAM,EAC5B,OAAO,EAAE,OAAO,EAChB,cAAc,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC,EACtD,OAAO,GAAE;QAAE,OAAO,EAAE,YAAY,EAAE,CAAA;KAEjC,EACD,SAAS,CAAC,EAAE,SAAS,GACnB,OAAO,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC;IAsBrC,SAAS,CACd,QAAQ,EAAE,CACT,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,KACV,IAAI,GACP,OAAO,CAAC,IAAI,CAAC;IA6FH,WAAW,CAAC,OAAO,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CAsCxD"}
|
|
@@ -35,11 +35,10 @@ const grpc_1 = require("../isomorphic/grpc");
|
|
|
35
35
|
class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
|
|
36
36
|
constructor(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
37
37
|
filters: [],
|
|
38
|
-
}, resubOpts
|
|
38
|
+
}, resubOpts) {
|
|
39
39
|
super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
|
|
40
40
|
this.client = client;
|
|
41
41
|
this.commitmentLevel = commitmentLevel;
|
|
42
|
-
this.enableReconnect = enableReconnect;
|
|
43
42
|
}
|
|
44
43
|
static async create(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
45
44
|
filters: [],
|
|
@@ -49,7 +48,7 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
49
48
|
const commitmentLevel =
|
|
50
49
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
51
50
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : CommitmentLevel.CONFIRMED;
|
|
52
|
-
return new grpcProgramAccountSubscriber(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts
|
|
51
|
+
return new grpcProgramAccountSubscriber(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
|
|
53
52
|
}
|
|
54
53
|
async subscribe(onChange) {
|
|
55
54
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
@@ -84,19 +83,6 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
84
83
|
entry: {},
|
|
85
84
|
transactionsStatus: {},
|
|
86
85
|
};
|
|
87
|
-
if (this.enableReconnect) {
|
|
88
|
-
this.stream.on('error', (error) => {
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
if (error.code === 1) {
|
|
91
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
92
|
-
console.error('GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:', error);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
console.error('GRPC (grpcProgramAccountSubscriber) unexpected error caught:', error);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
86
|
this.stream.on('data', (chunk) => {
|
|
101
87
|
var _a;
|
|
102
88
|
if (!chunk.account) {
|
|
@@ -179,8 +165,6 @@ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.W
|
|
|
179
165
|
reject(err);
|
|
180
166
|
}
|
|
181
167
|
});
|
|
182
|
-
this.stream.cancel();
|
|
183
|
-
this.stream.destroy();
|
|
184
168
|
}).catch((reason) => {
|
|
185
169
|
console.error(reason);
|
|
186
170
|
throw reason;
|
|
@@ -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
|
@@ -18,7 +18,6 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
|
18
18
|
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
|
|
19
19
|
private commitmentLevel: CommitmentLevel;
|
|
20
20
|
public listenerId?: number;
|
|
21
|
-
private enableReconnect: boolean;
|
|
22
21
|
|
|
23
22
|
private constructor(
|
|
24
23
|
client: Client,
|
|
@@ -27,13 +26,11 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
|
27
26
|
program: Program,
|
|
28
27
|
accountPublicKey: PublicKey,
|
|
29
28
|
decodeBuffer?: (buffer: Buffer) => T,
|
|
30
|
-
resubOpts?: ResubOpts
|
|
31
|
-
enableReconnect = false
|
|
29
|
+
resubOpts?: ResubOpts
|
|
32
30
|
) {
|
|
33
31
|
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
34
32
|
this.client = client;
|
|
35
33
|
this.commitmentLevel = commitmentLevel;
|
|
36
|
-
this.enableReconnect = enableReconnect;
|
|
37
34
|
}
|
|
38
35
|
|
|
39
36
|
public static async create<U>(
|
|
@@ -60,8 +57,7 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
|
60
57
|
program,
|
|
61
58
|
accountPublicKey,
|
|
62
59
|
decodeBuffer,
|
|
63
|
-
resubOpts
|
|
64
|
-
grpcConfigs.enableReconnect
|
|
60
|
+
resubOpts
|
|
65
61
|
);
|
|
66
62
|
}
|
|
67
63
|
|
|
@@ -95,26 +91,6 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
|
95
91
|
entry: {},
|
|
96
92
|
transactionsStatus: {},
|
|
97
93
|
};
|
|
98
|
-
|
|
99
|
-
if (this.enableReconnect) {
|
|
100
|
-
this.stream.on('error', (error) => {
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
if (error.code === 1) {
|
|
103
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
104
|
-
console.error(
|
|
105
|
-
'GRPC (grpcAccountSubscriber) Cancelled on client caught:',
|
|
106
|
-
error
|
|
107
|
-
);
|
|
108
|
-
return;
|
|
109
|
-
} else {
|
|
110
|
-
console.error(
|
|
111
|
-
'GRPC (grpcAccountSubscriber) unexpected error caught:',
|
|
112
|
-
error
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
94
|
this.stream.on('data', (chunk: SubscribeUpdate) => {
|
|
119
95
|
if (!chunk.account) {
|
|
120
96
|
return;
|
|
@@ -196,8 +172,6 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
|
196
172
|
reject(err);
|
|
197
173
|
}
|
|
198
174
|
});
|
|
199
|
-
this.stream.cancel();
|
|
200
|
-
this.stream.destroy();
|
|
201
175
|
}).catch((reason) => {
|
|
202
176
|
console.error(reason);
|
|
203
177
|
throw reason;
|
|
@@ -20,7 +20,6 @@ export class grpcProgramAccountSubscriber<
|
|
|
20
20
|
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
|
|
21
21
|
private commitmentLevel: CommitmentLevel;
|
|
22
22
|
public listenerId?: number;
|
|
23
|
-
private enableReconnect: boolean;
|
|
24
23
|
|
|
25
24
|
private constructor(
|
|
26
25
|
client: Client,
|
|
@@ -32,8 +31,7 @@ export class grpcProgramAccountSubscriber<
|
|
|
32
31
|
options: { filters: MemcmpFilter[] } = {
|
|
33
32
|
filters: [],
|
|
34
33
|
},
|
|
35
|
-
resubOpts?: ResubOpts
|
|
36
|
-
enableReconnect = false
|
|
34
|
+
resubOpts?: ResubOpts
|
|
37
35
|
) {
|
|
38
36
|
super(
|
|
39
37
|
subscriptionName,
|
|
@@ -45,7 +43,6 @@ export class grpcProgramAccountSubscriber<
|
|
|
45
43
|
);
|
|
46
44
|
this.client = client;
|
|
47
45
|
this.commitmentLevel = commitmentLevel;
|
|
48
|
-
this.enableReconnect = enableReconnect;
|
|
49
46
|
}
|
|
50
47
|
|
|
51
48
|
public static async create<U>(
|
|
@@ -76,8 +73,7 @@ export class grpcProgramAccountSubscriber<
|
|
|
76
73
|
program,
|
|
77
74
|
decodeBufferFn,
|
|
78
75
|
options,
|
|
79
|
-
resubOpts
|
|
80
|
-
grpcConfigs.enableReconnect
|
|
76
|
+
resubOpts
|
|
81
77
|
);
|
|
82
78
|
}
|
|
83
79
|
|
|
@@ -123,26 +119,6 @@ export class grpcProgramAccountSubscriber<
|
|
|
123
119
|
entry: {},
|
|
124
120
|
transactionsStatus: {},
|
|
125
121
|
};
|
|
126
|
-
|
|
127
|
-
if (this.enableReconnect) {
|
|
128
|
-
this.stream.on('error', (error) => {
|
|
129
|
-
// @ts-ignore
|
|
130
|
-
if (error.code === 1) {
|
|
131
|
-
// expected: 1 CANCELLED: Cancelled on client
|
|
132
|
-
console.error(
|
|
133
|
-
'GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:',
|
|
134
|
-
error
|
|
135
|
-
);
|
|
136
|
-
return;
|
|
137
|
-
} else {
|
|
138
|
-
console.error(
|
|
139
|
-
'GRPC (grpcProgramAccountSubscriber) unexpected error caught:',
|
|
140
|
-
error
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
122
|
this.stream.on('data', (chunk: SubscribeUpdate) => {
|
|
147
123
|
if (!chunk.account) {
|
|
148
124
|
return;
|
|
@@ -230,8 +206,6 @@ export class grpcProgramAccountSubscriber<
|
|
|
230
206
|
reject(err);
|
|
231
207
|
}
|
|
232
208
|
});
|
|
233
|
-
this.stream.cancel();
|
|
234
|
-
this.stream.destroy();
|
|
235
209
|
}).catch((reason) => {
|
|
236
210
|
console.error(reason);
|
|
237
211
|
throw reason;
|
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';
|