Skip to content

Commit

Permalink
feat: take trigger order (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjcaa authored Feb 22, 2024
1 parent 6d3435a commit e64944f
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 55 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file.
Version changes are pinned to SDK releases.

## [Unreleased]

## [1.22.0]

- Deprecate v1 trigger instructions.
- Add new take trigger order instruction.

## [1.21.4]

- Improve cancelAllMarketOrders for big transactions. ([#356](https://github.com/zetamarkets/sdk/pull/356))
Expand Down
31 changes: 31 additions & 0 deletions src/cross-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,37 @@ export class CrossClient {
);
}

public async takeTriggerOrder(
orderIndex: number,
asset: Asset,
orderMarginAccount: PublicKey
) {
let triggerAccount = utils.getTriggerOrder(
Exchange.programId,
orderMarginAccount,
new Uint8Array([orderIndex])
)[0];

let tx = new Transaction().add(
instructions.takeTriggerOrderIx(
asset,
triggerAccount,
orderIndex,
orderMarginAccount,
this._accountAddress,
this.provider.wallet.publicKey
)
);
return await utils.processTransaction(
this._provider,
tx,
undefined,
undefined,
undefined,
this._useVersionedTxs ? utils.getZetaLutArr() : undefined
);
}

public async editPriceTriggerOrder(
orderIndex: number,
newOrderPrice: number,
Expand Down
2 changes: 1 addition & 1 deletion src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ export class Exchange {
new Uint8Array([orderIndex])
)[0];
let tx = new Transaction().add(
instructions.cancelTriggerOrderIx(
instructions.forceCancelTriggerOrderIx(
orderIndex,
this._provider.wallet.publicKey,
triggerAccount,
Expand Down
93 changes: 92 additions & 1 deletion src/idl/zeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,27 @@
}
]
},
{
"name": "updateTakeTriggerOrderFeePercentage",
"accounts": [
{
"name": "state",
"isMut": true,
"isSigner": false
},
{
"name": "admin",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "newTakeTriggerOrderFeePercentage",
"type": "u64"
}
]
},
{
"name": "updateZetaState",
"accounts": [
Expand Down Expand Up @@ -4514,6 +4535,72 @@
}
]
},
{
"name": "takeTriggerOrder",
"accounts": [
{
"name": "triggerOrder",
"isMut": true,
"isSigner": false
},
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "pricing",
"isMut": false,
"isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
},
{
"name": "oracleBackupFeed",
"isMut": false,
"isSigner": false
},
{
"name": "oracleBackupProgram",
"isMut": false,
"isSigner": false
},
{
"name": "bids",
"isMut": false,
"isSigner": false
},
{
"name": "asks",
"isMut": false,
"isSigner": false
},
{
"name": "taker",
"isMut": true,
"isSigner": true
},
{
"name": "takerMarginAccount",
"isMut": true,
"isSigner": false
},
{
"name": "orderMarginAccount",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "triggerOrderBit",
"type": "u8"
}
]
},
{
"name": "executeTriggerOrder",
"accounts": [
Expand Down Expand Up @@ -7301,12 +7388,16 @@
"name": "nativeMakerTradeFeePercentage",
"type": "u64"
},
{
"name": "nativeTakeTriggerOrderFeePercentage",
"type": "u64"
},
{
"name": "padding",
"type": {
"array": [
"u8",
98
90
]
}
}
Expand Down
92 changes: 41 additions & 51 deletions src/program-instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,59 +685,33 @@ export function executeTriggerOrderV2Ix(
},
});
}
export function executeTriggerOrderIx(

export function takeTriggerOrderIx(
asset: Asset,
side: types.Side,
triggerOrderBit: number,
triggerOrder: PublicKey,
marginAccount: PublicKey,
openOrders: PublicKey
triggerOrderBit: number,
orderMarginAccount: PublicKey,
takerMarginAccount: PublicKey,
taker: PublicKey
): TransactionInstruction {
let marketData = Exchange.getPerpMarket(asset);

return Exchange.program.instruction.executeTriggerOrder(triggerOrderBit, {
return Exchange.program.instruction.takeTriggerOrder(triggerOrderBit, {
accounts: {
admin: Exchange.state.secondaryAdmin,
triggerOrder: triggerOrder,
placeOrderAccounts: {
state: Exchange.stateAddress,
pricing: Exchange.pricingAddress,
marginAccount: marginAccount,
dexProgram: constants.DEX_PID[Exchange.network],
tokenProgram: TOKEN_PROGRAM_ID,
serumAuthority: Exchange.serumAuthority,
openOrders: openOrders,
rent: SYSVAR_RENT_PUBKEY,
marketAccounts: {
market: marketData.serumMarket.address,
requestQueue: marketData.serumMarket.requestQueueAddress,
eventQueue: marketData.serumMarket.eventQueueAddress,
bids: marketData.serumMarket.bidsAddress,
asks: marketData.serumMarket.asksAddress,
coinVault: marketData.serumMarket.baseVaultAddress,
pcVault: marketData.serumMarket.quoteVaultAddress,
// User params.
orderPayerTokenAccount:
side == types.Side.BID
? marketData.quoteVault
: marketData.baseVault,
coinWallet: marketData.baseVault,
pcWallet: marketData.quoteVault,
},
oracle: Exchange.pricing.oracles[assetToIndex(asset)],
oracleBackupFeed:
Exchange.pricing.oracleBackupFeeds[assetToIndex(asset)],
oracleBackupProgram: constants.CHAINLINK_PID,
marketMint:
side == types.Side.BID
? marketData.serumMarket.quoteMintAddress
: marketData.serumMarket.baseMintAddress,
mintAuthority: Exchange.mintAuthority,
perpSyncQueue: Exchange.pricing.perpSyncQueues[assetToIndex(asset)],
},
state: Exchange.stateAddress,
pricing: Exchange.pricingAddress,
oracle: Exchange.pricing.oracles[assetToIndex(asset)],
oracleBackupFeed: Exchange.pricing.oracleBackupFeeds[assetToIndex(asset)],
oracleBackupProgram: constants.CHAINLINK_PID,
bids: marketData.serumMarket.bidsAddress,
asks: marketData.serumMarket.asksAddress,
taker,
takerMarginAccount,
orderMarginAccount,
},
});
}

export function cancelTriggerOrderV2Ix(
triggerOrderBit: number,
authority: PublicKey,
Expand All @@ -752,22 +726,23 @@ export function cancelTriggerOrderV2Ix(
},
});
}
export function cancelTriggerOrderIx(

export function forceCancelTriggerOrderIx(
triggerOrderBit: number,
payer: PublicKey,
authority: PublicKey,
triggerOrder: PublicKey,
marginAccount: PublicKey
): TransactionInstruction {
return Exchange.program.instruction.cancelTriggerOrder(triggerOrderBit, {
return Exchange.program.instruction.forceCancelTriggerOrder(triggerOrderBit, {
accounts: {
state: Exchange.stateAddress,
payer: payer,
admin: Exchange.state.secondaryAdmin,
admin: authority,
triggerOrder: triggerOrder,
marginAccount: marginAccount,
},
});
}

export function editTriggerOrderIx(
newOrderPrice: number,
newTriggerPrice: number,
Expand Down Expand Up @@ -2303,11 +2278,26 @@ export function editDelegatedPubkeyIx(
}

export function updateMakerTradeFeePercentageIx(
newNativeMakerTradeFeePercetange: anchor.BN,
newNativeMakerTradeFeePercentage: anchor.BN,
admin: PublicKey
) {
return Exchange.program.instruction.updateMakerTradeFeePercentage(
newNativeMakerTradeFeePercetange,
newNativeMakerTradeFeePercentage,
{
accounts: {
state: Exchange.stateAddress,
admin,
},
}
);
}

export function updateTakeTriggerOrderFeePercentageIx(
newTakeTriggerOrderFeePercentage: anchor.BN,
admin: PublicKey
) {
return Exchange.program.instruction.updateTakeTriggerOrderFeePercentage(
newTakeTriggerOrderFeePercentage,
{
accounts: {
state: Exchange.stateAddress,
Expand Down
1 change: 1 addition & 0 deletions src/program-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface State {
tickSizes: Array<number>;
tickSizesPadding: Array<number>;
nativeMakerTradeFeePercentage: anchor.BN;
nativeTakeTriggerOrderFeePercentage: anchor.BN;
padding: Array<number>;
}

Expand Down
Loading

0 comments on commit e64944f

Please sign in to comment.