Skip to content

Commit

Permalink
server changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anihamde committed Jan 7, 2025
1 parent 698624a commit 08a6a02
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 343 deletions.
106 changes: 58 additions & 48 deletions auction-server/api-types/src/opportunity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct OpportunityBidResult {
#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ProgramSvm {
Phantom,
SwapKamino,
Limo,
}

Expand Down Expand Up @@ -206,18 +206,15 @@ pub enum OpportunityCreateProgramParamsV1Svm {
#[serde_as(as = "DisplayFromStr")]
order_address: Pubkey,
},
/// Phantom program specific parameters for the opportunity.
#[serde(rename = "phantom")]
#[schema(title = "phantom")]
Phantom {
/// Kamino swap program specific parameters for the opportunity.
#[serde(rename = "kamino_swap")]
#[schema(title = "kamino_swap")]
KaminoSwap {
// TODO*: we should make this more generic, a la `Swap`
/// The user wallet address which requested the quote from the wallet.
#[schema(example = "DUcTi3rDyS5QEmZ4BNRBejtArmDCWaPYGfN44vBJXKL5", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
user_wallet_address: Pubkey,

/// The maximum slippage percentage that the user is willing to accept.
#[schema(example = 0.5, value_type = f64)]
maximum_slippage_percentage: f64,
},
}

