-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
250 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use { | ||
crate::bid::BidId, | ||
serde::{ | ||
Deserialize, | ||
Serialize, | ||
}, | ||
serde_with::{ | ||
serde_as, | ||
DisplayFromStr, | ||
}, | ||
solana_sdk::{ | ||
signature::Signature, | ||
transaction::VersionedTransaction, | ||
}, | ||
utoipa::ToSchema, | ||
}; | ||
|
||
/// Parameters needed to submit a quote from server. | ||
#[serde_as] | ||
#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)] | ||
pub struct SubmitQuote { | ||
/// The reference id for the quote that should be submitted. | ||
#[schema(example = "beedbeed-58cc-4372-a567-0e02b2c3d479", value_type=String)] | ||
pub reference_id: BidId, | ||
/// The signature of the user for the quote. | ||
#[schema(example = "Jb2urXPyEh4xiBgzYvwEFe4q1iMxG1DNxWGGQg94AmKgqFTwLAiTiHrYiYxwHUB4DV8u5ahNEVtMMDm3sNSRdTg", value_type = String)] | ||
#[serde_as(as = "DisplayFromStr")] | ||
pub user_signature: Signature, | ||
} | ||
|
||
/// Response to a quote submission. | ||
#[serde_as] | ||
#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)] | ||
pub struct SubmitQuoteResponse { | ||
/// The fully signed versioned transaction that was submitted. | ||
/// The transaction is encoded in base64. | ||
#[schema(example = "SGVsbG8sIFdvcmxkIQ==", value_type = String)] | ||
#[serde(with = "crate::serde::transaction_svm")] | ||
pub transaction: VersionedTransaction, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
auction-server/src/auction/repository/get_in_memory_bid_by_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use { | ||
super::Repository, | ||
crate::auction::{ | ||
entities, | ||
service::ChainTrait, | ||
}, | ||
}; | ||
|
||
impl<T: ChainTrait> Repository<T> { | ||
pub async fn get_in_memory_bid_by_id( | ||
&self, | ||
bid_id: entities::BidId, | ||
) -> Option<entities::Bid<T>> { | ||
let in_memory_bids = self.get_in_memory_bids().await; | ||
for bids in in_memory_bids.values() { | ||
for bid in bids { | ||
if bid.id == bid_id { | ||
return Some(bid.clone()); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use { | ||
super::{ | ||
verification::SwapAccounts, | ||
Service, | ||
}, | ||
crate::{ | ||
api::RestError, | ||
auction::entities::{ | ||
self, | ||
BidStatus, | ||
}, | ||
kernel::entities::Svm, | ||
}, | ||
solana_sdk::{ | ||
signature::Signature, | ||
transaction::VersionedTransaction, | ||
}, | ||
std::time::Duration, | ||
time::OffsetDateTime, | ||
}; | ||
|
||
pub struct SubmitQuoteInput { | ||
pub bid_id: entities::BidId, | ||
pub user_signature: Signature, | ||
} | ||
|
||
const DEADLINE_BUFFER: Duration = Duration::from_secs(2); | ||
|
||
impl Service<Svm> { | ||
pub async fn submit_quote( | ||
&self, | ||
input: SubmitQuoteInput, | ||
) -> Result<VersionedTransaction, RestError> { | ||
let bid: Option<entities::Bid<Svm>> = self.repo.get_in_memory_bid_by_id(input.bid_id).await; | ||
match bid { | ||
Some(mut bid) => { | ||
let swap_instruction = self | ||
.extract_express_relay_instruction( | ||
bid.chain_data.transaction.clone(), | ||
entities::BidPaymentInstructionType::Swap, | ||
) | ||
.map_err(|_| RestError::BadParameters("Invalid quote".to_string()))?; | ||
let SwapAccounts { user_wallet, .. } = self | ||
.extract_swap_accounts(&bid.chain_data.transaction, &swap_instruction) | ||
.await | ||
.map_err(|_| RestError::BadParameters("Invalid quote".to_string()))?; | ||
let swap_args = Self::extract_swap_data(&swap_instruction) | ||
.map_err(|_| RestError::BadParameters("Invalid quote".to_string()))?; | ||
|
||
if swap_args.deadline | ||
< (OffsetDateTime::now_utc() - DEADLINE_BUFFER).unix_timestamp() | ||
{ | ||
return Err(RestError::BadParameters("Quote is expired".to_string())); | ||
} | ||
|
||
if !input.user_signature.verify( | ||
&user_wallet.to_bytes(), | ||
&bid.chain_data.transaction.message.serialize(), | ||
) { | ||
return Err(RestError::BadParameters("Invalid signature".to_string())); | ||
} | ||
|
||
let user_signature_pos = bid | ||
.chain_data | ||
.transaction | ||
.message | ||
.static_account_keys() | ||
.iter() | ||
.position(|p| p.eq(&user_wallet)) | ||
.expect("User wallet not found in transaction"); | ||
bid.chain_data.transaction.signatures[user_signature_pos] = input.user_signature; | ||
|
||
// TODO add relayer signature after program update | ||
// self.add_relayer_signature(&mut bid); | ||
|
||
// TODO change it to a better state (Wait for user signature) | ||
if bid.status.is_submitted() { | ||
if bid.chain_data.bid_payment_instruction_type | ||
== entities::BidPaymentInstructionType::Swap | ||
{ | ||
self.send_transaction(&bid).await.map_err(|e| { | ||
tracing::error!(error = ?e, "Error sending quote transaction to network"); | ||
RestError::TemporarilyUnavailable | ||
})?; | ||
Ok(bid.chain_data.transaction) | ||
} else { | ||
Err(RestError::BadParameters("Invalid quote".to_string())) | ||
} | ||
} else { | ||
Err(RestError::BadParameters( | ||
"Quote is not valid anymore".to_string(), | ||
)) | ||
} | ||
} | ||
None => Err(RestError::BadParameters( | ||
"Quote is not valid anymore".to_string(), | ||
)), | ||
} | ||
} | ||
} |
Oops, something went wrong.