@drift-labs/sdk 2.97.0-beta.0 → 2.97.0-beta.10
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 +11 -4
- package/VERSION +1 -1
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.d.ts +6 -0
- package/lib/adminClient.js +53 -0
- package/lib/config.d.ts +1 -1
- package/lib/config.js +5 -3
- package/lib/constants/perpMarkets.js +20 -0
- package/lib/driftClient.d.ts +8 -1
- package/lib/driftClient.js +55 -4
- package/lib/idl/drift.json +247 -2
- package/lib/math/margin.d.ts +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +15 -3
- package/lib/types.d.ts +11 -0
- package/lib/types.js +6 -1
- package/lib/user.d.ts +1 -0
- package/lib/user.js +12 -8
- package/package.json +1 -1
- package/src/addresses/pda.ts +9 -0
- package/src/adminClient.ts +114 -0
- package/src/config.ts +12 -3
- package/src/constants/perpMarkets.ts +20 -0
- package/src/driftClient.ts +132 -11
- package/src/idl/drift.json +247 -2
- package/src/math/margin.ts +8 -4
- package/src/math/market.ts +19 -3
- package/src/types.ts +9 -0
- package/src/user.ts +21 -8
package/lib/math/margin.js
CHANGED
|
@@ -123,11 +123,11 @@ exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
|
|
|
123
123
|
* @param baseSize
|
|
124
124
|
* @returns
|
|
125
125
|
*/
|
|
126
|
-
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio) {
|
|
126
|
+
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode) {
|
|
127
127
|
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
128
128
|
const oracleData = driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex);
|
|
129
129
|
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, oracleData.price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
|
|
130
|
-
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio))
|
|
130
|
+
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio, userHighLeverageMode))
|
|
131
131
|
.mul(perpLiabilityValue)
|
|
132
132
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
133
133
|
return marginRequired;
|
|
@@ -138,8 +138,8 @@ exports.calculateMarginUSDCRequiredForTrade = calculateMarginUSDCRequiredForTrad
|
|
|
138
138
|
*
|
|
139
139
|
* Returns collateral required in the precision of the target collateral market.
|
|
140
140
|
*/
|
|
141
|
-
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio) {
|
|
142
|
-
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio);
|
|
141
|
+
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode) {
|
|
142
|
+
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode);
|
|
143
143
|
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
|
144
144
|
const collateralOracleData = driftClient.getOracleDataForSpotMarket(collateralIndex);
|
|
145
145
|
const scaledAssetWeight = (0, __1.calculateScaledInitialAssetWeight)(collateralMarket, collateralOracleData.price);
|
package/lib/math/market.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare function calculateAskPrice(market: PerpMarketAccount, oraclePrice
|
|
|
27
27
|
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: PerpMarketAccount): PerpMarketAccount;
|
|
28
28
|
export declare function calculateOracleReserveSpread(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
29
29
|
export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
|
|
30
|
-
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number): number;
|
|
30
|
+
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number, userHighLeverageMode?: boolean): number;
|
|
31
31
|
export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
|
|
32
32
|
export declare function calculateMarketAvailablePNL(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
|
|
33
33
|
export declare function calculateMarketMaxAvailableInsurance(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
|
package/lib/math/market.js
CHANGED
|
@@ -60,16 +60,28 @@ function calculateOracleSpread(price, oraclePriceData) {
|
|
|
60
60
|
return price.sub(oraclePriceData.price);
|
|
61
61
|
}
|
|
62
62
|
exports.calculateOracleSpread = calculateOracleSpread;
|
|
63
|
-
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0) {
|
|
63
|
+
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
|
|
64
|
+
let marginRationInitial;
|
|
65
|
+
let marginRatioMaintenance;
|
|
66
|
+
if (userHighLeverageMode &&
|
|
67
|
+
market.highLeverageMarginRatioInitial > 0 &&
|
|
68
|
+
market.highLeverageMarginRatioMaintenance) {
|
|
69
|
+
marginRationInitial = market.highLeverageMarginRatioInitial;
|
|
70
|
+
marginRatioMaintenance = market.highLeverageMarginRatioMaintenance;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
marginRationInitial = market.marginRatioInitial;
|
|
74
|
+
marginRatioMaintenance = market.marginRatioMaintenance;
|
|
75
|
+
}
|
|
64
76
|
let marginRatio;
|
|
65
77
|
switch (marginCategory) {
|
|
66
78
|
case 'Initial': {
|
|
67
79
|
// use lowest leverage between max allowed and optional user custom max
|
|
68
|
-
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(
|
|
80
|
+
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
|
|
69
81
|
break;
|
|
70
82
|
}
|
|
71
83
|
case 'Maintenance': {
|
|
72
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(
|
|
84
|
+
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
73
85
|
break;
|
|
74
86
|
}
|
|
75
87
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -71,6 +71,14 @@ export declare enum UserStatus {
|
|
|
71
71
|
REDUCE_ONLY = 4,
|
|
72
72
|
ADVANCED_LP = 8
|
|
73
73
|
}
|
|
74
|
+
export declare class MarginMode {
|
|
75
|
+
static readonly DEFAULT: {
|
|
76
|
+
default: {};
|
|
77
|
+
};
|
|
78
|
+
static readonly HIGH_LEVERAGE: {
|
|
79
|
+
highLeverage: {};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
74
82
|
export declare class ContractType {
|
|
75
83
|
static readonly PERPETUAL: {
|
|
76
84
|
perpetual: {};
|
|
@@ -753,6 +761,8 @@ export type PerpMarketAccount = {
|
|
|
753
761
|
fuelBoostTaker: number;
|
|
754
762
|
fuelBoostMaker: number;
|
|
755
763
|
fuelBoostPosition: number;
|
|
764
|
+
highLeverageMarginRatioInitial: number;
|
|
765
|
+
highLeverageMarginRatioMaintenance: number;
|
|
756
766
|
};
|
|
757
767
|
export type HistoricalOracleData = {
|
|
758
768
|
lastOraclePrice: BN;
|
|
@@ -1004,6 +1014,7 @@ export type UserAccount = {
|
|
|
1004
1014
|
openAuctions: number;
|
|
1005
1015
|
hasOpenAuction: boolean;
|
|
1006
1016
|
lastFuelBonusUpdateTs: number;
|
|
1017
|
+
marginMode: MarginMode;
|
|
1007
1018
|
};
|
|
1008
1019
|
export type SpotPosition = {
|
|
1009
1020
|
marketIndex: number;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
3
|
+
exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarginMode = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
var ExchangeStatus;
|
|
@@ -58,6 +58,11 @@ var UserStatus;
|
|
|
58
58
|
UserStatus[UserStatus["REDUCE_ONLY"] = 4] = "REDUCE_ONLY";
|
|
59
59
|
UserStatus[UserStatus["ADVANCED_LP"] = 8] = "ADVANCED_LP";
|
|
60
60
|
})(UserStatus = exports.UserStatus || (exports.UserStatus = {}));
|
|
61
|
+
class MarginMode {
|
|
62
|
+
}
|
|
63
|
+
exports.MarginMode = MarginMode;
|
|
64
|
+
MarginMode.DEFAULT = { default: {} };
|
|
65
|
+
MarginMode.HIGH_LEVERAGE = { highLeverage: {} };
|
|
61
66
|
class ContractType {
|
|
62
67
|
}
|
|
63
68
|
exports.ContractType = ContractType;
|
package/lib/user.d.ts
CHANGED
|
@@ -247,6 +247,7 @@ export declare class User {
|
|
|
247
247
|
isBeingLiquidated(): boolean;
|
|
248
248
|
hasStatus(status: UserStatus): boolean;
|
|
249
249
|
isBankrupt(): boolean;
|
|
250
|
+
isHighLeverageMode(): boolean;
|
|
250
251
|
/**
|
|
251
252
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
252
253
|
* @returns
|
package/lib/user.js
CHANGED
|
@@ -420,7 +420,7 @@ class User {
|
|
|
420
420
|
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount);
|
|
421
421
|
}
|
|
422
422
|
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount) {
|
|
423
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', this.getUserAccount().maxMarginRatio);
|
|
423
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
424
424
|
return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio));
|
|
425
425
|
}
|
|
426
426
|
/**
|
|
@@ -787,7 +787,7 @@ class User {
|
|
|
787
787
|
liabilityValue = (0, _1.calculatePerpLiabilityValue)(baseAssetAmount, valuationPrice, (0, types_1.isVariant)(market.contractType, 'prediction'));
|
|
788
788
|
}
|
|
789
789
|
if (marginCategory) {
|
|
790
|
-
let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
|
|
790
|
+
let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode()));
|
|
791
791
|
if (liquidationBuffer !== undefined) {
|
|
792
792
|
marginRatio = marginRatio.add(liquidationBuffer);
|
|
793
793
|
}
|
|
@@ -1032,7 +1032,7 @@ class User {
|
|
|
1032
1032
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
1033
1033
|
.div(marketPrice));
|
|
1034
1034
|
// margin ratio incorporting upper bound on size
|
|
1035
|
-
let marginRatio = (0, _1.calculateMarketMarginRatio)(market, maxSize, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1035
|
+
let marginRatio = (0, _1.calculateMarketMarginRatio)(market, maxSize, marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1036
1036
|
// use more fesible size since imf factor activated
|
|
1037
1037
|
let attempts = 0;
|
|
1038
1038
|
while (marginRatio > rawMarginRatio + 1e-4 && attempts < 10) {
|
|
@@ -1043,7 +1043,7 @@ class User {
|
|
|
1043
1043
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
1044
1044
|
.div(marketPrice));
|
|
1045
1045
|
// margin ratio incorporting more fesible target size
|
|
1046
|
-
marginRatio = (0, _1.calculateMarketMarginRatio)(market, targetSize, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1046
|
+
marginRatio = (0, _1.calculateMarketMarginRatio)(market, targetSize, marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1047
1047
|
attempts += 1;
|
|
1048
1048
|
}
|
|
1049
1049
|
// how much more liabilities can be opened w remaining free collateral
|
|
@@ -1133,6 +1133,9 @@ class User {
|
|
|
1133
1133
|
isBankrupt() {
|
|
1134
1134
|
return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
|
|
1135
1135
|
}
|
|
1136
|
+
isHighLeverageMode() {
|
|
1137
|
+
return (0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverage');
|
|
1138
|
+
}
|
|
1136
1139
|
/**
|
|
1137
1140
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
1138
1141
|
* @returns
|
|
@@ -1279,7 +1282,7 @@ class User {
|
|
|
1279
1282
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
1280
1283
|
liabilityValue = (0, _1.calculatePerpLiabilityValue)(baseAssetAmount, oraclePrice, (0, types_1.isVariant)(market.contractType, 'prediction'));
|
|
1281
1284
|
}
|
|
1282
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), 'Maintenance');
|
|
1285
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), 'Maintenance', this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1283
1286
|
return liabilityValue.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION);
|
|
1284
1287
|
};
|
|
1285
1288
|
const freeCollateralConsumptionBefore = calculateMarginRequirement(perpPosition);
|
|
@@ -1295,7 +1298,7 @@ class User {
|
|
|
1295
1298
|
// zero if include orders == false
|
|
1296
1299
|
const orderBaseAssetAmount = baseAssetAmount.sub(perpPosition.baseAssetAmount);
|
|
1297
1300
|
const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
|
|
1298
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1301
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1299
1302
|
const marginRatioQuotePrecision = new _1.BN(marginRatio)
|
|
1300
1303
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1301
1304
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -1372,7 +1375,8 @@ class User {
|
|
|
1372
1375
|
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio);
|
|
1373
1376
|
}
|
|
1374
1377
|
getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex) {
|
|
1375
|
-
return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio
|
|
1378
|
+
return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio, false // assume user cant be high leverage if they havent created user account ?
|
|
1379
|
+
);
|
|
1376
1380
|
}
|
|
1377
1381
|
/**
|
|
1378
1382
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
|
@@ -2002,7 +2006,7 @@ class User {
|
|
|
2002
2006
|
this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
|
|
2003
2007
|
const oraclePrice = _oraclePriceData.price;
|
|
2004
2008
|
const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, _1.calculateWorstCasePerpLiabilityValue)(settledLpPosition, perpMarket, oraclePrice);
|
|
2005
|
-
const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
|
|
2009
|
+
const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode()));
|
|
2006
2010
|
const _quoteOraclePriceData = quoteOraclePriceData ||
|
|
2007
2011
|
this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2008
2012
|
let marginRequirement = worstCaseLiabilityValue
|
package/package.json
CHANGED
package/src/addresses/pda.ts
CHANGED
|
@@ -300,3 +300,12 @@ export function getTokenProgramForSpotMarket(
|
|
|
300
300
|
}
|
|
301
301
|
return TOKEN_PROGRAM_ID;
|
|
302
302
|
}
|
|
303
|
+
|
|
304
|
+
export function getHighLeverageModeConfigPublicKey(
|
|
305
|
+
programId: PublicKey
|
|
306
|
+
): PublicKey {
|
|
307
|
+
return PublicKey.findProgramAddressSync(
|
|
308
|
+
[Buffer.from(anchor.utils.bytes.utf8.encode('high_leverage_mode_config'))],
|
|
309
|
+
programId
|
|
310
|
+
)[0];
|
|
311
|
+
}
|
package/src/adminClient.ts
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
getOpenbookV2FulfillmentConfigPublicKey,
|
|
33
33
|
getPythPullOraclePublicKey,
|
|
34
34
|
getUserStatsAccountPublicKey,
|
|
35
|
+
getHighLeverageModeConfigPublicKey,
|
|
35
36
|
} from './addresses/pda';
|
|
36
37
|
import { squareRootBN } from './math/utils';
|
|
37
38
|
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
|
@@ -1268,6 +1269,50 @@ export class AdminClient extends DriftClient {
|
|
|
1268
1269
|
);
|
|
1269
1270
|
}
|
|
1270
1271
|
|
|
1272
|
+
public async updatePerpMarketHighLeverageMarginRatio(
|
|
1273
|
+
perpMarketIndex: number,
|
|
1274
|
+
marginRatioInitial: number,
|
|
1275
|
+
marginRatioMaintenance: number
|
|
1276
|
+
): Promise<TransactionSignature> {
|
|
1277
|
+
const updatePerpMarketHighLeverageMarginRatioIx =
|
|
1278
|
+
await this.getUpdatePerpMarketHighLeverageMarginRatioIx(
|
|
1279
|
+
perpMarketIndex,
|
|
1280
|
+
marginRatioInitial,
|
|
1281
|
+
marginRatioMaintenance
|
|
1282
|
+
);
|
|
1283
|
+
|
|
1284
|
+
const tx = await this.buildTransaction(
|
|
1285
|
+
updatePerpMarketHighLeverageMarginRatioIx
|
|
1286
|
+
);
|
|
1287
|
+
|
|
1288
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1289
|
+
|
|
1290
|
+
return txSig;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
public async getUpdatePerpMarketHighLeverageMarginRatioIx(
|
|
1294
|
+
perpMarketIndex: number,
|
|
1295
|
+
marginRatioInitial: number,
|
|
1296
|
+
marginRatioMaintenance: number
|
|
1297
|
+
): Promise<TransactionInstruction> {
|
|
1298
|
+
return await this.program.instruction.updatePerpMarketHighLeverageMarginRatio(
|
|
1299
|
+
marginRatioInitial,
|
|
1300
|
+
marginRatioMaintenance,
|
|
1301
|
+
{
|
|
1302
|
+
accounts: {
|
|
1303
|
+
admin: this.isSubscribed
|
|
1304
|
+
? this.getStateAccount().admin
|
|
1305
|
+
: this.wallet.publicKey,
|
|
1306
|
+
state: await this.getStatePublicKey(),
|
|
1307
|
+
perpMarket: await getPerpMarketPublicKey(
|
|
1308
|
+
this.program.programId,
|
|
1309
|
+
perpMarketIndex
|
|
1310
|
+
),
|
|
1311
|
+
},
|
|
1312
|
+
}
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1271
1316
|
public async updatePerpMarketImfFactor(
|
|
1272
1317
|
perpMarketIndex: number,
|
|
1273
1318
|
imfFactor: number,
|
|
@@ -3847,4 +3892,73 @@ export class AdminClient extends DriftClient {
|
|
|
3847
3892
|
}
|
|
3848
3893
|
);
|
|
3849
3894
|
}
|
|
3895
|
+
|
|
3896
|
+
public async initializeHighLeverageModeConfig(
|
|
3897
|
+
maxUsers: number
|
|
3898
|
+
): Promise<TransactionSignature> {
|
|
3899
|
+
const initializeHighLeverageModeConfigIx =
|
|
3900
|
+
await this.getInitializeHighLeverageModeConfigIx(maxUsers);
|
|
3901
|
+
|
|
3902
|
+
const tx = await this.buildTransaction(initializeHighLeverageModeConfigIx);
|
|
3903
|
+
|
|
3904
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
3905
|
+
|
|
3906
|
+
return txSig;
|
|
3907
|
+
}
|
|
3908
|
+
|
|
3909
|
+
public async getInitializeHighLeverageModeConfigIx(
|
|
3910
|
+
maxUsers: number
|
|
3911
|
+
): Promise<TransactionInstruction> {
|
|
3912
|
+
return await this.program.instruction.initializeHighLeverageModeConfig(
|
|
3913
|
+
maxUsers,
|
|
3914
|
+
{
|
|
3915
|
+
accounts: {
|
|
3916
|
+
admin: this.isSubscribed
|
|
3917
|
+
? this.getStateAccount().admin
|
|
3918
|
+
: this.wallet.publicKey,
|
|
3919
|
+
state: await this.getStatePublicKey(),
|
|
3920
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
3921
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
3922
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
3923
|
+
this.program.programId
|
|
3924
|
+
),
|
|
3925
|
+
},
|
|
3926
|
+
}
|
|
3927
|
+
);
|
|
3928
|
+
}
|
|
3929
|
+
|
|
3930
|
+
public async updateUpdateHighLeverageModeConfig(
|
|
3931
|
+
maxUsers: number,
|
|
3932
|
+
reduceOnly: boolean
|
|
3933
|
+
): Promise<TransactionSignature> {
|
|
3934
|
+
const updateHighLeverageModeConfigIx =
|
|
3935
|
+
await this.getUpdateHighLeverageModeConfigIx(maxUsers, reduceOnly);
|
|
3936
|
+
|
|
3937
|
+
const tx = await this.buildTransaction(updateHighLeverageModeConfigIx);
|
|
3938
|
+
|
|
3939
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
3940
|
+
|
|
3941
|
+
return txSig;
|
|
3942
|
+
}
|
|
3943
|
+
|
|
3944
|
+
public async getUpdateHighLeverageModeConfigIx(
|
|
3945
|
+
maxUsers: number,
|
|
3946
|
+
reduceOnly: boolean
|
|
3947
|
+
): Promise<TransactionInstruction> {
|
|
3948
|
+
return await this.program.instruction.updateHighLeverageModeConfig(
|
|
3949
|
+
maxUsers,
|
|
3950
|
+
reduceOnly,
|
|
3951
|
+
{
|
|
3952
|
+
accounts: {
|
|
3953
|
+
admin: this.isSubscribed
|
|
3954
|
+
? this.getStateAccount().admin
|
|
3955
|
+
: this.wallet.publicKey,
|
|
3956
|
+
state: await this.getStatePublicKey(),
|
|
3957
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
3958
|
+
this.program.programId
|
|
3959
|
+
),
|
|
3960
|
+
},
|
|
3961
|
+
}
|
|
3962
|
+
);
|
|
3963
|
+
}
|
|
3850
3964
|
}
|
package/src/config.ts
CHANGED
|
@@ -114,16 +114,25 @@ export const initialize = (props: {
|
|
|
114
114
|
return currentConfig;
|
|
115
115
|
};
|
|
116
116
|
|
|
117
|
-
export function getMarketsAndOraclesForSubscription(
|
|
117
|
+
export function getMarketsAndOraclesForSubscription(
|
|
118
|
+
env: DriftEnv,
|
|
119
|
+
perpMarkets?: PerpMarketConfig[],
|
|
120
|
+
spotMarkets?: SpotMarketConfig[]
|
|
121
|
+
): {
|
|
118
122
|
perpMarketIndexes: number[];
|
|
119
123
|
spotMarketIndexes: number[];
|
|
120
124
|
oracleInfos: OracleInfo[];
|
|
121
125
|
} {
|
|
126
|
+
const perpMarketsToUse =
|
|
127
|
+
perpMarkets?.length > 0 ? perpMarkets : PerpMarkets[env];
|
|
128
|
+
const spotMarketsToUse =
|
|
129
|
+
spotMarkets?.length > 0 ? spotMarkets : SpotMarkets[env];
|
|
130
|
+
|
|
122
131
|
const perpMarketIndexes = [];
|
|
123
132
|
const spotMarketIndexes = [];
|
|
124
133
|
const oracleInfos = new Map<string, OracleInfo>();
|
|
125
134
|
|
|
126
|
-
for (const market of
|
|
135
|
+
for (const market of perpMarketsToUse) {
|
|
127
136
|
perpMarketIndexes.push(market.marketIndex);
|
|
128
137
|
oracleInfos.set(market.oracle.toString(), {
|
|
129
138
|
publicKey: market.oracle,
|
|
@@ -131,7 +140,7 @@ export function getMarketsAndOraclesForSubscription(env: DriftEnv): {
|
|
|
131
140
|
});
|
|
132
141
|
}
|
|
133
142
|
|
|
134
|
-
for (const spotMarket of
|
|
143
|
+
for (const spotMarket of spotMarketsToUse) {
|
|
135
144
|
spotMarketIndexes.push(spotMarket.marketIndex);
|
|
136
145
|
oracleInfos.set(spotMarket.oracle.toString(), {
|
|
137
146
|
publicKey: spotMarket.oracle,
|
|
@@ -896,6 +896,26 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
896
896
|
launchTs: 1727965864000,
|
|
897
897
|
oracleSource: OracleSource.Prelaunch,
|
|
898
898
|
},
|
|
899
|
+
{
|
|
900
|
+
fullName: 'DeBridge',
|
|
901
|
+
category: ['Bridge'],
|
|
902
|
+
symbol: 'DBR-PERP',
|
|
903
|
+
baseAssetSymbol: 'DBR',
|
|
904
|
+
marketIndex: 47,
|
|
905
|
+
oracle: new PublicKey('AQzxePg2vY52Cw4di1j5xF7BqetNPxogqYPgDBL7HXWn'),
|
|
906
|
+
launchTs: 1728574493000,
|
|
907
|
+
oracleSource: OracleSource.Prelaunch,
|
|
908
|
+
},
|
|
909
|
+
{
|
|
910
|
+
fullName: 'WLF-5B-1W',
|
|
911
|
+
category: ['Prediction'],
|
|
912
|
+
symbol: 'WLF-5B-1W-BET',
|
|
913
|
+
baseAssetSymbol: 'WLF-5B-1W',
|
|
914
|
+
marketIndex: 48,
|
|
915
|
+
oracle: new PublicKey('7LpRfPaWR7cQqN7CMkCmZjEQpWyqso5LGuKCvDXH5ZAr'),
|
|
916
|
+
launchTs: 1728574493000,
|
|
917
|
+
oracleSource: OracleSource.Prelaunch,
|
|
918
|
+
},
|
|
899
919
|
];
|
|
900
920
|
|
|
901
921
|
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
|
package/src/driftClient.ts
CHANGED
|
@@ -82,6 +82,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
82
82
|
import {
|
|
83
83
|
getDriftSignerPublicKey,
|
|
84
84
|
getDriftStateAccountPublicKey,
|
|
85
|
+
getHighLeverageModeConfigPublicKey,
|
|
85
86
|
getInsuranceFundStakeAccountPublicKey,
|
|
86
87
|
getOpenbookV2FulfillmentConfigPublicKey,
|
|
87
88
|
getPerpMarketPublicKey,
|
|
@@ -3333,9 +3334,23 @@ export class DriftClient {
|
|
|
3333
3334
|
}
|
|
3334
3335
|
|
|
3335
3336
|
public async settleExpiredMarketPoolsToRevenuePool(
|
|
3336
|
-
|
|
3337
|
+
marketIndex: number,
|
|
3337
3338
|
txParams?: TxParams
|
|
3338
3339
|
): Promise<TransactionSignature> {
|
|
3340
|
+
const { txSig } = await this.sendTransaction(
|
|
3341
|
+
await this.buildTransaction(
|
|
3342
|
+
await this.getSettleExpiredMarketPoolsToRevenuePoolIx(marketIndex),
|
|
3343
|
+
txParams
|
|
3344
|
+
),
|
|
3345
|
+
[],
|
|
3346
|
+
this.opts
|
|
3347
|
+
);
|
|
3348
|
+
return txSig;
|
|
3349
|
+
}
|
|
3350
|
+
|
|
3351
|
+
public async getSettleExpiredMarketPoolsToRevenuePoolIx(
|
|
3352
|
+
perpMarketIndex: number
|
|
3353
|
+
): Promise<TransactionInstruction> {
|
|
3339
3354
|
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
3340
3355
|
this.program.programId,
|
|
3341
3356
|
perpMarketIndex
|
|
@@ -3346,8 +3361,8 @@ export class DriftClient {
|
|
|
3346
3361
|
QUOTE_SPOT_MARKET_INDEX
|
|
3347
3362
|
);
|
|
3348
3363
|
|
|
3349
|
-
|
|
3350
|
-
|
|
3364
|
+
return await this.program.instruction.settleExpiredMarketPoolsToRevenuePool(
|
|
3365
|
+
{
|
|
3351
3366
|
accounts: {
|
|
3352
3367
|
state: await this.getStatePublicKey(),
|
|
3353
3368
|
admin: this.isSubscribed
|
|
@@ -3356,15 +3371,8 @@ export class DriftClient {
|
|
|
3356
3371
|
spotMarket: spotMarketPublicKey,
|
|
3357
3372
|
perpMarket: perpMarketPublicKey,
|
|
3358
3373
|
},
|
|
3359
|
-
}
|
|
3360
|
-
|
|
3361
|
-
const { txSig } = await this.sendTransaction(
|
|
3362
|
-
await this.buildTransaction(ix, txParams),
|
|
3363
|
-
[],
|
|
3364
|
-
this.opts
|
|
3374
|
+
}
|
|
3365
3375
|
);
|
|
3366
|
-
|
|
3367
|
-
return txSig;
|
|
3368
3376
|
}
|
|
3369
3377
|
|
|
3370
3378
|
public async cancelOrder(
|
|
@@ -4929,6 +4937,52 @@ export class DriftClient {
|
|
|
4929
4937
|
});
|
|
4930
4938
|
}
|
|
4931
4939
|
|
|
4940
|
+
public async updateUserFuelBonus(
|
|
4941
|
+
userAccountPublicKey: PublicKey,
|
|
4942
|
+
user: UserAccount,
|
|
4943
|
+
userAuthority: PublicKey,
|
|
4944
|
+
txParams?: TxParams
|
|
4945
|
+
): Promise<TransactionSignature> {
|
|
4946
|
+
const { txSig } = await this.sendTransaction(
|
|
4947
|
+
await this.buildTransaction(
|
|
4948
|
+
await this.getUpdateUserFuelBonusIx(
|
|
4949
|
+
userAccountPublicKey,
|
|
4950
|
+
user,
|
|
4951
|
+
userAuthority
|
|
4952
|
+
),
|
|
4953
|
+
txParams
|
|
4954
|
+
),
|
|
4955
|
+
[],
|
|
4956
|
+
this.opts
|
|
4957
|
+
);
|
|
4958
|
+
return txSig;
|
|
4959
|
+
}
|
|
4960
|
+
|
|
4961
|
+
public async getUpdateUserFuelBonusIx(
|
|
4962
|
+
userAccountPublicKey: PublicKey,
|
|
4963
|
+
userAccount: UserAccount,
|
|
4964
|
+
userAuthority: PublicKey
|
|
4965
|
+
): Promise<TransactionInstruction> {
|
|
4966
|
+
const userStatsAccountPublicKey = getUserStatsAccountPublicKey(
|
|
4967
|
+
this.program.programId,
|
|
4968
|
+
userAuthority
|
|
4969
|
+
);
|
|
4970
|
+
|
|
4971
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
4972
|
+
userAccounts: [userAccount],
|
|
4973
|
+
});
|
|
4974
|
+
|
|
4975
|
+
return await this.program.instruction.updateUserFuelBonus({
|
|
4976
|
+
accounts: {
|
|
4977
|
+
state: await this.getStatePublicKey(),
|
|
4978
|
+
user: userAccountPublicKey,
|
|
4979
|
+
userStats: userStatsAccountPublicKey,
|
|
4980
|
+
authority: this.wallet.publicKey,
|
|
4981
|
+
},
|
|
4982
|
+
remainingAccounts,
|
|
4983
|
+
});
|
|
4984
|
+
}
|
|
4985
|
+
|
|
4932
4986
|
public async updateUserOpenOrdersCount(
|
|
4933
4987
|
userAccountPublicKey: PublicKey,
|
|
4934
4988
|
user: UserAccount,
|
|
@@ -8049,6 +8103,73 @@ export class DriftClient {
|
|
|
8049
8103
|
return [postIxs, encodedVaaKeypair];
|
|
8050
8104
|
}
|
|
8051
8105
|
|
|
8106
|
+
public async enableUserHighLeverageMode(
|
|
8107
|
+
subAccountId: number,
|
|
8108
|
+
txParams?: TxParams
|
|
8109
|
+
): Promise<TransactionSignature> {
|
|
8110
|
+
const { txSig } = await this.sendTransaction(
|
|
8111
|
+
await this.buildTransaction(
|
|
8112
|
+
await this.getEnableHighLeverageModeIx(subAccountId),
|
|
8113
|
+
txParams
|
|
8114
|
+
),
|
|
8115
|
+
[],
|
|
8116
|
+
this.opts
|
|
8117
|
+
);
|
|
8118
|
+
return txSig;
|
|
8119
|
+
}
|
|
8120
|
+
|
|
8121
|
+
public async getEnableHighLeverageModeIx(subAccountId: number) {
|
|
8122
|
+
const ix = await this.program.instruction.enableUserHighLeverageMode(
|
|
8123
|
+
subAccountId,
|
|
8124
|
+
{
|
|
8125
|
+
accounts: {
|
|
8126
|
+
state: await this.getStatePublicKey(),
|
|
8127
|
+
user: getUserAccountPublicKeySync(
|
|
8128
|
+
this.program.programId,
|
|
8129
|
+
this.wallet.publicKey,
|
|
8130
|
+
subAccountId
|
|
8131
|
+
),
|
|
8132
|
+
authority: this.wallet.publicKey,
|
|
8133
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
8134
|
+
this.program.programId
|
|
8135
|
+
),
|
|
8136
|
+
},
|
|
8137
|
+
}
|
|
8138
|
+
);
|
|
8139
|
+
|
|
8140
|
+
return ix;
|
|
8141
|
+
}
|
|
8142
|
+
|
|
8143
|
+
public async disableUserHighLeverageMode(
|
|
8144
|
+
user: PublicKey,
|
|
8145
|
+
txParams?: TxParams
|
|
8146
|
+
): Promise<TransactionSignature> {
|
|
8147
|
+
const { txSig } = await this.sendTransaction(
|
|
8148
|
+
await this.buildTransaction(
|
|
8149
|
+
await this.getDisableHighLeverageModeIx(user),
|
|
8150
|
+
txParams
|
|
8151
|
+
),
|
|
8152
|
+
[],
|
|
8153
|
+
this.opts
|
|
8154
|
+
);
|
|
8155
|
+
return txSig;
|
|
8156
|
+
}
|
|
8157
|
+
|
|
8158
|
+
public async getDisableHighLeverageModeIx(user: PublicKey) {
|
|
8159
|
+
const ix = await this.program.instruction.disableUserHighLeverageMode({
|
|
8160
|
+
accounts: {
|
|
8161
|
+
state: await this.getStatePublicKey(),
|
|
8162
|
+
user,
|
|
8163
|
+
authority: this.wallet.publicKey,
|
|
8164
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
8165
|
+
this.program.programId
|
|
8166
|
+
),
|
|
8167
|
+
},
|
|
8168
|
+
});
|
|
8169
|
+
|
|
8170
|
+
return ix;
|
|
8171
|
+
}
|
|
8172
|
+
|
|
8052
8173
|
private handleSignedTransaction(signedTxs: SignedTxData[]) {
|
|
8053
8174
|
if (this.enableMetricsEvents && this.metricsEventEmitter) {
|
|
8054
8175
|
this.metricsEventEmitter.emit('txSigned', signedTxs);
|