@drift-labs/sdk
Version:
SDK for Drift Protocol
865 lines • 125 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminClient = void 0;
const web3_js_1 = require("@solana/web3.js");
const types_1 = require("./types");
const userName_1 = require("./userName");
const anchor_1 = require("@coral-xyz/anchor");
const anchor = __importStar(require("@coral-xyz/anchor"));
const pda_1 = require("./addresses/pda");
const utils_1 = require("./math/utils");
const spl_token_1 = require("@solana/spl-token");
const driftClient_1 = require("./driftClient");
const numericConstants_1 = require("./constants/numericConstants");
const trade_1 = require("./math/trade");
const amm_1 = require("./math/amm");
const phoenix_sdk_1 = require("@ellipsis-labs/phoenix-sdk");
const config_1 = require("./config");
const pythOracleUtils_1 = require("./util/pythOracleUtils");
const txConstants_1 = require("./constants/txConstants");
const OPENBOOK_PROGRAM_ID = new web3_js_1.PublicKey('opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb');
class AdminClient extends driftClient_1.DriftClient {
async initialize(usdcMint, _adminControlsPrices) {
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(await this.getStatePublicKey());
if (stateAccountRPCResponse.value !== null) {
throw new Error('Clearing house already initialized');
}
const [driftStatePublicKey] = await (0, pda_1.getDriftStateAccountPublicKeyAndNonce)(this.program.programId);
const initializeIx = await this.program.instruction.initialize({
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: driftStatePublicKey,
quoteAssetMint: usdcMint,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
driftSigner: this.getSignerPublicKey(),
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
},
});
const tx = await this.buildTransaction(initializeIx);
const { txSig } = await super.sendTransaction(tx, [], this.opts);
return [txSig];
}
async initializeSpotMarket(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor = 0, liquidatorFee = 0, ifLiquidationFee = 0, activeStatus = true, assetTier = types_1.AssetTier.COLLATERAL, scaleInitialAssetWeightStart = numericConstants_1.ZERO, withdrawGuardThreshold = numericConstants_1.ZERO, orderTickSize = numericConstants_1.ONE, orderStepSize = numericConstants_1.ONE, ifTotalFactor = 0, name = userName_1.DEFAULT_MARKET_NAME, marketIndex) {
const spotMarketIndex = marketIndex !== null && marketIndex !== void 0 ? marketIndex : this.getStateAccount().numberOfSpotMarkets;
const initializeIx = await this.getInitializeSpotMarketIx(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, liquidatorFee, ifLiquidationFee, activeStatus, assetTier, scaleInitialAssetWeightStart, withdrawGuardThreshold, orderTickSize, orderStepSize, ifTotalFactor, name, marketIndex);
const tx = await this.buildTransaction(initializeIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
await this.accountSubscriber.addSpotMarket(spotMarketIndex);
await this.accountSubscriber.addOracle({
source: oracleSource,
publicKey: oracle,
});
await this.accountSubscriber.setSpotOracleMap();
return txSig;
}
async getInitializeSpotMarketIx(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor = 0, liquidatorFee = 0, ifLiquidationFee = 0, activeStatus = true, assetTier = types_1.AssetTier.COLLATERAL, scaleInitialAssetWeightStart = numericConstants_1.ZERO, withdrawGuardThreshold = numericConstants_1.ZERO, orderTickSize = numericConstants_1.ONE, orderStepSize = numericConstants_1.ONE, ifTotalFactor = 0, name = userName_1.DEFAULT_MARKET_NAME, marketIndex) {
const spotMarketIndex = marketIndex !== null && marketIndex !== void 0 ? marketIndex : this.getStateAccount().numberOfSpotMarkets;
const spotMarket = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, spotMarketIndex);
const spotMarketVault = await (0, pda_1.getSpotMarketVaultPublicKey)(this.program.programId, spotMarketIndex);
const insuranceFundVault = await (0, pda_1.getInsuranceFundVaultPublicKey)(this.program.programId, spotMarketIndex);
const tokenProgram = (await this.connection.getAccountInfo(mint)).owner;
const nameBuffer = (0, userName_1.encodeName)(name);
const initializeIx = await this.program.instruction.initializeSpotMarket(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, liquidatorFee, ifLiquidationFee, activeStatus, assetTier, scaleInitialAssetWeightStart, withdrawGuardThreshold, orderTickSize, orderStepSize, ifTotalFactor, nameBuffer, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
spotMarket,
spotMarketVault,
insuranceFundVault,
driftSigner: this.getSignerPublicKey(),
spotMarketMint: mint,
oracle,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram,
},
});
return initializeIx;
}
async deleteInitializedSpotMarket(marketIndex) {
const deleteInitializeMarketIx = await this.getDeleteInitializedSpotMarketIx(marketIndex);
const tx = await this.buildTransaction(deleteInitializeMarketIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getDeleteInitializedSpotMarketIx(marketIndex) {
const spotMarketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
const spotMarketVaultPublicKey = await (0, pda_1.getSpotMarketVaultPublicKey)(this.program.programId, marketIndex);
const insuranceFundVaultPublicKey = await (0, pda_1.getInsuranceFundVaultPublicKey)(this.program.programId, marketIndex);
return await this.program.instruction.deleteInitializedSpotMarket(marketIndex, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
spotMarket: spotMarketPublicKey,
spotMarketVault: spotMarketVaultPublicKey,
insuranceFundVault: insuranceFundVaultPublicKey,
driftSigner: this.getSignerPublicKey(),
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
},
});
}
async initializeSerumFulfillmentConfig(marketIndex, serumMarket, serumProgram) {
const initializeIx = await this.getInitializeSerumFulfillmentConfigIx(marketIndex, serumMarket, serumProgram);
const tx = await this.buildTransaction(initializeIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getInitializeSerumFulfillmentConfigIx(marketIndex, serumMarket, serumProgram) {
const serumOpenOrders = (0, pda_1.getSerumOpenOrdersPublicKey)(this.program.programId, serumMarket);
const serumFulfillmentConfig = (0, pda_1.getSerumFulfillmentConfigPublicKey)(this.program.programId, serumMarket);
return await this.program.instruction.initializeSerumFulfillmentConfig(marketIndex, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
driftSigner: this.getSignerPublicKey(),
serumProgram,
serumMarket,
serumOpenOrders,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
serumFulfillmentConfig,
},
});
}
async initializePhoenixFulfillmentConfig(marketIndex, phoenixMarket) {
const initializeIx = await this.getInitializePhoenixFulfillmentConfigIx(marketIndex, phoenixMarket);
const tx = await this.buildTransaction(initializeIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getInitializePhoenixFulfillmentConfigIx(marketIndex, phoenixMarket) {
const phoenixFulfillmentConfig = (0, pda_1.getPhoenixFulfillmentConfigPublicKey)(this.program.programId, phoenixMarket);
return await this.program.instruction.initializePhoenixFulfillmentConfig(marketIndex, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
driftSigner: this.getSignerPublicKey(),
phoenixMarket: phoenixMarket,
phoenixProgram: phoenix_sdk_1.PROGRAM_ID,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
phoenixFulfillmentConfig,
},
});
}
async initializeOpenbookV2FulfillmentConfig(marketIndex, openbookMarket) {
const initializeIx = await this.getInitializeOpenbookV2FulfillmentConfigIx(marketIndex, openbookMarket);
const tx = await this.buildTransaction(initializeIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getInitializeOpenbookV2FulfillmentConfigIx(marketIndex, openbookMarket) {
const openbookFulfillmentConfig = (0, pda_1.getOpenbookV2FulfillmentConfigPublicKey)(this.program.programId, openbookMarket);
return this.program.instruction.initializeOpenbookV2FulfillmentConfig(marketIndex, {
accounts: {
baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
state: await this.getStatePublicKey(),
openbookV2Program: OPENBOOK_PROGRAM_ID,
openbookV2Market: openbookMarket,
driftSigner: this.getSignerPublicKey(),
openbookV2FulfillmentConfig: openbookFulfillmentConfig,
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
},
});
}
async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME) {
const currentPerpMarketIndex = this.getStateAccount().numberOfMarkets;
const initializeMarketIx = await this.getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, name);
const tx = await this.buildTransaction(initializeMarketIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
while (this.getStateAccount().numberOfMarkets <= currentPerpMarketIndex) {
await this.fetchAccounts();
}
await this.accountSubscriber.addPerpMarket(marketIndex);
await this.accountSubscriber.addOracle({
source: oracleSource,
publicKey: priceOracle,
});
await this.accountSubscriber.setPerpOracleMap();
return txSig;
}
async getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME) {
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
const nameBuffer = (0, userName_1.encodeName)(name);
return await this.program.instruction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, nameBuffer, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
oracle: priceOracle,
perpMarket: perpMarketPublicKey,
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
},
});
}
async initializePredictionMarket(perpMarketIndex) {
const updatePerpMarketConcentrationCoefIx = await this.getInitializePredictionMarketIx(perpMarketIndex);
const tx = await this.buildTransaction(updatePerpMarketConcentrationCoefIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getInitializePredictionMarketIx(perpMarketIndex) {
return await this.program.instruction.initializePredictionMarket({
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async deleteInitializedPerpMarket(marketIndex) {
const deleteInitializeMarketIx = await this.getDeleteInitializedPerpMarketIx(marketIndex);
const tx = await this.buildTransaction(deleteInitializeMarketIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getDeleteInitializedPerpMarketIx(marketIndex) {
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
return await this.program.instruction.deleteInitializedPerpMarket(marketIndex, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: perpMarketPublicKey,
},
});
}
async moveAmmPrice(perpMarketIndex, baseAssetReserve, quoteAssetReserve, sqrtK) {
const moveAmmPriceIx = await this.getMoveAmmPriceIx(perpMarketIndex, baseAssetReserve, quoteAssetReserve, sqrtK);
const tx = await this.buildTransaction(moveAmmPriceIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getMoveAmmPriceIx(perpMarketIndex, baseAssetReserve, quoteAssetReserve, sqrtK) {
const marketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
if (sqrtK == undefined) {
sqrtK = (0, utils_1.squareRootBN)(baseAssetReserve.mul(quoteAssetReserve));
}
return await this.program.instruction.moveAmmPrice(baseAssetReserve, quoteAssetReserve, sqrtK, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: marketPublicKey,
},
});
}
async updateK(perpMarketIndex, sqrtK) {
const updateKIx = await this.getUpdateKIx(perpMarketIndex, sqrtK);
const tx = await this.buildTransaction(updateKIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateKIx(perpMarketIndex, sqrtK) {
return await this.program.instruction.updateK(sqrtK, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
oracle: this.getPerpMarketAccount(perpMarketIndex).amm.oracle,
},
});
}
async recenterPerpMarketAmm(perpMarketIndex, pegMultiplier, sqrtK) {
const recenterPerpMarketAmmIx = await this.getRecenterPerpMarketAmmIx(perpMarketIndex, pegMultiplier, sqrtK);
const tx = await this.buildTransaction(recenterPerpMarketAmmIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getRecenterPerpMarketAmmIx(perpMarketIndex, pegMultiplier, sqrtK) {
const marketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.recenterPerpMarketAmm(pegMultiplier, sqrtK, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: marketPublicKey,
},
});
}
async recenterPerpMarketAmmCrank(perpMarketIndex, depth) {
const recenterPerpMarketAmmIx = await this.getRecenterPerpMarketAmmCrankIx(perpMarketIndex, depth);
const tx = await this.buildTransaction(recenterPerpMarketAmmIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getRecenterPerpMarketAmmCrankIx(perpMarketIndex, depth) {
return await this.program.instruction.recenterPerpMarketAmmCrank(depth !== null && depth !== void 0 ? depth : null, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
spotMarket: await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, numericConstants_1.QUOTE_SPOT_MARKET_INDEX),
oracle: this.getPerpMarketAccount(perpMarketIndex).amm.oracle,
},
});
}
async updatePerpMarketConcentrationScale(perpMarketIndex, concentrationScale) {
const updatePerpMarketConcentrationCoefIx = await this.getUpdatePerpMarketConcentrationScaleIx(perpMarketIndex, concentrationScale);
const tx = await this.buildTransaction(updatePerpMarketConcentrationCoefIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketConcentrationScaleIx(perpMarketIndex, concentrationScale) {
return await this.program.instruction.updatePerpMarketConcentrationCoef(concentrationScale, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async moveAmmToPrice(perpMarketIndex, targetPrice) {
const moveAmmPriceIx = await this.getMoveAmmToPriceIx(perpMarketIndex, targetPrice);
const tx = await this.buildTransaction(moveAmmPriceIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getMoveAmmToPriceIx(perpMarketIndex, targetPrice) {
const perpMarket = this.getPerpMarketAccount(perpMarketIndex);
const [direction, tradeSize, _] = (0, trade_1.calculateTargetPriceTrade)(perpMarket, targetPrice, new anchor_1.BN(1000), 'quote', undefined //todo
);
const [newQuoteAssetAmount, newBaseAssetAmount] = (0, amm_1.calculateAmmReservesAfterSwap)(perpMarket.amm, 'quote', tradeSize, (0, amm_1.getSwapDirection)('quote', direction));
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.moveAmmPrice(newBaseAssetAmount, newQuoteAssetAmount, perpMarket.amm.sqrtK, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
perpMarket: perpMarketPublicKey,
},
});
}
async repegAmmCurve(newPeg, perpMarketIndex) {
const repegAmmCurveIx = await this.getRepegAmmCurveIx(newPeg, perpMarketIndex);
const tx = await this.buildTransaction(repegAmmCurveIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getRepegAmmCurveIx(newPeg, perpMarketIndex) {
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
return await this.program.instruction.repegAmmCurve(newPeg, {
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
oracle: ammData.oracle,
perpMarket: perpMarketPublicKey,
},
});
}
async updatePerpMarketAmmOracleTwap(perpMarketIndex) {
const updatePerpMarketAmmOracleTwapIx = await this.getUpdatePerpMarketAmmOracleTwapIx(perpMarketIndex);
const tx = await this.buildTransaction(updatePerpMarketAmmOracleTwapIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketAmmOracleTwapIx(perpMarketIndex) {
const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.updatePerpMarketAmmOracleTwap({
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
oracle: ammData.oracle,
perpMarket: perpMarketPublicKey,
},
});
}
async resetPerpMarketAmmOracleTwap(perpMarketIndex) {
const resetPerpMarketAmmOracleTwapIx = await this.getResetPerpMarketAmmOracleTwapIx(perpMarketIndex);
const tx = await this.buildTransaction(resetPerpMarketAmmOracleTwapIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getResetPerpMarketAmmOracleTwapIx(perpMarketIndex) {
const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.resetPerpMarketAmmOracleTwap({
accounts: {
state: await this.getStatePublicKey(),
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
oracle: ammData.oracle,
perpMarket: perpMarketPublicKey,
},
});
}
async depositIntoPerpMarketFeePool(perpMarketIndex, amount, sourceVault) {
const depositIntoPerpMarketFeePoolIx = await this.getDepositIntoPerpMarketFeePoolIx(perpMarketIndex, amount, sourceVault);
const tx = await this.buildTransaction(depositIntoPerpMarketFeePoolIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getDepositIntoPerpMarketFeePoolIx(perpMarketIndex, amount, sourceVault) {
const spotMarket = this.getQuoteSpotMarketAccount();
return await this.program.instruction.depositIntoPerpMarketFeePool(amount, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
sourceVault,
driftSigner: this.getSignerPublicKey(),
quoteSpotMarket: spotMarket.pubkey,
spotMarketVault: spotMarket.vault,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
},
});
}
async updatePerpMarketPnlPool(perpMarketIndex, amount) {
const updatePerpMarketPnlPoolIx = await this.getUpdatePerpMarketPnlPoolIx(perpMarketIndex, amount);
const tx = await this.buildTransaction(updatePerpMarketPnlPoolIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketPnlPoolIx(perpMarketIndex, amount) {
return await this.program.instruction.updatePerpMarketPnlPool(amount, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
spotMarket: this.getQuoteSpotMarketAccount().pubkey,
spotMarketVault: this.getQuoteSpotMarketAccount().vault,
},
});
}
async depositIntoSpotMarketVault(spotMarketIndex, amount, sourceVault) {
const depositIntoPerpMarketFeePoolIx = await this.getDepositIntoSpotMarketVaultIx(spotMarketIndex, amount, sourceVault);
const tx = await this.buildTransaction(depositIntoPerpMarketFeePoolIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getDepositIntoSpotMarketVaultIx(spotMarketIndex, amount, sourceVault) {
const spotMarket = this.getSpotMarketAccount(spotMarketIndex);
const remainingAccounts = [];
this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
if (this.isTransferHook(spotMarket)) {
await this.addExtraAccountMetasToRemainingAccounts(spotMarket.mint, remainingAccounts);
}
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
return await this.program.instruction.depositIntoSpotMarketVault(amount, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
sourceVault,
spotMarket: spotMarket.pubkey,
spotMarketVault: spotMarket.vault,
tokenProgram,
},
remainingAccounts,
});
}
async updateAdmin(admin) {
const updateAdminIx = await this.getUpdateAdminIx(admin);
const tx = await this.buildTransaction(updateAdminIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateAdminIx(admin) {
return await this.program.instruction.updateAdmin(admin, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updatePerpMarketCurveUpdateIntensity(perpMarketIndex, curveUpdateIntensity) {
const updatePerpMarketCurveUpdateIntensityIx = await this.getUpdatePerpMarketCurveUpdateIntensityIx(perpMarketIndex, curveUpdateIntensity);
const tx = await this.buildTransaction(updatePerpMarketCurveUpdateIntensityIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketCurveUpdateIntensityIx(perpMarketIndex, curveUpdateIntensity) {
return await this.program.instruction.updatePerpMarketCurveUpdateIntensity(curveUpdateIntensity, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketTargetBaseAssetAmountPerLp(perpMarketIndex, targetBaseAssetAmountPerLP) {
const updatePerpMarketTargetBaseAssetAmountPerLpIx = await this.getUpdatePerpMarketTargetBaseAssetAmountPerLpIx(perpMarketIndex, targetBaseAssetAmountPerLP);
const tx = await this.buildTransaction(updatePerpMarketTargetBaseAssetAmountPerLpIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async updatePerpMarketAmmSummaryStats(perpMarketIndex, updateAmmSummaryStats, quoteAssetAmountWithUnsettledLp, netUnsettledFundingPnl, excludeTotalLiqFee) {
const updatePerpMarketMarginRatioIx = await this.getUpdatePerpMarketAmmSummaryStatsIx(perpMarketIndex, updateAmmSummaryStats, quoteAssetAmountWithUnsettledLp, netUnsettledFundingPnl, excludeTotalLiqFee);
const tx = await this.buildTransaction(updatePerpMarketMarginRatioIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketAmmSummaryStatsIx(perpMarketIndex, updateAmmSummaryStats, quoteAssetAmountWithUnsettledLp, netUnsettledFundingPnl, excludeTotalLiqFee) {
return await this.program.instruction.updatePerpMarketAmmSummaryStats({
updateAmmSummaryStats: updateAmmSummaryStats !== null && updateAmmSummaryStats !== void 0 ? updateAmmSummaryStats : null,
quoteAssetAmountWithUnsettledLp: quoteAssetAmountWithUnsettledLp !== null && quoteAssetAmountWithUnsettledLp !== void 0 ? quoteAssetAmountWithUnsettledLp : null,
netUnsettledFundingPnl: netUnsettledFundingPnl !== null && netUnsettledFundingPnl !== void 0 ? netUnsettledFundingPnl : null,
excludeTotalLiqFee: excludeTotalLiqFee !== null && excludeTotalLiqFee !== void 0 ? excludeTotalLiqFee : null,
}, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
spotMarket: await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, numericConstants_1.QUOTE_SPOT_MARKET_INDEX),
oracle: this.getPerpMarketAccount(perpMarketIndex).amm.oracle,
},
});
}
async getUpdatePerpMarketTargetBaseAssetAmountPerLpIx(perpMarketIndex, targetBaseAssetAmountPerLP) {
return await this.program.instruction.updatePerpMarketTargetBaseAssetAmountPerLp(targetBaseAssetAmountPerLP, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketMarginRatio(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
const updatePerpMarketMarginRatioIx = await this.getUpdatePerpMarketMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance);
const tx = await this.buildTransaction(updatePerpMarketMarginRatioIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
return await this.program.instruction.updatePerpMarketMarginRatio(marginRatioInitial, marginRatioMaintenance, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketHighLeverageMarginRatio(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
const updatePerpMarketHighLeverageMarginRatioIx = await this.getUpdatePerpMarketHighLeverageMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance);
const tx = await this.buildTransaction(updatePerpMarketHighLeverageMarginRatioIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketHighLeverageMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
return await this.program.instruction.updatePerpMarketHighLeverageMarginRatio(marginRatioInitial, marginRatioMaintenance, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketImfFactor(perpMarketIndex, imfFactor, unrealizedPnlImfFactor) {
const updatePerpMarketImfFactorIx = await this.getUpdatePerpMarketImfFactorIx(perpMarketIndex, imfFactor, unrealizedPnlImfFactor);
const tx = await this.buildTransaction(updatePerpMarketImfFactorIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketImfFactorIx(perpMarketIndex, imfFactor, unrealizedPnlImfFactor) {
return await this.program.instruction.updatePerpMarketImfFactor(imfFactor, unrealizedPnlImfFactor, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketBaseSpread(perpMarketIndex, baseSpread) {
const updatePerpMarketBaseSpreadIx = await this.getUpdatePerpMarketBaseSpreadIx(perpMarketIndex, baseSpread);
const tx = await this.buildTransaction(updatePerpMarketBaseSpreadIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketBaseSpreadIx(perpMarketIndex, baseSpread) {
return await this.program.instruction.updatePerpMarketBaseSpread(baseSpread, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updateAmmJitIntensity(perpMarketIndex, ammJitIntensity) {
const updateAmmJitIntensityIx = await this.getUpdateAmmJitIntensityIx(perpMarketIndex, ammJitIntensity);
const tx = await this.buildTransaction(updateAmmJitIntensityIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateAmmJitIntensityIx(perpMarketIndex, ammJitIntensity) {
return await this.program.instruction.updateAmmJitIntensity(ammJitIntensity, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updatePerpMarketName(perpMarketIndex, name) {
const updatePerpMarketNameIx = await this.getUpdatePerpMarketNameIx(perpMarketIndex, name);
const tx = await this.buildTransaction(updatePerpMarketNameIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketNameIx(perpMarketIndex, name) {
const nameBuffer = (0, userName_1.encodeName)(name);
return await this.program.instruction.updatePerpMarketName(nameBuffer, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
},
});
}
async updateSpotMarketName(spotMarketIndex, name) {
const updateSpotMarketNameIx = await this.getUpdateSpotMarketNameIx(spotMarketIndex, name);
const tx = await this.buildTransaction(updateSpotMarketNameIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateSpotMarketNameIx(spotMarketIndex, name) {
const nameBuffer = (0, userName_1.encodeName)(name);
return await this.program.instruction.updateSpotMarketName(nameBuffer, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
spotMarket: await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, spotMarketIndex),
},
});
}
async updateSpotMarketPoolId(spotMarketIndex, poolId) {
const updateSpotMarketPoolIdIx = await this.getUpdateSpotMarketPoolIdIx(spotMarketIndex, poolId);
const tx = await this.buildTransaction(updateSpotMarketPoolIdIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateSpotMarketPoolIdIx(spotMarketIndex, poolId) {
return await this.program.instruction.updateSpotMarketPoolId(poolId, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
spotMarket: await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, spotMarketIndex),
},
});
}
async updatePerpMarketPerLpBase(perpMarketIndex, perLpBase) {
const updatePerpMarketPerLpBaseIx = await this.getUpdatePerpMarketPerLpBaseIx(perpMarketIndex, perLpBase);
const tx = await this.buildTransaction(updatePerpMarketPerLpBaseIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketPerLpBaseIx(perpMarketIndex, perLpBase) {
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.updatePerpMarketPerLpBase(perLpBase, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: perpMarketPublicKey,
},
});
}
async updatePerpMarketMaxSpread(perpMarketIndex, maxSpread) {
const updatePerpMarketMaxSpreadIx = await this.getUpdatePerpMarketMaxSpreadIx(perpMarketIndex, maxSpread);
const tx = await this.buildTransaction(updatePerpMarketMaxSpreadIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpMarketMaxSpreadIx(perpMarketIndex, maxSpread) {
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
return await this.program.instruction.updatePerpMarketMaxSpread(maxSpread, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
perpMarket: perpMarketPublicKey,
},
});
}
async updatePerpFeeStructure(feeStructure) {
const updatePerpFeeStructureIx = await this.getUpdatePerpFeeStructureIx(feeStructure);
const tx = await this.buildTransaction(updatePerpFeeStructureIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdatePerpFeeStructureIx(feeStructure) {
return this.program.instruction.updatePerpFeeStructure(feeStructure, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateSpotFeeStructure(feeStructure) {
const updateSpotFeeStructureIx = await this.getUpdateSpotFeeStructureIx(feeStructure);
const tx = await this.buildTransaction(updateSpotFeeStructureIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateSpotFeeStructureIx(feeStructure) {
return await this.program.instruction.updateSpotFeeStructure(feeStructure, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateInitialPctToLiquidate(initialPctToLiquidate) {
const updateInitialPctToLiquidateIx = await this.getUpdateInitialPctToLiquidateIx(initialPctToLiquidate);
const tx = await this.buildTransaction(updateInitialPctToLiquidateIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateInitialPctToLiquidateIx(initialPctToLiquidate) {
return await this.program.instruction.updateInitialPctToLiquidate(initialPctToLiquidate, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateLiquidationDuration(liquidationDuration) {
const updateLiquidationDurationIx = await this.getUpdateLiquidationDurationIx(liquidationDuration);
const tx = await this.buildTransaction(updateLiquidationDurationIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateLiquidationDurationIx(liquidationDuration) {
return await this.program.instruction.updateLiquidationDuration(liquidationDuration, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio) {
const updateLiquidationMarginBufferRatioIx = await this.getUpdateLiquidationMarginBufferRatioIx(updateLiquidationMarginBufferRatio);
const tx = await this.buildTransaction(updateLiquidationMarginBufferRatioIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateLiquidationMarginBufferRatioIx(updateLiquidationMarginBufferRatio) {
return await this.program.instruction.updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateOracleGuardRails(oracleGuardRails) {
const updateOracleGuardRailsIx = await this.getUpdateOracleGuardRailsIx(oracleGuardRails);
const tx = await this.buildTransaction(updateOracleGuardRailsIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}
async getUpdateOracleGuardRailsIx(oracleGuardRails) {
return await this.program.instruction.updateOracleGuardRails(oracleGuardRails, {
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
});
}
async updateStateSettlementDuration(settlementDuration) {
const updateStateSettlementDurationIx = await this.