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
use core::{borrow::Borrow, cmp::Ordering, fmt::Debug};

use crate::{
    env::internal::{Env as _, RawVal, RawValConvertible},
    env::EnvObj,
    Binary, ConversionError, Env, EnvType, EnvVal, FixedBinary, IntoVal, Object,
};

/// Account references a Stellar account and provides access to information
/// about the account, such as its thresholds and signers.
#[derive(Clone)]
pub struct Account(FixedBinary<32>);

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<Binary> for Account {
    fn borrow(&self) -> &Binary {
        self.0.borrow()
    }
}

impl Borrow<Binary> for &Account {
    fn borrow(&self) -> &Binary {
        self.0.borrow()
    }
}

impl Borrow<Binary> for &mut Account {
    fn borrow(&self) -> &Binary {
        self.0.borrow()
    }
}

impl Borrow<FixedBinary<32>> for Account {
    fn borrow(&self) -> &FixedBinary<32> {
        self.0.borrow()
    }
}

impl Borrow<FixedBinary<32>> for &Account {
    fn borrow(&self) -> &FixedBinary<32> {
        self.0.borrow()
    }
}

impl Borrow<FixedBinary<32>> for &mut Account {
    fn borrow(&self) -> &FixedBinary<32> {
        self.0.borrow()
    }
}

impl AsRef<Binary> for Account {
    fn as_ref(&self) -> &Binary {
        self.0.as_ref()
    }
}

impl AsRef<FixedBinary<32>> for Account {
    fn as_ref(&self) -> &FixedBinary<32> {
        &self.0
    }
}

impl From<EnvType<[u8; 32]>> for Account {
    fn from(ev: EnvType<[u8; 32]>) -> Self {
        Account(ev.into())
    }
}

impl IntoVal<Env, Account> for [u8; 32] {
    fn into_val(self, env: &Env) -> Account {
        EnvType {
            env: env.clone(),
            val: self,
        }
        .into()
    }
}

impl TryFrom<EnvVal> for Account {
    type Error = ConversionError;

    fn try_from(ev: EnvVal) -> Result<Self, Self::Error> {
        let obj: EnvObj = ev.clone().try_into()?;
        obj.try_into()
    }
}

impl TryFrom<EnvObj> for Account {
    type Error = ConversionError;

    fn try_from(obj: EnvObj) -> Result<Self, Self::Error> {
        Ok(Account(obj.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()
    }

    /// Creates an account from a public key.
    ///
    /// TODO: Return a `Result` `Err` if the account does not exist. Currently panics if account does not exist.
    pub fn from_public_key(public_key: &FixedBinary<32>) -> Result<Account, ()> {
        let acc = Account(public_key.clone());
        // TODO: Fail when account doesn't exist. In the meantime cause a trap
        // at this point by trying to get some information about the account
        // which will trap if the account doesn't exist.
        _ = acc.low_threshold();
        Ok(acc)
    }

    /// Returns the low threshold for the Stellar account.
    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) }
    }

    /// Returns the medium threshold for the Stellar account.
    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) }
    }

    /// Returns the high threshold for the Stellar account.
    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) }
    }

    /// Returns the signer weight for the signer for this Stellar account. If
    /// the signer does not exist for the account, returns zero (`0`).
    pub fn signer_weight(&self, signer: &FixedBinary<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) }
    }
}