Skip to content

Commit

Permalink
fix memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Jan 15, 2025
1 parent 33c746a commit 2f9dfc7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 36 deletions.
49 changes: 24 additions & 25 deletions programs/jit-proxy/src/instructions/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,43 +601,42 @@ fn place_and_make_swift<'info>(
order_params: OrderParams,
swift_order_uuid: [u8; 8],
) -> Result<()> {
let drift_program = ctx.accounts.drift_program.to_account_info().clone();
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: ctx.accounts.state.to_account_info().clone(),
authority: ctx.accounts.authority.to_account_info().clone(),
user: ctx.accounts.taker.to_account_info().clone(),
user_stats: ctx.accounts.taker_stats.to_account_info().clone(),
swift_user_orders: ctx
.accounts
.taker_swift_user_orders
.to_account_info()
.clone(),
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_accounts_place_and_make = PlaceAndMakeSwift {
state: ctx.accounts.state.to_account_info().clone(),
user: ctx.accounts.user.to_account_info().clone(),
user_stats: ctx.accounts.user_stats.to_account_info().clone(),
authority: ctx.accounts.authority.to_account_info().clone(),
taker: ctx.accounts.taker.to_account_info().clone(),
taker_stats: ctx.accounts.taker_stats.to_account_info().clone(),
taker_swift_user_orders: ctx
.accounts
.taker_swift_user_orders
.to_account_info()
.clone(),
};

let cpi_context_place_taker_order =
CpiContext::new(drift_program.clone(), cpi_account_place_taker_order)
.with_remaining_accounts(ctx.remaining_accounts.iter().skip(1).cloned().collect());
.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(),
user_stats: ctx.accounts.user_stats.to_account_info().clone(),
authority: ctx.accounts.authority.to_account_info().clone(),
taker,
taker_stats,
taker_swift_user_orders,
};

let cpi_context_place_and_make = CpiContext::new(drift_program, cpi_accounts_place_and_make)
.with_remaining_accounts(ctx.remaining_accounts.into());

Expand Down
40 changes: 37 additions & 3 deletions ts/sdk/src/jitProxyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BN,
createMinimalEd25519VerifyIx,
DriftClient,
getSwiftUserAccountPublicKey,
isVariant,
MakerInfo,
MarketType,
Expand All @@ -12,7 +13,13 @@ import {
UserAccount,
} from '@drift-labs/sdk';
import { IDL, JitProxy } from './types/jit_proxy';
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
import {
PublicKey,
SYSVAR_INSTRUCTIONS_PUBKEY,
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
} from '@solana/web3.js';
import { Program } from '@coral-xyz/anchor';
import { TxSigAndSlot } from '@drift-labs/sdk';
import { SignedSwiftOrderParams } from '@drift-labs/sdk/lib/node/swift/types';
Expand Down Expand Up @@ -91,7 +98,7 @@ export class JitProxyClient {
params.signedSwiftOrderParams.orderParams,
]);
const swiftOrderParamsSignatureIx = createMinimalEd25519VerifyIx(
3,
1,
12,
swiftIxData,
0
Expand All @@ -101,6 +108,27 @@ export class JitProxyClient {
[swiftOrderParamsSignatureIx, ix],
txParams
);
let resp;
try {
const message = new TransactionMessage({
payerKey: this.driftClient.wallet.payer.publicKey,
recentBlockhash: (
await this.driftClient.connection.getLatestBlockhash()
).blockhash,
instructions: [swiftOrderParamsSignatureIx, ix],
}).compileToV0Message([this.driftClient.lookupTableAccount]);

const tx = new VersionedTransaction(message);
resp = await this.driftClient.connection.simulateTransaction(tx, {
sigVerify: false,
replaceRecentBlockhash: true,
commitment: 'processed',
});
console.log(resp);
} catch (e) {
console.error(e);
}

return await this.driftClient.sendTransaction(tx);
}

