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
use std::str::FromStr;

use anyhow::{bail, Result};
use serde::Deserialize;
use serde_json::json;
use solana_client::{rpc_client::RpcClient, rpc_request::RpcRequest};
use solana_program::pubkey::Pubkey;

pub fn get_nft_token_account(client: &RpcClient, mint: &str) -> Result<Pubkey> {
    let request = RpcRequest::Custom {
        method: "getTokenLargestAccounts",
    };
    let params = json!([mint.to_string(), { "commitment": "confirmed" }]);
    let result: JRpcResponse = client.send(request, params)?;

    let token_accounts: Vec<TokenAccount> = result
        .value
        .into_iter()
        .filter(|account| account.amount.parse::<u64>().unwrap() == 1)
        .collect();

    if token_accounts.len() > 1 {
        bail!(
            "Mint account {} had more than one token account with 1 token",
            mint
        );
    }

    if token_accounts.is_empty() {
        bail!("Mint account {} had zero token accounts with 1 token", mint);
    }

    let token_pubkey = Pubkey::from_str(&token_accounts[0].address)?;

    Ok(token_pubkey)
}

#[derive(Debug, Deserialize)]
pub struct JRpcResponse {
    value: Vec<TokenAccount>,
}

#[derive(Debug, Deserialize)]
pub struct TokenAccount {
    pub address: String,
    pub amount: String,
    pub decimals: u8,
    #[serde(rename = "uiAmount")]
    pub ui_amount: f32,
    #[serde(rename = "uiAmountString")]
    pub ui_amount_string: String,
}