@openbook-dex/openbook-v2
Advanced tools
Comparing version 0.1.10 to 0.2.0
@@ -10,2 +10,3 @@ /// <reference types="bn.js" /> | ||
export type Side = IdlTypes<OpenbookV2>['Side']; | ||
export type SelfTradeBehavior = IdlTypes<OpenbookV2>['SelfTradeBehavior']; | ||
export type PlaceOrderPeggedArgs = IdlTypes<OpenbookV2>['PlaceOrderPeggedArgs']; | ||
@@ -21,2 +22,3 @@ export type PlaceMultipleOrdersArgs = IdlTypes<OpenbookV2>['PlaceMultipleOrdersArgs']; | ||
export type LeafNode = IdlTypes<OpenbookV2>['LeafNode']; | ||
export type InnerNode = IdlTypes<OpenbookV2>['InnerNode']; | ||
export type AnyNode = IdlTypes<OpenbookV2>['AnyNode']; | ||
@@ -32,2 +34,3 @@ export type FillEvent = IdlTypes<OpenbookV2>['FillEvent']; | ||
txConfirmationCommitment?: Commitment; | ||
referrerWallet?: PublicKey; | ||
} | ||
@@ -41,2 +44,3 @@ export declare function nameToString(name: number[]): string; | ||
program: Program<OpenbookV2>; | ||
referrerWallet: PublicKey | undefined; | ||
private readonly idsSource; | ||
@@ -53,9 +57,5 @@ private readonly postSendTxCallback?; | ||
createProgramAccountIx(authority: PublicKey, size: number): Promise<[TransactionInstruction, Signer]>; | ||
deserializeMarketAccount(publicKey: PublicKey): Promise<MarketAccount | null>; | ||
deserializeOpenOrderAccount(publicKey: PublicKey): Promise<OpenOrdersAccount | null>; | ||
deserializeOpenOrdersIndexerAccount(publicKey: PublicKey): Promise<OpenOrdersIndexerAccount | null>; | ||
deserializeEventHeapAccount(publicKey: PublicKey): Promise<EventHeapAccount | null>; | ||
deserializeBookSide(publicKey: PublicKey): Promise<BookSideAccount | null>; | ||
priceData(key: BN): number; | ||
getLeafNodes(bookside: BookSideAccount): LeafNode[]; | ||
createMarketIx(payer: PublicKey, name: string, quoteMint: PublicKey, baseMint: PublicKey, quoteLotSize: BN, baseLotSize: BN, makerFee: BN, takerFee: BN, timeExpiry: BN, oracleA: PublicKey | null, oracleB: PublicKey | null, openOrdersAdmin: PublicKey | null, consumeEventsAdmin: PublicKey | null, closeMarketAdmin: PublicKey | null, oracleConfigParams?: OracleConfigParams, market?: Keypair, collectFeeAdmin?: PublicKey): Promise<[TransactionInstruction[], Signer[]]>; | ||
@@ -74,3 +74,3 @@ closeMarketIx(marketPublicKey: PublicKey, market: MarketAccount, solDestination: PublicKey, closeMarketAdmin?: Keypair | null): Promise<[TransactionInstruction, Signer[]]>; | ||
decodeMarket(data: Buffer): any; | ||
placeOrderIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrderIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrderPeggedIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderPeggedArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
@@ -80,5 +80,5 @@ placeTakeOrderIx(marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrdersIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, openOrdersAdmin: PublicKey | null, orderType: PlaceOrderType, bids: PlaceMultipleOrdersArgs[], asks: PlaceMultipleOrdersArgs[], limit?: number, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderById(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, orderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByClientId(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, clientOrderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelAllOrders(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, limit: number, side: Side | null, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByIdIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, orderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByClientIdIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, clientOrderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelAllOrdersIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, limit: number, side: Side | null, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
closeOpenOrdersIndexerIx(owner: Keypair, market: MarketAccount, openOrdersIndexer?: PublicKey): Promise<[TransactionInstruction, Signer[]]>; | ||
@@ -85,0 +85,0 @@ settleFundsIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, referrerAccount: PublicKey | null, penaltyPayer: PublicKey, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; |
@@ -23,2 +23,3 @@ "use strict"; | ||
program; | ||
referrerWallet; | ||
idsSource; | ||
@@ -42,2 +43,3 @@ postSendTxCallback; | ||
'processed'; | ||
this.referrerWallet = opts.referrerWallet; | ||
// TODO: evil side effect, but limited backtraces are a nightmare | ||
@@ -92,11 +94,2 @@ Error.stackTraceLimit = 1000; | ||
} | ||
// Get the MarketAccount from the market publicKey | ||
async deserializeMarketAccount(publicKey) { | ||
try { | ||
return await this.program.account.market.fetch(publicKey); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
async deserializeOpenOrderAccount(publicKey) { | ||
@@ -126,24 +119,2 @@ try { | ||
} | ||
async deserializeBookSide(publicKey) { | ||
try { | ||
return await this.program.account.bookSide.fetch(publicKey); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
priceData(key) { | ||
const shiftedValue = key.shrn(64); // Shift right by 64 bits | ||
return shiftedValue.toNumber(); // Convert BN to a regular number | ||
} | ||
// Get bids or asks from a bookside account | ||
getLeafNodes(bookside) { | ||
const leafNodesData = bookside.nodes.nodes.filter((x) => x.tag === 2); | ||
const leafNodes = []; | ||
for (const x of leafNodesData) { | ||
const leafNode = this.program.coder.types.decode('LeafNode', Buffer.from([0, ...x.data])); | ||
leafNodes.push(leafNode); | ||
} | ||
return leafNodes; | ||
} | ||
async createMarketIx(payer, name, quoteMint, baseMint, quoteLotSize, baseLotSize, makerFee, takerFee, timeExpiry, oracleA, oracleB, openOrdersAdmin, consumeEventsAdmin, closeMarketAdmin, oracleConfigParams = { | ||
@@ -269,17 +240,16 @@ confFilter: 0.1, | ||
let accountIndex = new anchor_1.BN(1); | ||
if (openOrdersIndexer == null) { | ||
if (openOrdersIndexer == null) | ||
openOrdersIndexer = this.findOpenOrdersIndexer(owner); | ||
try { | ||
const storedIndexer = await this.deserializeOpenOrdersIndexerAccount(openOrdersIndexer); | ||
if (storedIndexer == null) { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
else { | ||
accountIndex = new anchor_1.BN(storedIndexer.createdCounter + 1); | ||
} | ||
} | ||
catch { | ||
try { | ||
const storedIndexer = await this.deserializeOpenOrdersIndexerAccount(openOrdersIndexer); | ||
if (storedIndexer == null) { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
else { | ||
accountIndex = new anchor_1.BN(storedIndexer.createdCounter + 1); | ||
} | ||
} | ||
catch { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
const openOrdersAccount = this.findOpenOrderAtIndex(owner, accountIndex); | ||
@@ -365,3 +335,3 @@ ixs.push(await this.program.methods | ||
} | ||
async placeOrderIx(openOrdersPublicKey, marketPublicKey, market, userTokenAccount, openOrdersAdmin, args, remainingAccounts, openOrdersDelegate) { | ||
async placeOrderIx(openOrdersPublicKey, marketPublicKey, market, userTokenAccount, args, remainingAccounts, openOrdersDelegate) { | ||
const marketVault = args.side === utils_1.SideUtils.Bid | ||
@@ -375,8 +345,9 @@ ? market.marketQuoteVault | ||
})); | ||
const openOrdersAdmin = market.openOrdersAdmin.key.equals(web3_js_1.PublicKey.default) | ||
? null | ||
: market.openOrdersAdmin.key; | ||
const ix = await this.program.methods | ||
.placeOrder(args) | ||
.accounts({ | ||
signer: openOrdersDelegate != null | ||
? openOrdersDelegate.publicKey | ||
: this.walletPk, | ||
signer: openOrdersDelegate?.publicKey ?? this.walletPk, | ||
asks: market.asks, | ||
@@ -531,3 +502,3 @@ bids: market.bids, | ||
} | ||
async cancelOrderById(openOrdersPublicKey, openOrdersAccount, market, orderId, openOrdersDelegate) { | ||
async cancelOrderByIdIx(openOrdersPublicKey, openOrdersAccount, market, orderId, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -549,3 +520,3 @@ .cancelOrder(orderId) | ||
} | ||
async cancelOrderByClientId(openOrdersPublicKey, openOrdersAccount, market, clientOrderId, openOrdersDelegate) { | ||
async cancelOrderByClientIdIx(openOrdersPublicKey, openOrdersAccount, market, clientOrderId, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -567,3 +538,3 @@ .cancelOrderByClientOrderId(clientOrderId) | ||
} | ||
async cancelAllOrders(openOrdersPublicKey, openOrdersAccount, market, limit, side, openOrdersDelegate) { | ||
async cancelAllOrdersIx(openOrdersPublicKey, openOrdersAccount, market, limit, side, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -570,0 +541,0 @@ .cancelAllOrders(side, limit) |
import { IDL, type OpenbookV2 } from './openbook_v2'; | ||
export * from './client'; | ||
export * from './accounts/bookSide'; | ||
export * from './accounts/market'; | ||
export * from './accounts/openOrders'; | ||
export * from './market'; | ||
export * from './utils/utils'; | ||
export * from './utils/watcher'; | ||
export { IDL, type OpenbookV2 }; |
@@ -21,3 +21,7 @@ "use strict"; | ||
__exportStar(require("./client"), exports); | ||
__exportStar(require("./accounts/bookSide"), exports); | ||
__exportStar(require("./accounts/market"), exports); | ||
__exportStar(require("./accounts/openOrders"), exports); | ||
__exportStar(require("./market"), exports); | ||
__exportStar(require("./utils/utils"), exports); | ||
__exportStar(require("./utils/watcher"), exports); |
@@ -11,2 +11,3 @@ "use strict"; | ||
const connection = provider.connection; | ||
const additionalSigners = opts?.additionalSigners || []; | ||
if (connection.banksClient !== undefined) { | ||
@@ -19,3 +20,3 @@ const tx = new web3_js_1.Transaction(); | ||
[tx.recentBlockhash] = await connection.banksClient.getLatestBlockhash(); | ||
for (const signer of opts?.additionalSigners) { | ||
for (const signer of additionalSigners) { | ||
tx.partialSign(signer); | ||
@@ -35,3 +36,3 @@ } | ||
const message = web3_js_1.MessageV0.compile({ | ||
payerKey: provider.wallet.publicKey, | ||
payerKey: payer.publicKey, | ||
instructions: ixs, | ||
@@ -42,5 +43,4 @@ recentBlockhash: latestBlockhash.blockhash, | ||
let vtx = new web3_js_1.VersionedTransaction(message); | ||
if (opts?.additionalSigners !== undefined && | ||
opts?.additionalSigners.length !== 0) { | ||
vtx.sign([...opts?.additionalSigners]); | ||
if (additionalSigners !== undefined && additionalSigners.length !== 0) { | ||
vtx.sign([...additionalSigners]); | ||
} | ||
@@ -58,8 +58,3 @@ if (typeof payer.signTransaction === 'function' && | ||
}); | ||
// const signature = await connection.sendTransactionss( | ||
// vtx as any as VersionedTransaction, | ||
// { | ||
// skipPreflight: true, | ||
// }, | ||
// ); | ||
// console.log(`sent tx base64=${Buffer.from(vtx.serialize()).toString('base64')}`); | ||
if (opts?.postSendTxCallback !== undefined && | ||
@@ -75,6 +70,6 @@ opts?.postSendTxCallback !== null) { | ||
const txConfirmationCommitment = opts?.txConfirmationCommitment ?? 'processed'; | ||
let status; | ||
let result; | ||
if (latestBlockhash.blockhash != null && | ||
latestBlockhash.lastValidBlockHeight != null) { | ||
status = (await connection.confirmTransaction({ | ||
result = (await connection.confirmTransaction({ | ||
signature: signature, | ||
@@ -86,9 +81,9 @@ blockhash: latestBlockhash.blockhash, | ||
else { | ||
status = (await connection.confirmTransaction(signature, txConfirmationCommitment)).value; | ||
result = (await connection.confirmTransaction(signature, txConfirmationCommitment)).value; | ||
} | ||
if (status.err !== '' && status.err !== null) { | ||
console.warn('Tx status: ', status); | ||
if (result.err !== '' && result.err !== null) { | ||
console.warn('Tx failed result: ', result); | ||
throw new OpenBookError({ | ||
txid: signature, | ||
message: `${JSON.stringify(status)}`, | ||
message: `${JSON.stringify(result)}`, | ||
}); | ||
@@ -95,0 +90,0 @@ } |
import { PublicKey, TransactionInstruction } from '@solana/web3.js'; | ||
import BN from 'bn.js'; | ||
import { OpenBookV2Client } from '..'; | ||
export declare const SideUtils: { | ||
@@ -11,3 +12,3 @@ Bid: { | ||
}; | ||
export declare const OrderType: { | ||
export declare const PlaceOrderTypeUtils: { | ||
Limit: { | ||
@@ -29,3 +30,3 @@ limit: {}; | ||
}; | ||
export declare const SelfTradeBehavior: { | ||
export declare const SelfTradeBehaviorUtils: { | ||
DecrementTake: { | ||
@@ -43,2 +44,3 @@ decrementTake: {}; | ||
export declare const I64_MAX_BN: BN; | ||
export declare const ORDER_FEE_UNIT: BN; | ||
export declare function bpsToDecimal(bps: number): number; | ||
@@ -61,1 +63,4 @@ export declare function percentageToDecimal(percentage: number): number; | ||
export declare function createAssociatedTokenAccountIdempotentInstruction(payer: PublicKey, owner: PublicKey, mint: PublicKey): Promise<TransactionInstruction>; | ||
export declare function initReadOnlyOpenbookClient(): OpenBookV2Client; | ||
export declare function initOpenbookClient(): OpenBookV2Client; | ||
export declare function sleep(ms: number): Promise<unknown>; |
@@ -6,6 +6,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createAssociatedTokenAccountIdempotentInstruction = exports.getAssociatedTokenAddress = exports.toUiDecimals = exports.toNative = exports.percentageToDecimal = exports.bpsToDecimal = exports.I64_MAX_BN = exports.U64_MAX_BN = exports.SelfTradeBehavior = exports.OrderType = exports.SideUtils = void 0; | ||
exports.sleep = exports.initOpenbookClient = exports.initReadOnlyOpenbookClient = exports.createAssociatedTokenAccountIdempotentInstruction = exports.getAssociatedTokenAddress = exports.toUiDecimals = exports.toNative = exports.percentageToDecimal = exports.bpsToDecimal = exports.ORDER_FEE_UNIT = exports.I64_MAX_BN = exports.U64_MAX_BN = exports.SelfTradeBehaviorUtils = exports.PlaceOrderTypeUtils = exports.SideUtils = void 0; | ||
const web3_js_1 = require("@solana/web3.js"); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const spl_token_1 = require("@solana/spl-token"); | ||
const __1 = require(".."); | ||
const anchor_1 = require("@coral-xyz/anchor"); | ||
exports.SideUtils = { | ||
@@ -15,3 +17,3 @@ Bid: { bid: {} }, | ||
}; | ||
exports.OrderType = { | ||
exports.PlaceOrderTypeUtils = { | ||
Limit: { limit: {} }, | ||
@@ -23,3 +25,3 @@ ImmediateOrCancel: { immediateOrCancel: {} }, | ||
}; | ||
exports.SelfTradeBehavior = { | ||
exports.SelfTradeBehaviorUtils = { | ||
DecrementTake: { decrementTake: {} }, | ||
@@ -34,2 +36,3 @@ CancelProvide: { cancelProvide: {} }, | ||
exports.I64_MAX_BN = new bn_js_1.default('9223372036854775807').toTwos(64); | ||
exports.ORDER_FEE_UNIT = new bn_js_1.default(1e6); | ||
function bpsToDecimal(bps) { | ||
@@ -44,3 +47,3 @@ return bps / 10000; | ||
function toNative(uiAmount, decimals) { | ||
return new bn_js_1.default((uiAmount * Math.pow(10, decimals)).toFixed(0)); | ||
return new bn_js_1.default(Math.round(uiAmount * Math.pow(10, decimals))); | ||
} | ||
@@ -94,1 +97,21 @@ exports.toNative = toNative; | ||
exports.createAssociatedTokenAccountIdempotentInstruction = createAssociatedTokenAccountIdempotentInstruction; | ||
function initReadOnlyOpenbookClient() { | ||
const conn = new web3_js_1.Connection(process.env.SOL_RPC_URL); | ||
const stubWallet = new anchor_1.Wallet(web3_js_1.Keypair.generate()); | ||
const provider = new anchor_1.AnchorProvider(conn, stubWallet, {}); | ||
return new __1.OpenBookV2Client(provider); | ||
} | ||
exports.initReadOnlyOpenbookClient = initReadOnlyOpenbookClient; | ||
function initOpenbookClient() { | ||
const conn = new web3_js_1.Connection(process.env.SOL_RPC_URL, 'processed'); | ||
const wallet = new anchor_1.Wallet(web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(process.env.KEYPAIR)))); | ||
const provider = new anchor_1.AnchorProvider(conn, wallet, {}); | ||
return new __1.OpenBookV2Client(provider, undefined, { | ||
prioritizationFee: 10_000, | ||
}); | ||
} | ||
exports.initOpenbookClient = initOpenbookClient; | ||
function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
exports.sleep = sleep; |
@@ -19,2 +19,3 @@ import { BN, Program, } from '@coral-xyz/anchor'; | ||
program; | ||
referrerWallet; | ||
idsSource; | ||
@@ -38,2 +39,3 @@ postSendTxCallback; | ||
'processed'; | ||
this.referrerWallet = opts.referrerWallet; | ||
// TODO: evil side effect, but limited backtraces are a nightmare | ||
@@ -88,11 +90,2 @@ Error.stackTraceLimit = 1000; | ||
} | ||
// Get the MarketAccount from the market publicKey | ||
async deserializeMarketAccount(publicKey) { | ||
try { | ||
return await this.program.account.market.fetch(publicKey); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
async deserializeOpenOrderAccount(publicKey) { | ||
@@ -122,24 +115,2 @@ try { | ||
} | ||
async deserializeBookSide(publicKey) { | ||
try { | ||
return await this.program.account.bookSide.fetch(publicKey); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
priceData(key) { | ||
const shiftedValue = key.shrn(64); // Shift right by 64 bits | ||
return shiftedValue.toNumber(); // Convert BN to a regular number | ||
} | ||
// Get bids or asks from a bookside account | ||
getLeafNodes(bookside) { | ||
const leafNodesData = bookside.nodes.nodes.filter((x) => x.tag === 2); | ||
const leafNodes = []; | ||
for (const x of leafNodesData) { | ||
const leafNode = this.program.coder.types.decode('LeafNode', Buffer.from([0, ...x.data])); | ||
leafNodes.push(leafNode); | ||
} | ||
return leafNodes; | ||
} | ||
async createMarketIx(payer, name, quoteMint, baseMint, quoteLotSize, baseLotSize, makerFee, takerFee, timeExpiry, oracleA, oracleB, openOrdersAdmin, consumeEventsAdmin, closeMarketAdmin, oracleConfigParams = { | ||
@@ -265,17 +236,16 @@ confFilter: 0.1, | ||
let accountIndex = new BN(1); | ||
if (openOrdersIndexer == null) { | ||
if (openOrdersIndexer == null) | ||
openOrdersIndexer = this.findOpenOrdersIndexer(owner); | ||
try { | ||
const storedIndexer = await this.deserializeOpenOrdersIndexerAccount(openOrdersIndexer); | ||
if (storedIndexer == null) { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
else { | ||
accountIndex = new BN(storedIndexer.createdCounter + 1); | ||
} | ||
} | ||
catch { | ||
try { | ||
const storedIndexer = await this.deserializeOpenOrdersIndexerAccount(openOrdersIndexer); | ||
if (storedIndexer == null) { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
else { | ||
accountIndex = new BN(storedIndexer.createdCounter + 1); | ||
} | ||
} | ||
catch { | ||
ixs.push(await this.createOpenOrdersIndexerIx(openOrdersIndexer, owner)); | ||
} | ||
const openOrdersAccount = this.findOpenOrderAtIndex(owner, accountIndex); | ||
@@ -361,3 +331,3 @@ ixs.push(await this.program.methods | ||
} | ||
async placeOrderIx(openOrdersPublicKey, marketPublicKey, market, userTokenAccount, openOrdersAdmin, args, remainingAccounts, openOrdersDelegate) { | ||
async placeOrderIx(openOrdersPublicKey, marketPublicKey, market, userTokenAccount, args, remainingAccounts, openOrdersDelegate) { | ||
const marketVault = args.side === SideUtils.Bid | ||
@@ -371,8 +341,9 @@ ? market.marketQuoteVault | ||
})); | ||
const openOrdersAdmin = market.openOrdersAdmin.key.equals(PublicKey.default) | ||
? null | ||
: market.openOrdersAdmin.key; | ||
const ix = await this.program.methods | ||
.placeOrder(args) | ||
.accounts({ | ||
signer: openOrdersDelegate != null | ||
? openOrdersDelegate.publicKey | ||
: this.walletPk, | ||
signer: openOrdersDelegate?.publicKey ?? this.walletPk, | ||
asks: market.asks, | ||
@@ -527,3 +498,3 @@ bids: market.bids, | ||
} | ||
async cancelOrderById(openOrdersPublicKey, openOrdersAccount, market, orderId, openOrdersDelegate) { | ||
async cancelOrderByIdIx(openOrdersPublicKey, openOrdersAccount, market, orderId, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -545,3 +516,3 @@ .cancelOrder(orderId) | ||
} | ||
async cancelOrderByClientId(openOrdersPublicKey, openOrdersAccount, market, clientOrderId, openOrdersDelegate) { | ||
async cancelOrderByClientIdIx(openOrdersPublicKey, openOrdersAccount, market, clientOrderId, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -563,3 +534,3 @@ .cancelOrderByClientOrderId(clientOrderId) | ||
} | ||
async cancelAllOrders(openOrdersPublicKey, openOrdersAccount, market, limit, side, openOrdersDelegate) { | ||
async cancelAllOrdersIx(openOrdersPublicKey, openOrdersAccount, market, limit, side, openOrdersDelegate) { | ||
const ix = await this.program.methods | ||
@@ -566,0 +537,0 @@ .cancelAllOrders(side, limit) |
import { IDL } from './openbook_v2'; | ||
export * from './client'; | ||
export * from './accounts/bookSide'; | ||
export * from './accounts/market'; | ||
export * from './accounts/openOrders'; | ||
export * from './market'; | ||
export * from './utils/utils'; | ||
export * from './utils/watcher'; | ||
export { IDL }; |
@@ -5,2 +5,3 @@ import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet'; | ||
const connection = provider.connection; | ||
const additionalSigners = opts?.additionalSigners || []; | ||
if (connection.banksClient !== undefined) { | ||
@@ -13,3 +14,3 @@ const tx = new Transaction(); | ||
[tx.recentBlockhash] = await connection.banksClient.getLatestBlockhash(); | ||
for (const signer of opts?.additionalSigners) { | ||
for (const signer of additionalSigners) { | ||
tx.partialSign(signer); | ||
@@ -29,3 +30,3 @@ } | ||
const message = MessageV0.compile({ | ||
payerKey: provider.wallet.publicKey, | ||
payerKey: payer.publicKey, | ||
instructions: ixs, | ||
@@ -36,5 +37,4 @@ recentBlockhash: latestBlockhash.blockhash, | ||
let vtx = new VersionedTransaction(message); | ||
if (opts?.additionalSigners !== undefined && | ||
opts?.additionalSigners.length !== 0) { | ||
vtx.sign([...opts?.additionalSigners]); | ||
if (additionalSigners !== undefined && additionalSigners.length !== 0) { | ||
vtx.sign([...additionalSigners]); | ||
} | ||
@@ -52,8 +52,3 @@ if (typeof payer.signTransaction === 'function' && | ||
}); | ||
// const signature = await connection.sendTransactionss( | ||
// vtx as any as VersionedTransaction, | ||
// { | ||
// skipPreflight: true, | ||
// }, | ||
// ); | ||
// console.log(`sent tx base64=${Buffer.from(vtx.serialize()).toString('base64')}`); | ||
if (opts?.postSendTxCallback !== undefined && | ||
@@ -69,6 +64,6 @@ opts?.postSendTxCallback !== null) { | ||
const txConfirmationCommitment = opts?.txConfirmationCommitment ?? 'processed'; | ||
let status; | ||
let result; | ||
if (latestBlockhash.blockhash != null && | ||
latestBlockhash.lastValidBlockHeight != null) { | ||
status = (await connection.confirmTransaction({ | ||
result = (await connection.confirmTransaction({ | ||
signature: signature, | ||
@@ -80,9 +75,9 @@ blockhash: latestBlockhash.blockhash, | ||
else { | ||
status = (await connection.confirmTransaction(signature, txConfirmationCommitment)).value; | ||
result = (await connection.confirmTransaction(signature, txConfirmationCommitment)).value; | ||
} | ||
if (status.err !== '' && status.err !== null) { | ||
console.warn('Tx status: ', status); | ||
if (result.err !== '' && result.err !== null) { | ||
console.warn('Tx failed result: ', result); | ||
throw new OpenBookError({ | ||
txid: signature, | ||
message: `${JSON.stringify(status)}`, | ||
message: `${JSON.stringify(result)}`, | ||
}); | ||
@@ -89,0 +84,0 @@ } |
@@ -1,4 +0,6 @@ | ||
import { PublicKey, SystemProgram, TransactionInstruction, } from '@solana/web3.js'; | ||
import { Connection, Keypair, PublicKey, SystemProgram, TransactionInstruction, } from '@solana/web3.js'; | ||
import BN from 'bn.js'; | ||
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, } from '@solana/spl-token'; | ||
import { OpenBookV2Client } from '..'; | ||
import { AnchorProvider, Wallet } from '@coral-xyz/anchor'; | ||
export const SideUtils = { | ||
@@ -8,3 +10,3 @@ Bid: { bid: {} }, | ||
}; | ||
export const OrderType = { | ||
export const PlaceOrderTypeUtils = { | ||
Limit: { limit: {} }, | ||
@@ -16,3 +18,3 @@ ImmediateOrCancel: { immediateOrCancel: {} }, | ||
}; | ||
export const SelfTradeBehavior = { | ||
export const SelfTradeBehaviorUtils = { | ||
DecrementTake: { decrementTake: {} }, | ||
@@ -27,2 +29,3 @@ CancelProvide: { cancelProvide: {} }, | ||
export const I64_MAX_BN = new BN('9223372036854775807').toTwos(64); | ||
export const ORDER_FEE_UNIT = new BN(1e6); | ||
export function bpsToDecimal(bps) { | ||
@@ -35,3 +38,3 @@ return bps / 10000; | ||
export function toNative(uiAmount, decimals) { | ||
return new BN((uiAmount * Math.pow(10, decimals)).toFixed(0)); | ||
return new BN(Math.round(uiAmount * Math.pow(10, decimals))); | ||
} | ||
@@ -81,1 +84,18 @@ export function toUiDecimals(nativeAmount, decimals) { | ||
} | ||
export function initReadOnlyOpenbookClient() { | ||
const conn = new Connection(process.env.SOL_RPC_URL); | ||
const stubWallet = new Wallet(Keypair.generate()); | ||
const provider = new AnchorProvider(conn, stubWallet, {}); | ||
return new OpenBookV2Client(provider); | ||
} | ||
export function initOpenbookClient() { | ||
const conn = new Connection(process.env.SOL_RPC_URL, 'processed'); | ||
const wallet = new Wallet(Keypair.fromSecretKey(Uint8Array.from(JSON.parse(process.env.KEYPAIR)))); | ||
const provider = new AnchorProvider(conn, wallet, {}); | ||
return new OpenBookV2Client(provider, undefined, { | ||
prioritizationFee: 10_000, | ||
}); | ||
} | ||
export function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |
@@ -10,2 +10,3 @@ /// <reference types="bn.js" /> | ||
export type Side = IdlTypes<OpenbookV2>['Side']; | ||
export type SelfTradeBehavior = IdlTypes<OpenbookV2>['SelfTradeBehavior']; | ||
export type PlaceOrderPeggedArgs = IdlTypes<OpenbookV2>['PlaceOrderPeggedArgs']; | ||
@@ -21,2 +22,3 @@ export type PlaceMultipleOrdersArgs = IdlTypes<OpenbookV2>['PlaceMultipleOrdersArgs']; | ||
export type LeafNode = IdlTypes<OpenbookV2>['LeafNode']; | ||
export type InnerNode = IdlTypes<OpenbookV2>['InnerNode']; | ||
export type AnyNode = IdlTypes<OpenbookV2>['AnyNode']; | ||
@@ -32,2 +34,3 @@ export type FillEvent = IdlTypes<OpenbookV2>['FillEvent']; | ||
txConfirmationCommitment?: Commitment; | ||
referrerWallet?: PublicKey; | ||
} | ||
@@ -41,2 +44,3 @@ export declare function nameToString(name: number[]): string; | ||
program: Program<OpenbookV2>; | ||
referrerWallet: PublicKey | undefined; | ||
private readonly idsSource; | ||
@@ -53,9 +57,5 @@ private readonly postSendTxCallback?; | ||
createProgramAccountIx(authority: PublicKey, size: number): Promise<[TransactionInstruction, Signer]>; | ||
deserializeMarketAccount(publicKey: PublicKey): Promise<MarketAccount | null>; | ||
deserializeOpenOrderAccount(publicKey: PublicKey): Promise<OpenOrdersAccount | null>; | ||
deserializeOpenOrdersIndexerAccount(publicKey: PublicKey): Promise<OpenOrdersIndexerAccount | null>; | ||
deserializeEventHeapAccount(publicKey: PublicKey): Promise<EventHeapAccount | null>; | ||
deserializeBookSide(publicKey: PublicKey): Promise<BookSideAccount | null>; | ||
priceData(key: BN): number; | ||
getLeafNodes(bookside: BookSideAccount): LeafNode[]; | ||
createMarketIx(payer: PublicKey, name: string, quoteMint: PublicKey, baseMint: PublicKey, quoteLotSize: BN, baseLotSize: BN, makerFee: BN, takerFee: BN, timeExpiry: BN, oracleA: PublicKey | null, oracleB: PublicKey | null, openOrdersAdmin: PublicKey | null, consumeEventsAdmin: PublicKey | null, closeMarketAdmin: PublicKey | null, oracleConfigParams?: OracleConfigParams, market?: Keypair, collectFeeAdmin?: PublicKey): Promise<[TransactionInstruction[], Signer[]]>; | ||
@@ -74,3 +74,3 @@ closeMarketIx(marketPublicKey: PublicKey, market: MarketAccount, solDestination: PublicKey, closeMarketAdmin?: Keypair | null): Promise<[TransactionInstruction, Signer[]]>; | ||
decodeMarket(data: Buffer): any; | ||
placeOrderIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrderIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrderPeggedIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userTokenAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderPeggedArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
@@ -80,5 +80,5 @@ placeTakeOrderIx(marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, openOrdersAdmin: PublicKey | null, args: PlaceOrderArgs, remainingAccounts: PublicKey[], openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
placeOrdersIx(openOrdersPublicKey: PublicKey, marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, openOrdersAdmin: PublicKey | null, orderType: PlaceOrderType, bids: PlaceMultipleOrdersArgs[], asks: PlaceMultipleOrdersArgs[], limit?: number, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderById(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, orderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByClientId(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, clientOrderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelAllOrders(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, limit: number, side: Side | null, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByIdIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, orderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelOrderByClientIdIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, clientOrderId: BN, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
cancelAllOrdersIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, market: MarketAccount, limit: number, side: Side | null, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; | ||
closeOpenOrdersIndexerIx(owner: Keypair, market: MarketAccount, openOrdersIndexer?: PublicKey): Promise<[TransactionInstruction, Signer[]]>; | ||
@@ -85,0 +85,0 @@ settleFundsIx(openOrdersPublicKey: PublicKey, openOrdersAccount: OpenOrdersAccount, marketPublicKey: PublicKey, market: MarketAccount, userBaseAccount: PublicKey, userQuoteAccount: PublicKey, referrerAccount: PublicKey | null, penaltyPayer: PublicKey, openOrdersDelegate?: Keypair): Promise<[TransactionInstruction, Signer[]]>; |
import { IDL, type OpenbookV2 } from './openbook_v2'; | ||
export * from './client'; | ||
export * from './accounts/bookSide'; | ||
export * from './accounts/market'; | ||
export * from './accounts/openOrders'; | ||
export * from './market'; | ||
export * from './utils/utils'; | ||
export * from './utils/watcher'; | ||
export { IDL, type OpenbookV2 }; | ||
//# sourceMappingURL=index.d.ts.map |
import { PublicKey, TransactionInstruction } from '@solana/web3.js'; | ||
import BN from 'bn.js'; | ||
import { OpenBookV2Client } from '..'; | ||
export declare const SideUtils: { | ||
@@ -11,3 +12,3 @@ Bid: { | ||
}; | ||
export declare const OrderType: { | ||
export declare const PlaceOrderTypeUtils: { | ||
Limit: { | ||
@@ -29,3 +30,3 @@ limit: {}; | ||
}; | ||
export declare const SelfTradeBehavior: { | ||
export declare const SelfTradeBehaviorUtils: { | ||
DecrementTake: { | ||
@@ -43,2 +44,3 @@ decrementTake: {}; | ||
export declare const I64_MAX_BN: BN; | ||
export declare const ORDER_FEE_UNIT: BN; | ||
export declare function bpsToDecimal(bps: number): number; | ||
@@ -61,2 +63,5 @@ export declare function percentageToDecimal(percentage: number): number; | ||
export declare function createAssociatedTokenAccountIdempotentInstruction(payer: PublicKey, owner: PublicKey, mint: PublicKey): Promise<TransactionInstruction>; | ||
export declare function initReadOnlyOpenbookClient(): OpenBookV2Client; | ||
export declare function initOpenbookClient(): OpenBookV2Client; | ||
export declare function sleep(ms: number): Promise<unknown>; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@openbook-dex/openbook-v2", | ||
"version": "0.1.10", | ||
"version": "0.2.0", | ||
"description": "Typescript Client for openbook-v2 program.", | ||
@@ -34,3 +34,3 @@ "repository": "https://github.com/openbook-dex/openbook-v2/", | ||
"@coral-xyz/anchor": "^0.28.1-beta.2", | ||
"@solana/spl-token": "0.3.8", | ||
"@solana/spl-token": "^0.4.0", | ||
"@solana/web3.js": "^1.77.3", | ||
@@ -55,2 +55,3 @@ "big.js": "^6.2.1" | ||
"ts-mocha": "^10.0.0", | ||
"ts-node": "^10.9.2", | ||
"typescript": "*" | ||
@@ -57,0 +58,0 @@ }, |
@@ -28,5 +28,5 @@ # OpenBook V2 | ||
| ---- | ------- | ------------------------------------------- | | ||
| v1.1 | mainnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
| v1.1 | devnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
| v1.1 | testnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
| v1.7 | mainnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
| v1.7 | devnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
| v1.7 | testnet | opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb | | ||
@@ -107,1 +107,10 @@ ## Building & testing | ||
``` | ||
### TS Testing | ||
```bash | ||
export SOL_RPC_URL=https://a.b.c | ||
export KEYPAIR="[1,2,3,4,...]" | ||
yarn ts/client/src/test/market.ts | ||
yarn ts/client/src/test/openOrders.ts | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
730497
73
19079
114
17
7
1
+ Added@solana/codecs@2.0.0-rc.1(transitive)
+ Added@solana/codecs-core@2.0.0-rc.1(transitive)
+ Added@solana/codecs-data-structures@2.0.0-rc.1(transitive)
+ Added@solana/codecs-numbers@2.0.0-rc.1(transitive)
+ Added@solana/codecs-strings@2.0.0-rc.1(transitive)
+ Added@solana/errors@2.0.0-rc.1(transitive)
+ Added@solana/options@2.0.0-rc.1(transitive)
+ Added@solana/spl-token@0.4.9(transitive)
+ Added@solana/spl-token-group@0.0.7(transitive)
+ Added@solana/spl-token-metadata@0.1.6(transitive)
+ Addedchalk@5.3.0(transitive)
+ Addedcommander@12.1.0(transitive)
+ Addedfastestsmallesttextencoderdecoder@1.0.22(transitive)
+ Addedjayson@4.1.2(transitive)
+ Addedtypescript@5.7.2(transitive)
- Removed@solana/spl-token@0.3.8(transitive)
- Removedjayson@4.1.3(transitive)
Updated@solana/spl-token@^0.4.0