-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathutils.rs
More file actions
59 lines (54 loc) · 1.96 KB
/
utils.rs
File metadata and controls
59 lines (54 loc) · 1.96 KB
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
use clap_v3::ArgMatches;
use colored::Colorize;
use eyre::eyre;
use solana_clap_v3_utils::keypair::signer_from_path;
use solana_sdk::signature::read_keypair_file;
use solana_sdk::{signer::keypair::Keypair, signer::Signer, transaction::VersionedTransaction};
use squads_multisig::solana_client::nonblocking::rpc_client::RpcClient;
use squads_multisig::solana_client::{
client_error::ClientErrorKind,
rpc_request::{RpcError, RpcResponseErrorData},
rpc_response::RpcSimulateTransactionResult,
};
pub fn create_keypair_from_path(
keypair_path: String,
) -> Result<Keypair, Box<dyn std::error::Error>> {
read_keypair_file(&keypair_path).map_err(|e| e.into())
}
pub fn create_signer_from_path(
keypair_path: String,
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
let mut wallet_manager = None;
let matches = ArgMatches::default();
signer_from_path(&matches, &keypair_path, "Keypair", &mut wallet_manager)
}
pub async fn send_and_confirm_transaction(
transaction: &VersionedTransaction,
rpc_client: &RpcClient,
) -> eyre::Result<String> {
// Try to send and confirm the transaction
match rpc_client.send_and_confirm_transaction(transaction).await {
Ok(signature) => {
println!(
"Transaction confirmed: {}\n\n",
signature.to_string().green()
);
Ok(signature.to_string())
}
Err(err) => {
if let ClientErrorKind::RpcError(RpcError::RpcResponseError {
data:
RpcResponseErrorData::SendTransactionPreflightFailure(
RpcSimulateTransactionResult {
logs: Some(logs), ..
},
),
..
}) = &err.kind
{
println!("Simulation logs:\n\n{}\n", logs.join("\n").yellow());
}
Err(eyre!("Transaction failed: {}", err.to_string().red()))
}
}
}