-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathswap.rs
123 lines (110 loc) · 4.02 KB
/
swap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use jup_ag::{QuoteConfig, SwapRequest};
use solana_sdk::transaction::VersionedTransaction;
use {
solana_client::nonblocking::rpc_client::RpcClient,
solana_sdk::{
commitment_config::CommitmentConfig,
hash::Hash,
pubkey,
signature::{read_keypair_file, Keypair, Signer},
},
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 = read_keypair_file("swap_example.json").unwrap_or_else(|err| {
println!("------------------------------------------------------------------------------------------------");
println!("Failed to read `swap_example.json`: {err}");
println!();
println!("An ephemeral keypair will be used instead. For a more realistic example, create a new keypair at");
println!("that location and fund it with a small amount of SOL.");
println!("------------------------------------------------------------------------------------------------");
println!();
Keypair::new()
});
let rpc_client = RpcClient::new_with_commitment(
"https://api.metaplex.solana.com".into(),
CommitmentConfig::confirmed(),
);
let msol_token_address =
spl_associated_token_account::get_associated_token_address(&keypair.pubkey(), &msol);
println!(
"Pre-swap SOL balance: {}",
amount_to_ui_amount(rpc_client.get_balance(&keypair.pubkey()).await?, 9)
);
println!(
"Pre-swap mSOL balance: {}",
amount_to_ui_amount(
rpc_client
.get_token_account_balance(&msol_token_address)
.await?
.amount
.parse::<u64>()?,
9
)
);
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 jup_ag::Swap {
mut swap_transaction,
last_valid_block_height: _,
} = jup_ag::swap(request).await?;
let recent_blockhash_for_swap: Hash = rpc_client.get_latest_blockhash().await?;
swap_transaction
.message
.set_recent_blockhash(recent_blockhash_for_swap); // Updating to latest blockhash to not error out
let swap_transaction = VersionedTransaction::try_new(swap_transaction.message, &[&keypair])?;
println!(
"Simulating swap transaction: {}",
swap_transaction.signatures[0]
);
let response = rpc_client.simulate_transaction(&swap_transaction).await?;
println!(" {:#?}", response.value);
println!("Sending transaction: {}", swap_transaction.signatures[0]);
let _ = rpc_client
.send_and_confirm_transaction_with_spinner(&swap_transaction)
.await?;
println!(
"Post-swap SOL balance: {}",
amount_to_ui_amount(rpc_client.get_balance(&keypair.pubkey()).await?, 9)
);
println!(
"Post-swap mSOL balance: {}",
amount_to_ui_amount(
rpc_client
.get_token_account_balance(&msol_token_address)
.await?
.amount
.parse::<u64>()?,
9
)
);
Ok(())
}