forked from Near-Bridge-Lab/btc-bridge
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaccount.rs
More file actions
145 lines (128 loc) · 3.95 KB
/
account.rs
File metadata and controls
145 lines (128 loc) · 3.95 KB
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
use crate::{near, u128_dec_format, AccountId, Contract};
use near_sdk::env;
use std::collections::HashSet;
#[near(serializers = [borsh, json])]
#[derive(Clone)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug))]
pub struct OutstandingInfo {
pub token_id: AccountId,
#[serde(with = "u128_dec_format")]
pub amount: u128,
}
#[near(serializers = [borsh, json])]
#[derive(Clone)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug))]
pub struct Account {
pub account_id: AccountId,
pub btc_pending_sign_ids: HashSet<String>,
pub btc_pending_verify_list: HashSet<String>,
}
/// Old Account format (v0.7.5 and earlier) with single pending sign id.
#[near(serializers = [borsh])]
#[derive(Clone)]
pub struct AccountV0 {
pub account_id: AccountId,
pub btc_pending_sign_id: Option<String>,
pub btc_pending_verify_list: HashSet<String>,
}
impl From<AccountV0> for Account {
fn from(v: AccountV0) -> Self {
let mut btc_pending_sign_ids = HashSet::new();
if let Some(id) = v.btc_pending_sign_id {
btc_pending_sign_ids.insert(id);
}
Self {
account_id: v.account_id,
btc_pending_sign_ids,
btc_pending_verify_list: v.btc_pending_verify_list,
}
}
}
#[near(serializers = [borsh])]
pub enum VAccount {
V0(AccountV0),
Current(Account),
}
impl From<VAccount> for Account {
fn from(v: VAccount) -> Self {
match v {
VAccount::V0(c) => c.into(),
VAccount::Current(c) => c,
}
}
}
impl From<&VAccount> for Account {
fn from(v: &VAccount) -> Self {
match v {
VAccount::V0(c) => c.clone().into(),
VAccount::Current(c) => c.clone(),
}
}
}
impl<'a> From<&'a mut VAccount> for &'a mut Account {
fn from(v: &'a mut VAccount) -> Self {
// Lazy migrate V0 -> Current on first mutable access
if let VAccount::V0(old) = v {
let migrated: Account = old.clone().into();
*v = VAccount::Current(migrated);
}
match v {
VAccount::Current(c) => c,
_ => unreachable!(),
}
}
}
impl From<Account> for VAccount {
fn from(c: Account) -> Self {
VAccount::Current(c)
}
}
impl Account {
pub fn new(account_id: &AccountId) -> Self {
Self {
account_id: account_id.clone(),
btc_pending_sign_ids: HashSet::new(),
btc_pending_verify_list: HashSet::new(),
}
}
pub fn pending_sign_count(&self) -> u32 {
u32::try_from(self.btc_pending_sign_ids.len()).unwrap_or(u32::MAX)
}
}
impl Contract {
pub fn check_account_exists(&self, account_id: &AccountId) -> bool {
self.data().accounts.contains_key(account_id)
}
pub fn internal_get_account(&self, account_id: &AccountId) -> Option<Account> {
self.data().accounts.get(account_id).map(Account::from)
}
pub fn internal_unwrap_account(&self, account_id: &AccountId) -> Account {
self.internal_get_account(account_id).unwrap_or_else(|| {
env::panic_str(&format!("ERR_ACCOUNT_NOT_REGISTERED: {}", account_id))
})
}
pub fn internal_unwrap_mut_account(&mut self, account_id: &AccountId) -> &mut Account {
self.data_mut()
.accounts
.get_mut(account_id)
.map(|o| o.into())
.unwrap_or_else(|| {
env::panic_str(&format!("ERR_ACCOUNT_NOT_REGISTERED: {}", account_id))
})
}
pub fn internal_unwrap_or_create_mut_account(
&mut self,
account_id: &AccountId,
) -> &mut Account {
self.data_mut()
.accounts
.entry(account_id.clone())
.or_insert(Account::new(account_id).into())
.into()
}
pub fn internal_set_account(&mut self, account_id: &AccountId, account: Account) {
self.data_mut()
.accounts
.insert(account_id.clone(), account.into());
}
}