helius-labs/helius-rust-sdk

I'm trying to print out only the token amount and address

Closed this issue · 1 comments

I am trying to get some token information of a particular address.

I just started working with Rust

Thank you very much

use helius::error::HeliusError;
use helius::Helius;
use helius::types::{Cluster, GetTokenAccounts, TokenAccountsList};
use crate::walletgen;

#[tokio::main]
pub async fn get_balance() -> Result<(), HeliusError> {
    // Loads api key
    // Select network
    let cluster: Cluster = Cluster::MainnetBeta;

    // Load address from memory
    let owner_address = Some("2RtGg6fsFiiF1EQzHqbd66AhW7R5bWeQGpTbv2UMkCdW".to_string());
    // let owner_address = Some(walletgen::Address::get_stored_address().to_string());

    let helius: Helius = Helius::new(api_key, cluster).unwrap();

    let request: GetTokenAccounts = GetTokenAccounts {
        owner: owner_address,
        page: Some(1),
        limit: Some(1000),
        ..Default::default()
    };

    // TODO: remove the excess infomation coming from the reposnse body
    let response: TokenAccountsList = helius.rpc().get_token_accounts(request).await?;

    if !response.token_accounts.is_empty() {
        for token_account in response.token_accounts {
            println!("Token Address: {:?} - Amount: {:?}", token_account.address, token_account.amount.unwrap_or_default());
        }
    } else {
        println!("A fresh start.. Let's go fishing.. ;)");
    }

    Ok(())
}

gm! You're very close! Here's the updated code:

use helius::error::Result;
use helius::Helius;
use helius::types::{Cluster, GetTokenAccounts, TokenAccountsList};

#[tokio::main]
pub async fn main() -> Result<()> {
    let api_key: &str = "api-key";
    let cluster: Cluster = Cluster::MainnetBeta;

    let owner_address: Option<String> = Some("2RtGg6fsFiiF1EQzHqbd66AhW7R5bWeQGpTbv2UMkCdW".to_string());

    let helius: Helius = Helius::new(api_key, cluster).unwrap();

    let request: GetTokenAccounts = GetTokenAccounts {
        owner: owner_address,
        page: Some(1),
        limit: Some(1000),
        ..Default::default()
    };

    let response: TokenAccountsList = helius.rpc().get_token_accounts(request).await?;

    if !response.token_accounts.is_empty() {
        for token_account in response.token_accounts {
            println!("Token Address: {:?} - Amount: {:?}", token_account.address, token_account.amount.unwrap_or_default());
        }
    } else {
        println!("A fresh start.. Let's go fishing.. ;)");
    }

    Ok(())
}

The main difference here is that the main function is called main — if you try doing cargo run with your "main" function designated by tokio as get_balance, you'll run into errors. You can also clean up its return type using the Result type alias, which returns <T, HeliusError> to shorten the code.

I'm going to go ahead and close this issue. If you have any additional questions, please come to the Helius discord and ask there. Thanks, and happy coding!