@drift-labs/sdk 0.2.0-master.29 → 0.2.0-master.30
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/README.md +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
- package/lib/accounts/types.d.ts +4 -6
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +5 -5
- package/lib/addresses/pda.js +5 -5
- package/lib/admin.d.ts +27 -28
- package/lib/admin.js +12 -37
- package/lib/clearingHouse.d.ts +61 -62
- package/lib/clearingHouse.js +92 -110
- package/lib/clearingHouseConfig.d.ts +2 -4
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +16 -16
- package/lib/config.d.ts +2 -4
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -8
- package/lib/constants/numericConstants.js +17 -18
- package/lib/constants/perpMarkets.d.ts +2 -3
- package/lib/constants/perpMarkets.js +3 -3
- package/lib/constants/spotMarkets.d.ts +2 -1
- package/lib/constants/spotMarkets.js +6 -4
- package/lib/dlob/DLOB.d.ts +13 -13
- package/lib/dlob/DLOB.js +36 -40
- package/lib/dlob/DLOBNode.js +2 -2
- package/lib/events/sort.js +1 -1
- package/lib/examples/makeTradeExample.js +3 -3
- package/lib/idl/clearing_house.json +359 -310
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +10 -10
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +9 -9
- package/lib/math/margin.js +3 -3
- package/lib/math/market.d.ts +4 -4
- package/lib/math/market.js +7 -7
- package/lib/math/oracles.js +6 -8
- package/lib/math/position.d.ts +2 -2
- package/lib/math/position.js +9 -9
- package/lib/math/repeg.js +7 -6
- package/lib/math/spotBalance.js +5 -5
- package/lib/math/spotPosition.js +2 -2
- package/lib/math/trade.d.ts +6 -6
- package/lib/math/trade.js +15 -19
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/types.d.ts +63 -51
- package/lib/types.js +1 -1
- package/package.json +2 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
- package/src/accounts/types.ts +4 -5
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +10 -10
- package/src/admin.ts +42 -79
- package/src/clearingHouse.ts +164 -212
- package/src/clearingHouseConfig.ts +2 -3
- package/src/clearingHouseUser.ts +35 -35
- package/src/config.ts +3 -4
- package/src/constants/numericConstants.ts +19 -21
- package/src/constants/perpMarkets.ts +5 -5
- package/src/constants/spotMarkets.ts +8 -5
- package/src/dlob/DLOB.ts +54 -70
- package/src/dlob/DLOBNode.ts +5 -6
- package/src/events/sort.ts +1 -1
- package/src/examples/makeTradeExample.js +2 -2
- package/src/examples/makeTradeExample.ts +5 -8
- package/src/idl/clearing_house.json +359 -310
- package/src/math/amm.ts +14 -11
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +13 -11
- package/src/math/margin.ts +4 -5
- package/src/math/market.ts +5 -5
- package/src/math/oracles.ts +9 -9
- package/src/math/position.ts +11 -19
- package/src/math/repeg.ts +8 -7
- package/src/math/spotBalance.ts +6 -6
- package/src/math/spotPosition.ts +2 -2
- package/src/math/trade.ts +17 -21
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/types.ts +69 -51
- package/tests/dlob/helpers.ts +56 -4
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/events/txEventCache.js +0 -71
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/repeg.js +0 -128
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/userName.js +0 -20
- package/src/wallet.js +0 -35
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import { IWallet } from './types';
|
|
3
|
-
import { BN } from '@project-serum/anchor';
|
|
4
3
|
import { OracleInfo } from './oracles/types';
|
|
5
4
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
6
5
|
import { DriftEnv } from './config';
|
|
@@ -14,8 +13,8 @@ export type ClearingHouseConfig = {
|
|
|
14
13
|
txSenderConfig?: TxSenderConfig;
|
|
15
14
|
userIds?: number[];
|
|
16
15
|
activeUserId?: number;
|
|
17
|
-
perpMarketIndexes?:
|
|
18
|
-
spotMarketIndexes?:
|
|
16
|
+
perpMarketIndexes?: number[];
|
|
17
|
+
spotMarketIndexes?: number[];
|
|
19
18
|
oracleInfos?: OracleInfo[];
|
|
20
19
|
env?: DriftEnv;
|
|
21
20
|
userStats?: boolean;
|
package/src/clearingHouseUser.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from './types';
|
|
12
12
|
import { calculateEntryPrice } from './math/position';
|
|
13
13
|
import {
|
|
14
|
-
|
|
14
|
+
PRICE_PRECISION,
|
|
15
15
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
16
16
|
ZERO,
|
|
17
17
|
TEN_THOUSAND,
|
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
DataAndSlot,
|
|
30
30
|
} from './accounts/types';
|
|
31
31
|
import {
|
|
32
|
-
|
|
32
|
+
calculateReservePrice,
|
|
33
33
|
calculateBaseAssetValue,
|
|
34
34
|
calculatePositionFundingPNL,
|
|
35
35
|
calculatePositionPNL,
|
|
@@ -125,13 +125,13 @@ export class ClearingHouseUser {
|
|
|
125
125
|
* @param marketIndex
|
|
126
126
|
* @returns userPosition
|
|
127
127
|
*/
|
|
128
|
-
public getUserPosition(marketIndex:
|
|
129
|
-
return this.getUserAccount().perpPositions.find(
|
|
130
|
-
position.marketIndex
|
|
128
|
+
public getUserPosition(marketIndex: number): PerpPosition | undefined {
|
|
129
|
+
return this.getUserAccount().perpPositions.find(
|
|
130
|
+
(position) => position.marketIndex === marketIndex
|
|
131
131
|
);
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
public getEmptyPosition(marketIndex:
|
|
134
|
+
public getEmptyPosition(marketIndex: number): PerpPosition {
|
|
135
135
|
return {
|
|
136
136
|
baseAssetAmount: ZERO,
|
|
137
137
|
remainderBaseAssetAmount: ZERO,
|
|
@@ -139,7 +139,7 @@ export class ClearingHouseUser {
|
|
|
139
139
|
marketIndex,
|
|
140
140
|
quoteAssetAmount: ZERO,
|
|
141
141
|
quoteEntryAmount: ZERO,
|
|
142
|
-
openOrders:
|
|
142
|
+
openOrders: 0,
|
|
143
143
|
openBids: ZERO,
|
|
144
144
|
openAsks: ZERO,
|
|
145
145
|
settledPnl: ZERO,
|
|
@@ -193,7 +193,7 @@ export class ClearingHouseUser {
|
|
|
193
193
|
* @returns : the dust base asset amount (ie, < stepsize)
|
|
194
194
|
* @returns : pnl from settle
|
|
195
195
|
*/
|
|
196
|
-
public getSettledLPPosition(marketIndex:
|
|
196
|
+
public getSettledLPPosition(marketIndex: number): [PerpPosition, BN, BN] {
|
|
197
197
|
const _position = this.getUserPosition(marketIndex);
|
|
198
198
|
const position = this.getClonedPosition(_position);
|
|
199
199
|
|
|
@@ -297,7 +297,7 @@ export class ClearingHouseUser {
|
|
|
297
297
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
298
298
|
* @returns : Precision QUOTE_PRECISION
|
|
299
299
|
*/
|
|
300
|
-
public getBuyingPower(marketIndex:
|
|
300
|
+
public getBuyingPower(marketIndex: number): BN {
|
|
301
301
|
return this.getFreeCollateral()
|
|
302
302
|
.mul(this.getMaxLeverage(marketIndex, 'Initial'))
|
|
303
303
|
.div(TEN_THOUSAND);
|
|
@@ -355,7 +355,7 @@ export class ClearingHouseUser {
|
|
|
355
355
|
*/
|
|
356
356
|
public getUnrealizedPNL(
|
|
357
357
|
withFunding?: boolean,
|
|
358
|
-
marketIndex?:
|
|
358
|
+
marketIndex?: number,
|
|
359
359
|
withWeightMarginCategory?: MarginCategory
|
|
360
360
|
): BN {
|
|
361
361
|
const quoteSpotMarket = this.clearingHouse.getQuoteSpotMarketAccount();
|
|
@@ -400,7 +400,7 @@ export class ClearingHouseUser {
|
|
|
400
400
|
* calculates unrealized funding payment pnl
|
|
401
401
|
* @returns : Precision QUOTE_PRECISION
|
|
402
402
|
*/
|
|
403
|
-
public getUnrealizedFundingPNL(marketIndex?:
|
|
403
|
+
public getUnrealizedFundingPNL(marketIndex?: number): BN {
|
|
404
404
|
return this.getUserAccount()
|
|
405
405
|
.perpPositions.filter((pos) =>
|
|
406
406
|
marketIndex ? pos.marketIndex === marketIndex : true
|
|
@@ -414,7 +414,7 @@ export class ClearingHouseUser {
|
|
|
414
414
|
}
|
|
415
415
|
|
|
416
416
|
public getSpotMarketLiabilityValue(
|
|
417
|
-
marketIndex?:
|
|
417
|
+
marketIndex?: number,
|
|
418
418
|
marginCategory?: MarginCategory,
|
|
419
419
|
liquidationBuffer?: BN,
|
|
420
420
|
includeOpenOrders?: boolean
|
|
@@ -424,7 +424,7 @@ export class ClearingHouseUser {
|
|
|
424
424
|
if (
|
|
425
425
|
isSpotPositionAvailable(spotPosition) ||
|
|
426
426
|
(marketIndex !== undefined &&
|
|
427
|
-
|
|
427
|
+
spotPosition.marketIndex !== marketIndex)
|
|
428
428
|
) {
|
|
429
429
|
return totalLiabilityValue;
|
|
430
430
|
}
|
|
@@ -432,7 +432,7 @@ export class ClearingHouseUser {
|
|
|
432
432
|
const spotMarketAccount: SpotMarketAccount =
|
|
433
433
|
this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
434
434
|
|
|
435
|
-
if (spotPosition.marketIndex
|
|
435
|
+
if (spotPosition.marketIndex === QUOTE_SPOT_MARKET_INDEX) {
|
|
436
436
|
if (isVariant(spotPosition.balanceType, 'borrow')) {
|
|
437
437
|
const tokenAmount = getTokenAmount(
|
|
438
438
|
spotPosition.balance,
|
|
@@ -566,7 +566,7 @@ export class ClearingHouseUser {
|
|
|
566
566
|
}
|
|
567
567
|
|
|
568
568
|
public getSpotMarketAssetValue(
|
|
569
|
-
marketIndex?:
|
|
569
|
+
marketIndex?: number,
|
|
570
570
|
marginCategory?: MarginCategory,
|
|
571
571
|
includeOpenOrders?: boolean
|
|
572
572
|
): BN {
|
|
@@ -575,7 +575,7 @@ export class ClearingHouseUser {
|
|
|
575
575
|
if (
|
|
576
576
|
isSpotPositionAvailable(spotPosition) ||
|
|
577
577
|
(marketIndex !== undefined &&
|
|
578
|
-
|
|
578
|
+
spotPosition.marketIndex !== marketIndex)
|
|
579
579
|
) {
|
|
580
580
|
return totalAssetValue;
|
|
581
581
|
}
|
|
@@ -584,7 +584,7 @@ export class ClearingHouseUser {
|
|
|
584
584
|
const spotMarketAccount: SpotMarketAccount =
|
|
585
585
|
this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
586
586
|
|
|
587
|
-
if (spotPosition.marketIndex
|
|
587
|
+
if (spotPosition.marketIndex === QUOTE_SPOT_MARKET_INDEX) {
|
|
588
588
|
if (isVariant(spotPosition.balanceType, 'deposit')) {
|
|
589
589
|
const tokenAmount = getTokenAmount(
|
|
590
590
|
spotPosition.balance,
|
|
@@ -765,7 +765,7 @@ export class ClearingHouseUser {
|
|
|
765
765
|
let baseAssetValue = baseAssetAmount
|
|
766
766
|
.abs()
|
|
767
767
|
.mul(valuationPrice)
|
|
768
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(
|
|
768
|
+
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(PRICE_PRECISION));
|
|
769
769
|
|
|
770
770
|
if (marginCategory) {
|
|
771
771
|
let marginRatio = new BN(
|
|
@@ -803,7 +803,7 @@ export class ClearingHouseUser {
|
|
|
803
803
|
* @returns : Precision QUOTE_PRECISION
|
|
804
804
|
*/
|
|
805
805
|
public getPerpPositionValue(
|
|
806
|
-
marketIndex:
|
|
806
|
+
marketIndex: number,
|
|
807
807
|
oraclePriceData: OraclePriceData
|
|
808
808
|
): BN {
|
|
809
809
|
const userPosition =
|
|
@@ -832,7 +832,7 @@ export class ClearingHouseUser {
|
|
|
832
832
|
|
|
833
833
|
/**
|
|
834
834
|
* calculates average exit price (optionally for closing up to 100% of position)
|
|
835
|
-
* @returns : Precision
|
|
835
|
+
* @returns : Precision PRICE_PRECISION
|
|
836
836
|
*/
|
|
837
837
|
public getPositionEstimatedExitPriceAndPnl(
|
|
838
838
|
position: PerpPosition,
|
|
@@ -849,7 +849,7 @@ export class ClearingHouseUser {
|
|
|
849
849
|
|
|
850
850
|
if (amountToClose) {
|
|
851
851
|
if (amountToClose.eq(ZERO)) {
|
|
852
|
-
return [
|
|
852
|
+
return [calculateReservePrice(market, oraclePriceData), ZERO];
|
|
853
853
|
}
|
|
854
854
|
position = {
|
|
855
855
|
baseAssetAmount: amountToClose,
|
|
@@ -880,13 +880,13 @@ export class ClearingHouseUser {
|
|
|
880
880
|
|
|
881
881
|
const exitPrice = baseAssetValue
|
|
882
882
|
.mul(AMM_TO_QUOTE_PRECISION_RATIO)
|
|
883
|
-
.mul(
|
|
883
|
+
.mul(PRICE_PRECISION)
|
|
884
884
|
.div(position.baseAssetAmount.abs());
|
|
885
885
|
|
|
886
886
|
const pnlPerBase = exitPrice.sub(entryPrice);
|
|
887
887
|
const pnl = pnlPerBase
|
|
888
888
|
.mul(position.baseAssetAmount)
|
|
889
|
-
.div(
|
|
889
|
+
.div(PRICE_PRECISION)
|
|
890
890
|
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
891
891
|
|
|
892
892
|
return [exitPrice, pnl];
|
|
@@ -929,7 +929,7 @@ export class ClearingHouseUser {
|
|
|
929
929
|
* @returns : Precision TEN_THOUSAND
|
|
930
930
|
*/
|
|
931
931
|
public getMaxLeverage(
|
|
932
|
-
marketIndex:
|
|
932
|
+
marketIndex: number,
|
|
933
933
|
category: MarginCategory = 'Initial'
|
|
934
934
|
): BN {
|
|
935
935
|
const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
|
|
@@ -1026,7 +1026,7 @@ export class ClearingHouseUser {
|
|
|
1026
1026
|
* @param PerpPosition
|
|
1027
1027
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
1028
1028
|
* @param partial
|
|
1029
|
-
* @returns Precision :
|
|
1029
|
+
* @returns Precision : PRICE_PRECISION
|
|
1030
1030
|
*/
|
|
1031
1031
|
public liquidationPrice(
|
|
1032
1032
|
perpPosition: Pick<PerpPosition, 'marketIndex'>,
|
|
@@ -1068,7 +1068,7 @@ export class ClearingHouseUser {
|
|
|
1068
1068
|
quoteAssetAmount: new BN(0),
|
|
1069
1069
|
lastCumulativeFundingRate: ZERO,
|
|
1070
1070
|
quoteEntryAmount: new BN(0),
|
|
1071
|
-
openOrders:
|
|
1071
|
+
openOrders: 0,
|
|
1072
1072
|
openBids: new BN(0),
|
|
1073
1073
|
openAsks: new BN(0),
|
|
1074
1074
|
settledPnl: ZERO,
|
|
@@ -1097,7 +1097,7 @@ export class ClearingHouseUser {
|
|
|
1097
1097
|
const marginRequirementExcludingTargetMarket =
|
|
1098
1098
|
this.getUserAccount().perpPositions.reduce(
|
|
1099
1099
|
(totalMarginRequirement, position) => {
|
|
1100
|
-
if (
|
|
1100
|
+
if (position.marketIndex !== perpPosition.marketIndex) {
|
|
1101
1101
|
const market = this.clearingHouse.getPerpMarketAccount(
|
|
1102
1102
|
position.marketIndex
|
|
1103
1103
|
);
|
|
@@ -1180,7 +1180,7 @@ export class ClearingHouseUser {
|
|
|
1180
1180
|
|
|
1181
1181
|
let markPriceAfterTrade;
|
|
1182
1182
|
if (positionBaseSizeChange.eq(ZERO)) {
|
|
1183
|
-
markPriceAfterTrade =
|
|
1183
|
+
markPriceAfterTrade = calculateReservePrice(
|
|
1184
1184
|
this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex),
|
|
1185
1185
|
this.getOracleDataForMarket(perpPosition.marketIndex)
|
|
1186
1186
|
);
|
|
@@ -1208,10 +1208,10 @@ export class ClearingHouseUser {
|
|
|
1208
1208
|
* Calculates the estimated liquidation price for a position after closing a quote amount of the position.
|
|
1209
1209
|
* @param positionMarketIndex
|
|
1210
1210
|
* @param closeQuoteAmount
|
|
1211
|
-
* @returns : Precision
|
|
1211
|
+
* @returns : Precision PRICE_PRECISION
|
|
1212
1212
|
*/
|
|
1213
1213
|
public liquidationPriceAfterClose(
|
|
1214
|
-
positionMarketIndex:
|
|
1214
|
+
positionMarketIndex: number,
|
|
1215
1215
|
closeQuoteAmount: BN
|
|
1216
1216
|
): BN {
|
|
1217
1217
|
const currentPosition =
|
|
@@ -1258,7 +1258,7 @@ export class ClearingHouseUser {
|
|
|
1258
1258
|
* @returns tradeSizeAllowed : Precision QUOTE_PRECISION
|
|
1259
1259
|
*/
|
|
1260
1260
|
public getMaxTradeSizeUSDC(
|
|
1261
|
-
targetMarketIndex:
|
|
1261
|
+
targetMarketIndex: number,
|
|
1262
1262
|
tradeSide: PositionDirection
|
|
1263
1263
|
): BN {
|
|
1264
1264
|
const currentPosition =
|
|
@@ -1343,7 +1343,7 @@ export class ClearingHouseUser {
|
|
|
1343
1343
|
* @returns leverageRatio : Precision TEN_THOUSAND
|
|
1344
1344
|
*/
|
|
1345
1345
|
public accountLeverageRatioAfterTrade(
|
|
1346
|
-
targetMarketIndex:
|
|
1346
|
+
targetMarketIndex: number,
|
|
1347
1347
|
tradeQuoteAmount: BN,
|
|
1348
1348
|
tradeSide: PositionDirection
|
|
1349
1349
|
): BN {
|
|
@@ -1407,7 +1407,7 @@ export class ClearingHouseUser {
|
|
|
1407
1407
|
* @param marketToIgnore
|
|
1408
1408
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
1409
1409
|
*/
|
|
1410
|
-
private getTotalPerpPositionValueExcludingMarket(marketToIgnore:
|
|
1410
|
+
private getTotalPerpPositionValueExcludingMarket(marketToIgnore: number): BN {
|
|
1411
1411
|
const currentPerpPosition =
|
|
1412
1412
|
this.getUserPosition(marketToIgnore) ||
|
|
1413
1413
|
this.getEmptyPosition(marketToIgnore);
|
|
@@ -1425,7 +1425,7 @@ export class ClearingHouseUser {
|
|
|
1425
1425
|
return this.getTotalPerpPositionValue().sub(currentPerpPositionValueUSDC);
|
|
1426
1426
|
}
|
|
1427
1427
|
|
|
1428
|
-
private getOracleDataForMarket(marketIndex:
|
|
1428
|
+
private getOracleDataForMarket(marketIndex: number): OraclePriceData {
|
|
1429
1429
|
const oracleKey =
|
|
1430
1430
|
this.clearingHouse.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1431
1431
|
const oracleData =
|
|
@@ -1433,7 +1433,7 @@ export class ClearingHouseUser {
|
|
|
1433
1433
|
|
|
1434
1434
|
return oracleData;
|
|
1435
1435
|
}
|
|
1436
|
-
private getOracleDataForSpotMarket(marketIndex:
|
|
1436
|
+
private getOracleDataForSpotMarket(marketIndex: number): OraclePriceData {
|
|
1437
1437
|
const oracleKey =
|
|
1438
1438
|
this.clearingHouse.getSpotMarketAccount(marketIndex).oracle;
|
|
1439
1439
|
|
package/src/config.ts
CHANGED
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
DevnetSpotMarkets,
|
|
11
11
|
MainnetSpotMarkets,
|
|
12
12
|
} from './constants/spotMarkets';
|
|
13
|
-
import { BN } from '@project-serum/anchor';
|
|
14
13
|
import { OracleInfo } from './oracles/types';
|
|
15
14
|
|
|
16
15
|
type DriftConfig = {
|
|
@@ -28,7 +27,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
28
27
|
devnet: {
|
|
29
28
|
ENV: 'devnet',
|
|
30
29
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
31
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
30
|
+
CLEARING_HOUSE_PROGRAM_ID: 'By7XjakxXVnQ9gMZ4VT98DenTgBCeP295A58ybzgwVPZ',
|
|
32
31
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
33
32
|
PERP_MARKETS: DevnetPerpMarkets,
|
|
34
33
|
SPOT_MARKETS: DevnetSpotMarkets,
|
|
@@ -68,8 +67,8 @@ export const initialize = (props: {
|
|
|
68
67
|
};
|
|
69
68
|
|
|
70
69
|
export function getMarketsAndOraclesForSubscription(env: DriftEnv): {
|
|
71
|
-
perpMarketIndexes:
|
|
72
|
-
spotMarketIndexes:
|
|
70
|
+
perpMarketIndexes: number[];
|
|
71
|
+
spotMarketIndexes: number[];
|
|
73
72
|
oracleInfos: OracleInfo[];
|
|
74
73
|
} {
|
|
75
74
|
const perpMarketIndexes = [];
|
|
@@ -12,17 +12,17 @@ export const TEN_MILLION = TEN_THOUSAND.mul(TEN_THOUSAND);
|
|
|
12
12
|
export const MAX_LEVERAGE = new BN(5);
|
|
13
13
|
|
|
14
14
|
export const QUOTE_PRECISION_EXP = new BN(6);
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
17
|
-
export const FUNDING_RATE_PRECISION_EXP =
|
|
18
|
-
|
|
15
|
+
export const FUNDING_RATE_BUFFER_PRECISION_EXP = new BN(3);
|
|
16
|
+
export const PRICE_PRECISION_EXP = new BN(6);
|
|
17
|
+
export const FUNDING_RATE_PRECISION_EXP = PRICE_PRECISION_EXP.mul(
|
|
18
|
+
FUNDING_RATE_BUFFER_PRECISION_EXP
|
|
19
19
|
);
|
|
20
|
-
export const PEG_PRECISION_EXP = new BN(
|
|
21
|
-
export const AMM_RESERVE_PRECISION_EXP = new BN(
|
|
20
|
+
export const PEG_PRECISION_EXP = new BN(6);
|
|
21
|
+
export const AMM_RESERVE_PRECISION_EXP = new BN(9);
|
|
22
22
|
|
|
23
|
-
export const
|
|
24
|
-
export const
|
|
25
|
-
|
|
23
|
+
export const SPOT_MARKET_RATE_PRECISION_EXP = new BN(6);
|
|
24
|
+
export const SPOT_MARKET_RATE_PRECISION = new BN(10).pow(
|
|
25
|
+
SPOT_MARKET_RATE_PRECISION_EXP
|
|
26
26
|
);
|
|
27
27
|
|
|
28
28
|
export const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = new BN(10);
|
|
@@ -31,9 +31,8 @@ export const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = new BN(10).pow(
|
|
|
31
31
|
);
|
|
32
32
|
|
|
33
33
|
export const SPOT_MARKET_UTILIZATION_PRECISION = new BN(1000000);
|
|
34
|
-
export const SPOT_MARKET_RATE_PRECISION = new BN(1000000);
|
|
35
34
|
export const SPOT_MARKET_WEIGHT_PRECISION = new BN(10000);
|
|
36
|
-
export const SPOT_MARKET_BALANCE_PRECISION_EXP = new BN(
|
|
35
|
+
export const SPOT_MARKET_BALANCE_PRECISION_EXP = new BN(9);
|
|
37
36
|
export const SPOT_MARKET_BALANCE_PRECISION = new BN(10).pow(
|
|
38
37
|
SPOT_MARKET_BALANCE_PRECISION_EXP
|
|
39
38
|
);
|
|
@@ -45,9 +44,9 @@ export const SPOT_MARKET_IMF_PRECISION = new BN(10).pow(
|
|
|
45
44
|
export const LIQUIDATION_FEE_PRECISION = new BN(1000000);
|
|
46
45
|
|
|
47
46
|
export const QUOTE_PRECISION = new BN(10).pow(QUOTE_PRECISION_EXP);
|
|
48
|
-
export const
|
|
49
|
-
export const
|
|
50
|
-
|
|
47
|
+
export const PRICE_PRECISION = new BN(10).pow(PRICE_PRECISION_EXP);
|
|
48
|
+
export const FUNDING_RATE_BUFFER_PRECISION = new BN(10).pow(
|
|
49
|
+
FUNDING_RATE_BUFFER_PRECISION_EXP
|
|
51
50
|
);
|
|
52
51
|
export const PEG_PRECISION = new BN(10).pow(PEG_PRECISION_EXP);
|
|
53
52
|
|
|
@@ -57,18 +56,17 @@ export const BASE_PRECISION = AMM_RESERVE_PRECISION;
|
|
|
57
56
|
export const BASE_PRECISION_EXP = AMM_RESERVE_PRECISION_EXP;
|
|
58
57
|
|
|
59
58
|
export const AMM_TO_QUOTE_PRECISION_RATIO =
|
|
60
|
-
AMM_RESERVE_PRECISION.div(QUOTE_PRECISION); // 10^
|
|
61
|
-
export const PRICE_DIV_PEG =
|
|
62
|
-
export const PRICE_TO_QUOTE_PRECISION =
|
|
63
|
-
MARK_PRICE_PRECISION.div(QUOTE_PRECISION);
|
|
59
|
+
AMM_RESERVE_PRECISION.div(QUOTE_PRECISION); // 10^3
|
|
60
|
+
export const PRICE_DIV_PEG = PRICE_PRECISION.div(PEG_PRECISION); //10^1
|
|
61
|
+
export const PRICE_TO_QUOTE_PRECISION = PRICE_PRECISION.div(QUOTE_PRECISION); // 10^1
|
|
64
62
|
export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
|
|
65
|
-
AMM_RESERVE_PRECISION.mul(PEG_PRECISION).div(QUOTE_PRECISION); // 10^
|
|
63
|
+
AMM_RESERVE_PRECISION.mul(PEG_PRECISION).div(QUOTE_PRECISION); // 10^9
|
|
66
64
|
export const MARGIN_PRECISION = TEN_THOUSAND;
|
|
67
|
-
export const BID_ASK_SPREAD_PRECISION = new BN(1000000);
|
|
65
|
+
export const BID_ASK_SPREAD_PRECISION = new BN(1000000); // 10^6
|
|
68
66
|
|
|
69
67
|
export const ONE_YEAR = new BN(31536000);
|
|
70
68
|
|
|
71
|
-
export const QUOTE_SPOT_MARKET_INDEX =
|
|
69
|
+
export const QUOTE_SPOT_MARKET_INDEX = 0;
|
|
72
70
|
|
|
73
71
|
export const LAMPORTS_PRECISION = new BN(LAMPORTS_PER_SOL);
|
|
74
72
|
export const LAMPORTS_EXP = new BN(Math.log10(LAMPORTS_PER_SOL));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OracleSource } from '../';
|
|
2
2
|
import { DriftEnv } from '../';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ export type PerpMarketConfig = {
|
|
|
7
7
|
category?: string[];
|
|
8
8
|
symbol: string;
|
|
9
9
|
baseAssetSymbol: string;
|
|
10
|
-
marketIndex:
|
|
10
|
+
marketIndex: number;
|
|
11
11
|
launchTs: number;
|
|
12
12
|
oracle: PublicKey;
|
|
13
13
|
oracleSource: OracleSource;
|
|
@@ -19,7 +19,7 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
19
19
|
category: ['L1', 'Infra'],
|
|
20
20
|
symbol: 'SOL-PERP',
|
|
21
21
|
baseAssetSymbol: 'SOL',
|
|
22
|
-
marketIndex:
|
|
22
|
+
marketIndex: 0,
|
|
23
23
|
oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
|
24
24
|
launchTs: 1655751353000,
|
|
25
25
|
oracleSource: OracleSource.PYTH,
|
|
@@ -29,7 +29,7 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
29
29
|
category: ['L1', 'Payment'],
|
|
30
30
|
symbol: 'BTC-PERP',
|
|
31
31
|
baseAssetSymbol: 'BTC',
|
|
32
|
-
marketIndex:
|
|
32
|
+
marketIndex: 1,
|
|
33
33
|
oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
34
34
|
launchTs: 1655751353000,
|
|
35
35
|
oracleSource: OracleSource.PYTH,
|
|
@@ -39,7 +39,7 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
39
39
|
category: ['L1', 'Infra'],
|
|
40
40
|
symbol: 'ETH-PERP',
|
|
41
41
|
baseAssetSymbol: 'ETH',
|
|
42
|
-
marketIndex:
|
|
42
|
+
marketIndex: 2,
|
|
43
43
|
oracle: new PublicKey('EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw'),
|
|
44
44
|
launchTs: 1637691133472,
|
|
45
45
|
oracleSource: OracleSource.PYTH,
|
|
@@ -9,12 +9,13 @@ import {
|
|
|
9
9
|
|
|
10
10
|
export type SpotMarketConfig = {
|
|
11
11
|
symbol: string;
|
|
12
|
-
marketIndex:
|
|
12
|
+
marketIndex: number;
|
|
13
13
|
oracle: PublicKey;
|
|
14
14
|
mint: PublicKey;
|
|
15
15
|
oracleSource: OracleSource;
|
|
16
16
|
precision: BN;
|
|
17
17
|
precisionExp: BN;
|
|
18
|
+
serumMarket?: PublicKey;
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
export const WRAPPED_SOL_MINT = new PublicKey(
|
|
@@ -24,7 +25,7 @@ export const WRAPPED_SOL_MINT = new PublicKey(
|
|
|
24
25
|
export const DevnetSpotMarkets: SpotMarketConfig[] = [
|
|
25
26
|
{
|
|
26
27
|
symbol: 'USDC',
|
|
27
|
-
marketIndex:
|
|
28
|
+
marketIndex: 0,
|
|
28
29
|
oracle: PublicKey.default,
|
|
29
30
|
oracleSource: OracleSource.QUOTE_ASSET,
|
|
30
31
|
mint: new PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
|
|
@@ -33,28 +34,30 @@ export const DevnetSpotMarkets: SpotMarketConfig[] = [
|
|
|
33
34
|
},
|
|
34
35
|
{
|
|
35
36
|
symbol: 'SOL',
|
|
36
|
-
marketIndex:
|
|
37
|
+
marketIndex: 1,
|
|
37
38
|
oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
|
38
39
|
oracleSource: OracleSource.PYTH,
|
|
39
40
|
mint: new PublicKey(WRAPPED_SOL_MINT),
|
|
40
41
|
precision: LAMPORTS_PRECISION,
|
|
41
42
|
precisionExp: LAMPORTS_EXP,
|
|
43
|
+
serumMarket: new PublicKey('8N37SsnTu8RYxtjrV9SStjkkwVhmU8aCWhLvwduAPEKW'),
|
|
42
44
|
},
|
|
43
45
|
{
|
|
44
46
|
symbol: 'BTC',
|
|
45
|
-
marketIndex:
|
|
47
|
+
marketIndex: 2,
|
|
46
48
|
oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
47
49
|
oracleSource: OracleSource.PYTH,
|
|
48
50
|
mint: new PublicKey('3BZPwbcqB5kKScF3TEXxwNfx5ipV13kbRVDvfVp5c6fv'),
|
|
49
51
|
precision: SPOT_MARKET_BALANCE_PRECISION,
|
|
50
52
|
precisionExp: SPOT_MARKET_BALANCE_PRECISION_EXP,
|
|
53
|
+
serumMarket: new PublicKey('AGsmbVu3MS9u68GEYABWosQQCZwmLcBHu4pWEuBYH7Za'),
|
|
51
54
|
},
|
|
52
55
|
];
|
|
53
56
|
|
|
54
57
|
export const MainnetSpotMarkets: SpotMarketConfig[] = [
|
|
55
58
|
{
|
|
56
59
|
symbol: 'USDC',
|
|
57
|
-
marketIndex:
|
|
60
|
+
marketIndex: 0,
|
|
58
61
|
oracle: PublicKey.default,
|
|
59
62
|
oracleSource: OracleSource.QUOTE_ASSET,
|
|
60
63
|
mint: new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
|