Skip to content

Commit

Permalink
sdk: cancel existing orders tx feature (#678)
Browse files Browse the repository at this point in the history
* sdk: cancel existing orders tx feature

* sdk: divide by zero bugfix

* sdk: fix cancel existing orders feedback
  • Loading branch information
evanpipta authored Nov 8, 2023
1 parent 196595b commit ca38571
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 38 deletions.
61 changes: 49 additions & 12 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,7 @@ export class DriftClient {
* @param makerInfo
* @param txParams
* @param bracketOrdersParams
* @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
* @returns
*/
public async sendMarketOrderAndGetSignedFillTx(
Expand All @@ -2457,8 +2458,13 @@ export class DriftClient {
makerInfo?: MakerInfo | MakerInfo[],
txParams?: TxParams,
bracketOrdersParams = new Array<OptionalOrderParams>(),
referrerInfo?: ReferrerInfo
): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> {
referrerInfo?: ReferrerInfo,
cancelExistingOrders?: boolean
): Promise<{
txSig: TransactionSignature;
signedFillTx: Transaction;
signedCancelExistingOrdersTx?: Transaction;
}> {
const marketIndex = orderParams.marketIndex;
const orderId = userAccount.nextOrderId;
const bracketOrderIxs = [];
Expand All @@ -2483,6 +2489,23 @@ export class DriftClient {
referrerInfo
);

let cancelOrdersIx: TransactionInstruction;
let cancelExistingOrdersTx: Transaction;
if (cancelExistingOrders) {
cancelOrdersIx = await this.getCancelOrdersIx(
orderParams.marketType,
orderParams.marketIndex,
null
);

//@ts-ignore
cancelExistingOrdersTx = await this.buildTransaction(
[cancelOrdersIx],
txParams,
this.txVersion
);
}

// use versioned transactions if there is a lookup table account and wallet is compatible
if (this.txVersion === 0) {
const versionedMarketOrderTx = await this.buildTransaction(
Expand All @@ -2495,21 +2518,31 @@ export class DriftClient {
txParams,
0
);
const [signedVersionedMarketOrderTx, signedVersionedFillTx] =
await this.provider.wallet.signAllTransactions([
//@ts-ignore

const [
signedVersionedMarketOrderTx,
signedVersionedFillTx,
signedCancelExistingOrdersTx,
] = await this.provider.wallet.signAllTransactions(
[
versionedMarketOrderTx,
//@ts-ignore
versionedFillTx,
]);
cancelExistingOrdersTx,
].filter((tx) => tx !== undefined)
);
const { txSig, slot } = await this.txSender.sendRawTransaction(
signedVersionedMarketOrderTx.serialize(),
this.opts
);
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);

// @ts-ignore
return { txSig, signedFillTx: signedVersionedFillTx };
return {
txSig,
// @ts-ignore
signedFillTx: signedVersionedFillTx,
// @ts-ignore
signedCancelExistingOrdersTx,
};
} else {
const marketOrderTx = wrapInTx(
placePerpOrderIx,
Expand Down Expand Up @@ -2537,8 +2570,12 @@ export class DriftClient {
marketOrderTx.feePayer = userAccount.authority;
fillTx.feePayer = userAccount.authority;

const [signedMarketOrderTx, signedFillTx] =
await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx]);
const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] =
await this.provider.wallet.signAllTransactions(
[marketOrderTx, fillTx, cancelExistingOrdersTx].filter(
(tx) => tx !== undefined
)
);
const { txSig, slot } = await this.sendTransaction(
signedMarketOrderTx,
[],
Expand All @@ -2547,7 +2584,7 @@ export class DriftClient {
);
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);

return { txSig, signedFillTx };
return { txSig, signedFillTx, signedCancelExistingOrdersTx };
}
}

Expand Down
52 changes: 26 additions & 26 deletions sdk/src/math/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,15 @@ export function calculateEstimatedPerpEntryPrice(
}
}

const entryPrice = cumulativeQuoteFilled
.mul(BASE_PRECISION)
.div(cumulativeBaseFilled);
const entryPrice =
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
? cumulativeQuoteFilled.mul(BASE_PRECISION).div(cumulativeBaseFilled)
: ZERO;

const priceImpact = entryPrice
.sub(bestPrice)
.mul(PRICE_PRECISION)
.div(bestPrice)
.abs();
const priceImpact =
bestPrice && bestPrice.gt(ZERO)
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
: ZERO;

return {
entryPrice,
Expand Down Expand Up @@ -858,15 +858,15 @@ export function calculateEstimatedSpotEntryPrice(
}
}

const entryPrice = cumulativeQuoteFilled
.mul(basePrecision)
.div(cumulativeBaseFilled);
const entryPrice =
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
: ZERO;

const priceImpact = entryPrice
.sub(bestPrice)
.mul(PRICE_PRECISION)
.div(bestPrice)
.abs();
const priceImpact =
bestPrice && bestPrice.gt(ZERO)
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
: ZERO;

return {
entryPrice,
Expand Down Expand Up @@ -900,8 +900,8 @@ export function calculateEstimatedEntryPriceWithL2(
const levels = [...(takerIsLong ? l2.asks : l2.bids)];
let nextLevel = levels.shift();

let bestPrice;
let worstPrice;
let bestPrice: BN;
let worstPrice: BN;
if (nextLevel) {
bestPrice = nextLevel.price;
worstPrice = nextLevel.price;
Expand Down Expand Up @@ -945,15 +945,15 @@ export function calculateEstimatedEntryPriceWithL2(
}
}

const entryPrice = cumulativeQuoteFilled
.mul(basePrecision)
.div(cumulativeBaseFilled);
const entryPrice =
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
: ZERO;

const priceImpact = entryPrice
.sub(bestPrice)
.mul(PRICE_PRECISION)
.div(bestPrice)
.abs();
const priceImpact =
bestPrice && bestPrice.gt(ZERO)
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
: ZERO;

return {
entryPrice,
Expand Down

0 comments on commit ca38571

Please sign in to comment.