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
//! Provides the macros needed to write fvm contracts
//!
//! ## Basic Usage
//! Macros can be used in contract for simplify contract preparation process,
//! like this:
//! ``` no_run
//! # #[cfg(not(feature = "advance"))]
//! use fvm_macros::contract;
//! # #[cfg(not(feature = "advance"))]
//! use fvm_macros::storage;
//! # #[cfg(not(feature = "advance"))]
//! use fvm_std::collections::hyper_map::HyperMap;
//!
//! # #[cfg(not(feature = "advance"))]
//! #[storage]
//! pub struct SetHash {
//! map: HyperMap<String, String>,
//! }
//!
//! # #[cfg(not(feature = "advance"))]
//! #[contract]
//! impl SetHash {
//! fn new() -> Self {
//! Self { map: HyperMap::new() }
//! }
//!
//! pub fn set_hash(&mut self, key: String, value: String) {
//! self.map.insert(key, value);
//! }
//!
//! pub fn get_hash(&mut self, key: String) -> &String {
//! self.map.get(&key).unwrap()
//! }
//! }
//! ```
//!
//! ## Contract Mode
//! Now write contract have two mode:
//! - **normal mode**
//!
//! Contract with normal mode limited data storage format, and user can write contract more
//! convenient. User can use all macros except `advance_contract`.
//! - **advance mode**
//!
//! Contract with advance mode not limited data storage format, and it must open advance
//! feature with `fvm-std` and 'fvm-macros' lib to use this mode. The execute speed of contract
//! in this mode would be fast then the contract in normal mode.
//!
use ;
use ;
use ;
use ItemImpl;
/// Generate storage attribute for struct
///
/// ## Example
/// ``` no_run
/// # #[cfg(not(feature = "advance"))]
/// use fvm_macros::storage;
/// # #[cfg(not(feature = "advance"))]
/// use fvm_std::collections::hyper_map::HyperMap;
///
/// # #[cfg(not(feature = "advance"))]
/// #[storage]
/// pub struct SetHash {
/// map: HyperMap<String, String>,
/// }
/// ```
///
/// The attributes of the marked struct by `#[storage]` will be automatically
/// mapped to the ledger
///
/// Mark method implementation as contract method
///
/// ## Example
/// ``` no_run
/// # #[cfg(not(feature = "advance"))]
/// use fvm_macros::contract;
/// # #[cfg(not(feature = "advance"))]
/// use fvm_macros::storage;
/// # #[cfg(not(feature = "advance"))]
/// use fvm_std::collections::hyper_map::HyperMap;
///
/// # #[cfg(not(feature = "advance"))]
/// #[storage]
/// pub struct SetHash {
/// map: HyperMap<String, String>,
/// }
///
/// # #[cfg(not(feature = "advance"))]
/// #[contract]
/// impl SetHash {
/// fn new() -> Self {
/// Self { map: HyperMap::new() }
/// }
///
/// pub fn set_hash(&mut self, key: String, value: String) {
/// self.map.insert(key, value);
/// }
/// }
/// ```
///
/// The methods of the marked impl by `#[contract]` will be considered as contract methods,
/// note that:
/// - impl methods must have one init method with signature `fn new() -> Self`
/// - the method with `pub` can be called by user, and other can not
/// - no `pub` method can not be cross-called, and will no generate abi
///
/// Mark method implementation as advance contract method
///
/// ## Example
/// ``` no_run
/// # #[cfg(feature = "advance")]
/// {
/// use fvm_std::advance as ledger;
/// use fvm_macros::advance_contract;
///
/// pub struct SetHash {}
///
/// #[advance_contract]
/// impl SetHash {
/// fn new() {}
///
/// pub fn set_hash(key: String, value: String) {
/// ledger::storage_write(key.as_bytes(), value.as_bytes());
/// }
/// // other methods...
/// }
/// }
/// ```
///
/// The methods of the marked impl by `#[advance_contract]` will be considered as contract methods,
/// and the use of `fvm_std` should be in advance mode, note that:
/// - this macro can only used in `advance` mode
/// - impl methods must have one init method with signature `fn new()`
/// - the method with `pub` can be called by user, and other can not
/// - no `pub` method can not be cross-called, and will no generate abi
///
/// Generate cross call attribute
/// this macro has two ways to use.
/// - First cross call by address.
///
/// ## Example
///
/// ```no_run
/// use fvm_std::prelude::{String, Address};
/// use fvm_macros::cross;
/// # #[cfg(not(feature = "advance"))]
///
/// #[cross("0x1000203012030120302130210321000000000000")]
/// trait SimulateBank {
/// fn transfer_value(addr1: &Address, addr2: Address, amount: u64) -> bool;
/// fn get_account_balance(addr: Address) -> u64;
/// }
/// ```
/// - Second cross call by cns, attribute with `cns_name`.
///
/// ## Example
///
/// ```no_run
/// use fvm_std::prelude::{String, Address};
/// use fvm_macros::cross;
///
/// # #[cfg(not(feature = "advance"))]
/// #[cross(cns_name="HelloContract")]
/// trait SimulateBankCNS {
/// fn transfer_value(addr1: &Address, addr2: Address, amount: u64) -> bool;
/// fn get_account_balance(addr: Address) -> u64;
/// }
/// ```
///
/// Parallel level 2
/// This attribute could have two fields. `field_name` and `para_index`.
/// `field_name` point out which StoreField will parallel
/// `para_index` the para index of this StoreField, used for `HyperMap`
/// ```rust
/// # #[cfg(not(feature = "advance"))]
/// {
/// use fvm_std::collections::hyper_map::HyperMap;
/// use fvm_macros::parallel_field;
/// #[parallel_field]
/// fn demo(key: String) {}
///
/// #[parallel_field(field_name="key")]
/// fn demo1(key: String) {}
///
/// #[parallel_field(field_name="key")]
/// #[parallel_field(field_name="key2", para_index="1")]
/// fn demo2(key: String, key2: HyperMap<String, String>) {}
/// }
/// ```
/// Parallel level 1
/// Used while cross call the parallel
/// the attribute has three fields.
/// `address`: cross contract address
/// `address_index`: if `address` is empty, then you should declare the cross address in function params, this is the index of params.
/// `methods`: the name of cross call the methods
/// `cns_name`: CNS of contract.
/// `cns_index`: CNS of contract index.
/// ```rust
/// use fvm_std::types::Address;
/// use fvm_macros::parallel_cross;
///
/// #[parallel_cross(address="0x0", methods="call")]
/// fn demo1() {}
///
/// #[parallel_cross(address_index=1, methods="call")]
/// fn demo2(addr: Address) {}
///
/// #[parallel_cross(cns_name="Demo", methods="call")]
/// fn demo3(addr: Address) {}
///
/// #[parallel_cross(cns_index=1, methods="call")]
/// fn demo4(addr: Address) {}
/// ```
/// Mark parallel call method
///
/// This attribute used for call the contract method, while the called method has parallel info.
/// ```rust
/// use fvm_macros::parallel_call_method;
/// use fvm_macros::parallel_field;
///
/// #[parallel_field(field_name="key1")]
/// #[parallel_call_method(methods="method2")]
/// fn method1(){
/// method2();
/// }
///
/// #[parallel_field(field_name="key")]
/// fn method2(){}
/// ```