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
use std::error::Error;

use flow_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
    let mut client = TonicHyperFlowClient::mainnet().await?;
    client.ping().await?;

    let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
    let start_height = latest_block_height - 100;

    println!(
        "Searching for accounts created within the last 100 blocks ({}-{})...",
        start_height, latest_block_height
    );

    let mut accounts = Vec::new();

    for events in client
        .events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
        .await?
        .results
        .iter()
    {
        for event in events.events.iter() {
            let val: cadence_json::ValueOwned = serde_json::from_slice(&event.payload)?;

            if let cadence_json::ValueOwned::Event(c) = val {
                for field in c.fields.into_iter().filter(|f| f.name == "address") {
                    if let cadence_json::ValueOwned::Address(addr) = field.value {
                        accounts.push(client.account_at_latest_block(&addr.data).await?);
                    }
                }
            }
        }
    }

    println!("Found {} accounts.", accounts.len());

    if let Some(acc) = accounts.into_iter().max_by_key(|acc| acc.balance) {
        println!(
            "\nThe richest account is 0x{} with a balance of {}.",
            hex::encode(acc.address),
            acc.balance
        );
    }

    Ok(())
}