Skip to content

Commit

Permalink
Self trade behaviour (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipzeta authored Aug 26, 2024
1 parent a29f3d2 commit 0135937
Show file tree
Hide file tree
Showing 5 changed files with 669 additions and 13 deletions.
24 changes: 14 additions & 10 deletions src/cross-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ export class CrossClient {

let tifOffsetToUse = utils.getTIFOffset(market, options.tifOptions);

let orderIx = instructions.placePerpOrderV4Ix(
let orderIx = instructions.placePerpOrderV5Ix(
asset,
price,
size,
Expand All @@ -1419,7 +1419,8 @@ export class CrossClient {
this.accountAddress,
this._provider.wallet.publicKey,
openOrdersPda,
this._whitelistTradingFeesAddress
this._whitelistTradingFeesAddress,
options.selfTradeBehavior
);

tx.add(orderIx);
Expand Down Expand Up @@ -1737,7 +1738,7 @@ export class CrossClient {

let txs = this.createCancelTriggerOrdersTxs(triggerOrderIndexes);

let placeIx = instructions.placePerpOrderV4Ix(
let placeIx = instructions.placePerpOrderV5Ix(
asset,
price,
size,
Expand All @@ -1752,7 +1753,8 @@ export class CrossClient {
this.accountAddress,
this._provider.wallet.publicKey,
this._openOrdersAccounts[assetToIndex(asset)],
this._whitelistTradingFeesAddress
this._whitelistTradingFeesAddress,
options.selfTradeBehavior
);

// Edge case where user has 0 trigger orders
Expand Down Expand Up @@ -2274,7 +2276,7 @@ export class CrossClient {
let market = Exchange.getPerpMarket(asset);
let tifOffset = utils.getTIFOffset(market, options.tifOptions);

return instructions.placePerpOrderV4Ix(
return instructions.placePerpOrderV5Ix(
asset,
price,
size,
Expand All @@ -2289,7 +2291,8 @@ export class CrossClient {
this.accountAddress,
this._provider.wallet.publicKey,
openOrdersAccount,
this._whitelistTradingFeesAddress
this._whitelistTradingFeesAddress,
options.selfTradeBehavior
);
}

Expand Down Expand Up @@ -2470,7 +2473,7 @@ export class CrossClient {
let tifOffsetToUse = utils.getTIFOffset(market, options.tifOptions);

tx.add(
instructions.placePerpOrderV4Ix(
instructions.placePerpOrderV5Ix(
asset,
newOrderPrice,
newOrderSize,
Expand All @@ -2485,7 +2488,8 @@ export class CrossClient {
this.accountAddress,
this._provider.wallet.publicKey,
this._openOrdersAccounts[assetIndex],
this._whitelistTradingFeesAddress
this._whitelistTradingFeesAddress,
options.selfTradeBehavior
)
);

Expand Down Expand Up @@ -2540,7 +2544,7 @@ export class CrossClient {
let tifOffsetToUse = utils.getTIFOffset(market, newOptions.tifOptions);

tx.add(
instructions.placePerpOrderV4Ix(
instructions.placePerpOrderV5Ix(
asset,
newOrderPrice,
newOrderSize,
Expand Down Expand Up @@ -2613,7 +2617,7 @@ export class CrossClient {
let tifOffsetToUse = utils.getTIFOffset(market, newOptions.tifOptions);

tx.add(
instructions.placePerpOrderV4Ix(
instructions.placePerpOrderV5Ix(
asset,
newOrderPrice,
newOrderSize,
Expand Down
212 changes: 212 additions & 0 deletions src/idl/zeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4680,6 +4680,204 @@
}
]
},
{
"name": "placePerpOrderV5",
"accounts": [
{
"name": "authority",
"isMut": false,
"isSigner": true
},
{
"name": "placeOrderAccounts",
"accounts": [
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "pricing",
"isMut": false,
"isSigner": false
},
{
"name": "marginAccount",
"isMut": true,
"isSigner": false
},
{
"name": "dexProgram",
"isMut": false,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "openOrders",
"isMut": true,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "marketAccounts",
"accounts": [
{
"name": "market",
"isMut": true,
"isSigner": false
},
{
"name": "requestQueue",
"isMut": true,
"isSigner": false
},
{
"name": "eventQueue",
"isMut": true,
"isSigner": false
},
{
"name": "bids",
"isMut": true,
"isSigner": false
},
{
"name": "asks",
"isMut": true,
"isSigner": false
},
{
"name": "orderPayerTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "coinVault",
"isMut": true,
"isSigner": false
},
{
"name": "pcVault",
"isMut": true,
"isSigner": false
},
{
"name": "coinWallet",
"isMut": true,
"isSigner": false
},
{
"name": "pcWallet",
"isMut": true,
"isSigner": false
}
]
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
},
{
"name": "oracleBackupFeed",
"isMut": false,
"isSigner": false
},
{
"name": "oracleBackupProgram",
"isMut": false,
"isSigner": false
},
{
"name": "marketMint",
"isMut": true,
"isSigner": false
},
{
"name": "mintAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "perpSyncQueue",
"isMut": true,
"isSigner": false
}
]
}
],
"args": [
{
"name": "price",
"type": "u64"
},
{
"name": "size",
"type": "u64"
},
{
"name": "side",
"type": {
"defined": "Side"
}
},
{
"name": "orderType",
"type": {
"defined": "OrderType"
}
},
{
"name": "reduceOnly",
"type": "bool"
},
{
"name": "clientOrderId",
"type": {
"option": "u64"
}
},
{
"name": "tag",
"type": {
"option": "string"
}
},
{
"name": "tifOffset",
"type": {
"option": "u16"
}
},
{
"name": "asset",
"type": {
"defined": "Asset"
}
},
{
"name": "selfTradeBehavior",
"type": {
"option": {
"defined": "SelfTradeBehaviorZeta"
}
}
}
]
},
{
"name": "placeMultiOrders",
"accounts": [
Expand Down Expand Up @@ -10314,6 +10512,20 @@
}
]
}
},
{
"name": "SelfTradeBehaviorZeta",
"type": {
"kind": "enum",
"variants": [
{
"name": "CancelProvide"
},
{
"name": "AbortTransaction"
}
]
}
}
],
"events": [
Expand Down
10 changes: 7 additions & 3 deletions src/program-instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export function closeOpenOrdersV2Ix(
});
}

export function placePerpOrderV4Ix(
export function placePerpOrderV5Ix(
asset: Asset,
price: number,
size: number,
Expand All @@ -506,7 +506,8 @@ export function placePerpOrderV4Ix(
marginAccount: PublicKey,
authority: PublicKey,
openOrders: PublicKey,
whitelistTradingFeesAccount: PublicKey | undefined
whitelistTradingFeesAccount: PublicKey | undefined,
selfTradeBehavior?: types.SelfTradeBehavior
): TransactionInstruction {
if (tag.length > constants.MAX_ORDER_TAG_LENGTH) {
throw Error(
Expand All @@ -525,7 +526,7 @@ export function placePerpOrderV4Ix(
},
]
: [];
return Exchange.program.instruction.placePerpOrderV4(
return Exchange.program.instruction.placePerpOrderV5(
new anchor.BN(price),
new anchor.BN(size),
types.toProgramSide(side),
Expand All @@ -535,6 +536,9 @@ export function placePerpOrderV4Ix(
new String(tag),
tifOffset == 0 ? null : tifOffset,
toProgramAsset(asset),
selfTradeBehavior != undefined
? types.toProgramSelfTradeBehavior(selfTradeBehavior)
: null,
{
accounts: {
authority: authority,
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ export function fromProgramOrderType(orderType: any): OrderType {
return OrderType.POSTONLYFRONT;
}

export enum SelfTradeBehavior {
CANCELPROVIDE,
ABORTTRANSACTION,
}

export function toProgramSelfTradeBehavior(behavior: SelfTradeBehavior) {
if (behavior == SelfTradeBehavior.CANCELPROVIDE) return { cancelProvide: {} };
if (behavior == SelfTradeBehavior.ABORTTRANSACTION)
return { abortTransaction: {} };
}

export enum Side {
BID,
ASK,
Expand Down Expand Up @@ -598,6 +609,7 @@ export interface OrderOptions {
clientOrderId?: number;
tag?: string;
blockhash?: { blockhash: string; lastValidBlockHeight: number };
selfTradeBehavior?: SelfTradeBehavior;
}

export interface TriggerOrderOptions {
Expand Down
Loading

0 comments on commit 0135937

Please sign in to comment.