Skip to content

Commit

Permalink
sdk: place and take + bracket order updates (#809)
Browse files Browse the repository at this point in the history
* changes for bracket orders and placeandtake

* updates

* remove comment + update changelog

* separate functions

* prettify
  • Loading branch information
lowkeynicc authored Jan 6, 2024
1 parent 1e2d0ab commit 4fd8932
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 89 deletions.
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>(),
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

0 comments on commit 4fd8932

Please sign in to comment.