casper_client/types/
account.rs1use std::fmt::{self, Display, Formatter};
2
3use itertools::Itertools;
4use serde::{Deserialize, Serialize};
5
6use casper_types::{account::AccountHash, URef};
7
8#[cfg(doc)]
9use crate::types::Contract;
10use crate::types::NamedKey;
11
12#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
17#[serde(deny_unknown_fields)]
18pub struct AssociatedKey {
19 account_hash: AccountHash,
20 weight: u8,
21}
22
23impl AssociatedKey {
24 pub fn account_hash(&self) -> &AccountHash {
26 &self.account_hash
27 }
28
29 pub fn weight(&self) -> u8 {
31 self.weight
32 }
33}
34
35impl Display for AssociatedKey {
36 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
37 write!(
38 formatter,
39 "associated-key {{ {}, weight: {} }}",
40 self.account_hash, self.weight
41 )
42 }
43}
44
45#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
47#[serde(deny_unknown_fields)]
48pub struct ActionThresholds {
49 deployment: u8,
50 key_management: u8,
51}
52
53impl ActionThresholds {
54 pub fn deployment(&self) -> u8 {
56 self.deployment
57 }
58
59 pub fn key_management(&self) -> u8 {
61 self.key_management
62 }
63}
64
65impl Display for ActionThresholds {
66 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
67 write!(
68 formatter,
69 "action-thresholds {{ deployment: {}, key-management: {} }}",
70 self.deployment, self.key_management
71 )
72 }
73}
74
75#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
77#[serde(deny_unknown_fields)]
78pub struct Account {
79 account_hash: AccountHash,
80 named_keys: Vec<NamedKey>,
81 main_purse: URef,
82 associated_keys: Vec<AssociatedKey>,
83 action_thresholds: ActionThresholds,
84}
85
86impl Account {
87 pub fn account_hash(&self) -> &AccountHash {
89 &self.account_hash
90 }
91
92 pub fn named_keys(&self) -> impl Iterator<Item = &NamedKey> {
94 self.named_keys.iter()
95 }
96
97 pub fn main_purse(&self) -> &URef {
99 &self.main_purse
100 }
101
102 pub fn associated_keys(&self) -> impl Iterator<Item = &AssociatedKey> {
104 self.associated_keys.iter()
105 }
106
107 pub fn action_thresholds(&self) -> ActionThresholds {
109 self.action_thresholds
110 }
111}
112
113impl Display for Account {
114 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
115 write!(
116 formatter,
117 "account {{ {}, main-purse {}, {}, named keys {{{}}}, associated-keys {{{}}} }}",
118 self.account_hash,
119 self.main_purse,
120 self.action_thresholds,
121 self.named_keys.iter().format(", "),
122 self.associated_keys.iter().format(", ")
123 )
124 }
125}