@drift-labs/sdk-browser 2.106.0-beta.5 → 2.106.0-beta.7
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/idl/drift.json +1 -1
- package/lib/browser/math/oracles.d.ts +5 -1
- package/lib/browser/math/oracles.js +21 -1
- package/lib/browser/user.js +14 -2
- package/lib/node/idl/drift.json +1 -1
- package/lib/node/math/oracles.d.ts +5 -1
- package/lib/node/math/oracles.js +21 -1
- package/lib/node/user.js +14 -2
- package/package.json +1 -1
- package/src/idl/drift.json +1 -1
- package/src/math/oracles.ts +41 -1
- package/src/user.ts +30 -7
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.106.0-beta.
|
|
1
|
+
2.106.0-beta.7
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import { AMM, OracleGuardRails } from '../types';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
|
|
6
|
+
import { BN, HistoricalOracleData, OracleSource, PerpMarketAccount } from '../index';
|
|
7
7
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: OraclePriceData): [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;
|
|
@@ -12,3 +12,7 @@ export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracle
|
|
|
12
12
|
export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
|
|
13
13
|
export declare function getNewOracleConfPct(amm: AMM, oraclePriceData: OraclePriceData, reservePrice: BN, now: BN): BN;
|
|
14
14
|
export declare function trimVaaSignatures(vaa: Buffer, n?: number): Buffer;
|
|
15
|
+
export declare function getMultipleBetweenOracleSources(firstOracleSource: OracleSource, secondOracleSource: OracleSource): {
|
|
16
|
+
numerator: BN;
|
|
17
|
+
denominator: BN;
|
|
18
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
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
6
|
const index_1 = require("../index");
|
|
@@ -132,3 +132,23 @@ function trimVaaSignatures(vaa, n = 3) {
|
|
|
132
132
|
return trimmedVaa;
|
|
133
133
|
}
|
|
134
134
|
exports.trimVaaSignatures = trimVaaSignatures;
|
|
135
|
+
function getMultipleBetweenOracleSources(firstOracleSource, secondOracleSource) {
|
|
136
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
137
|
+
(0, types_1.isVariant)(secondOracleSource, 'pyth1MPull')) {
|
|
138
|
+
return { numerator: new index_1.BN(1000000), denominator: new index_1.BN(1) };
|
|
139
|
+
}
|
|
140
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
141
|
+
(0, types_1.isVariant)(secondOracleSource, 'pyth1KPull')) {
|
|
142
|
+
return { numerator: new index_1.BN(1000), denominator: new index_1.BN(1) };
|
|
143
|
+
}
|
|
144
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1MPull') &&
|
|
145
|
+
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
146
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1000000) };
|
|
147
|
+
}
|
|
148
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1KPull') &&
|
|
149
|
+
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
150
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1000) };
|
|
151
|
+
}
|
|
152
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1) };
|
|
153
|
+
}
|
|
154
|
+
exports.getMultipleBetweenOracleSources = getMultipleBetweenOracleSources;
|
package/lib/browser/user.js
CHANGED
|
@@ -1174,7 +1174,13 @@ class User {
|
|
|
1174
1174
|
if (perpMarketWithSameOracle) {
|
|
1175
1175
|
const perpPosition = this.getPerpPositionWithLPSettle(perpMarketWithSameOracle.marketIndex, undefined, true)[0];
|
|
1176
1176
|
if (perpPosition) {
|
|
1177
|
-
|
|
1177
|
+
let freeCollateralDeltaForPerp = this.calculateFreeCollateralDeltaForPerp(perpMarketWithSameOracle, perpPosition, numericConstants_1.ZERO, oraclePrice);
|
|
1178
|
+
if (freeCollateralDeltaForPerp) {
|
|
1179
|
+
const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.oracleSource, perpMarketWithSameOracle.amm.oracleSource);
|
|
1180
|
+
freeCollateralDeltaForPerp = freeCollateralDeltaForPerp
|
|
1181
|
+
.mul(numerator)
|
|
1182
|
+
.div(denominator);
|
|
1183
|
+
}
|
|
1178
1184
|
freeCollateralDelta = freeCollateralDelta.add(freeCollateralDeltaForPerp || numericConstants_1.ZERO);
|
|
1179
1185
|
}
|
|
1180
1186
|
}
|
|
@@ -1223,7 +1229,13 @@ class User {
|
|
|
1223
1229
|
const spotPosition = this.getSpotPosition(spotMarketWithSameOracle.marketIndex);
|
|
1224
1230
|
if (spotPosition) {
|
|
1225
1231
|
const signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
|
|
1226
|
-
|
|
1232
|
+
let spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount, marginCategory);
|
|
1233
|
+
if (spotFreeCollateralDelta) {
|
|
1234
|
+
const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.amm.oracleSource, spotMarketWithSameOracle.oracleSource);
|
|
1235
|
+
spotFreeCollateralDelta = spotFreeCollateralDelta
|
|
1236
|
+
.mul(numerator)
|
|
1237
|
+
.div(denominator);
|
|
1238
|
+
}
|
|
1227
1239
|
freeCollateralDelta = freeCollateralDelta.add(spotFreeCollateralDelta || numericConstants_1.ZERO);
|
|
1228
1240
|
}
|
|
1229
1241
|
}
|
package/lib/node/idl/drift.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import { AMM, OracleGuardRails } from '../types';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
|
|
6
|
+
import { BN, HistoricalOracleData, OracleSource, PerpMarketAccount } from '../index';
|
|
7
7
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: OraclePriceData): [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;
|
|
@@ -12,3 +12,7 @@ export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracle
|
|
|
12
12
|
export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
|
|
13
13
|
export declare function getNewOracleConfPct(amm: AMM, oraclePriceData: OraclePriceData, reservePrice: BN, now: BN): BN;
|
|
14
14
|
export declare function trimVaaSignatures(vaa: Buffer, n?: number): Buffer;
|
|
15
|
+
export declare function getMultipleBetweenOracleSources(firstOracleSource: OracleSource, secondOracleSource: OracleSource): {
|
|
16
|
+
numerator: BN;
|
|
17
|
+
denominator: BN;
|
|
18
|
+
};
|
package/lib/node/math/oracles.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
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
6
|
const index_1 = require("../index");
|
|
@@ -132,3 +132,23 @@ function trimVaaSignatures(vaa, n = 3) {
|
|
|
132
132
|
return trimmedVaa;
|
|
133
133
|
}
|
|
134
134
|
exports.trimVaaSignatures = trimVaaSignatures;
|
|
135
|
+
function getMultipleBetweenOracleSources(firstOracleSource, secondOracleSource) {
|
|
136
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
137
|
+
(0, types_1.isVariant)(secondOracleSource, 'pyth1MPull')) {
|
|
138
|
+
return { numerator: new index_1.BN(1000000), denominator: new index_1.BN(1) };
|
|
139
|
+
}
|
|
140
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pythPull') &&
|
|
141
|
+
(0, types_1.isVariant)(secondOracleSource, 'pyth1KPull')) {
|
|
142
|
+
return { numerator: new index_1.BN(1000), denominator: new index_1.BN(1) };
|
|
143
|
+
}
|
|
144
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1MPull') &&
|
|
145
|
+
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
146
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1000000) };
|
|
147
|
+
}
|
|
148
|
+
if ((0, types_1.isVariant)(firstOracleSource, 'pyth1KPull') &&
|
|
149
|
+
(0, types_1.isVariant)(secondOracleSource, 'pythPull')) {
|
|
150
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1000) };
|
|
151
|
+
}
|
|
152
|
+
return { numerator: new index_1.BN(1), denominator: new index_1.BN(1) };
|
|
153
|
+
}
|
|
154
|
+
exports.getMultipleBetweenOracleSources = getMultipleBetweenOracleSources;
|
package/lib/node/user.js
CHANGED
|
@@ -1174,7 +1174,13 @@ class User {
|
|
|
1174
1174
|
if (perpMarketWithSameOracle) {
|
|
1175
1175
|
const perpPosition = this.getPerpPositionWithLPSettle(perpMarketWithSameOracle.marketIndex, undefined, true)[0];
|
|
1176
1176
|
if (perpPosition) {
|
|
1177
|
-
|
|
1177
|
+
let freeCollateralDeltaForPerp = this.calculateFreeCollateralDeltaForPerp(perpMarketWithSameOracle, perpPosition, numericConstants_1.ZERO, oraclePrice);
|
|
1178
|
+
if (freeCollateralDeltaForPerp) {
|
|
1179
|
+
const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.oracleSource, perpMarketWithSameOracle.amm.oracleSource);
|
|
1180
|
+
freeCollateralDeltaForPerp = freeCollateralDeltaForPerp
|
|
1181
|
+
.mul(numerator)
|
|
1182
|
+
.div(denominator);
|
|
1183
|
+
}
|
|
1178
1184
|
freeCollateralDelta = freeCollateralDelta.add(freeCollateralDeltaForPerp || numericConstants_1.ZERO);
|
|
1179
1185
|
}
|
|
1180
1186
|
}
|
|
@@ -1223,7 +1229,13 @@ class User {
|
|
|
1223
1229
|
const spotPosition = this.getSpotPosition(spotMarketWithSameOracle.marketIndex);
|
|
1224
1230
|
if (spotPosition) {
|
|
1225
1231
|
const signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
|
|
1226
|
-
|
|
1232
|
+
let spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount, marginCategory);
|
|
1233
|
+
if (spotFreeCollateralDelta) {
|
|
1234
|
+
const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.amm.oracleSource, spotMarketWithSameOracle.oracleSource);
|
|
1235
|
+
spotFreeCollateralDelta = spotFreeCollateralDelta
|
|
1236
|
+
.mul(numerator)
|
|
1237
|
+
.div(denominator);
|
|
1238
|
+
}
|
|
1227
1239
|
freeCollateralDelta = freeCollateralDelta.add(spotFreeCollateralDelta || numericConstants_1.ZERO);
|
|
1228
1240
|
}
|
|
1229
1241
|
}
|
package/package.json
CHANGED
package/src/idl/drift.json
CHANGED
package/src/math/oracles.ts
CHANGED
|
@@ -9,7 +9,12 @@ import {
|
|
|
9
9
|
FIVE_MINUTE,
|
|
10
10
|
PERCENTAGE_PRECISION,
|
|
11
11
|
} from '../constants/numericConstants';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
BN,
|
|
14
|
+
HistoricalOracleData,
|
|
15
|
+
OracleSource,
|
|
16
|
+
PerpMarketAccount,
|
|
17
|
+
} from '../index';
|
|
13
18
|
import { assert } from '../assert/assert';
|
|
14
19
|
|
|
15
20
|
export function oraclePriceBands(
|
|
@@ -226,3 +231,38 @@ export function trimVaaSignatures(vaa: Buffer, n = 3): Buffer {
|
|
|
226
231
|
trimmedVaa[5] = n;
|
|
227
232
|
return trimmedVaa;
|
|
228
233
|
}
|
|
234
|
+
|
|
235
|
+
export function getMultipleBetweenOracleSources(
|
|
236
|
+
firstOracleSource: OracleSource,
|
|
237
|
+
secondOracleSource: OracleSource
|
|
238
|
+
): { numerator: BN; denominator: BN } {
|
|
239
|
+
if (
|
|
240
|
+
isVariant(firstOracleSource, 'pythPull') &&
|
|
241
|
+
isVariant(secondOracleSource, 'pyth1MPull')
|
|
242
|
+
) {
|
|
243
|
+
return { numerator: new BN(1000000), denominator: new BN(1) };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (
|
|
247
|
+
isVariant(firstOracleSource, 'pythPull') &&
|
|
248
|
+
isVariant(secondOracleSource, 'pyth1KPull')
|
|
249
|
+
) {
|
|
250
|
+
return { numerator: new BN(1000), denominator: new BN(1) };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (
|
|
254
|
+
isVariant(firstOracleSource, 'pyth1MPull') &&
|
|
255
|
+
isVariant(secondOracleSource, 'pythPull')
|
|
256
|
+
) {
|
|
257
|
+
return { numerator: new BN(1), denominator: new BN(1000000) };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (
|
|
261
|
+
isVariant(firstOracleSource, 'pyth1KPull') &&
|
|
262
|
+
isVariant(secondOracleSource, 'pythPull')
|
|
263
|
+
) {
|
|
264
|
+
return { numerator: new BN(1), denominator: new BN(1000) };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return { numerator: new BN(1), denominator: new BN(1) };
|
|
268
|
+
}
|
package/src/user.ts
CHANGED
|
@@ -92,7 +92,10 @@ import {
|
|
|
92
92
|
getWorstCaseTokenAmounts,
|
|
93
93
|
isSpotPositionAvailable,
|
|
94
94
|
} from './math/spotPosition';
|
|
95
|
-
import {
|
|
95
|
+
import {
|
|
96
|
+
calculateLiveOracleTwap,
|
|
97
|
+
getMultipleBetweenOracleSources,
|
|
98
|
+
} from './math/oracles';
|
|
96
99
|
import { getPerpMarketTierNumber, getSpotMarketTierNumber } from './math/tiers';
|
|
97
100
|
import { StrictOraclePrice } from './oracles/strictOraclePrice';
|
|
98
101
|
|
|
@@ -2219,7 +2222,7 @@ export class User {
|
|
|
2219
2222
|
true
|
|
2220
2223
|
)[0];
|
|
2221
2224
|
if (perpPosition) {
|
|
2222
|
-
|
|
2225
|
+
let freeCollateralDeltaForPerp =
|
|
2223
2226
|
this.calculateFreeCollateralDeltaForPerp(
|
|
2224
2227
|
perpMarketWithSameOracle,
|
|
2225
2228
|
perpPosition,
|
|
@@ -2227,6 +2230,16 @@ export class User {
|
|
|
2227
2230
|
oraclePrice
|
|
2228
2231
|
);
|
|
2229
2232
|
|
|
2233
|
+
if (freeCollateralDeltaForPerp) {
|
|
2234
|
+
const { numerator, denominator } = getMultipleBetweenOracleSources(
|
|
2235
|
+
market.oracleSource,
|
|
2236
|
+
perpMarketWithSameOracle.amm.oracleSource
|
|
2237
|
+
);
|
|
2238
|
+
freeCollateralDeltaForPerp = freeCollateralDeltaForPerp
|
|
2239
|
+
.mul(numerator)
|
|
2240
|
+
.div(denominator);
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2230
2243
|
freeCollateralDelta = freeCollateralDelta.add(
|
|
2231
2244
|
freeCollateralDeltaForPerp || ZERO
|
|
2232
2245
|
);
|
|
@@ -2342,12 +2355,22 @@ export class User {
|
|
|
2342
2355
|
spotPosition.balanceType
|
|
2343
2356
|
);
|
|
2344
2357
|
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2358
|
+
let spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(
|
|
2359
|
+
spotMarketWithSameOracle,
|
|
2360
|
+
signedTokenAmount,
|
|
2361
|
+
marginCategory
|
|
2362
|
+
);
|
|
2363
|
+
|
|
2364
|
+
if (spotFreeCollateralDelta) {
|
|
2365
|
+
const { numerator, denominator } = getMultipleBetweenOracleSources(
|
|
2366
|
+
market.amm.oracleSource,
|
|
2367
|
+
spotMarketWithSameOracle.oracleSource
|
|
2350
2368
|
);
|
|
2369
|
+
spotFreeCollateralDelta = spotFreeCollateralDelta
|
|
2370
|
+
.mul(numerator)
|
|
2371
|
+
.div(denominator);
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2351
2374
|
freeCollateralDelta = freeCollateralDelta.add(
|
|
2352
2375
|
spotFreeCollateralDelta || ZERO
|
|
2353
2376
|
);
|