-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.rs
202 lines (169 loc) · 6.31 KB
/
utils.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
use std::collections::HashSet;
// use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, AccountId, Balance, Timestamp, Gas, ext_contract};
use near_sdk::json_types::U128;
use crate::errors::{E501_INVALID_FARM_INFO, E502_INVALID_TOKEN_ID};
uint::construct_uint!(
pub struct U256(4);
);
pub type DurationSec = u32;
pub const LOVE_DECIMAL: u8 = 18;
pub const DAY_SEC: DurationSec = 60 * 60 * 24;
pub const DEFAULT_MIN_VOTING_DURATION_SEC: DurationSec = DAY_SEC * 3;
pub const DEFAULT_MAX_VOTING_DURATION_SEC: DurationSec = DAY_SEC * 30;
pub const DEFAULT_MIN_PROPOSAL_START_VOTE_OFFSET_SEC: u32 = DAY_SEC;
pub const DEFAULT_MIN_LOCKING_DURATION_SEC: DurationSec = DAY_SEC * 30;
pub const DEFAULT_MAX_LOCKING_DURATION_SEC: DurationSec = DAY_SEC * 30 * 12;
pub const DEFAULT_MAX_LOCKING_REWARD_RATIO: u32 = 20000;
pub const MIN_LOCKING_REWARD_RATIO: u32 = 10000;
pub const TGAS: u64 = 1_000_000_000_000;
pub const GAS_FOR_LPT_TRANSFER: Gas = Gas(20 * TGAS);
pub const GAS_FOR_RESOLVE_LPT_TRANSFER: Gas = Gas(10 * TGAS);
pub const GAS_FOR_REWARD_TRANSFER: Gas = Gas(20 * TGAS);
pub const GAS_FOR_RESOLVE_REWARD_TRANSFER: Gas = Gas(10 * TGAS);
pub const GAS_FOR_REMOVED_PROPOSAL_ASSETS: Gas = Gas(20 * TGAS);
pub const GAS_FOR_RESOLVE_REMOVED_PROPOSAL_ASSETS: Gas = Gas(10 * TGAS);
pub const DESCRIPTION_LIMIT: usize = 2048;
pub const MIN_FIRST_LOCK: u128 = 10u128.pow(22);
pub const STORAGE_BALANCE_MIN_BOUND: u128 = 1_250_000_000_000_000_000_000;
pub mod u64_dec_format {
use near_sdk::serde::de;
use near_sdk::serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(num: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&num.to_string())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer)?
.parse()
.map_err(de::Error::custom)
}
}
pub mod u128_dec_format {
use near_sdk::serde::de;
use near_sdk::serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(num: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&num.to_string())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer)?
.parse()
.map_err(de::Error::custom)
}
}
pub mod u128_vec_format {
use near_sdk::serde::de;
use near_sdk::serde::ser::Serialize;
use near_sdk::serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(num: &Vec<u128>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut res = Vec::with_capacity(num.len());
for value in num {
res.push(value.to_string());
}
Vec::serialize(&res, serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u128>, D::Error>
where
D: Deserializer<'de>,
{
let str_vec: Vec<String> = Vec::deserialize(deserializer)?;
let mut res = Vec::with_capacity(str_vec.len());
for s in str_vec.into_iter() {
let item: u128 = s.parse().map_err(de::Error::custom)?;
res.push(item);
}
Ok(res)
}
}
pub mod u128_map_format {
use near_sdk::serde::de;
use near_sdk::serde::ser::Serialize;
use near_sdk::serde::{Deserialize, Deserializer, Serializer};
use near_sdk::{AccountId, Balance};
use std::collections::HashMap;
pub fn serialize<S>(info: &HashMap<AccountId, Balance>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut res = Vec::with_capacity(info.len());
for (account_id, balance) in info {
res.push((account_id, balance.to_string()));
}
Vec::serialize(&res, serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<HashMap<AccountId, Balance>, D::Error>
where
D: Deserializer<'de>,
{
let str_vec: Vec<(AccountId, String)> = Vec::deserialize(deserializer)?;
let mut res = HashMap::new();
for (account_id, balance_str) in str_vec.into_iter() {
let balance: u128 = balance_str.parse().map_err(de::Error::custom)?;
res.insert(account_id, balance);
}
Ok(res)
}
}
pub fn to_nano(sec: u32) -> Timestamp {
Timestamp::from(sec) * 10u64.pow(9)
}
pub fn nano_to_sec(nano: u64) -> u32 {
(nano / 10u64.pow(9)) as u32
}
pub(crate) fn u128_ratio(a: u128, num: u128, denom: u128) -> Balance {
(U256::from(a) * U256::from(num) / U256::from(denom)).as_u128()
}
pub fn extra_incentive_tokens(farm_info: String) -> HashSet<AccountId> {
let (farm_tokens_str, _) = farm_info.split_once('&').unwrap_or_else(|| env::panic_str(E501_INVALID_FARM_INFO));
farm_tokens_str.split('|').into_iter().map(|a| a.parse().unwrap_or_else(|_| env::panic_str(E502_INVALID_TOKEN_ID))).collect()
}
#[ext_contract(ext_multi_fungible_token)]
pub trait MultiFungibleToken {
fn mft_transfer(
&mut self,
token_id: String,
receiver_id: AccountId,
amount: U128,
memo: Option<String>,
);
}
#[ext_contract(ext_self)]
pub trait TokenPostActions {
fn callback_post_withdraw_reward(
&mut self, token_id: AccountId, sender_id: AccountId, amount: U128,
);
fn callback_removed_proposal_assets(
&mut self, token_id: AccountId, receiver_id: AccountId, amount: U128,
);
fn callback_withdraw_lpt(&mut self, sender_id: AccountId, amount: U128);
fn callback_withdraw_lpt_lostfound(&mut self, receiver_id: AccountId, amount: U128);
fn callback_withdraw_lpt_slashed(&mut self, sender_id: AccountId, amount: U128);
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn test_extra_incentive_tokens() {
assert_eq!(HashSet::from(
["noct.near".parse().unwrap(), "nref.near".parse().unwrap()]),
extra_incentive_tokens("noct.near|nref.near&2657".to_string()));
assert_eq!(HashSet::from(
["nusdt.near".parse().unwrap(), "nusdc.near".parse().unwrap(), "ndai.near".parse().unwrap()]),
extra_incentive_tokens("nusdt.near|nusdc.near|ndai.near&1910".to_string()));
}
}