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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use diem_client::{views::EventView, Client};
use diem_types::{
    account_address::AccountAddress,
    account_state::AccountState,
    account_state_blob::AccountStateBlob,
    event::{EventHandle, EventKey},
};
use futures::future::join_all;
use std::convert::TryFrom;

const BATCH_SIZE: u64 = 500;

pub struct DiemEventsFetcher(Client);

impl DiemEventsFetcher {
    pub fn new(url: &str) -> Result<Self> {
        Ok(Self(Client::new(url)))
    }

    async fn get_account_state(&self, account: AccountAddress) -> Result<Option<AccountState>> {
        let account_state: AccountStateBlob = {
            let blob = self
                .0
                .get_account_state_with_proof(account, None, None)
                .await?
                .into_inner()
                .blob
                .ok_or_else(|| anyhow::anyhow!("missing account state blob"))?;
            bcs::from_bytes(&blob)?
        };
        Ok(Some(AccountState::try_from(&account_state)?))
    }

    pub async fn get_payment_event_handles(
        &self,
        account: AccountAddress,
    ) -> Result<Option<(EventHandle, EventHandle)>> {
        match self.get_account_state(account).await? {
            Some(account_state) => Ok(account_state.get_account_resource()?.map(|resource| {
                (
                    resource.sent_events().clone(),
                    resource.received_events().clone(),
                )
            })),
            None => Ok(None),
        }
    }

    pub async fn get_events(
        &self,
        event_key: &EventKey,
        start: u64,
        limit: u64,
    ) -> Result<Vec<EventView>> {
        let resp = self
            .0
            .get_events(*event_key, start, limit)
            .await?
            .into_inner();

        Ok(resp)
    }

    pub async fn get_all_events(&self, event_handle: &EventHandle) -> Result<Vec<EventView>> {
        if event_handle.count() == 0 {
            return Ok(vec![]);
        }
        let mut futures = vec![];
        let mut i: u64 = 0;
        while i.wrapping_add(BATCH_SIZE) < event_handle.count() {
            futures.push(self.get_events(event_handle.key(), i, BATCH_SIZE));
            i = i.wrapping_add(BATCH_SIZE);
        }
        futures.push(self.get_events(event_handle.key(), i, event_handle.count().wrapping_sub(i)));

        let mut result = vec![];
        for response in join_all(futures.into_iter()).await {
            match response {
                Ok(mut events) => result.append(&mut events),
                Err(e) => return Err(e),
            }
        }
        Ok(result)
    }
}