This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
forked from integritee-network/worker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalance_storage.rs
243 lines (223 loc) · 7.7 KB
/
balance_storage.rs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// This file is part of Polkadex.
// Copyright (C) 2020-2021 Polkadex oü and Supercomputing Systems AG
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::polkadex_gateway::GatewayError;
use codec::Encode;
use log::*;
use polkadex_sgx_primitives::BalancesData;
use polkadex_sgx_primitives::{AccountId, AssetId, Balance};
use sgx_tstd::collections::HashMap;
use sgx_tstd::vec::Vec;
use crate::channel_storage::{load_sender, ChannelType};
use crate::polkadex_balance_storage::balances::*;
use crate::polkadex_balance_storage::polkadex_balance_key::*;
pub type EncodedKey = Vec<u8>;
#[derive(Debug)]
pub struct PolkadexBalanceStorage {
/// map (tokenID, AccountID) -> (balance free, balance reserved)
pub storage: HashMap<EncodedKey, Balances>,
}
fn balance_change(account: PolkadexBalanceKey, new_balance: Balances) -> Result<(), GatewayError> {
load_sender()
.map_err(|_| GatewayError::UnableToLoadPointer)?
.send(ChannelType::Balances(account, new_balance))
.map_err(|_| GatewayError::UndefinedBehaviour)?;
Ok(())
}
impl PolkadexBalanceStorage {
pub fn create() -> PolkadexBalanceStorage {
PolkadexBalanceStorage {
storage: HashMap::new(),
}
}
pub fn read_balance(&self, token: AssetId, acc: AccountId) -> Option<&Balances> {
let key = PolkadexBalanceKey::from(token, acc).encode();
debug!("reading balance from key: {:?}", key);
self.storage.get(&key)
}
pub fn initialize_balance(
&mut self,
token: AssetId,
acc: AccountId,
free: Balance,
) -> Result<(), GatewayError> {
let key = PolkadexBalanceKey::from(token, acc.clone()).encode();
debug!("creating new entry for key: {:?}", key);
self.storage.insert(key, Balances::from(free, 0u128));
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(free, 0u128),
)?;
Ok(())
}
pub fn set_free_balance(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.free = amt;
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(amt, balance.reserved),
)?;
Ok(())
}
None => {
error!("Account Id or Asset id not avalaible");
Err(GatewayError::AccountIdOrAssetIdNotFound)
}
}
}
pub fn set_reserve_balance(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.reserved = amt;
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(balance.free, amt),
)?;
Ok(())
}
None => {
error!("Account Id or Asset id not avalaible");
Err(GatewayError::AccountIdOrAssetIdNotFound)
}
}
}
pub fn deposit(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.free = balance.free.saturating_add(amt);
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(balance.free, balance.reserved),
)?;
Ok(())
}
None => {
debug!("No entry available for given token- and AccountId, creating new.");
self.initialize_balance(token, acc, amt)?;
Ok(())
}
}
}
pub fn withdraw(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.free = balance.free.saturating_sub(amt);
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(balance.free, balance.reserved),
)?;
Ok(())
}
None => {
error!("Account Id or Asset id not avalaible");
Err(GatewayError::AccountIdOrAssetIdNotFound)
}
}
}
pub fn reduce_free_balance(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.free = balance
.free
.checked_sub(amt)
.ok_or(GatewayError::LimitOrderPriceNotFound)?; //FIXME Error type
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(balance.free, balance.reserved),
)?;
Ok(())
}
None => {
error!("Account Id or Asset id not avalaible");
Err(GatewayError::AccountIdOrAssetIdNotFound)
}
}
}
pub fn increase_free_balance(
&mut self,
token: AssetId,
acc: AccountId,
amt: Balance,
) -> Result<(), GatewayError> {
match self
.storage
.get_mut(&PolkadexBalanceKey::from(token, acc.clone()).encode())
{
Some(balance) => {
balance.free = balance
.free
.checked_add(amt)
.ok_or(GatewayError::LimitOrderPriceNotFound)?; //FIXME Error Type
balance_change(
PolkadexBalanceKey::from(token, acc),
Balances::from(balance.free, balance.reserved),
)?;
Ok(())
}
None => {
self.initialize_balance(token, acc, amt)?;
Ok(())
}
}
}
pub fn extend_from_disk_data(&mut self, data: Vec<BalancesData>) {
self.storage.extend(data.into_iter().map(|entry| {
(
PolkadexBalanceKey::from(entry.asset_id, entry.account_id).encode(),
Balances::from(entry.free, entry.reserved),
)
}));
}
// We can write functions which settle balances for two trades but we need to know the trade structure for it
}