Expand Down Expand Up @@ -197,6 +225,7 @@ export class JitProxyClient {
subAccountId,
uuid,
marketIndex,
signedSwiftOrderParams,
}: JitSwiftIxParams): Promise<TransactionInstruction> {
subAccountId =
subAccountId !== undefined
Expand Down Expand Up @@ -232,14 +261,19 @@ export class JitProxyClient {
};

return this.program.methods
.jitSwift(jitSwiftParams)
.jitSwift(signedSwiftOrderParams.orderParams, jitSwiftParams)
.accounts({
taker: takerKey,
takerStats: takerStatsKey,
takerSwiftUserOrders: getSwiftUserAccountPublicKey(
this.driftClient.program.programId,
takerKey
),
state: await this.driftClient.getStatePublicKey(),
user: await this.driftClient.getUserAccountPublicKey(subAccountId),
userStats: this.driftClient.getUserStatsAccountPublicKey(),
driftProgram: this.driftClient.program.programId,
ixSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
})
.remainingAccounts(remainingAccounts)
.instruction();
Expand Down
11 changes: 3 additions & 8 deletions ts/sdk/src/jitter/baseJitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ export abstract class BaseJitter {
this.driftClient,
new BulkAccountLoader(this.driftClient.connection, 'confirmed', 0)
);

this.swiftOrderSubscriber = swiftOrderSubscriber;
this.slotSubscriber = slotSubscriber;
this.swiftOrderSubscriber = swiftOrderSubscriber;
}

async subscribe(): Promise<void> {
Expand Down Expand Up @@ -248,7 +247,7 @@ export abstract class BaseJitter {
};
swiftOrder.price = getAuctionPrice(
swiftOrder,
this.slotSubscriber.getSlot(),
this.slotSubscriber?.getSlot(),
this.driftClient.getOracleDataForPerpMarket(swiftOrder.marketIndex)
.price
);
Expand Down Expand Up @@ -282,11 +281,7 @@ export abstract class BaseJitter {
const perpMarketAccount = this.driftClient.getPerpMarketAccount(
swiftOrder.marketIndex
);
if (
swiftOrder.baseAssetAmount
.sub(swiftOrder.baseAssetAmountFilled)
.lte(perpMarketAccount.amm.minOrderSize)
) {
if (swiftOrder.baseAssetAmount.lt(perpMarketAccount.amm.minOrderSize)) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions ts/sdk/src/jitter/jitterShotgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DriftClient,
Order,
PostOnlyParams,
SlotSubscriber,
SwiftOrderSubscriber,
UserAccount,
UserStatsMap,
Expand All @@ -19,19 +20,22 @@ export class JitterShotgun extends BaseJitter {
driftClient,
userStatsMap,
swiftOrderSubscriber,
slotSubscriber,
}: {
driftClient: DriftClient;
auctionSubscriber: AuctionSubscriber;
jitProxyClient: JitProxyClient;
userStatsMap?: UserStatsMap;
swiftOrderSubscriber?: SwiftOrderSubscriber;
slotSubscriber?: SlotSubscriber;
}) {
super({
auctionSubscriber,
jitProxyClient,
driftClient,
userStatsMap,
swiftOrderSubscriber,
slotSubscriber,
});
}

Expand Down Expand Up @@ -174,6 +178,7 @@ export class JitterShotgun extends BaseJitter {
return;
} catch (e) {
console.error(`Failed to fill ${orderSignature}`);
console.log(e);
if (e.message.includes('0x1770') || e.message.includes('0x1771')) {
console.log('Order does not cross params yet, retrying');
} else if (e.message.includes('0x1779')) {
Expand Down
1 change: 1 addition & 0 deletions ts/sdk/src/jitter/jitterSniper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class JitterSniper extends BaseJitter {
driftClient,
userStatsMap,
swiftOrderSubscriber,
slotSubscriber,
});
this.slotSubscriber = slotSubscriber;
}
Expand Down
18 changes: 18 additions & 0 deletions ts/sdk/src/types/jit_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ export type JitProxy = {
name: 'driftProgram';
isMut: false;
isSigner: false;
},
{
name: 'ixSysvar';
isMut: false;
isSigner: false;
}
];
args: [
{
name: 'swiftOrderParamsMessageBytes';
type: 'bytes';
},
{
name: 'params';
type: {
Expand Down Expand Up @@ -476,8 +485,17 @@ export const IDL: JitProxy = {
isMut: false,
isSigner: false,
},
{
name: 'ixSysvar',
isMut: false,
isSigner: false,
},
],
args: [
{
name: 'swiftOrderParamsMessageBytes',
type: 'bytes',
},
{
name: 'params',
type: {
Expand Down

0 comments on commit 2f9dfc7

Please sign in to comment.