Skip to content

Commit

Permalink
sdk: remaining openbook stuff (#1165)
Browse files Browse the repository at this point in the history
* add openbookv2 subscriber

* remove weird artifacts

* revert accidental prettiers

* revert prettiers

* pr comment

* prettify fix

* remaining openbook sdk stuff

* revert prettier

* fxi
  • Loading branch information
soundsonacid authored Jul 30, 2024
1 parent 083b87d commit 24239ff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
3 changes: 3 additions & 0 deletions sdk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DriftConfig = {
USDC_MINT_ADDRESS: string;
SERUM_V3: string;
PHOENIX: string;
OPENBOOK: string;
V2_ALPHA_TICKET_MINT_ADDRESS: string;
PERP_MARKETS: PerpMarketConfig[];
SPOT_MARKETS: SpotMarketConfig[];
Expand All @@ -46,6 +47,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
SERUM_V3: 'DESVgJVGajEgKGXhb6XmqDHGz3VjdgP7rEVESBgxmroY',
PHOENIX: 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY',
OPENBOOK: 'opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb',
V2_ALPHA_TICKET_MINT_ADDRESS:
'DeEiGWfCMP9psnLGkxGrBBMEAW5Jv8bBGMN8DCtFRCyB',
PERP_MARKETS: DevnetPerpMarkets,
Expand All @@ -61,6 +63,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
USDC_MINT_ADDRESS: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
SERUM_V3: 'srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX',
PHOENIX: 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY',
OPENBOOK: 'opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb',
V2_ALPHA_TICKET_MINT_ADDRESS:
'Cmvhycb6LQvvzaShGw4iDHRLzeSSryioAsU98DSSkMNa',
PERP_MARKETS: MainnetPerpMarkets,
Expand Down
1 change: 1 addition & 0 deletions sdk/src/constants/spotMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type SpotMarketConfig = {
precisionExp: BN;
serumMarket?: PublicKey;
phoenixMarket?: PublicKey;
openbookMarket?: PublicKey;
launchTs?: number;
pythFeedId?: string;
};
Expand Down
29 changes: 19 additions & 10 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createInitializeAccountInstruction,
getAssociatedTokenAddress,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
import {
StateAccount,
Expand Down Expand Up @@ -119,7 +120,6 @@ import { isSpotPositionAvailable } from './math/spotPosition';
import { calculateMarketMaxAvailableInsurance } from './math/market';
import { fetchUserStatsAccount } from './accounts/fetch';
import { castNumberToSpotPrecision } from './math/spotMarket';
import { getTokenProgramForSpotMarket } from './addresses/pda';
import {
JupiterClient,
QuoteResponse,
Expand Down Expand Up @@ -1973,7 +1973,7 @@ export class DriftClient {
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);

this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
const tokenProgram = getTokenProgramForSpotMarket(spotMarketAccount);
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarketAccount);
return await this.program.instruction.deposit(
marketIndex,
amount,
Expand Down Expand Up @@ -2060,6 +2060,15 @@ export class DriftClient {
return result;
}

public getTokenProgramForSpotMarket(
spotMarketAccount: SpotMarketAccount
): PublicKey {
if (spotMarketAccount.tokenProgram === 1) {
return TOKEN_2022_PROGRAM_ID;
}
return TOKEN_PROGRAM_ID;
}

public addTokenMintToRemainingAccounts(
spotMarketAccount: SpotMarketAccount,
remainingAccounts: AccountMeta[]
Expand Down Expand Up @@ -2486,7 +2495,7 @@ export class DriftClient {
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);

this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
const tokenProgram = getTokenProgramForSpotMarket(spotMarketAccount);
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarketAccount);

return await this.program.instruction.withdraw(
marketIndex,
Expand Down Expand Up @@ -4250,7 +4259,7 @@ export class DriftClient {
outAssociatedTokenAccount
);
if (!accountInfo) {
const tokenProgram = getTokenProgramForSpotMarket(outMarket);
const tokenProgram = this.getTokenProgramForSpotMarket(outMarket);

preInstructions.push(
this.createAssociatedTokenAccountIdempotentInstruction(
Expand All @@ -4274,7 +4283,7 @@ export class DriftClient {
inAssociatedTokenAccount
);
if (!accountInfo) {
const tokenProgram = getTokenProgramForSpotMarket(outMarket);
const tokenProgram = this.getTokenProgramForSpotMarket(outMarket);

preInstructions.push(
this.createAssociatedTokenAccountIdempotentInstruction(
Expand Down Expand Up @@ -4497,8 +4506,8 @@ export class DriftClient {
const outSpotMarket = this.getSpotMarketAccount(outMarketIndex);
const inSpotMarket = this.getSpotMarketAccount(inMarketIndex);

const outTokenProgram = getTokenProgramForSpotMarket(outSpotMarket);
const inTokenProgram = getTokenProgramForSpotMarket(inSpotMarket);
const outTokenProgram = this.getTokenProgramForSpotMarket(outSpotMarket);
const inTokenProgram = this.getTokenProgramForSpotMarket(inSpotMarket);

if (!outTokenProgram.equals(inTokenProgram)) {
remainingAccounts.push({
Expand Down Expand Up @@ -6651,7 +6660,7 @@ export class DriftClient {

const remainingAccounts = [];
this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
const tokenProgram = getTokenProgramForSpotMarket(spotMarket);
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
const ix = this.program.instruction.addInsuranceFundStake(
marketIndex,
amount,
Expand Down Expand Up @@ -6891,7 +6900,7 @@ export class DriftClient {

const remainingAccounts = [];
this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
const tokenProgram = getTokenProgramForSpotMarket(spotMarketAccount);
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarketAccount);
const removeStakeIx =
await this.program.instruction.removeInsuranceFundStake(marketIndex, {
accounts: {
Expand Down Expand Up @@ -7022,7 +7031,7 @@ export class DriftClient {

const remainingAccounts = [];
this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
const tokenProgram = getTokenProgramForSpotMarket(spotMarket);
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
const ix = await this.program.instruction.depositIntoSpotMarketRevenuePool(
amount,
{
Expand Down
10 changes: 4 additions & 6 deletions sdk/src/openbook/openbookV2Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ export class OpenbookV2Subscriber implements L2OrderBookGenerator {
this.subscribed = true;
}

public async getBestBid(): Promise<BN | undefined> {
const bids = await this.market.loadBids();
const bestBid = bids.best();
public getBestBid(): BN | undefined {
const bestBid = this.market.bids.best();

if (bestBid === undefined) {
return undefined;
Expand All @@ -106,9 +105,8 @@ export class OpenbookV2Subscriber implements L2OrderBookGenerator {
return new BN(Math.floor(bestBid.price * PRICE_PRECISION.toNumber()));
}

public async getBestAsk(): Promise<BN | undefined> {
const asks = await this.market.loadAsks();
const bestAsk = asks.best();
public getBestAsk(): BN | undefined {
const bestAsk = this.market.asks.best();

if (bestAsk === undefined) {
return undefined;
Expand Down

0 comments on commit 24239ff

Please sign in to comment.