client/rpc/types/cfx/
account.rs

1// Copyright 2019 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use super::RpcAddress;
6use cfx_addr::Network;
7use cfx_types::{H256, U256};
8use primitives::Account as PrimitiveAccount;
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct Account {
14    // This field isn't part of Account RLP but is helpful for debugging.
15    pub address: RpcAddress,
16    pub balance: U256,
17    pub nonce: U256,
18    pub code_hash: H256,
19    pub staking_balance: U256,
20    pub collateral_for_storage: U256,
21    pub accumulated_interest_return: U256,
22    pub admin: RpcAddress,
23}
24
25impl Account {
26    pub fn try_from(
27        account: PrimitiveAccount, network: Network,
28    ) -> Result<Self, String> {
29        let collateral_for_storage = account.collateral_for_storage
30            + account
31                .sponsor_info
32                .storage_points
33                .as_ref()
34                .map_or(U256::zero(), |x| x.used);
35        Ok(Self {
36            address: RpcAddress::try_from_h160(
37                account.address().address,
38                network,
39            )?,
40            balance: account.balance.into(),
41            nonce: account.nonce.into(),
42            code_hash: account.code_hash.into(),
43            staking_balance: account.staking_balance.into(),
44            collateral_for_storage,
45            accumulated_interest_return: account
46                .accumulated_interest_return
47                .into(),
48            admin: RpcAddress::try_from_h160(account.admin, network)?,
49        })
50    }
51}