diff --git a/sdk/src/constants/txConstants.ts b/sdk/src/constants/txConstants.ts index df3e00180..c16c23eae 100644 --- a/sdk/src/constants/txConstants.ts +++ b/sdk/src/constants/txConstants.ts @@ -4,3 +4,5 @@ export const NOT_CONFIRMED_ERROR_CODE = -1001; export const FUEL_RESET_LOG_ACCOUNT = new PublicKey( 'FuE1gqp2fzw2sDNLrbZqKsqpphJcoSW6HPaSJjGd4RZ4' ); + +export const DEFAULT_COMMITMENT_LEVEL = 'confirmed'; diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index ef3562e45..df47a025d 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -221,6 +221,7 @@ import { } from './math/builder'; import { TitanClient, SwapMode as TitanSwapMode } from './titan/titanClient'; import { UnifiedSwapClient } from './swap/UnifiedSwapClient'; +import { DEFAULT_COMMITMENT_LEVEL } from './constants'; /** * Union type for swap clients (Titan and Jupiter) - Legacy type @@ -428,19 +429,17 @@ export class DriftClient { }); } - this.marketLookupTable = config.marketLookupTable; - if (!this.marketLookupTable) { - this.marketLookupTable = new PublicKey( - configs[this.env].MARKET_LOOKUP_TABLE - ); - } + this.marketLookupTable = config.marketLookupTable + ? config.marketLookupTable + : (this.marketLookupTable = new PublicKey( + configs[this.env].MARKET_LOOKUP_TABLE + )); - this.marketLookupTables = config.marketLookupTables; - if (!this.marketLookupTables) { - this.marketLookupTables = configs[this.env].MARKET_LOOKUP_TABLES.map( - (tableAddr) => new PublicKey(tableAddr) - ); - } + this.marketLookupTables = config.marketLookupTables + ? config.marketLookupTables + : configs[this.env].MARKET_LOOKUP_TABLES.map( + (tableAddr) => new PublicKey(tableAddr) + ); const delistedMarketSetting = config.delistedMarketSetting || DelistedMarketSetting.Unsubscribe; @@ -8881,8 +8880,14 @@ export class DriftClient { ); const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey(); + const liquidatorUser = this.getUserAccount(liquidatorSubAccountId); + if (!liquidatorUser) { + throw new Error( + `Liquidator user account not found for subaccount id ${liquidatorSubAccountId}` + ); + } const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount], + userAccounts: [liquidatorUser, userAccount], useMarketLastSlotCache: true, writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex], }); @@ -8943,7 +8948,17 @@ export class DriftClient { lookupTables: AddressLookupTableAccount[]; }> { const liabilityMarket = this.getSpotMarketAccount(liabilityMarketIndex); + if (!liabilityMarket) { + throw new Error( + `Liability spot market account not found for index ${liabilityMarketIndex}` + ); + } const assetMarket = this.getSpotMarketAccount(assetMarketIndex); + if (!assetMarket) { + throw new Error( + `Asset spot market account not found for index ${assetMarketIndex}` + ); + } if (!quote) { const fetchedQuote = await jupiterClient.getQuote({ @@ -8982,7 +8997,7 @@ export class DriftClient { outputMint: liabilityMarket.mint, }); - const preInstructions = []; + const preInstructions: TransactionInstruction[] = []; if (!liabilityTokenAccount) { const tokenProgram = this.getTokenProgramForSpotMarket(liabilityMarket); liabilityTokenAccount = await this.getAssociatedTokenAccount( @@ -9092,7 +9107,17 @@ export class DriftClient { }); const liabilitySpotMarket = this.getSpotMarketAccount(liabilityMarketIndex); + if (!liabilitySpotMarket) { + throw new Error( + `Liability spot market account not found for index ${liabilityMarketIndex}` + ); + } const assetSpotMarket = this.getSpotMarketAccount(assetMarketIndex); + if (!assetSpotMarket) { + throw new Error( + `Asset spot market account not found for index ${assetMarketIndex}` + ); + } const liabilityTokenProgram = this.getTokenProgramForSpotMarket(liabilitySpotMarket); @@ -9208,7 +9233,17 @@ export class DriftClient { }); const inSpotMarket = this.getSpotMarketAccount(inMarketIndex); + if (!inSpotMarket) { + throw new Error( + `In spot market account not found for index ${inMarketIndex}` + ); + } const outSpotMarket = this.getSpotMarketAccount(outMarketIndex); + if (!outSpotMarket) { + throw new Error( + `Out spot market account not found for index ${outMarketIndex}` + ); + } if (this.isToken2022(inSpotMarket) || this.isToken2022(outSpotMarket)) { remainingAccounts.push({ @@ -9334,9 +9369,15 @@ export class DriftClient { liquidatorSubAccountId ); const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey(); + const liquidatorUser = this.getUserAccount(liquidatorSubAccountId); + if (!liquidatorUser) { + throw new Error( + `Liquidator user account not found for subaccount id ${liquidatorSubAccountId}` + ); + } const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount], + userAccounts: [liquidatorUser, userAccount], writablePerpMarketIndexes: [perpMarketIndex], writableSpotMarketIndexes: [liabilityMarketIndex], }); @@ -9409,9 +9450,15 @@ export class DriftClient { liquidatorSubAccountId ); const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey(); + const liquidatorUser = this.getUserAccount(liquidatorSubAccountId); + if (!liquidatorUser) { + throw new Error( + `Liquidator user account not found for subaccount id ${liquidatorSubAccountId}` + ); + } const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount], + userAccounts: [liquidatorUser, userAccount], writablePerpMarketIndexes: [perpMarketIndex], writableSpotMarketIndexes: [assetMarketIndex], }); @@ -9473,9 +9520,15 @@ export class DriftClient { liquidatorSubAccountId ); const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey(); + const liquidatorUser = this.getUserAccount(liquidatorSubAccountId); + if (!liquidatorUser) { + throw new Error( + `Liquidator user account not found for subaccount id ${liquidatorSubAccountId}` + ); + } const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount], + userAccounts: [liquidatorUser, userAccount], writablePerpMarketIndexes: [marketIndex], writableSpotMarketIndexes: [QUOTE_SPOT_MARKET_INDEX], }); @@ -9542,12 +9595,22 @@ export class DriftClient { ); const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey(); + const liquidatorUser = this.getUserAccount(liquidatorSubAccountId); + if (!liquidatorUser) { + throw new Error( + `Liquidator user account not found for subaccount id ${liquidatorSubAccountId}` + ); + } + const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount], + userAccounts: [liquidatorUser, userAccount], writableSpotMarketIndexes: [marketIndex], }); const spotMarket = this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarket); this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts); @@ -9627,6 +9690,11 @@ export class DriftClient { perpMarketIndex: number ): Promise { const perpMarket = this.getPerpMarketAccount(perpMarketIndex); + if (!perpMarket) { + throw new Error( + `Perp market account not found for index ${perpMarketIndex}` + ); + } if (!isVariant(perpMarket.amm.oracleSource, 'prelaunch')) { throw new Error(`Wrong oracle source ${perpMarket.amm.oracleSource}`); @@ -9662,8 +9730,13 @@ export class DriftClient { makers: [PublicKey, PublicKey][] ): Promise { const perpMarket = this.getPerpMarketAccount(perpMarketIndex); + if (!perpMarket) { + throw new Error( + `Perp market account not found for index ${perpMarketIndex}` + ); + } - const remainingAccounts = []; + const remainingAccounts: AccountMeta[] = []; for (const [maker, makerStats] of makers) { remainingAccounts.push({ pubkey: maker, @@ -9711,7 +9784,7 @@ export class DriftClient { userAccountPublicKey )) as UserAccount; - const writablePerpMarketIndexes = []; + const writablePerpMarketIndexes: number[] = []; for (const position of userAccount.perpPositions) { if (!positionIsAvailable(position)) { writablePerpMarketIndexes.push(position.marketIndex); @@ -9736,15 +9809,25 @@ export class DriftClient { this.eventEmitter.emit(eventName, data); } - public getOracleDataForPerpMarket(marketIndex: number): OraclePriceData { + public getOracleDataForPerpMarket( + marketIndex: number + ): OraclePriceData | undefined { return this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket( marketIndex - ).data; + )?.data; } - public getMMOracleDataForPerpMarket(marketIndex: number): MMOraclePriceData { + public getMMOracleDataForPerpMarket( + marketIndex: number + ): MMOraclePriceData | undefined { const perpMarket = this.getPerpMarketAccount(marketIndex); + if (!perpMarket) { + throw new Error(`Perp market account not found for index ${marketIndex}`); + } const oracleData = this.getOracleDataForPerpMarket(marketIndex); + if (!oracleData) { + return undefined; + } const stateAccountAndSlot = this.accountSubscriber.getStateAccountAndSlot(); const isMMOracleActive = !perpMarket.amm.mmOracleSlot.eq(ZERO); const pctDiff = perpMarket.amm.mmOraclePrice @@ -9773,7 +9856,7 @@ export class DriftClient { isExchangeOracleMoreRecent = false; } else if ( !doSlotCheckForRecency && - oracleData.sequenceId < mmOracleSequenceId + (oracleData.sequenceId ?? ZERO) < mmOracleSequenceId ) { isExchangeOracleMoreRecent = false; } @@ -9810,10 +9893,12 @@ export class DriftClient { } } - public getOracleDataForSpotMarket(marketIndex: number): OraclePriceData { + public getOracleDataForSpotMarket( + marketIndex: number + ): OraclePriceData | undefined { return this.accountSubscriber.getOraclePriceDataAndSlotForSpotMarket( marketIndex - ).data; + )?.data; } public async initializeInsuranceFundStake( @@ -9840,9 +9925,14 @@ export class DriftClient { marketIndex ); + const spotMarket = this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } + const accounts = { insuranceFundStake: ifStakeAccountPublicKey, - spotMarket: this.getSpotMarketAccount(marketIndex).pubkey, + spotMarket: spotMarket.pubkey, userStats: getUserStatsAccountPublicKey( this.program.programId, this.wallet.publicKey // only allow payer to initialize own insurance fund stake account @@ -9868,6 +9958,9 @@ export class DriftClient { collateralAccountPublicKey: PublicKey ): Promise { const spotMarket = this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, this.wallet.publicKey, @@ -9988,9 +10081,12 @@ export class DriftClient { */ fromSubaccount?: boolean; }): Promise { - const addIfStakeIxs = []; + const addIfStakeIxs: TransactionInstruction[] = []; const spotMarketAccount = this.getSpotMarketAccount(marketIndex); + if (!spotMarketAccount) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT); const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey); @@ -10084,6 +10180,9 @@ export class DriftClient { txParams?: TxParams ): Promise { const spotMarketAccount = this.getSpotMarketAccount(marketIndex); + if (!spotMarketAccount) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, this.wallet.publicKey, @@ -10119,6 +10218,9 @@ export class DriftClient { txParams?: TxParams ): Promise { const spotMarketAccount = this.getSpotMarketAccount(marketIndex); + if (!spotMarketAccount) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, this.wallet.publicKey, @@ -10154,8 +10256,11 @@ export class DriftClient { collateralAccountPublicKey: PublicKey, txParams?: TxParams ): Promise { - const removeIfStakeIxs = []; + const removeIfStakeIxs: TransactionInstruction[] = []; const spotMarketAccount = this.getSpotMarketAccount(marketIndex); + if (!spotMarketAccount) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, this.wallet.publicKey, @@ -10265,6 +10370,9 @@ export class DriftClient { ): Promise { const marketIndex = QUOTE_SPOT_MARKET_INDEX; const spotMarket = this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, authority, @@ -10304,6 +10412,9 @@ export class DriftClient { ): Promise { const marketIndex = GOV_SPOT_MARKET_INDEX; const spotMarket = this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey( this.program.programId, authority, @@ -10344,6 +10455,11 @@ export class DriftClient { spotMarketIndex: number ): Promise { const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex); + if (!spotMarketAccount) { + throw new Error( + `Spot market account not found for index ${spotMarketIndex}` + ); + } const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarketAccount); const remainingAccounts = []; @@ -10392,13 +10508,24 @@ export class DriftClient { spotMarketIndex: number, perpMarketIndex: number ): Promise { + const userAccount = this.getUserAccount(); + if (!userAccount) { + throw new Error( + `User account not found. Something is wrong with driftClient config` + ); + } const remainingAccounts = this.getRemainingAccounts({ - userAccounts: [this.getUserAccount()], + userAccounts: [userAccount], writablePerpMarketIndexes: [perpMarketIndex], writableSpotMarketIndexes: [spotMarketIndex], }); const spotMarket = this.getSpotMarketAccount(spotMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${spotMarketIndex}` + ); + } const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarket); return await this.program.instruction.resolvePerpPnlDeficit( @@ -10424,6 +10551,9 @@ export class DriftClient { userTokenAccountPublicKey: PublicKey ): Promise { const spotMarket = await this.getSpotMarketAccount(marketIndex); + if (!spotMarket) { + throw new Error(`Spot market account not found for index ${marketIndex}`); + } const remainingAccounts = []; this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts); @@ -10477,9 +10607,17 @@ export class DriftClient { public getPerpMarketExtendedInfo( marketIndex: number - ): PerpMarketExtendedInfo { + ): PerpMarketExtendedInfo | undefined { const marketAccount = this.getPerpMarketAccount(marketIndex); + if (!marketAccount) { + return undefined; + } const quoteAccount = this.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX); + if (!quoteAccount) { + throw new Error( + `Quote spot market account not found. Something is wrong with driftClient config` + ); + } const extendedInfo: PerpMarketExtendedInfo = { marketIndex, @@ -10530,18 +10668,23 @@ export class DriftClient { feeTier.makerRebateNumerator / feeTier.makerRebateDenominator; if (marketIndex !== undefined) { - let marketAccount = null; + let marketAccount: + | null + | undefined + | PerpMarketAccount + | SpotMarketAccount = null; + let feeAdjustment = 1; if (isVariant(marketType, 'perp')) { marketAccount = this.getPerpMarketAccount(marketIndex); + feeAdjustment = marketAccount?.feeAdjustment ?? 0; } else { marketAccount = this.getSpotMarketAccount(marketIndex); } - - takerFee += (takerFee * marketAccount.feeAdjustment) / 100; + takerFee += (takerFee * feeAdjustment) / 100; if (userHLM) { takerFee *= 2; } - makerFee += (makerFee * marketAccount.feeAdjustment) / 100; + makerFee += (makerFee * feeAdjustment) / 100; } return { @@ -10592,7 +10735,9 @@ export class DriftClient { return this.receiverProgram; } - public async getSwitchboardOnDemandProgram(): Promise> { + public async getSwitchboardOnDemandProgram(): Promise< + Program30 | undefined + > { if (this.sbOnDemandProgram === undefined) { this.sbOnDemandProgram = await AnchorUtils.loadProgramFromConnection( this.connection @@ -10895,6 +11040,9 @@ export class DriftClient { numSignatures = 3 ): Promise { const program = await this.getSwitchboardOnDemandProgram(); + if (!program) { + return undefined; + } const feedAccount = new PullFeed(program, feed); if (!this.sbProgramFeedConfigs) { this.sbProgramFeedConfigs = new Map(); @@ -10923,7 +11071,7 @@ export class DriftClient { feed: PublicKey, recentSlothash?: Slothash, numSignatures = 3 - ): Promise { + ): Promise { const pullIx = await this.getPostSwitchboardOnDemandUpdateAtomicIx( feed, recentSlothash, @@ -11056,7 +11204,12 @@ export class DriftClient { const signingAuthority = overrides?.signingAuthority ?? this.wallet.publicKey; const userAccount = - overrides?.user.getUserAccount() ?? this.getUserAccount(subAccountId); + overrides?.user?.getUserAccount() ?? this.getUserAccount(subAccountId); + if (!userAccount) { + throw new Error( + `User account not found for subAccountId ${subAccountId}` + ); + } const remainingAccounts = this.getRemainingAccounts({ userAccounts: depositToTradeArgs?.isMakingNewAccount ? [] : [userAccount], @@ -11196,6 +11349,11 @@ export class DriftClient { spotMarketIndex: number ): Promise { const spotMarket = await this.getSpotMarketAccount(spotMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${spotMarketIndex}` + ); + } return this.program.instruction.pauseSpotMarketDepositWithdraw({ accounts: { state: await this.getStatePublicKey(), @@ -11252,12 +11410,17 @@ export class DriftClient { data.set(oraclePrice.toArrayLike(Buffer, 'le', 8), 5); // next 8 bytes data.set(oracleSequenceId.toArrayLike(Buffer, 'le', 8), 13); // next 8 bytes + const perpMarket = this.getPerpMarketAccount(marketIndex); + if (!perpMarket) { + throw new Error(`Perp market account not found for index ${marketIndex}`); + } + // Build the instruction manually return new TransactionInstruction({ programId: this.program.programId, keys: [ { - pubkey: this.getPerpMarketAccount(marketIndex).pubkey, + pubkey: perpMarket.pubkey, isWritable: true, isSigner: false, }, @@ -11308,12 +11471,16 @@ export class DriftClient { data.set(discriminatorBuffer, 0); data.writeInt8(ammSpreadAdjustment, 5); // next byte + const perpMarket = this.getPerpMarketAccount(marketIndex); + if (!perpMarket) { + throw new Error(`Perp market account not found for index ${marketIndex}`); + } // Build the instruction manually return new TransactionInstruction({ programId: this.program.programId, keys: [ { - pubkey: this.getPerpMarketAccount(marketIndex).pubkey, + pubkey: perpMarket.pubkey, isWritable: true, isSigner: false, }, @@ -11482,12 +11649,17 @@ export class DriftClient { readablePerpMarketIndex: perpMarketIndexes, }); + const quoteMarket = this.getSpotMarketAccount(0); + if (!quoteMarket) { + throw new Error(`Quote spot market account not found for index 0`); + } + return this.program.instruction.updateAmmCache({ accounts: { state: await this.getStatePublicKey(), keeper: this.wallet.publicKey, ammCache: getAmmCachePublicKey(this.program.programId), - quoteMarket: this.getSpotMarketAccount(0).pubkey, + quoteMarket: quoteMarket.pubkey, }, remainingAccounts, }); @@ -11511,6 +11683,11 @@ export class DriftClient { constituent: ConstituentAccount ): Promise { const spotMarket = this.getSpotMarketAccount(constituent.spotMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${constituent.spotMarketIndex}` + ); + } return this.program.instruction.updateConstituentOracleInfo({ accounts: { keeper: this.wallet.publicKey, @@ -11572,12 +11749,26 @@ export class DriftClient { lpPool, outMarketIndex ); + + const inSpotMarket = this.getSpotMarketAccount(inMarketIndex); + if (!inSpotMarket) { + throw new Error( + `Spot market account not found for index ${inMarketIndex}` + ); + } + const outSpotMarket = this.getSpotMarketAccount(outMarketIndex); + if (!outSpotMarket) { + throw new Error( + `Spot market account not found for index ${outMarketIndex}` + ); + } + const userInTokenAccount = await getAssociatedTokenAddress( - this.getSpotMarketAccount(inMarketIndex).mint, + inSpotMarket.mint, userAuthority ); const userOutTokenAccount = await getAssociatedTokenAddress( - this.getSpotMarketAccount(outMarketIndex).mint, + outSpotMarket.mint, userAuthority ); const inConstituent = getConstituentPublicKey( @@ -11590,8 +11781,8 @@ export class DriftClient { lpPool, outMarketIndex ); - const inMarketMint = this.getSpotMarketAccount(inMarketIndex).mint; - const outMarketMint = this.getSpotMarketAccount(outMarketIndex).mint; + const inMarketMint = inSpotMarket.mint; + const outMarketMint = outSpotMarket.mint; const constituentTargetBase = getConstituentTargetBasePublicKey( this.program.programId, @@ -11790,6 +11981,11 @@ export class DriftClient { }); const spotMarket = this.getSpotMarketAccount(inMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${inMarketIndex}` + ); + } const inMarketMint = spotMarket.mint; const isSolMarket = inMarketMint.equals(WRAPPED_SOL_MINT); @@ -12005,6 +12201,11 @@ export class DriftClient { }); const spotMarket = this.getSpotMarketAccount(outMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${outMarketIndex}` + ); + } const outMarketMint = spotMarket.mint; const outConstituent = getConstituentPublicKey( this.program.programId, @@ -12116,6 +12317,11 @@ export class DriftClient { }); const spotMarket = this.getSpotMarketAccount(outMarketIndex); + if (!spotMarket) { + throw new Error( + `Spot market account not found for index ${outMarketIndex}` + ); + } const outMarketMint = spotMarket.mint; const outConstituent = getConstituentPublicKey( this.program.programId, @@ -12350,11 +12556,11 @@ export class DriftClient { lpPoolId: number, perpMarketIndexes: number[] ): Promise { - const remainingAccounts = []; + const remainingAccounts: AccountMeta[] = []; remainingAccounts.push( ...perpMarketIndexes.map((index) => { return { - pubkey: this.getPerpMarketAccount(index).pubkey, + pubkey: this.getPerpMarketAccount(index)?.pubkey ?? PublicKey.default, isSigner: false, isWritable: true, }; @@ -12462,7 +12668,8 @@ export class DriftClient { txVersion: txVersion ?? this.txVersion, txParams: txParams ?? this.txParams, connection: this.connection, - preFlightCommitment: this.opts.preflightCommitment, + preFlightCommitment: + this.opts?.preflightCommitment ?? DEFAULT_COMMITMENT_LEVEL, fetchAllMarketLookupTableAccounts: this.fetchAllLookupTableAccounts.bind(this), lookupTables, @@ -12479,17 +12686,20 @@ export class DriftClient { lookupTables?: AddressLookupTableAccount[], forceVersionedTransaction?: boolean ): Promise<(Transaction | VersionedTransaction)[]> { - return this.txHandler.buildBulkTransactions({ + const txns = await this.txHandler.buildBulkTransactions({ instructions, txVersion: txVersion ?? this.txVersion, txParams: txParams ?? this.txParams, connection: this.connection, - preFlightCommitment: this.opts.preflightCommitment, + preFlightCommitment: + this.opts?.preflightCommitment ?? DEFAULT_COMMITMENT_LEVEL, fetchAllMarketLookupTableAccounts: this.fetchAllLookupTableAccounts.bind(this), lookupTables, forceVersionedTransaction, }); + + return txns.filter(Boolean) as (Transaction | VersionedTransaction)[]; } async buildTransactionsMap( @@ -12507,7 +12717,8 @@ export class DriftClient { txVersion: txVersion ?? this.txVersion, txParams: txParams ?? this.txParams, connection: this.connection, - preFlightCommitment: this.opts.preflightCommitment, + preFlightCommitment: + this.opts?.preflightCommitment ?? DEFAULT_COMMITMENT_LEVEL, fetchAllMarketLookupTableAccounts: this.fetchAllLookupTableAccounts.bind(this), lookupTables, @@ -12530,7 +12741,8 @@ export class DriftClient { txVersion: txVersion ?? this.txVersion, txParams: txParams ?? this.txParams, connection: this.connection, - preFlightCommitment: this.opts.preflightCommitment, + preFlightCommitment: + this.opts?.preflightCommitment ?? DEFAULT_COMMITMENT_LEVEL, fetchAllMarketLookupTableAccounts: this.fetchAllLookupTableAccounts.bind(this), lookupTables, diff --git a/sdk/src/markets.ts b/sdk/src/markets.ts new file mode 100644 index 000000000..298080f14 --- /dev/null +++ b/sdk/src/markets.ts @@ -0,0 +1,24 @@ +import { DriftEnv } from './config'; +import { SpotMarketConfig, SpotMarkets } from './constants'; +import { PerpMarketConfig, PerpMarkets } from './constants/perpMarkets'; +import { isOneOfVariant } from './types'; + +export const getActivePerpMarkets = ( + driftEnv: DriftEnv +): PerpMarketConfig[] => { + return PerpMarkets[driftEnv ?? 'mainnet-beta'].filter( + (market) => + !market.marketStatus || + !isOneOfVariant(market.marketStatus, ['delisted', 'settlement']) + ); +}; + +export const getActiveSpotMarkets = ( + driftEnv: DriftEnv +): SpotMarketConfig[] => { + return SpotMarkets[driftEnv ?? 'mainnet-beta'].filter( + (market) => + !market.marketStatus || + !isOneOfVariant(market.marketStatus, ['delisted', 'settlement']) + ); +};