Concordium / concordium-rust-sdk

Rust SDK for the Concordium blockchain.
https://docs.rs/concordium-rust-sdk/latest/concordium_rust_sdk/
Mozilla Public License 2.0
13 stars 18 forks source link

Unable to use the AccountInfo struct #124

Closed swastkk closed 1 year ago

swastkk commented 1 year ago

Bug Description Unable to use the AccountInfo Struct after declaring a variable

 let account_info_response = client.get_account_info(acc, &block).await?;

Steps to Reproduce

move |acc| {
        let mut client = client.clone();
        async move {
            let block = finalized_block;
            let account_info_response = client.get_account_info(acc, &block).await?;

            // Process the account info as needed
            if let Some(account_info) = account_info_response.response {
                // Access the account_amount field
                let account_amount = account_info
                    .account_amount
                    .expect("Account amount not found"); // Handle None if needed
                println!("Account Amount: {:?}", account_amount);
                Ok::<_, anyhow::Error>(account_amount)
            } else {
                eprintln!("No account info found for account: {:?}", acc);
                // Handle the case where account_info_response.response is None
                // You can return an appropriate value or error here
                // For now, returning 0 as an example
                Ok::<_, anyhow::Error>(0.into())
            }
        }
    }

Expected Result

I wanted to get the Struct fields like account_amount , account_address Actual Result Error

mismatched types
expected struct `AccountInfo`
     found enum `std::option::Option<_>`rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B15%5D?15#file%3A%2F%2F%2Fhome%2Fswastik%2FDesktop%2Fconcordium-lc1c%2Fsrc-tauri%2Fsrc%2Fmain.rs)
main.rs(424, 41): this expression has type `AccountInfo`
main.rs(424, 41): you might have meant to use field `account_stake` whose type is `std::option::Option<AccountStakingInfo>`: `account_info_response.response.account_stake`
account_info: {unknown}

Versions

abizjak commented 1 year ago

https://docs.rs/concordium-rust-sdk/latest/concordium_rust_sdk/v2/struct.Client.html#method.get_account_info

The get_account_info returns an AccountInfo. If the account is not found then an Err is returned (and you can check if it is not found using https://docs.rs/concordium-rust-sdk/latest/concordium_rust_sdk/endpoints/enum.QueryError.html#method.is_not_found )

So

let ai = client.get_account_info(acc, &block).await;
match ai {
   Ok(ai) => {

                let account_amount = ai.response
                    .account_amount;
                println!("Account Amount: {:?}", account_amount);
                Ok::<_, anyhow::Error>(account_amount)
   }
   Err(e) if e.is_not_found() => {

                eprintln!("No account info found for account: {:?}", acc);
                // Handle the case where account_info_response.response is None
                // You can return an appropriate value or error here
                // For now, returning 0 as an example
                Ok::<_, anyhow::Error>(0.into())
   }
   Err(e) => return Err(e)
}
swastkk commented 1 year ago

Also getting this

no method named `expect` found for struct `concordium_rust_sdk::smart_contracts::concordium_contracts_common::Amount` in the current scope
abizjak commented 1 year ago

I removed the .expect. I did not notice it and just copy pasted your response.

swastkk commented 1 year ago

This is my complete code but I am getting a large error Can you help me out to fix this.

async fn consens_info() -> anyhow::Result<()>{
    let endpoint_node = Endpoint::from_str("http://127.0.0.1:20100")?;
    let mut client = v2::Client::new(endpoint_node)
        .await?;

    let info = client.get_consensus_info().await?;
    let finalized_block = info.last_finalized_block;

    println!("Listing accounts in block {}.", finalized_block);
    move |acc| {
        let mut client = client.clone();
        let block = finalized_block;
        async move {
            let ai = client.get_account_info(acc, &block).await;
            match ai {
                Ok(ai) => {
                    let account_amount = ai.response
                        .account_amount;
                    println!("Account Amount: {:?}", account_amount);
                    Ok::<_, anyhow::Error>(account_amount)
                }
                Err(err) => {
                    // Handle specific error cases
                    if err.is_not_found() {
                        eprintln!("No account info found for account: {:?}", acc);
                        // Handle the case where account_info_response.response is None
                        // You can return an appropriate value or error here
                        // For now, returning 0 as an example
                        Ok::<_, anyhow::Error>(0.into())
                    } else {
                        // Handle other errors
                        Err(err.into())
                    }
                }
            }
        }
    }

}
abizjak commented 1 year ago

So this block


    move |acc| {
        let mut client = client.clone();
        let block = finalized_block;
        async move {
            let ai = client.get_account_info(acc, &block).await;
            match ai {
                Ok(ai) => {
                    let account_amount = ai.response
                        .account_amount;
                    println!("Account Amount: {:?}", account_amount);
                    Ok::<_, anyhow::Error>(account_amount)
                }
                Err(err) => {
                    // Handle specific error cases
                    if err.is_not_found() {
                        eprintln!("No account info found for account: {:?}", acc);
                        // Handle the case where account_info_response.response is None
                        // You can return an appropriate value or error here
                        // For now, returning 0 as an example
                        Ok::<_, anyhow::Error>(0.into())
                    } else {
                        // Handle other errors
                        Err(err.into())
                    }
                }
            }
        }
    }

returns a closure, but the return type of your function states that it is a Result<()>.

What do you want your function to do exactly?

swastkk commented 1 year ago

I want my func to return the account_amount in a variable from the last finalized_block of the chain.