-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
endpoint swap-instructions implementation (#14)
* endpoint swap-instructions * cargo format + bump version * cargo clippy * better error * no need to clone, remained from tests
- Loading branch information
Showing
6 changed files
with
245 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use jup_ag::{QuoteConfig, SwapRequest}; | ||
use solana_sdk::{pubkey, signature::Keypair, signature::Signer}; | ||
use spl_token::{amount_to_ui_amount, ui_amount_to_amount}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let sol = pubkey!("So11111111111111111111111111111111111111112"); | ||
let msol = pubkey!("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"); | ||
|
||
let keypair = Keypair::new(); | ||
|
||
let slippage_bps = 100; | ||
let only_direct_routes = false; | ||
let quotes = jup_ag::quote( | ||
sol, | ||
msol, | ||
ui_amount_to_amount(0.01, 9), | ||
QuoteConfig { | ||
only_direct_routes, | ||
slippage_bps: Some(slippage_bps), | ||
..QuoteConfig::default() | ||
}, | ||
) | ||
.await?; | ||
|
||
let route = quotes.route_plan[0] | ||
.swap_info | ||
.label | ||
.clone() | ||
.unwrap_or_else(|| "Unknown DEX".to_string()); | ||
println!( | ||
"Quote: {} SOL for {} mSOL via {} (worst case with slippage: {}). Impact: {:.2}%", | ||
amount_to_ui_amount(quotes.in_amount, 9), | ||
amount_to_ui_amount(quotes.out_amount, 9), | ||
route, | ||
amount_to_ui_amount(quotes.other_amount_threshold, 9), | ||
quotes.price_impact_pct * 100. | ||
); | ||
|
||
let request: SwapRequest = SwapRequest::new(keypair.pubkey(), quotes.clone()); | ||
|
||
let swap_instructions = jup_ag::swap_instructions(request).await?; | ||
|
||
println!("Swap Instructions: {:?}", swap_instructions); | ||
|
||
Ok(()) | ||
} |
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,104 @@ | ||
// Deserialize Instruction with a custom function | ||
pub mod instruction { | ||
use serde::{Deserialize, Deserializer}; | ||
use solana_sdk::{instruction::AccountMeta, instruction::Instruction, pubkey::Pubkey}; | ||
use std::str::FromStr; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Instruction, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct InstructionFields { | ||
accounts: Vec<AccountMetaFields>, | ||
data: String, | ||
program_id: String, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct AccountMetaFields { | ||
pubkey: String, | ||
is_signer: bool, | ||
is_writable: bool, | ||
} | ||
|
||
let fields = InstructionFields::deserialize(deserializer)?; | ||
let program_id = Pubkey::from_str(&fields.program_id) | ||
.map_err(|e| serde::de::Error::custom(format!("Error parsing programId: {}", e)))?; | ||
|
||
let accounts = fields | ||
.accounts | ||
.into_iter() | ||
.map(|acc| { | ||
let pubkey = Pubkey::from_str(&acc.pubkey).map_err(|e| { | ||
serde::de::Error::custom(format!("Error parsing pubkey: {}", e)) | ||
})?; | ||
Ok(AccountMeta { | ||
pubkey, | ||
is_signer: acc.is_signer, | ||
is_writable: acc.is_writable, | ||
}) | ||
}) | ||
.collect::<Result<Vec<AccountMeta>, _>>()?; | ||
|
||
let instruction = Instruction { | ||
program_id, | ||
accounts, | ||
data: base64::decode(&fields.data) | ||
.map_err(|e| serde::de::Error::custom(format!("Error decoding data: {}", e)))?, | ||
}; | ||
|
||
Ok(instruction) | ||
} | ||
} | ||
|
||
// Deserialize Option<Instruction> with a custom function | ||
pub mod option_instruction { | ||
use serde::{Deserialize, Deserializer}; | ||
use solana_sdk::instruction::Instruction; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Instruction>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let value: serde_json::Value = Deserialize::deserialize(deserializer)?; | ||
|
||
match value { | ||
serde_json::Value::Null => Ok(None), | ||
_ => crate::field_instruction::instruction::deserialize(value) | ||
.map_err(|e| { | ||
serde::de::Error::custom(format!( | ||
"Error deserialize optional instruction: {}", | ||
e | ||
)) | ||
}) | ||
.map(Some), | ||
} | ||
} | ||
} | ||
|
||
// Deserialize Vec<Instruction> with a custom function | ||
pub mod vec_instruction { | ||
use serde::{Deserialize, Deserializer}; | ||
use solana_sdk::instruction::Instruction; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Instruction>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let values: Vec<serde_json::Value> = Deserialize::deserialize(deserializer)?; | ||
let mut instructions = Vec::new(); | ||
|
||
for value in values { | ||
let instruction: Instruction = | ||
crate::field_instruction::instruction::deserialize(value).map_err(|e| { | ||
serde::de::Error::custom(format!("Error deserialize vec instruction: {}", e)) | ||
})?; | ||
instructions.push(instruction); | ||
} | ||
|
||
Ok(instructions) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
pub mod vec { | ||
use { | ||
serde::{de, Deserializer, Serializer}, | ||
serde::{Deserialize, Serialize}, | ||
solana_sdk::pubkey::Pubkey, | ||
std::str::FromStr, | ||
}; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Pubkey>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let vec_str: Vec<String> = Vec::deserialize(deserializer)?; | ||
let mut vec_pubkey = Vec::new(); | ||
for s in vec_str { | ||
let pubkey = Pubkey::from_str(&s).map_err(de::Error::custom)?; | ||
vec_pubkey.push(pubkey); | ||
} | ||
Ok(vec_pubkey) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn serialize<S>(vec_pubkey: &[Pubkey], serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let vec_str: Vec<String> = vec_pubkey.iter().map(|pubkey| pubkey.to_string()).collect(); | ||
vec_str.serialize(serializer) | ||
} | ||
} | ||
|
||
pub mod option { | ||
use { | ||
serde::{Serialize, Serializer}, | ||
solana_sdk::pubkey::Pubkey, | ||
}; | ||
|
||
pub fn serialize<S>(t: &Option<Pubkey>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match t { | ||
Some(pubkey) => pubkey.to_string().serialize(serializer), | ||
None => serializer.serialize_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