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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use serde::{Deserialize, Serialize};
use crate::num_rational::Rational;
use crate::types::Gas;
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct Fee {
pub send_sir: Gas,
pub send_not_sir: Gas,
pub execution: Gas,
}
impl Fee {
#[inline]
pub fn send_fee(&self, sir: bool) -> Gas {
if sir {
self.send_sir
} else {
self.send_not_sir
}
}
pub fn exec_fee(&self) -> Gas {
self.execution
}
fn min_send_and_exec_fee(&self) -> Gas {
std::cmp::min(self.send_sir, self.send_not_sir) + self.execution
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct RuntimeFeesConfig {
pub action_receipt_creation_config: Fee,
pub data_receipt_creation_config: DataReceiptCreationConfig,
pub action_creation_config: ActionCreationConfig,
pub storage_usage_config: StorageUsageConfig,
pub burnt_gas_reward: Rational,
pub pessimistic_gas_price_inflation_ratio: Rational,
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct DataReceiptCreationConfig {
pub base_cost: Fee,
pub cost_per_byte: Fee,
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct ActionCreationConfig {
pub create_account_cost: Fee,
pub deploy_contract_cost: Fee,
pub deploy_contract_cost_per_byte: Fee,
pub function_call_cost: Fee,
pub function_call_cost_per_byte: Fee,
pub transfer_cost: Fee,
pub stake_cost: Fee,
pub add_key_cost: AccessKeyCreationConfig,
pub delete_key_cost: Fee,
pub delete_account_cost: Fee,
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct AccessKeyCreationConfig {
pub full_access_cost: Fee,
pub function_call_cost: Fee,
pub function_call_cost_per_byte: Fee,
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct StorageUsageConfig {
pub num_bytes_account: u64,
pub num_extra_bytes_record: u64,
}
impl RuntimeFeesConfig {
pub fn test() -> Self {
#[allow(clippy::unreadable_literal)]
Self {
action_receipt_creation_config: Fee {
send_sir: 108059500000,
send_not_sir: 108059500000,
execution: 108059500000,
},
data_receipt_creation_config: DataReceiptCreationConfig {
base_cost: Fee {
send_sir: 4697339419375,
send_not_sir: 4697339419375,
execution: 4697339419375,
},
cost_per_byte: Fee {
send_sir: 59357464,
send_not_sir: 59357464,
execution: 59357464,
},
},
action_creation_config: ActionCreationConfig {
create_account_cost: Fee {
send_sir: 99607375000,
send_not_sir: 99607375000,
execution: 99607375000,
},
deploy_contract_cost: Fee {
send_sir: 184765750000,
send_not_sir: 184765750000,
execution: 184765750000,
},
deploy_contract_cost_per_byte: Fee {
send_sir: 6812999,
send_not_sir: 6812999,
execution: 6812999,
},
function_call_cost: Fee {
send_sir: 2319861500000,
send_not_sir: 2319861500000,
execution: 2319861500000,
},
function_call_cost_per_byte: Fee {
send_sir: 2235934,
send_not_sir: 2235934,
execution: 2235934,
},
transfer_cost: Fee {
send_sir: 115123062500,
send_not_sir: 115123062500,
execution: 115123062500,
},
stake_cost: Fee {
send_sir: 141715687500,
send_not_sir: 141715687500,
execution: 102217625000,
},
add_key_cost: AccessKeyCreationConfig {
full_access_cost: Fee {
send_sir: 101765125000,
send_not_sir: 101765125000,
execution: 101765125000,
},
function_call_cost: Fee {
send_sir: 102217625000,
send_not_sir: 102217625000,
execution: 102217625000,
},
function_call_cost_per_byte: Fee {
send_sir: 1925331,
send_not_sir: 1925331,
execution: 1925331,
},
},
delete_key_cost: Fee {
send_sir: 94946625000,
send_not_sir: 94946625000,
execution: 94946625000,
},
delete_account_cost: Fee {
send_sir: 147489000000,
send_not_sir: 147489000000,
execution: 147489000000,
},
},
storage_usage_config: StorageUsageConfig {
num_bytes_account: 100,
num_extra_bytes_record: 40,
},
burnt_gas_reward: Rational::new(3, 10),
pessimistic_gas_price_inflation_ratio: Rational::new(103, 100),
}
}
pub fn free() -> Self {
let free = Fee { send_sir: 0, send_not_sir: 0, execution: 0 };
RuntimeFeesConfig {
action_receipt_creation_config: free.clone(),
data_receipt_creation_config: DataReceiptCreationConfig {
base_cost: free.clone(),
cost_per_byte: free.clone(),
},
action_creation_config: ActionCreationConfig {
create_account_cost: free.clone(),
deploy_contract_cost: free.clone(),
deploy_contract_cost_per_byte: free.clone(),
function_call_cost: free.clone(),
function_call_cost_per_byte: free.clone(),
transfer_cost: free.clone(),
stake_cost: free.clone(),
add_key_cost: AccessKeyCreationConfig {
full_access_cost: free.clone(),
function_call_cost: free.clone(),
function_call_cost_per_byte: free.clone(),
},
delete_key_cost: free.clone(),
delete_account_cost: free,
},
storage_usage_config: StorageUsageConfig {
num_bytes_account: 0,
num_extra_bytes_record: 0,
},
burnt_gas_reward: Rational::from_integer(0),
pessimistic_gas_price_inflation_ratio: Rational::from_integer(0),
}
}
pub fn min_receipt_with_function_call_gas(&self) -> Gas {
self.action_receipt_creation_config.min_send_and_exec_fee()
+ self.action_creation_config.function_call_cost.min_send_and_exec_fee()
}
}
pub fn transfer_exec_fee(cfg: &ActionCreationConfig, is_receiver_implicit: bool) -> Gas {
if is_receiver_implicit {
cfg.create_account_cost.exec_fee()
+ cfg.add_key_cost.full_access_cost.exec_fee()
+ cfg.transfer_cost.exec_fee()
} else {
cfg.transfer_cost.exec_fee()
}
}
pub fn transfer_send_fee(
cfg: &ActionCreationConfig,
sender_is_receiver: bool,
is_receiver_implicit: bool,
) -> Gas {
if is_receiver_implicit {
cfg.create_account_cost.send_fee(sender_is_receiver)
+ cfg.add_key_cost.full_access_cost.send_fee(sender_is_receiver)
+ cfg.transfer_cost.send_fee(sender_is_receiver)
} else {
cfg.transfer_cost.send_fee(sender_is_receiver)
}
}
pub mod u128_dec_format {
use serde::de;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(num: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}", num))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
u128::from_str_radix(&s, 10).map_err(de::Error::custom)
}
}