Skip to content

Commit

Permalink
Add more to swap
Browse files Browse the repository at this point in the history
  • Loading branch information
danimhr committed Jan 14, 2025
1 parent 6f7077b commit 238a0d1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 24 additions & 15 deletions auction-server/src/auction/service/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,15 @@ impl Service<Svm> {
async fn extract_account(
&self,
tx: &VersionedTransaction,
submit_bid_instruction: &CompiledInstruction,
instruction: &CompiledInstruction,
position: usize,
) -> Result<Pubkey, RestError> {
let static_accounts = tx.message.static_account_keys();
let tx_lookup_tables = tx.message.address_table_lookups();

let account_position = submit_bid_instruction
.accounts
.get(position)
.ok_or_else(|| {
RestError::BadParameters("Account not found in submit_bid instruction".to_string())
})?;
let account_position = instruction.accounts.get(position).ok_or_else(|| {
RestError::BadParameters("Account not found in submit_bid instruction".to_string())
})?;

let account_position: usize = (*account_position).into();
if let Some(account) = static_accounts.get(account_position) {
Expand Down Expand Up @@ -580,7 +577,7 @@ impl Service<Svm> {
.await?;

let (bid_amount, tokens) = match swap_data.fee_token {
FeeToken::Input => (
FeeToken::Output => (
swap_data.amount_input,
QuoteTokens::InputTokenSpecified {
input_token: TokenAmountSvm {
Expand All @@ -590,7 +587,7 @@ impl Service<Svm> {
output_token: mint_output,
},
),
FeeToken::Output => (
FeeToken::Input => (
swap_data.amount_output,
QuoteTokens::OutputTokenSpecified {
input_token: mint_input,
Expand Down Expand Up @@ -626,12 +623,10 @@ impl Service<Svm> {
}
}

fn all_signatures_exists(
fn relayer_signer_exists(
&self,
message_bytes: &[u8],
accounts: &[Pubkey],
signatures: &[Signature],
missing_signers: &[Pubkey],
) -> Result<(), RestError> {
let relayer_pubkey = self.config.chain_config.express_relay.relayer.pubkey();
let relayer_exists = accounts[..signatures.len()]
Expand All @@ -644,9 +639,18 @@ impl Service<Svm> {
relayer_pubkey
)));
}
Ok(())
}

fn all_signatures_exists(
&self,
message_bytes: &[u8],
accounts: &[Pubkey],
signatures: &[Signature],
missing_signers: &[Pubkey],
) -> Result<(), RestError> {
for (signature, pubkey) in signatures.iter().zip(accounts.iter()) {
if missing_signers.contains(pubkey) || pubkey.eq(&relayer_pubkey) {
if missing_signers.contains(pubkey) {
continue;
}
if !signature.verify(pubkey.as_ref(), message_bytes) {
Expand All @@ -658,7 +662,6 @@ impl Service<Svm> {
}
Ok(())
}

async fn verify_signatures(
&self,
bid: &entities::BidCreate<Svm>,
Expand Down Expand Up @@ -698,7 +701,13 @@ impl Service<Svm> {
)
}
SubmitType::ByServer => {
self.all_signatures_exists(&message_bytes, accounts, &signatures, &[])
self.relayer_signer_exists(accounts, &signatures)?;
self.all_signatures_exists(
&message_bytes,
accounts,
&signatures,
&[self.config.chain_config.express_relay.relayer.pubkey()],
)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions auction-server/src/opportunity/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,9 @@ pub async fn get_opportunities(
(status = 404, description = "No quote available right now", body = ErrorBodyResponse),
),)]
pub async fn post_quote(
auth: Auth,
State(store): State<Arc<StoreNew>>,
Json(params): Json<QuoteCreate>,
) -> Result<Json<Quote>, RestError> {
let program = get_program(&auth)?;
if program != ProgramSvm::SwapKamino {
return Err(RestError::Forbidden);
}

let quote = store
.opportunity_service_svm
.get_quote(GetQuoteInput {
Expand Down
12 changes: 6 additions & 6 deletions auction-server/src/opportunity/entities/opportunity_svm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ impl From<OpportunitySvm> for api::OpportunitySvm {
))
.expect("Failed to get sell token from opportunity svm");
let tokens = if buy_token.amount == 0 {
api::QuoteTokens::OutputTokenSpecified {
input_token: buy_token.token,
output_token: sell_token.clone().into(),
api::QuoteTokens::InputTokenSpecified {
output_token: buy_token.token,
input_token: sell_token.clone().into(),
}
} else {
if sell_token.amount != 0 {
tracing::error!(opportunity = ?val, "Both token amounts are specified for swap opportunity");
}
api::QuoteTokens::InputTokenSpecified {
input_token: buy_token.clone().into(),
output_token: sell_token.token,
api::QuoteTokens::OutputTokenSpecified {
output_token: buy_token.clone().into(),
input_token: sell_token.token,
}
};
api::OpportunityParamsV1ProgramSvm::Swap {
Expand Down
1 change: 0 additions & 1 deletion sdk/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ futures-util = "0.3.31"
ethers = { workspace = true }
solana-sdk = { workspace = true }
solana-rpc-client = { workspace = true }
spl-associated-token-account = "5.0.1"
2 changes: 2 additions & 0 deletions sdk/rust/simple-searcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ tokio-stream = { workspace = true }
rand = { workspace = true }
clap = { workspace = true, features = ["derive", "env"] }
solana-rpc-client = { workspace = true }
spl-associated-token-account = "5.0.1"
spl-token = "6.0.0"
33 changes: 31 additions & 2 deletions sdk/rust/simple-searcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
OpportunityMode,
OpportunityParamsSvm,
OpportunityParamsV1ProgramSvm,
QuoteTokens,
},
ws::ServerUpdateResponse,
SvmChainUpdate,
Expand All @@ -32,6 +33,7 @@ use {
WsClient,
},
rand::Rng,
spl_associated_token_account::instruction::create_associated_token_account_idempotent,
std::{
collections::HashMap,
str::FromStr,
Expand Down Expand Up @@ -227,15 +229,42 @@ impl SimpleSearcher {
)
.await
}
OpportunityParamsV1ProgramSvm::Swap { .. } => {
OpportunityParamsV1ProgramSvm::Swap { tokens, .. } => {
let (input_token, output_token) = match tokens {
QuoteTokens::InputTokenSpecified {
input_token,
output_token,
} => (input_token.token, output_token),
QuoteTokens::OutputTokenSpecified {
input_token,
output_token,
} => (input_token, output_token.token),
};
// TODO fix the token_program_id
let create_input_account_ix = create_associated_token_account_idempotent(
&payer.pubkey(),
&payer.pubkey(),
&input_token,
&spl_token::id(),
);
let create_output_account_ix = create_associated_token_account_idempotent(
&payer.pubkey(),
&payer.pubkey(),
&output_token,
&spl_token::id(),
);
self.client
.new_bid(
opportunity.clone(),
svm::NewBidParams {
amount: 100,
deadline,
block_hash: svm_update.blockhash,
instructions: vec![fee_ix],
instructions: vec![
fee_ix,
create_input_account_ix,
create_output_account_ix,
],
payer: payer.pubkey(),
slot: Some(opportunity.slot),
searcher: payer.pubkey(),
Expand Down
54 changes: 24 additions & 30 deletions sdk/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use {
Serialize,
},
solana_sdk::transaction::Transaction,
spl_associated_token_account::get_associated_token_address,
std::{
collections::HashMap,
marker::PhantomData,
Expand Down Expand Up @@ -750,42 +749,37 @@ impl Biddable for api_types::opportunity::OpportunitySvm {
)),
}?;

let (mint_input, mint_output, mint_fee, fee_token, amount_input, amount_output) =
match tokens {
QuoteTokens::InputTokenSpecified {
input_token,
output_token,
} => (
input_token.token,
output_token,
input_token.token,
FeeToken::Input,
input_token.amount,
params.amount,
),
QuoteTokens::OutputTokenSpecified {
input_token,
output_token,
} => (
input_token,
output_token.token,
output_token.token,
FeeToken::Output,
params.amount,
output_token.amount,
),
};
let (mint_input, mint_output, fee_token, amount_input, amount_output) = match tokens
{
QuoteTokens::InputTokenSpecified {
input_token,
output_token,
} => (
input_token.token,
output_token,
FeeToken::Output,
input_token.amount,
params.amount,
),
QuoteTokens::OutputTokenSpecified {
input_token,
output_token,
} => (
input_token,
output_token.token,
FeeToken::Input,
params.amount,
output_token.amount,
),
};

let mut instructions = params.instructions;
instructions.push(svm::Svm::get_swap_instruction(NewSwapInstructionParams {
searcher: params.searcher,
trader: user_wallet_address,
searcher_input_ta: None,
searcher_output_ta: None,
router_fee_receiver_ta: get_associated_token_address(
&router_account,
&mint_fee,
),
router_fee_receiver_ta: router_account,
fee_receiver_relayer: params.fee_receiver_relayer,
mint_input,
mint_output,
Expand Down

0 comments on commit 238a0d1

Please sign in to comment.