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
// Copyright 2019-2023 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use anyhow::anyhow;
use fil_actor_datacap_state::v12::DATACAP_GRANULARITY;
use fil_actors_shared::ext::TokenStateExt;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::address::{Address, Payload};
use num::traits::Euclid;
use num::BigInt;
use serde::Serialize;

/// Datacap actor method.
pub type Method = fil_actor_datacap_state::v10::Method;

/// Datacap actor address.
pub const ADDRESS: Address = Address::new_id(7);

/// Datacap actor state.
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum State {
    V9(fil_actor_datacap_state::v9::State),
    V10(fil_actor_datacap_state::v10::State),
    V11(fil_actor_datacap_state::v11::State),
    V12(fil_actor_datacap_state::v12::State),
    V13(fil_actor_datacap_state::v13::State),
    V14(fil_actor_datacap_state::v14::State),
}

impl State {
    // NOTE: This code currently mimics that of Lotus and is only used for RPC compatibility.
    pub fn verified_client_data_cap<BS>(
        &self,
        store: &BS,
        addr: Address,
    ) -> anyhow::Result<Option<BigInt>>
    where
        BS: Blockstore,
    {
        let id = match addr.payload() {
            Payload::ID(id) => Ok(*id),
            _ => Err(anyhow!("can only look up ID addresses")),
        }?;

        let int = match self {
            State::V9(state) => state.token.get_balance_opt(store, id),
            State::V11(state) => state.token.get_balance_opt(store, id),
            State::V12(state) => state.token.get_balance_opt(store, id),
            State::V13(state) => state.token.get_balance_opt(store, id),
            State::V14(state) => state.token.get_balance_opt(store, id),
            _ => return Err(anyhow!("not supported in actors > v8")),
        }?;
        Ok(int
            .map(|amount| amount.atto().to_owned())
            .map(|opt| opt.div_euclid(&BigInt::from(DATACAP_GRANULARITY))))
    }
}