-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.rs
154 lines (126 loc) · 4.7 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
// use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, Balance, Timestamp, Duration, AccountId, Gas, ext_contract};
use near_sdk::json_types::U128;
use crate::errors::{E406_INVALID_FARM_ID, E308_INVALID_SEED_ID};
uint::construct_uint!(
pub struct U256(4);
);
pub const DEFAULT_SEED_SLASH_RATE: u32 = 200;
pub const DEFAULT_SEED_MIN_LOCKING_DURATION_SEC: DurationSec = 3600 * 24 * 30;
pub const DEFAULT_MAX_NUM_FARMS_PER_SEED: u32 = 32;
pub const DEFAULT_MAX_NUM_FARMS_PER_BOOSTER: u32 = 64;
pub const DEFAULT_MAX_LOCKING_DURATION_SEC: DurationSec = 3600 * 24 * 30 * 12;
pub const DEFAULT_MAX_LOCKING_REWARD_RATIO: u32 = 20000;
pub const MIN_LOCKING_REWARD_RATIO: u32 = 10000;
pub const MAX_NUM_SEEDS_PER_BOOSTER: usize = 16;
pub const AVAILABLE_MS_FOR_NEXT_OWNER_ACCEPT: u64 = 168 * 3600 * 1000;
pub const STORAGE_BALANCE_MIN_BOUND: u128 = 100_000_000_000_000_000_000_000;
pub const TGAS: u64 = 1_000_000_000_000;
pub const GAS_FOR_SEED_TRANSFER: Gas = Gas(20 * TGAS);
pub const GAS_FOR_RESOLVE_SEED_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 NANOS_PER_DAY: Duration = 24 * 60 * 60 * 10u64.pow(9);
pub const MIN_SEED_DEPOSIT: u128 = 1_000_000_000_000_000_000;
pub const BP_DENOM: u128 = 10000;
pub const MFT_TAG: &str = ":";
pub const SEED_TAG: &str = "@";
pub const FARM_ID_PREFIX: &str = "#";
pub type SeedId = String;
pub type FarmId = String;
pub type DurationSec = u32;
pub const NO_DEPOSIT: Balance = 0;
pub const ONE_YOCTO: Balance = 1;
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 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 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 gen_farm_id(seed_id: &SeedId, index: usize) -> FarmId {
format!("{}{}{}", seed_id, FARM_ID_PREFIX, index)
}
pub fn parse_farm_id(farm_id: &FarmId) -> (SeedId, u32) {
let pos = farm_id.rfind(FARM_ID_PREFIX).expect(E406_INVALID_FARM_ID);
let (seed_id, last) = farm_id.split_at(pos);
(seed_id.to_string(), (last.split_at(1).1).parse::<u32>().unwrap())
}
pub fn parse_seed_id(seed_id: &SeedId) -> (AccountId, Option<String>) {
let v: Vec<&str> = seed_id.split(SEED_TAG).collect();
if v.len() == 1 {
let token: AccountId = v[0].parse().unwrap();
(token, None)
} else if v.len() == 2 {
let token: AccountId = v[0].parse().unwrap();
(token, Some(v[1].to_string()))
} else {
env::panic_str(E308_INVALID_SEED_ID)
}
}
#[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,
farmer_id: AccountId,
amount: U128,
);
fn callback_withdraw_seed(&mut self, seed_id: SeedId, sender_id: AccountId, amount: U128);
fn callback_withdraw_seed_slashed(&mut self, seed_id: SeedId, amount: U128);
fn callback_withdraw_seed_lostfound(&mut self, seed_id: SeedId, sender_id: AccountId, amount: U128);
fn callback_withdraw_beneficiary(&mut self, farm_id: FarmId, amount: U128);
fn callback_withdraw_outdated_beneficiary(&mut self, farm_id: FarmId, amount: U128);
fn callback_withdraw_undistributed(&mut self, farm_id: FarmId, amount: U128);
}
pub fn wrap_mft_token_id(token_id: &str) -> String {
format!("{}{}", MFT_TAG, token_id)
}