Repository navigation
Implement bitget /swap adapter for solana solver #5107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AryanGodara
wants to merge
5
commits into
aryan/be-252-bitget-scaffold
Choose a base branch
from
aryan/be-253-bitget-adapter
base: aryan/be-252-bitget-scaffold
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+509
−88
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
93fdc01
implement bitget /swap adapter for solana solver
AryanGodara 7101ec8
Merge remote-tracking branch 'origin/main' into aryan/be-253-bitget-a…
AryanGodara 26c247f
Merge remote-tracking branch 'origin/aryan/be-252-bitget-scaffold' in…
AryanGodara eba01f4
Merge remote-tracking branch 'origin/aryan/be-252-bitget-scaffold' in…
AryanGodara 7a49dad
address review: tolerate missing endpoint slash, tighten bitget swap …
AryanGodara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,240 @@ | ||
| //! DTOs for Bitget's `/swap` request and response. | ||
|
|
||
| use { | ||
| crate::dex::{Error, Order, Swap, dto::ApiInstruction}, | ||
| serde::{Deserialize, Serialize}, | ||
| serde_with::{DisplayFromStr, serde_as}, | ||
| solana_sdk::pubkey::Pubkey, | ||
| spl_associated_token_account_interface::address::get_associated_token_address_with_program_id, | ||
| spl_token_2022_interface::inline_spl_token, | ||
| }; | ||
|
|
||
| /// Bitget's aggregator. Naming it lets `/swap` skip the `/quote` call. | ||
| const MARKET: &str = "bgwaggregator"; | ||
| /// Bitget's smallest fee. Without a fee its route also needs Bitget's | ||
| /// co-signature, and the settlement has a single signer. | ||
| const FEE_RATE: f64 = 0.0001; | ||
|
|
||
| /// Body of the `/swap` request for a sell order, amounts in atoms. | ||
| #[serde_as] | ||
| #[derive(Serialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct SwapRequest { | ||
| from_chain: &'static str, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| from_contract: Pubkey, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| from_amount: u64, | ||
| to_chain: &'static str, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| to_contract: Pubkey, | ||
| amount_unit: &'static str, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| from_address: Pubkey, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| to_address: Pubkey, | ||
| market: &'static str, | ||
| /// Percent. | ||
| slippage: f64, | ||
| fee_rate: f64, | ||
| request_mod: &'static str, | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| sol_max_accounts: u16, | ||
| } | ||
|
|
||
| impl SwapRequest { | ||
| pub fn new(order: &Order, taker: &Pubkey, slippage_bps: u16, max_accounts: u16) -> Self { | ||
| Self { | ||
| from_chain: "sol", | ||
| from_contract: order.sell_mint, | ||
| from_amount: order.sell_amount, | ||
| to_chain: "sol", | ||
| to_contract: order.buy_mint, | ||
| amount_unit: "wei", | ||
| from_address: *taker, | ||
| to_address: *taker, | ||
| market: MARKET, | ||
| slippage: f64::from(slippage_bps) / 100.0, | ||
| fee_rate: FEE_RATE, | ||
| request_mod: "rich", | ||
| sol_max_accounts: max_accounts, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Bitget's response wrapper: `status` 0 carries `data`, any other an | ||
| /// [`error_code`](https://web3.bitget.com/en/docs/swap-order#error-code-list). | ||
| #[derive(Deserialize)] | ||
| pub struct Envelope { | ||
| status: i64, | ||
| error_code: Option<i64>, | ||
| msg: Option<String>, | ||
| data: Option<serde_json::Value>, | ||
| } | ||
|
|
||
| impl Envelope { | ||
| pub fn into_data(self, http_status: u16) -> Result<SwapResponse, Error> { | ||
| if self.status != 0 { | ||
| let code = self.error_code.unwrap_or_default(); | ||
| return Err(match code { | ||
| // No route, or a Bitget-side failure the next auction retries. | ||
| 80000 | 80001 | 80004 | 80005 | 80008..=80012 | 80014 | 80020 => Error::NotFound, | ||
| _ => Error::Api { | ||
| status: http_status, | ||
| body: format!("error_code {code}: {}", self.msg.unwrap_or_default()), | ||
| }, | ||
| }); | ||
| } | ||
| let data = self.data.ok_or(Error::NotFound)?; | ||
| serde_json::from_value(data).map_err(|err| Error::BadResponse(format!("swap data: {err}"))) | ||
| } | ||
| } | ||
|
|
||
| /// The parts of a rich-mode `/swap` response we need. `minAmount` is the | ||
| /// output after Bitget's fee, which its program enforces on chain. | ||
| #[serde_as] | ||
| #[derive(Deserialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct SwapResponse { | ||
| #[serde_as(as = "DisplayFromStr")] | ||
| min_amount: u64, | ||
| instruction_lists: Vec<ApiInstruction>, | ||
| #[serde_as(as = "Vec<DisplayFromStr>")] | ||
| address_lookup_table_account: Vec<Pubkey>, | ||
| } | ||
|
|
||
| impl SwapResponse { | ||
| /// Bitget pays the taker's buy-mint ATA, so a transfer of `minAmount` moves | ||
| /// the output on to `order.buy_destination` and positive slippage stays | ||
| /// with the taker. ComputeBudget instructions are dropped: the driver sets | ||
| /// its own. | ||
| pub fn into_swap(self, order: &Order, taker: &Pubkey) -> Result<Swap, Error> { | ||
| let mut instructions = Vec::with_capacity(self.instruction_lists.len() + 1); | ||
| for instruction in self.instruction_lists { | ||
| let instruction = instruction.into_instruction()?; | ||
| if instruction.program_id != solana_compute_budget_interface::ID { | ||
| instructions.push(instruction); | ||
| } | ||
| } | ||
| let accounts = || { | ||
| instructions | ||
| .iter() | ||
| .flat_map(|instruction| &instruction.accounts) | ||
| }; | ||
| if accounts().any(|account| account.is_signer && account.pubkey != *taker) { | ||
| return Err(Error::BadResponse( | ||
| "route needs a signer besides the taker".into(), | ||
| )); | ||
| } | ||
| let (token_program, output) = [inline_spl_token::ID, spl_token_2022_interface::ID] | ||
| .into_iter() | ||
| .map(|program| { | ||
| let ata = | ||
| get_associated_token_address_with_program_id(taker, &order.buy_mint, &program); | ||
| (program, ata) | ||
| }) | ||
| .find(|(_, ata)| accounts().any(|account| account.pubkey == *ata)) | ||
| .ok_or_else(|| Error::BadResponse("route doesn't pay the taker's ATA".into()))?; | ||
| // Unchecked because the auction carries no decimals. Token-2022 mints | ||
| // with a transfer fee or hook reject it, and simulation drops them. | ||
| #[allow(deprecated)] | ||
| let transfer = spl_token_2022_interface::instruction::transfer( | ||
| &token_program, | ||
| &output, | ||
| &order.buy_destination, | ||
| taker, | ||
| &[], | ||
| self.min_amount, | ||
| ) | ||
| .expect("both token programs pass the program check"); | ||
| instructions.push(transfer); | ||
| Ok(Swap { | ||
| in_amount: order.sell_amount, | ||
| out_amount: self.min_amount, | ||
| instructions, | ||
| address_lookup_tables: self.address_lookup_table_account, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, crate::dex::Side}; | ||
|
|
||
| fn order() -> Order { | ||
| Order { | ||
| sell_mint: Pubkey::new_unique(), | ||
| buy_mint: Pubkey::new_unique(), | ||
| buy_destination: Pubkey::new_unique(), | ||
| sell_amount: 1_000, | ||
| buy_amount: 1, | ||
| side: Side::Sell, | ||
| } | ||
| } | ||
|
|
||
| fn response( | ||
| order: &Order, | ||
| taker: &Pubkey, | ||
| token_program: &Pubkey, | ||
| extra_signer: Option<Pubkey>, | ||
| ) -> SwapResponse { | ||
| let output = | ||
| get_associated_token_address_with_program_id(taker, &order.buy_mint, token_program); | ||
| let mut accounts = vec![ | ||
| serde_json::json!({"pubkey": taker.to_string(), "isSigner": true, "isWritable": true}), | ||
| serde_json::json!({"pubkey": output.to_string(), "isSigner": false, "isWritable": true}), | ||
| ]; | ||
| if let Some(signer) = extra_signer { | ||
| accounts.push(serde_json::json!({"pubkey": signer.to_string(), "isSigner": true, "isWritable": false})); | ||
| } | ||
| serde_json::from_value(serde_json::json!({ | ||
| "minAmount": "990", | ||
| "instructionLists": [ | ||
| {"programId": solana_compute_budget_interface::ID.to_string(), "accounts": [], "data": "AnBkCAA="}, | ||
| {"programId": Pubkey::new_unique().to_string(), "accounts": accounts, "data": "AQ=="}, | ||
| ], | ||
| "addressLookupTableAccount": [Pubkey::new_unique().to_string()], | ||
| })) | ||
| .unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn moves_the_minimum_output_into_the_buffer() { | ||
| let taker = Pubkey::new_unique(); | ||
| let order = order(); | ||
| for program in [inline_spl_token::ID, spl_token_2022_interface::ID] { | ||
| let swap = response(&order, &taker, &program, None) | ||
| .into_swap(&order, &taker) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!((swap.in_amount, swap.out_amount), (1_000, 990)); | ||
| assert_eq!(swap.address_lookup_tables.len(), 1); | ||
| // ComputeBudget dropped: the route, then the transfer. | ||
| assert_eq!(swap.instructions.len(), 2); | ||
| let transfer = &swap.instructions[1]; | ||
| assert_eq!(transfer.program_id, program); | ||
| let paid = | ||
| get_associated_token_address_with_program_id(&taker, &order.buy_mint, &program); | ||
| assert_eq!(transfer.accounts[0].pubkey, paid); | ||
| assert_eq!(transfer.accounts[1].pubkey, order.buy_destination); | ||
| assert_eq!(transfer.accounts[2].pubkey, taker); | ||
| assert_eq!(transfer.data[1..], 990u64.to_le_bytes()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_a_route_needing_another_signer() { | ||
| let taker = Pubkey::new_unique(); | ||
| let order = order(); | ||
| let response = response( | ||
| &order, | ||
| &taker, | ||
| &inline_spl_token::ID, | ||
| Some(Pubkey::new_unique()), | ||
| ); | ||
| assert!(matches!( | ||
| response.into_swap(&order, &taker), | ||
| Err(Error::BadResponse(_)) | ||
| )); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike the Jupiter DTO (which marks
address_lookup_table_addresses,setup_instructions, andcleanup_instructionwith#[serde(default)]), neitherinstruction_listsnoraddress_lookup_table_accounthas a default here. If Bitget ever omits (or sendsnullfor)addressLookupTableAccount— e.g. a direct route that uses no lookup tables — deserialization fails outright and the order is silently dropped asBadResponseinstead of being quoted. Consider adding#[serde(default)]to be defensive and consistent with the Jupiter adapter:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Every /swap response i saw had addressLookupTableAccount, and a missing field already logs as quote failed.