Skip to content

Commit

Permalink
successfull jit
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Jan 15, 2025
1 parent 2f9dfc7 commit 02d46e1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 62 deletions.
32 changes: 3 additions & 29 deletions programs/jit-proxy/src/instructions/jit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::Pubkey;
use anchor_lang::prelude::*;
use drift::controller::position::PositionDirection;
use drift::cpi::accounts::{PlaceAndMake, PlaceAndMakeSwift, PlaceSwiftTakerOrder};
use drift::cpi::accounts::{PlaceAndMake, PlaceAndMakeSwift};
use drift::error::DriftResult;
use drift::instructions::optional_accounts::{load_maps, AccountMaps};
use drift::math::casting::Cast;
Expand Down Expand Up @@ -111,7 +111,6 @@ pub fn jit<'c: 'info, 'info>(

pub fn jit_swift<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, JitSwift<'info>>,
swift_order_params_message_bytes: Vec<u8>,
params: JitSwiftParams,
) -> Result<()> {
let clock = Clock::get()?;
Expand Down Expand Up @@ -167,12 +166,7 @@ pub fn jit_swift<'c: 'info, 'info>(
drop(taker);
drop(maker);

place_and_make_swift(
&ctx,
swift_order_params_message_bytes,
order_params,
params.swift_order_uuid,
)?;
place_and_make_swift(&ctx, order_params, params.swift_order_uuid)?;

let taker = ctx.accounts.taker.load()?;

Expand Down Expand Up @@ -430,6 +424,7 @@ pub struct JitSwift<'info> {
#[account(mut)]
pub taker_stats: AccountLoader<'info, UserStats>,
/// CHECK: checked in SwiftUserOrdersZeroCopy checks
#[account(mut)]
pub taker_swift_user_orders: AccountInfo<'info>,
pub authority: Signer<'info>,
pub drift_program: Program<'info, Drift>,
Expand Down Expand Up @@ -597,36 +592,15 @@ fn place_and_make<'info>(

fn place_and_make_swift<'info>(
ctx: &Context<'_, '_, '_, 'info, JitSwift<'info>>,
swift_order_params_message_bytes: Vec<u8>,
order_params: OrderParams,
swift_order_uuid: [u8; 8],
) -> Result<()> {
let drift_program = ctx.accounts.drift_program.to_account_info();
let state = ctx.accounts.state.to_account_info();
let authority = ctx.accounts.authority.to_account_info();
let taker = ctx.accounts.taker.to_account_info();
let taker_stats = ctx.accounts.taker_stats.to_account_info();
let taker_swift_user_orders = ctx.accounts.taker_swift_user_orders.to_account_info();

let cpi_account_place_taker_order = PlaceSwiftTakerOrder {
state: state.clone(),
authority: authority.clone(),
user: taker.clone(),
user_stats: taker_stats.clone(),
swift_user_orders: taker_swift_user_orders.clone(),
ix_sysvar: ctx.accounts.ix_sysvar.clone(),
};

let cpi_context_place_taker_order =
CpiContext::new(drift_program.clone(), cpi_account_place_taker_order)
.with_remaining_accounts(ctx.remaining_accounts.into());
drift::cpi::place_swift_taker_order(
cpi_context_place_taker_order,
swift_order_params_message_bytes,
)?;

msg!("Made it past first cpi");

let cpi_accounts_place_and_make = PlaceAndMakeSwift {
state,
user: ctx.accounts.user.to_account_info().clone(),
Expand Down
3 changes: 1 addition & 2 deletions programs/jit-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ pub mod jit_proxy {

pub fn jit_swift<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, JitSwift<'info>>,
swift_order_params_message_bytes: Vec<u8>,
params: JitSwiftParams,
) -> Result<()> {
instructions::jit_swift(ctx, swift_order_params_message_bytes, params)
instructions::jit_swift(ctx, params)
}

pub fn check_order_constraints<'c: 'info, 'info>(
Expand Down
37 changes: 16 additions & 21 deletions ts/sdk/src/jitProxyClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
BN,
createMinimalEd25519VerifyIx,
DriftClient,
getSwiftUserAccountPublicKey,
isVariant,
Expand Down Expand Up @@ -84,28 +83,24 @@ export class JitProxyClient {

public async jitSwift(
params: JitSwiftIxParams,
txParams?: TxParams
txParams?: TxParams,
precedingIxs?: TransactionInstruction[]
): Promise<TxSigAndSlot> {
const messageLengthBuffer = Buffer.alloc(2);
messageLengthBuffer.writeUInt16LE(
params.signedSwiftOrderParams.orderParams.length
const swiftTakerIxs = await this.driftClient.getPlaceSwiftTakerPerpOrderIxs(
params.signedSwiftOrderParams,
params.marketIndex,
{
taker: params.takerKey,
takerStats: params.takerStatsKey,
takerUserAccount: params.taker,
},
params.authorityToUse,
precedingIxs
);

const swiftIxData = Buffer.concat([
params.signedSwiftOrderParams.signature,
params.authorityToUse.toBytes(),
messageLengthBuffer,
params.signedSwiftOrderParams.orderParams,
]);
const swiftOrderParamsSignatureIx = createMinimalEd25519VerifyIx(
1,
12,
swiftIxData,
0
);
const ix = await this.getJitSwiftIx(params);
const tx = await this.driftClient.buildTransaction(
[swiftOrderParamsSignatureIx, ix],
[...swiftTakerIxs, ix],
txParams
);
let resp;
Expand All @@ -115,7 +110,7 @@ export class JitProxyClient {
recentBlockhash: (
await this.driftClient.connection.getLatestBlockhash()
).blockhash,
instructions: [swiftOrderParamsSignatureIx, ix],
instructions: [...swiftTakerIxs, ix],
}).compileToV0Message([this.driftClient.lookupTableAccount]);

const tx = new VersionedTransaction(message);
Expand Down Expand Up @@ -225,7 +220,6 @@ export class JitProxyClient {
subAccountId,
uuid,
marketIndex,
signedSwiftOrderParams,
}: JitSwiftIxParams): Promise<TransactionInstruction> {
subAccountId =
subAccountId !== undefined
Expand Down Expand Up @@ -261,14 +255,15 @@ export class JitProxyClient {
};

return this.program.methods
.jitSwift(signedSwiftOrderParams.orderParams, jitSwiftParams)
.jitSwift(jitSwiftParams)
.accounts({
taker: takerKey,
takerStats: takerStatsKey,
takerSwiftUserOrders: getSwiftUserAccountPublicKey(
this.driftClient.program.programId,
takerKey
),
authority: this.driftClient.wallet.payer.publicKey,
state: await this.driftClient.getStatePublicKey(),
user: await this.driftClient.getUserAccountPublicKey(subAccountId),
userStats: this.driftClient.getUserStatsAccountPublicKey(),
Expand Down
12 changes: 2 additions & 10 deletions ts/sdk/src/types/jit_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export type JitProxy = {
},
{
name: 'takerSwiftUserOrders';
isMut: false;
isMut: true;
isSigner: false;
},
{
Expand All @@ -100,10 +100,6 @@ export type JitProxy = {
}
];
args: [
{
name: 'swiftOrderParamsMessageBytes';
type: 'bytes';
},
{
name: 'params';
type: {
Expand Down Expand Up @@ -472,7 +468,7 @@ export const IDL: JitProxy = {
},
{
name: 'takerSwiftUserOrders',
isMut: false,
isMut: true,
isSigner: false,
},
{
Expand All @@ -492,10 +488,6 @@ export const IDL: JitProxy = {
},
],
args: [
{
name: 'swiftOrderParamsMessageBytes',
type: 'bytes',
},
{
name: 'params',
type: {
Expand Down

0 comments on commit 02d46e1

Please sign in to comment.