@drift-labs/sdk 2.97.0-beta.2 → 2.97.0-beta.21
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 +43 -2
- package/lib/constants/spotMarkets.js +2 -2
- package/lib/decode/user.js +10 -0
- package/lib/driftClient.d.ts +10 -1
- package/lib/driftClient.js +80 -1
- package/lib/idl/drift.json +267 -6
- package/lib/idl/switchboard_on_demand_30.json +195 -37
- package/lib/math/margin.d.ts +3 -3
- package/lib/math/margin.js +10 -10
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +15 -3
- package/lib/types.d.ts +21 -1
- package/lib/types.js +11 -1
- package/lib/user.d.ts +2 -1
- package/lib/user.js +14 -10
- 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 +44 -2
- package/src/constants/spotMarkets.ts +2 -2
- package/src/decode/user.ts +11 -1
- package/src/driftClient.ts +169 -1
- package/src/idl/drift.json +267 -6
- package/src/idl/switchboard_on_demand_30.json +195 -37
- package/src/math/margin.ts +20 -12
- package/src/math/market.ts +19 -3
- package/src/types.ts +21 -1
- package/src/user.ts +26 -10
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);
|
|
@@ -1368,11 +1371,12 @@ class User {
|
|
|
1368
1371
|
.neg();
|
|
1369
1372
|
return this.liquidationPrice(positionMarketIndex, closeBaseAmount, estimatedEntryPrice);
|
|
1370
1373
|
}
|
|
1371
|
-
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize) {
|
|
1372
|
-
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio);
|
|
1374
|
+
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice) {
|
|
1375
|
+
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio, undefined, estEntryPrice);
|
|
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,
|
|
@@ -728,9 +728,9 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
728
728
|
symbol: 'CLOUD-PERP',
|
|
729
729
|
baseAssetSymbol: 'CLOUD',
|
|
730
730
|
marketIndex: 31,
|
|
731
|
-
oracle: new PublicKey('
|
|
731
|
+
oracle: new PublicKey('FNFejcXENaPgKaCTfstew9vSSvdQPnXjGTkJjUnnYvHU'),
|
|
732
732
|
launchTs: 1717597648000,
|
|
733
|
-
oracleSource: OracleSource.
|
|
733
|
+
oracleSource: OracleSource.SWITCHBOARD_ON_DEMAND,
|
|
734
734
|
},
|
|
735
735
|
{
|
|
736
736
|
fullName: 'IO',
|
|
@@ -896,6 +896,48 @@ 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('53j4mz7cQV7mAZekKbV3n2L4bY7jY6eXdgaTkWDLYxq4'),
|
|
906
|
+
launchTs: 1728574493000,
|
|
907
|
+
oracleSource: OracleSource.PYTH_PULL,
|
|
908
|
+
pythFeedId:
|
|
909
|
+
'0xf788488fe2df341b10a498e0a789f03209c0938d9ed04bc521f8224748d6d236',
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
fullName: 'WLF-5B-1W',
|
|
913
|
+
category: ['Prediction'],
|
|
914
|
+
symbol: 'WLF-5B-1W-BET',
|
|
915
|
+
baseAssetSymbol: 'WLF-5B-1W',
|
|
916
|
+
marketIndex: 48,
|
|
917
|
+
oracle: new PublicKey('7LpRfPaWR7cQqN7CMkCmZjEQpWyqso5LGuKCvDXH5ZAr'),
|
|
918
|
+
launchTs: 1728574493000,
|
|
919
|
+
oracleSource: OracleSource.Prelaunch,
|
|
920
|
+
},
|
|
921
|
+
{
|
|
922
|
+
fullName: 'VRSTPN-WIN-F1-24-DRVRS-CHMP',
|
|
923
|
+
category: ['Prediction', 'Sport'],
|
|
924
|
+
symbol: 'VRSTPN-WIN-F1-24-DRVRS-CHMP-BET',
|
|
925
|
+
baseAssetSymbol: 'VRSTPN-WIN-F1-24-DRVRS-CHMP',
|
|
926
|
+
marketIndex: 49,
|
|
927
|
+
oracle: new PublicKey('E36rvXEwysWeiToXCpWfHVADd8bzzyR4w83ZSSwxAxqG'),
|
|
928
|
+
launchTs: 1729209600000,
|
|
929
|
+
oracleSource: OracleSource.Prelaunch,
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
fullName: 'LNDO-WIN-F1-24-US-GP',
|
|
933
|
+
category: ['Prediction', 'Sport'],
|
|
934
|
+
symbol: 'LNDO-WIN-F1-24-US-GP-BET',
|
|
935
|
+
baseAssetSymbol: 'LNDO-WIN-F1-24-US-GP',
|
|
936
|
+
marketIndex: 50,
|
|
937
|
+
oracle: new PublicKey('6AVy1y9SnJECnosQaiK2uY1kcT4ZEBf1F4DMvhxgvhUo'),
|
|
938
|
+
launchTs: 1729209600000,
|
|
939
|
+
oracleSource: OracleSource.Prelaunch,
|
|
940
|
+
},
|
|
899
941
|
];
|
|
900
942
|
|
|
901
943
|
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
|
|
@@ -374,8 +374,8 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
|
|
|
374
374
|
{
|
|
375
375
|
symbol: 'CLOUD',
|
|
376
376
|
marketIndex: 21,
|
|
377
|
-
oracle: new PublicKey('
|
|
378
|
-
oracleSource: OracleSource.
|
|
377
|
+
oracle: new PublicKey('FNFejcXENaPgKaCTfstew9vSSvdQPnXjGTkJjUnnYvHU'),
|
|
378
|
+
oracleSource: OracleSource.SWITCHBOARD_ON_DEMAND,
|
|
379
379
|
mint: new PublicKey('CLoUDKc4Ane7HeQcPpE3YHnznRxhMimJ4MyaUqyHFzAu'),
|
|
380
380
|
precision: new BN(10).pow(NINE),
|
|
381
381
|
precisionExp: NINE,
|
package/src/decode/user.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
UserAccount,
|
|
12
12
|
} from '../types';
|
|
13
13
|
import { PublicKey } from '@solana/web3.js';
|
|
14
|
-
import { BN } from '../';
|
|
14
|
+
import { BN, MarginMode } from '../';
|
|
15
15
|
import { ZERO } from '../';
|
|
16
16
|
|
|
17
17
|
function readUnsignedBigInt64LE(buffer: Buffer, offset: number): BN {
|
|
@@ -326,6 +326,15 @@ export function decodeUser(buffer: Buffer): UserAccount {
|
|
|
326
326
|
const hasOpenAuction = buffer.readUInt8(offset) === 1;
|
|
327
327
|
offset += 1;
|
|
328
328
|
|
|
329
|
+
let marginMode: MarginMode;
|
|
330
|
+
const marginModeNum = buffer.readUInt8(offset);
|
|
331
|
+
if (marginModeNum === 0) {
|
|
332
|
+
marginMode = MarginMode.DEFAULT;
|
|
333
|
+
} else {
|
|
334
|
+
marginMode = MarginMode.HIGH_LEVERAGE;
|
|
335
|
+
}
|
|
336
|
+
offset += 1;
|
|
337
|
+
|
|
329
338
|
// @ts-ignore
|
|
330
339
|
return {
|
|
331
340
|
authority,
|
|
@@ -354,5 +363,6 @@ export function decodeUser(buffer: Buffer): UserAccount {
|
|
|
354
363
|
hasOpenOrder,
|
|
355
364
|
openAuctions,
|
|
356
365
|
hasOpenAuction,
|
|
366
|
+
marginMode,
|
|
357
367
|
};
|
|
358
368
|
}
|
package/src/driftClient.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from '@solana/spl-token';
|
|
20
20
|
import {
|
|
21
21
|
DriftClientMetricsEvents,
|
|
22
|
+
HighLeverageModeConfig,
|
|
22
23
|
isVariant,
|
|
23
24
|
IWallet,
|
|
24
25
|
MakerInfo,
|
|
@@ -82,6 +83,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
82
83
|
import {
|
|
83
84
|
getDriftSignerPublicKey,
|
|
84
85
|
getDriftStateAccountPublicKey,
|
|
86
|
+
getHighLeverageModeConfigPublicKey,
|
|
85
87
|
getInsuranceFundStakeAccountPublicKey,
|
|
86
88
|
getOpenbookV2FulfillmentConfigPublicKey,
|
|
87
89
|
getPerpMarketPublicKey,
|
|
@@ -4936,6 +4938,79 @@ export class DriftClient {
|
|
|
4936
4938
|
});
|
|
4937
4939
|
}
|
|
4938
4940
|
|
|
4941
|
+
public async updateUserFuelBonus(
|
|
4942
|
+
userAccountPublicKey: PublicKey,
|
|
4943
|
+
user: UserAccount,
|
|
4944
|
+
userAuthority: PublicKey,
|
|
4945
|
+
txParams?: TxParams
|
|
4946
|
+
): Promise<TransactionSignature> {
|
|
4947
|
+
const { txSig } = await this.sendTransaction(
|
|
4948
|
+
await this.buildTransaction(
|
|
4949
|
+
await this.getUpdateUserFuelBonusIx(
|
|
4950
|
+
userAccountPublicKey,
|
|
4951
|
+
user,
|
|
4952
|
+
userAuthority
|
|
4953
|
+
),
|
|
4954
|
+
txParams
|
|
4955
|
+
),
|
|
4956
|
+
[],
|
|
4957
|
+
this.opts
|
|
4958
|
+
);
|
|
4959
|
+
return txSig;
|
|
4960
|
+
}
|
|
4961
|
+
|
|
4962
|
+
public async getUpdateUserFuelBonusIx(
|
|
4963
|
+
userAccountPublicKey: PublicKey,
|
|
4964
|
+
userAccount: UserAccount,
|
|
4965
|
+
userAuthority: PublicKey
|
|
4966
|
+
): Promise<TransactionInstruction> {
|
|
4967
|
+
const userStatsAccountPublicKey = getUserStatsAccountPublicKey(
|
|
4968
|
+
this.program.programId,
|
|
4969
|
+
userAuthority
|
|
4970
|
+
);
|
|
4971
|
+
|
|
4972
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
4973
|
+
userAccounts: [userAccount],
|
|
4974
|
+
});
|
|
4975
|
+
|
|
4976
|
+
return await this.program.instruction.updateUserFuelBonus({
|
|
4977
|
+
accounts: {
|
|
4978
|
+
state: await this.getStatePublicKey(),
|
|
4979
|
+
user: userAccountPublicKey,
|
|
4980
|
+
userStats: userStatsAccountPublicKey,
|
|
4981
|
+
authority: this.wallet.publicKey,
|
|
4982
|
+
},
|
|
4983
|
+
remainingAccounts,
|
|
4984
|
+
});
|
|
4985
|
+
}
|
|
4986
|
+
|
|
4987
|
+
public async updateUserStatsReferrerInfo(
|
|
4988
|
+
userStatsAccountPublicKey: PublicKey,
|
|
4989
|
+
txParams?: TxParams
|
|
4990
|
+
): Promise<TransactionSignature> {
|
|
4991
|
+
const { txSig } = await this.sendTransaction(
|
|
4992
|
+
await this.buildTransaction(
|
|
4993
|
+
await this.getUpdateUserStatsReferrerInfoIx(userStatsAccountPublicKey),
|
|
4994
|
+
txParams
|
|
4995
|
+
),
|
|
4996
|
+
[],
|
|
4997
|
+
this.opts
|
|
4998
|
+
);
|
|
4999
|
+
return txSig;
|
|
5000
|
+
}
|
|
5001
|
+
|
|
5002
|
+
public async getUpdateUserStatsReferrerInfoIx(
|
|
5003
|
+
userStatsAccountPublicKey: PublicKey
|
|
5004
|
+
): Promise<TransactionInstruction> {
|
|
5005
|
+
return await this.program.instruction.updateUserStatsReferrerInfo({
|
|
5006
|
+
accounts: {
|
|
5007
|
+
state: await this.getStatePublicKey(),
|
|
5008
|
+
userStats: userStatsAccountPublicKey,
|
|
5009
|
+
authority: this.wallet.publicKey,
|
|
5010
|
+
},
|
|
5011
|
+
});
|
|
5012
|
+
}
|
|
5013
|
+
|
|
4939
5014
|
public async updateUserOpenOrdersCount(
|
|
4940
5015
|
userAccountPublicKey: PublicKey,
|
|
4941
5016
|
user: UserAccount,
|
|
@@ -6499,7 +6574,10 @@ export class DriftClient {
|
|
|
6499
6574
|
const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
6500
6575
|
|
|
6501
6576
|
const remainingAccounts = this.getRemainingAccounts({
|
|
6502
|
-
userAccounts: [
|
|
6577
|
+
userAccounts: [
|
|
6578
|
+
userAccount,
|
|
6579
|
+
...makerInfos.map((makerInfo) => makerInfo.makerUserAccount),
|
|
6580
|
+
],
|
|
6503
6581
|
useMarketLastSlotCache: true,
|
|
6504
6582
|
writablePerpMarketIndexes: [marketIndex],
|
|
6505
6583
|
});
|
|
@@ -8056,6 +8134,96 @@ export class DriftClient {
|
|
|
8056
8134
|
return [postIxs, encodedVaaKeypair];
|
|
8057
8135
|
}
|
|
8058
8136
|
|
|
8137
|
+
public async enableUserHighLeverageMode(
|
|
8138
|
+
subAccountId: number,
|
|
8139
|
+
txParams?: TxParams
|
|
8140
|
+
): Promise<TransactionSignature> {
|
|
8141
|
+
const { txSig } = await this.sendTransaction(
|
|
8142
|
+
await this.buildTransaction(
|
|
8143
|
+
await this.getEnableHighLeverageModeIx(subAccountId),
|
|
8144
|
+
txParams
|
|
8145
|
+
),
|
|
8146
|
+
[],
|
|
8147
|
+
this.opts
|
|
8148
|
+
);
|
|
8149
|
+
return txSig;
|
|
8150
|
+
}
|
|
8151
|
+
|
|
8152
|
+
public async getEnableHighLeverageModeIx(subAccountId: number) {
|
|
8153
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
8154
|
+
userAccounts: [this.getUserAccount(subAccountId)],
|
|
8155
|
+
});
|
|
8156
|
+
|
|
8157
|
+
const ix = await this.program.instruction.enableUserHighLeverageMode(
|
|
8158
|
+
subAccountId,
|
|
8159
|
+
{
|
|
8160
|
+
accounts: {
|
|
8161
|
+
state: await this.getStatePublicKey(),
|
|
8162
|
+
user: getUserAccountPublicKeySync(
|
|
8163
|
+
this.program.programId,
|
|
8164
|
+
this.wallet.publicKey,
|
|
8165
|
+
subAccountId
|
|
8166
|
+
),
|
|
8167
|
+
authority: this.wallet.publicKey,
|
|
8168
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
8169
|
+
this.program.programId
|
|
8170
|
+
),
|
|
8171
|
+
},
|
|
8172
|
+
remainingAccounts,
|
|
8173
|
+
}
|
|
8174
|
+
);
|
|
8175
|
+
|
|
8176
|
+
return ix;
|
|
8177
|
+
}
|
|
8178
|
+
|
|
8179
|
+
public async disableUserHighLeverageMode(
|
|
8180
|
+
user: PublicKey,
|
|
8181
|
+
userAccount?: UserAccount,
|
|
8182
|
+
txParams?: TxParams
|
|
8183
|
+
): Promise<TransactionSignature> {
|
|
8184
|
+
const { txSig } = await this.sendTransaction(
|
|
8185
|
+
await this.buildTransaction(
|
|
8186
|
+
await this.getDisableHighLeverageModeIx(user, userAccount),
|
|
8187
|
+
txParams
|
|
8188
|
+
),
|
|
8189
|
+
[],
|
|
8190
|
+
this.opts
|
|
8191
|
+
);
|
|
8192
|
+
return txSig;
|
|
8193
|
+
}
|
|
8194
|
+
|
|
8195
|
+
public async getDisableHighLeverageModeIx(
|
|
8196
|
+
user: PublicKey,
|
|
8197
|
+
userAccount?: UserAccount
|
|
8198
|
+
) {
|
|
8199
|
+
const remainingAccounts = userAccount
|
|
8200
|
+
? this.getRemainingAccounts({
|
|
8201
|
+
userAccounts: [userAccount],
|
|
8202
|
+
})
|
|
8203
|
+
: undefined;
|
|
8204
|
+
|
|
8205
|
+
const ix = await this.program.instruction.disableUserHighLeverageMode({
|
|
8206
|
+
accounts: {
|
|
8207
|
+
state: await this.getStatePublicKey(),
|
|
8208
|
+
user,
|
|
8209
|
+
authority: this.wallet.publicKey,
|
|
8210
|
+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
|
|
8211
|
+
this.program.programId
|
|
8212
|
+
),
|
|
8213
|
+
},
|
|
8214
|
+
remainingAccounts,
|
|
8215
|
+
});
|
|
8216
|
+
|
|
8217
|
+
return ix;
|
|
8218
|
+
}
|
|
8219
|
+
|
|
8220
|
+
public async fetchHighLeverageModeConfig(): Promise<HighLeverageModeConfig> {
|
|
8221
|
+
const config = await this.program.account.highLeverageModeConfig.fetch(
|
|
8222
|
+
getHighLeverageModeConfigPublicKey(this.program.programId)
|
|
8223
|
+
);
|
|
8224
|
+
return config as HighLeverageModeConfig;
|
|
8225
|
+
}
|
|
8226
|
+
|
|
8059
8227
|
private handleSignedTransaction(signedTxs: SignedTxData[]) {
|
|
8060
8228
|
if (this.enableMetricsEvents && this.metricsEventEmitter) {
|
|
8061
8229
|
this.metricsEventEmitter.emit('txSigned', signedTxs);
|