client/rpc/types/cfx/
account.rs1use 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 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}