Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: place and take + bracket order updates #809

Merged
merged 6 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Features
- sdk: move bracket orders into single instruction
- sdk: add ability to do placeAndTake order with bracket orders attached
- sdk: add option to cancel existing orders in market for place and take order

- program: increase full perp liquidation threshold ([#807](https://github.com/drift-labs/protocol-v2/pull/807))
- program: remove spot fee pool transfer ([#800](https://github.com/drift-labs/protocol-v2/pull/800))
Expand Down
90 changes: 71 additions & 19 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2573,21 +2573,12 @@ export class DriftClient {
}> {
const marketIndex = orderParams.marketIndex;
const orderId = userAccount.nextOrderId;
const bracketOrderIxs = [];

const placePerpOrderIx = await this.getPlacePerpOrderIx(
orderParams,
const ordersIx = await this.getPlaceOrdersIx(
[orderParams, ...bracketOrdersParams],
userAccount.subAccountId
);

for (const bracketOrderParams of bracketOrdersParams) {
const placeBracketOrderIx = await this.getPlacePerpOrderIx(
bracketOrderParams,
userAccount.subAccountId
);
bracketOrderIxs.push(placeBracketOrderIx);
}

let cancelOrdersIx: TransactionInstruction;
let cancelExistingOrdersTx: Transaction;
if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
Expand All @@ -2609,7 +2600,7 @@ export class DriftClient {
// use versioned transactions if there is a lookup table account and wallet is compatible
if (this.txVersion === 0) {
const versionedMarketOrderTx = await this.buildTransaction(
[placePerpOrderIx].concat(bracketOrderIxs),
ordersIx,
txParams,
0
);
Expand Down Expand Up @@ -2658,15 +2649,11 @@ export class DriftClient {
};
} else {
const marketOrderTx = wrapInTx(
placePerpOrderIx,
ordersIx,
txParams?.computeUnits,
txParams?.computeUnitsPrice
);

if (bracketOrderIxs.length > 0) {
marketOrderTx.add(...bracketOrderIxs);
}

// Apply the latest blockhash to the txs so that we can sign before sending them
const currentBlockHash = (
await this.connection.getLatestBlockhash('finalized')
Expand Down Expand Up @@ -3093,7 +3080,7 @@ export class DriftClient {
}

public async getPlaceOrdersIx(
params: OrderParams[],
params: OptionalOrderParams[],
subAccountId?: number
): Promise<TransactionInstruction> {
const user = await this.getUserAccountPublicKey(subAccountId);
Expand All @@ -3118,7 +3105,9 @@ export class DriftClient {
useMarketLastSlotCache: true,
});

return await this.program.instruction.placeOrders(params, {
const formattedParams = params.map((item) => getOrderParams(item));

return await this.program.instruction.placeOrders(formattedParams, {
accounts: {
state: await this.getStatePublicKey(),
user,
Expand Down Expand Up @@ -4334,6 +4323,69 @@ export class DriftClient {
return txSig;
}

public async placeAndTakePerpWithAdditionalOrders(
orderParams: OptionalOrderParams,
makerInfo?: MakerInfo | MakerInfo[],
referrerInfo?: ReferrerInfo,
bracketOrdersParams = new Array<OptionalOrderParams>(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have this in a separate method to avoid making a breaking change? also idt bot users will use the bracket orders/cancel exisitng orders so its nice to keep it simple

txParams?: TxParams,
subAccountId?: number,
cancelExistingOrders?: boolean
): Promise<{
txSig: TransactionSignature;
signedCancelExistingOrdersTx?: Transaction;
}> {
let signedCancelExistingOrdersTx: Transaction;

if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
const cancelOrdersIx = await this.getCancelOrdersIx(
orderParams.marketType,
orderParams.marketIndex,
null,
subAccountId
);

const cancelExistingOrdersTx = await this.buildTransaction(
[cancelOrdersIx],
txParams,
this.txVersion
);

// @ts-ignore
signedCancelExistingOrdersTx = await this.provider.wallet.signTransaction(
cancelExistingOrdersTx
);
}

const ixs = [];

const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(
orderParams,
makerInfo,
referrerInfo,
subAccountId
);

ixs.push(placeAndTakeIx);

if (bracketOrdersParams.length > 0) {
const bracketOrdersIx = await this.getPlaceOrdersIx(
bracketOrdersParams,
subAccountId
);
ixs.push(bracketOrdersIx);
}

const { txSig, slot } = await this.sendTransaction(
await this.buildTransaction(ixs, txParams),
[],
this.opts
);
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);

return { txSig, signedCancelExistingOrdersTx };
}

public async getPlaceAndTakePerpOrderIx(
orderParams: OptionalOrderParams,
makerInfo?: MakerInfo | MakerInfo[],
Expand Down
Loading
Loading