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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use core::{
borrow::Borrow,
cmp::Ordering,
fmt::{Debug, Display},
};
use crate::{
env::internal::{Env as _, RawVal, RawValConvertible},
env::EnvObj,
Bytes, BytesN, ConversionError, Env, EnvVal, IntoVal, Object, TryFromVal, TryIntoVal,
};
#[derive(Clone)]
pub struct Account(BytesN<32>);
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AccountError {
DoesNotExist,
}
impl Display for AccountError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
AccountError::DoesNotExist => write!(f, "account does not exist"),
}
}
}
impl Debug for Account {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Account(")?;
Debug::fmt(&self.0, f)?;
write!(f, ")")?;
Ok(())
}
}
impl Eq for Account {}
impl PartialEq for Account {
fn eq(&self, other: &Self) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}
impl PartialOrd for Account {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}
impl Ord for Account {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
let env = self.env();
let v = env.obj_cmp(self.0.to_raw(), other.0.to_raw());
let i = i32::try_from(v).unwrap();
i.cmp(&0)
}
}
impl Borrow<Bytes> for Account {
fn borrow(&self) -> &Bytes {
self.0.borrow()
}
}
impl Borrow<Bytes> for &Account {
fn borrow(&self) -> &Bytes {
self.0.borrow()
}
}
impl Borrow<Bytes> for &mut Account {
fn borrow(&self) -> &Bytes {
self.0.borrow()
}
}
impl Borrow<BytesN<32>> for Account {
fn borrow(&self) -> &BytesN<32> {
self.0.borrow()
}
}
impl Borrow<BytesN<32>> for &Account {
fn borrow(&self) -> &BytesN<32> {
self.0.borrow()
}
}
impl Borrow<BytesN<32>> for &mut Account {
fn borrow(&self) -> &BytesN<32> {
self.0.borrow()
}
}
impl AsRef<Bytes> for Account {
fn as_ref(&self) -> &Bytes {
self.0.as_ref()
}
}
impl AsRef<BytesN<32>> for Account {
fn as_ref(&self) -> &BytesN<32> {
&self.0
}
}
impl IntoVal<Env, Account> for [u8; 32] {
fn into_val(self, env: &Env) -> Account {
Account(self.into_val(env))
}
}
impl TryFromVal<Env, Object> for Account {
type Error = ConversionError;
fn try_from_val(env: &Env, val: Object) -> Result<Self, Self::Error> {
Ok(Account(val.try_into_val(env)?))
}
}
impl TryFromVal<Env, RawVal> for Account {
type Error = <Account as TryFromVal<Env, Object>>::Error;
fn try_from_val(env: &Env, val: RawVal) -> Result<Self, Self::Error> {
<_ as TryFromVal<_, Object>>::try_from_val(env, val.try_into()?)
}
}
impl From<Account> for RawVal {
fn from(a: Account) -> Self {
a.0.into()
}
}
impl From<Account> for EnvVal {
fn from(a: Account) -> Self {
a.0.into()
}
}
impl From<Account> for EnvObj {
fn from(a: Account) -> Self {
a.0.into()
}
}
impl Account {
pub(crate) fn env(&self) -> &Env {
self.0.env()
}
pub(crate) fn as_raw(&self) -> &RawVal {
self.0.as_raw()
}
pub(crate) fn as_object(&self) -> &Object {
self.0.as_object()
}
pub(crate) fn to_raw(&self) -> RawVal {
self.0.to_raw()
}
pub(crate) fn to_object(&self) -> Object {
self.0.to_object()
}
pub fn from_public_key(public_key: &BytesN<32>) -> Result<Account, AccountError> {
let env = public_key.env();
if env.account_exists(public_key.to_object()).is_false() {
return Err(AccountError::DoesNotExist);
}
Ok(Account(public_key.clone()))
}
pub fn exists(public_key: &BytesN<32>) -> bool {
let env = public_key.env();
env.account_exists(public_key.to_object()).is_true()
}
pub fn low_threshold(&self) -> u32 {
let env = self.env();
let val = env.account_get_low_threshold(self.to_object());
unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) }
}
pub fn medium_threshold(&self) -> u32 {
let env = self.env();
let val = env.account_get_medium_threshold(self.to_object());
unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) }
}
pub fn high_threshold(&self) -> u32 {
let env = self.env();
let val = env.account_get_high_threshold(self.to_object());
unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) }
}
pub fn signer_weight(&self, signer: &BytesN<32>) -> u32 {
let env = self.env();
let val = env.account_get_signer_weight(self.to_object(), signer.to_object());
unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) }
}
}