Skip to content

Commit

Permalink
Merge branch 'master' into nour/pyth-local-val-test
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi authored Dec 18, 2024
2 parents 842e214 + 40eb187 commit 00a50d5
Show file tree
Hide file tree
Showing 23 changed files with 935 additions and 1,168 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,25 +224,35 @@ jobs:
run: |
VERSION=$(node -e "console.log(require('./package.json').version);")
[[ "$VERSION" == *beta* ]] && npm version prerelease --preid=beta || npm version preminor --preid=beta
echo "PACKAGE_VERSION=$(node -e "console.log(require('./package.json').version);")" >> $GITHUB_ENV
- name: Git commit
id: git-commit
run: |
VERSION=$(node -e "console.log(require('./package.json').version);")
echo "version=$VERSION" >> $GITHUB_OUTPUT
git config user.name "GitHub Actions"
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git add ..
git commit -a -m "sdk: release v$VERSION"
git commit -a -m "sdk: release v$PACKAGE_VERSION"
git pull --rebase origin master
git push origin HEAD || {
echo "Push failed. Retrying after pulling latest changes..."
git pull --rebase origin master
git push origin HEAD
}
echo "version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
- name: Publish to npm
run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Build sdk for browser
run: |
yarn
yarn build:browser
# Update package name for browser version while keeping the same version
node -e "const pkg = require('./package.json'); pkg.name = pkg.name + '-browser'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));"
- name: Publish browser version to npm
run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
emit-dispatch-events:
runs-on: ubicloud
needs: [release, check-for-sdk-changes]
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub enum ErrorCode {
SigVerificationFailed,
#[msg("Market index mismatched b/w taker and maker swift order params")]
MismatchedSwiftOrderParamsMarketIndex,
#[msg("Swift only available for market/oracle perp orders")]
#[msg("Invalid swift order param")]
InvalidSwiftOrderParam,
#[msg("Place and take order success condition failed")]
PlaceAndTakeOrderSuccessConditionFailed,
Expand Down
51 changes: 20 additions & 31 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ use crate::state::fulfillment_params::serum::SerumFulfillmentParams;
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
use crate::state::insurance_fund_stake::InsuranceFundStake;
use crate::state::oracle_map::OracleMap;
use crate::state::order_params::{
OrderParams, PlaceOrderOptions, SwiftOrderParamsMessage, SwiftServerMessage,
};
use crate::state::order_params::{OrderParams, PlaceOrderOptions, SwiftOrderParamsMessage};
use crate::state::paused_operations::PerpOperation;
use crate::state::perp_market::{ContractType, MarketStatus, PerpMarket};
use crate::state::perp_market_map::{
Expand Down Expand Up @@ -603,11 +601,8 @@ pub fn handle_update_user_open_orders_count<'info>(ctx: Context<UpdateUserIdle>)

pub fn handle_place_swift_taker_order<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, PlaceSwiftTakerOrder<'info>>,
swift_message_bytes: Vec<u8>,
swift_order_params_message_bytes: Vec<u8>,
) -> Result<()> {
let swift_message: SwiftServerMessage =
SwiftServerMessage::deserialize(&mut &swift_message_bytes[..]).unwrap();
let taker_order_params_message: SwiftOrderParamsMessage =
SwiftOrderParamsMessage::deserialize(&mut &swift_order_params_message_bytes[..]).unwrap();

Expand All @@ -634,7 +629,6 @@ pub fn handle_place_swift_taker_order<'c: 'info, 'info>(
taker_key,
&mut taker,
&mut swift_taker,
swift_message,
taker_order_params_message,
&ctx.accounts.ix_sysvar.to_account_info(),
&perp_market_map,
Expand All @@ -649,7 +643,6 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
taker_key: Pubkey,
taker: &mut RefMut<User>,
swift_account: &mut SwiftUserOrdersZeroCopyMut,
swift_message: SwiftServerMessage,
taker_order_params_message: SwiftOrderParamsMessage,
ix_sysvar: &AccountInfo<'info>,
perp_market_map: &PerpMarketMap,
Expand All @@ -665,20 +658,12 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
// Authenticate the swift param message
let ix_idx = load_current_index_checked(ix_sysvar)?;
validate!(
ix_idx > 1,
ix_idx > 0,
ErrorCode::InvalidVerificationIxIndex,
"instruction index must be greater than 1 for two sig verifies"
"instruction index must be greater than 0 for one sig verifies"
)?;

// Verify data from first verify ix
let ix: Instruction = load_instruction_at_checked(ix_idx as usize - 2, ix_sysvar)?;
verify_ed25519_msg(
&ix,
&swift_server::id().to_bytes(),
&digest_struct!(swift_message),
)?;

// Verify data from second verify ix
// Verify data from verify ix
let digest_hex = digest_struct_hex!(taker_order_params_message);
let ix: Instruction = load_instruction_at_checked(ix_idx as usize - 1, ix_sysvar)?;
verify_ed25519_msg(
Expand All @@ -687,12 +672,7 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
arrayref::array_ref!(digest_hex, 0, 64),
)?;

// Verify that sig from swift server corresponds to order message
if swift_message.swift_order_signature != extract_ed25519_ix_signature(&ix.data)? {
msg!("Swift order signature does not match the order signature");
return Err(ErrorCode::SigVerificationFailed.into());
}

let signature = extract_ed25519_ix_signature(&ix.data)?;
let clock = &Clock::get()?;

// First order must be a taker order
Expand All @@ -715,7 +695,14 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
}

// Set max slot for the order early so we set correct swift order id
let order_slot = swift_message.slot;
let order_slot = taker_order_params_message.slot;
if order_slot < clock.slot - 500 {
msg!(
"Swift order slot {} is too old: must be within 500 slots of current slot",
order_slot
);
return Err(print_error!(ErrorCode::InvalidSwiftOrderParam)().into());
}
let market_index = matching_taker_order_params.market_index;
let max_slot = order_slot.safe_add(
matching_taker_order_params
Expand All @@ -735,7 +722,11 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
}

// Dont place order if swift order already exists
let swift_order_id = SwiftOrderId::new(swift_message.uuid, max_slot, taker.next_order_id);
let swift_order_id = SwiftOrderId::new(
taker_order_params_message.uuid,
max_slot,
taker.next_order_id,
);
if swift_account.check_exists_and_prune_stale_swift_order_ids(swift_order_id, clock.slot) {
msg!("Swift order already exists for taker {}");
return Ok(());
Expand All @@ -757,10 +748,8 @@ pub fn place_swift_taker_order<'c: 'info, 'info>(
},
)?;

let order_params_hash = base64::encode(
solana_program::hash::hash(&swift_message.swift_order_signature.try_to_vec().unwrap())
.as_ref(),
);
let order_params_hash =
base64::encode(solana_program::hash::hash(&signature.try_to_vec().unwrap()).as_ref());

emit!(SwiftOrderRecord {
user: taker_key,
Expand Down
Loading

0 comments on commit 00a50d5

Please sign in to comment.