-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlist_nfts.rs
73 lines (62 loc) · 1.84 KB
/
list_nfts.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
use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
#[derive(serde::Deserialize)]
struct Env {
rpc_url: url::Url,
wallet_pubkey: String,
}
#[derive(serde::Deserialize)]
struct Parsed {
info: SplToken,
}
#[derive(serde::Deserialize)]
struct SplToken {
mint: String,
#[serde(rename(deserialize = "tokenAmount"))]
token_amount: Amount,
}
#[allow(dead_code)]
#[derive(serde::Deserialize)]
struct Amount {
amount: String,
#[serde(rename(deserialize = "uiAmountString"))]
ui_amount_string: String,
#[serde(rename(deserialize = "uiAmount"))]
ui_amount: f64,
decimals: u8,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let env = envy::from_env::<Env>()?;
let client = RpcClient::new(env.rpc_url.to_string());
let wallet: Pubkey = env.wallet_pubkey.parse()?;
let tokens = get_token_accounts(&client, &wallet)?;
let accounts: Vec<_> = tokens
.iter()
.filter_map(|x| {
if let solana_account_decoder::UiAccountData::Json(d) = &x.account.data {
Some(d)
} else {
None
}
})
.collect();
let parsed: Vec<_> = accounts
.iter()
.filter_map(|x| serde_json::from_value::<Parsed>(x.parsed.clone()).ok())
.filter(|x| x.info.token_amount.decimals == 0 && x.info.token_amount.ui_amount == 1.0)
.map(|x| x.info.mint)
.collect();
println!("NFTs owned by {}:", wallet.to_string());
println!("{:#?}", parsed);
Ok(())
}
fn get_token_accounts(
rpc: &RpcClient,
owner: &Pubkey,
) -> Result<Vec<solana_client::rpc_response::RpcKeyedAccount>, Box<dyn std::error::Error>> {
rpc.get_token_accounts_by_owner(
&owner,
solana_client::rpc_request::TokenAccountsFilter::ProgramId(spl_token::ID),
)
.map_err(|e| e.into())
}