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
mod anchor_extensions;
mod decimals;
mod testing;
pub use anchor_extensions::{AnchorClientErrorExt, GetProgramAccounts};
pub use decimals::ApplyDecimal;
pub use testing::{
admin_wallet, cluster, commitment_level, create_ata, create_token, create_wallet, mint_to,
user_wallet,
};
use anchor_lang::prelude::*;
use anyhow::{anyhow, Error};
use fehler::{throw, throws};
use solana_sdk::{signature::Keypair, signer::keypair::read_keypair_file};
use std::cmp::Ordering;
use std::path::PathBuf;
pub trait Duplicate {
fn clone(&self) -> Self;
}
impl Duplicate for Keypair {
fn clone(&self) -> Self {
Keypair::from_bytes(&self.to_bytes()).unwrap()
}
}
#[throws(Error)]
pub fn sort_token_pair((token_a, token_b): (Pubkey, Pubkey)) -> (Pubkey, Pubkey) {
match token_a.cmp(&token_b) {
Ordering::Less => (token_a, token_b),
Ordering::Equal => throw!(anyhow!("The pair of tokens are equal")),
Ordering::Greater => (token_b, token_a),
}
}
#[throws(Error)]
pub fn load_keypair(src: &str) -> Keypair {
let maybe_keypair = shellexpand::full(&src)
.map_err(|e| anyhow!(e))
.and_then(|path| -> Result<_, Error> { Ok(PathBuf::from(&*path).canonicalize()?) })
.and_then(|path| read_keypair_file(&path).map_err(|_| anyhow!("Cannot read keypair")));
match maybe_keypair {
Ok(keypair) => keypair,
Err(_) => Keypair::from_bytes(&bs58::decode(src).into_vec()?)?,
}
}