Expand Down Expand Up @@ -311,19 +308,15 @@ pub enum OpportunityParamsV1ProgramSvm {
#[serde_as(as = "DisplayFromStr")]
order_address: Pubkey,
},
/// Phantom program specific parameters for the opportunity.
#[serde(rename = "phantom")]
#[schema(title = "phantom")]
Phantom {
/// Swap program specific parameters for the opportunity.
#[serde(rename = "swap")]
#[schema(title = "swap")]
Swap {
/// The user wallet address which requested the quote from the wallet.
#[schema(example = "DUcTi3rDyS5QEmZ4BNRBejtArmDCWaPYGfN44vBJXKL5", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
user_wallet_address: Pubkey,

/// The maximum slippage percentage that the user is willing to accept.
#[schema(example = 0.5, value_type = f64)]
maximum_slippage_percentage: f64,

/// The permission account to be permitted by the ER contract for the opportunity execution of the protocol.
#[schema(example = "DUcTi3rDyS5QEmZ4BNRBejtArmDCWaPYGfN44vBJXKL5", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
Expand All @@ -334,11 +327,21 @@ pub enum OpportunityParamsV1ProgramSvm {
#[serde_as(as = "DisplayFromStr")]
router_account: Pubkey,

/// The token searcher will send.
sell_token: TokenAmountSvm,
/// Details about the tokens to be swapped. Either the input token amount or the output token amount must be specified.
#[schema(inline)]
tokens: QuoteTokens,
},
}

/// The token searcher will receive.
buy_token: TokenAmountSvm,
#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug, ToResponse)]
pub enum QuoteTokens {
InputTokenSpecified {
input_token: TokenAmountSvm,
output_token: Pubkey,
},
OutputTokenSpecified {
input_token: Pubkey,
output_token: TokenAmountSvm,
},
}

Expand Down Expand Up @@ -466,48 +469,58 @@ pub struct OpportunityBidEvm {
pub signature: Signature,
}

/// Parameters needed to create a new opportunity from the Phantom wallet.
/// Parameters needed to create a new opportunity from the swap request.
/// Auction server will extract the output token price for the auction.
#[serde_as]
#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
pub struct QuoteCreatePhantomV1Svm {
pub struct QuoteCreateV1SvmParams {
/// The user wallet address which requested the quote from the wallet.
#[schema(example = "DUcTi3rDyS5QEmZ4BNRBejtArmDCWaPYGfN44vBJXKL5", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
pub user_wallet_address: Pubkey,
pub user_wallet_address: Pubkey,
/// The token mint address of the input token.
#[schema(example = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
pub input_token_mint: Pubkey,
pub input_token_mint: Pubkey,
/// The token mint address of the output token.
#[schema(example = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
pub output_token_mint: Pubkey,
/// The input token amount that the user wants to swap.
#[schema(example = 100)]
pub input_token_amount: u64,
/// The maximum slippage percentage that the user is willing to accept.
#[schema(example = 0.5)]
pub maximum_slippage_percentage: f64,
pub output_token_mint: Pubkey,
/// The token amount that the user wants to swap out of/into.
#[schema(inline)]
pub specified_token_amount: SpecifiedTokenAmount,
/// The router account to send referral fees to.
#[schema(example = "DUcTi3rDyS5QEmZ4BNRBejtArmDCWaPYGfN44vBJXKL5", value_type = String)]
#[serde_as(as = "DisplayFromStr")]
pub router: Pubkey,
/// The chain id for creating the quote.
#[schema(example = "solana", value_type = String)]
pub chain_id: ChainId,
pub chain_id: ChainId,
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
#[serde(tag = "program")]
pub enum QuoteCreateV1Svm {
#[serde(rename = "phantom")]
#[schema(title = "phantom")]
Phantom(QuoteCreatePhantomV1Svm),
#[serde(tag = "side")]
pub enum SpecifiedTokenAmount {
#[serde(rename = "input")]
#[schema(title = "input")]
InputToken {
#[schema(example = 100)]
amount: u64,
},
#[serde(rename = "output")]
#[schema(title = "output")]
OutputToken {
#[schema(example = 50)]
amount: u64,
},
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
#[serde(tag = "version")]
pub enum QuoteCreateSvm {
#[serde(rename = "v1")]
#[schema(title = "v1")]
V1(QuoteCreateV1Svm),
V1(QuoteCreateV1SvmParams),
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
Expand All @@ -522,20 +535,17 @@ pub struct QuoteV1Svm {
/// The signed transaction for the quote to be executed on chain which is valid until the expiration time.
#[schema(example = "SGVsbG8sIFdvcmxkIQ==", value_type = String)]
#[serde(with = "crate::serde::transaction_svm")]
pub transaction: VersionedTransaction,
pub transaction: VersionedTransaction,
/// The expiration time of the quote (in seconds since the Unix epoch).
#[schema(example = 1_700_000_000_000_000i64, value_type = i64)]
pub expiration_time: i64,
pub expiration_time: i64,
/// The input token amount that the user wants to swap.
pub input_token: TokenAmountSvm,
pub input_token: TokenAmountSvm,
/// The output token amount that the user will receive.
pub output_token: TokenAmountSvm,
/// The maximum slippage percentage that the user is willing to accept.
#[schema(example = 0.5)]
pub maximum_slippage_percentage: f64,
pub output_token: TokenAmountSvm,
/// The chain id for the quote.
#[schema(example = "solana", value_type = String)]
pub chain_id: ChainId,
pub chain_id: ChainId,
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
Expand All @@ -558,7 +568,7 @@ impl OpportunityCreateSvm {
match self {
OpportunityCreateSvm::V1(params) => match &params.program_params {
OpportunityCreateProgramParamsV1Svm::Limo { .. } => ProgramSvm::Limo,
OpportunityCreateProgramParamsV1Svm::Phantom { .. } => ProgramSvm::Phantom,
OpportunityCreateProgramParamsV1Svm::KaminoSwap { .. } => ProgramSvm::SwapKamino,
},
}
}
Expand Down
46 changes: 42 additions & 4 deletions auction-server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ fn build_evm_contracts() {
}

const SUBMIT_BID_INSTRUCTION_SVM: &str = "submit_bid";
const PERMISSION_ACCOUNT_SVM: &str = "permission";
const ROUTER_ACCOUNT_SVM: &str = "router";
const SUBMIT_BID_PERMISSION_ACCOUNT_SVM: &str = "permission";
const SUBMIT_BID_ROUTER_ACCOUNT_SVM: &str = "router";

const SWAP_INSTRUCTION_SVM: &str = "swap";
const SWAP_ROUTER_ACCOUNT_SVM: &str = "router_fee_receiver_ta";
const SWAP_USER_WALLET_ACCOUNT_SVM: &str = "trader";
const SWAP_MINT_INPUT_ACCOUNT_SVM: &str = "mint_input";
const SWAP_MINT_OUTPUT_ACCOUNT_SVM: &str = "mint_output";
const IDL_LOCATION: &str = "../contracts/svm/target/idl/express_relay.json";

fn extract_account_position(idl: Idl, instruction_name: &str, account_name: &str) -> usize {
Expand Down Expand Up @@ -71,15 +77,47 @@ fn verify_and_extract_idl_data() {
extract_account_position(
express_relay_idl.clone(),
SUBMIT_BID_INSTRUCTION_SVM,
PERMISSION_ACCOUNT_SVM,
SUBMIT_BID_PERMISSION_ACCOUNT_SVM,
)
);
println!(
"cargo:rustc-env=SUBMIT_BID_ROUTER_ACCOUNT_POSITION={}",
extract_account_position(
express_relay_idl.clone(),
SUBMIT_BID_INSTRUCTION_SVM,
ROUTER_ACCOUNT_SVM,
SUBMIT_BID_ROUTER_ACCOUNT_SVM,
)
);
println!(
"cargo::rustc-env=SWAP_ROUTER_ACCOUNT_POSITION={}",
extract_account_position(
express_relay_idl.clone(),
SWAP_INSTRUCTION_SVM,
SWAP_ROUTER_ACCOUNT_SVM,
)
);
println!(
"cargo::rustc-env=SWAP_USER_WALLET_ACCOUNT_POSITION={}",
extract_account_position(
express_relay_idl.clone(),
SWAP_INSTRUCTION_SVM,
SWAP_USER_WALLET_ACCOUNT_SVM,
)
);
println!(
"cargo::rustc-env=SWAP_MINT_INPUT_ACCOUNT_POSITION={}",
extract_account_position(
express_relay_idl.clone(),
SWAP_INSTRUCTION_SVM,
SWAP_MINT_INPUT_ACCOUNT_SVM,
)
);
println!(
"cargo::rustc-env=SWAP_MINT_OUTPUT_ACCOUNT_POSITION={}",
extract_account_position(
express_relay_idl.clone(),
SWAP_INSTRUCTION_SVM,
SWAP_MINT_OUTPUT_ACCOUNT_SVM,
)
);
}
Expand Down
5 changes: 3 additions & 2 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ pub async fn start_api(run_options: RunOptions, store: Arc<StoreNew>) -> Result<
api_types::bid::Bids,
api_types::SvmChainUpdate,
api_types::opportunity::SpecifiedTokenAmount,
api_types::opportunity::OpportunityBidEvm,
api_types::opportunity::OpportunityBidResult,
api_types::opportunity::OpportunityMode,
Expand All @@ -360,10 +361,10 @@ pub async fn start_api(run_options: RunOptions, store: Arc<StoreNew>) -> Result<
api_types::opportunity::OpportunityParamsV1Evm,
api_types::opportunity::QuoteCreate,
api_types::opportunity::QuoteCreateSvm,
api_types::opportunity::QuoteCreateV1Svm,
api_types::opportunity::QuoteCreatePhantomV1Svm,
api_types::opportunity::QuoteCreateV1SvmParams,
api_types::opportunity::Quote,
api_types::opportunity::QuoteSvm,
api_types::opportunity::QuoteTokens,
api_types::opportunity::QuoteV1Svm,
api_types::opportunity::OpportunityDelete,
api_types::opportunity::OpportunityDeleteSvm,
Expand Down
1 change: 1 addition & 0 deletions auction-server/src/auction/entities/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Auction<T: ChainTrait> {
pub bids: Vec<Bid<T>>,
}

#[derive(PartialEq)]
pub enum SubmitType {
ByServer,
ByOther,
Expand Down
12 changes: 8 additions & 4 deletions auction-server/src/auction/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ pub mod verification;
pub mod workers;

pub struct ExpressRelaySvm {
pub program_id: Pubkey,
pub relayer: Keypair,
pub permission_account_position: usize,
pub router_account_position: usize,
pub program_id: Pubkey,
pub relayer: Keypair,
pub permission_account_position_submit_bid: usize,
pub router_account_position_submit_bid: usize,
pub router_account_position_swap: usize,
pub user_wallet_account_position_swap: usize,
pub mint_input_account_position_swap: usize,
pub mint_output_account_position_swap: usize,
}

pub struct ConfigSvm {
Expand Down
Loading

0 comments on commit 08a6a02

Please sign in to comment.