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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate proc_macro;

mod contract;
mod ink_test;
mod trait_def;

use proc_macro::TokenStream;

/// Entry point for writing ink! smart contracts.
///
/// If you are a beginner trying to learn ink! we recommend you to check out
/// our extensive [ink! workshop](https://substrate.dev/substrate-contracts-workshop/#/).
///
/// **Note:** In all below examples we will be using `ink_lang` crate aliased as just `ink`.
///           You can do this yourself by adding the following line to your code:
///           `use ink_lang as ink;`
///
/// # Description
///
/// The macro does analysis on the provided smart contract code and generates
/// proper code.
///
/// ink! smart contracts can compile in several different modes.
/// There are two main compilation models using either
/// - on-chain mode: `no_std` + WebAssembly as target
/// - off-chain mode: `std`
///
/// We generally use the on-chain mode for actual smart contract deployment
/// whereas we use the off-chain mode for smart contract testing using the
/// off-chain environment provided by the `ink_env` crate.
///
/// # Usage
///
/// ## Header Arguments
///
/// The `#[ink::contract]` macro can be provided with some additional comma-separated
/// header arguments:
///
/// - `dynamic_storage_allocator: bool`
///
///     Tells the ink! code generator to allow usage of ink!'s built-in dynamic
///     storage allocator.
///     - `true`: Use the dynamic storage allocator provided by ink!.
///     - `false`: Do NOT use the dynamic storage allocator provided by ink!.
///
///     This feature is generally only needed for smart contracts that try to model
///     their data in a way that contains storage entites within other storage
///     entities.
///
///     Contract writers should try to write smart contracts that do not depend on the
///     dynamic storage allocator since enabling it comes at a cost of increased Wasm
///     file size. Although it will enable interesting use cases. Use it with care!
///
///     An example for this is the following type that could potentially be used
///     within a contract's storage struct definition:
///     ```
///     // A storage vector of storage vectors.
///     # use ink_storage as storage;
///     # type _unused =
///     storage::Vec<storage::Vec<i32>>
///     # ;
///     ```
///
///     **Usage Example:**
///     ```
///     # use ink_lang as ink;
///     #[ink::contract(dynamic_storage_allocator = true)]
///     mod my_contract {
///         # #[ink(storage)]
///         # pub struct MyStorage;
///         # impl MyStorage {
///         #     #[ink(constructor)]
///         #     pub fn construct() -> Self { MyStorage {} }
///         #     #[ink(message)]
///         #     pub fn message(&self) {}
///         # }
///         // ...
///     }
///     ```
///
///     **Default value:** `false`
///
/// - `compile_as_dependency: bool`
///
///     Tells the ink! code generator to **always** or **never**
///     compile the smart contract as if it was used as a dependency of another ink!
///     smart contract.
///
///     Normally this flag is only really useful for ink! developers who
///     want to inspect code generation of ink! smart contracts.
///     The author is not aware of any particular practical use case for users that
///     makes use of this flag but contract writers are encouraged to disprove this.
///
///     Note that it is recommended to make use of the built-in crate feature
///     `ink-as-dependency` to flag smart contract dependencies listed in a contract's
///     `Cargo.toml` as actual dependencies to ink!.
///
///     **Usage Example:**
///     ```
///     # use ink_lang as ink;
///     #[ink::contract(compile_as_dependency = true)]
///     mod my_contract {
///         # #[ink(storage)]
///         # pub struct MyStorage;
///         # impl MyStorage {
///         #     #[ink(constructor)]
///         #     pub fn construct() -> Self { MyStorage {} }
///         #     #[ink(message)]
///         #     pub fn message(&self) {}
///         # }
///         // ...
///     }
///     ```
///
///     **Default value:** Depends on the crate feature propagation of `Cargo.toml`.
///
/// - `env: impl Environment`
///
///     Tells the ink! code generator which environment to use for the ink! smart contract.
///     The environment must implement the `Environment` (defined in `ink_env`) trait and provides
///     all the necessary fundamental type definitions for `Balance`, `AccountId` etc.
///
///     When using a custom `Environment` implementation for a smart contract all types
///     that it exposes to the ink! smart contract and the mirrored types used in the runtime
///     must be aligned with respect to SCALE encoding and semantics.
///
///     **Usage Example:**
///
///     Given a custom `Environment` implementation:
///     ```
///     pub struct MyEnvironment;
///
///     impl ink_env::Environment for MyEnvironment {
///         const MAX_EVENT_TOPICS: usize = 3;
///         type AccountId = u64;
///         type Balance = u128;
///         type Hash = [u8; 32];
///         type Timestamp = u64;
///         type BlockNumber = u32;
///     }
///     ```
///     A user might implement their ink! smart contract using the above custom `Environment`
///     implementation as demonstrated below:
///     ```
///     # use ink_lang as ink;
///     #[ink::contract(env_types = MyEnvironment)]
///     mod my_contract {
///         # pub struct MyEnvironment;
///         #
///         # impl ink_env::Environment for MyEnvironment {
///         #     const MAX_EVENT_TOPICS: usize = 3;
///         #     type AccountId = u64;
///         #     type Balance = u128;
///         #     type Hash = [u8; 32];
///         #     type Timestamp = u64;
///         #     type BlockNumber = u32;
///         # }
///         #
///         # #[ink(storage)]
///         # pub struct MyStorage;
///         # impl MyStorage {
///         #     #[ink(constructor)]
///         #     pub fn construct() -> Self { MyStorage {} }
///         #     #[ink(message)]
///         #     pub fn message(&self) {}
///         # }
///         // ...
///     }
///     ```
///
///     **Default value:** `DefaultEnvironment` defined in `ink_env` crate.
///
/// ## Anaylsis
///
/// The `#[ink::contract]` macro fully analyses its input smart contract
/// against invalid arguments and structure.
///
/// Some example rules include but are not limited to:
///
/// - There must be exactly one `#[ink(storage)]` struct.
///
///     This struct defines the layout of the storage that the ink! smart contract operates on.
///     The user is able to use a variety of built-in facilities, combine them in various ways
///     or even provide their own implementations of storage data structures.
///
///     For more information visit the `ink_storage` crate documentation.
///
///     **Example:**
///
///     ```
///     # use ink_lang as ink;
///     #[ink::contract]
///     mod flipper {
///         #[ink(storage)]
///         pub struct Flipper {
///             value: bool,
///         }
///         # impl Flipper {
///         #     #[ink(constructor)]
///         #     pub fn construct() -> Self { Flipper { value: false } }
///         #     #[ink(message)]
///         #     pub fn message(&self) {}
///         # }
///     }
///     ```
///
/// - There must be at least one `#[ink(constructor)]` defined method.
///
///     Methods flagged with `#[ink(constructor)]` are special in that they are dispatchable
///     upon contract instantiation. A contract may define multiple such constructors which
///     allow users of the contract to instantiate a contract in multiple different ways.
///
///     **Example:**
///
///     Given the `Flipper` contract definition above we add an `#[ink(constructor)]`
///     as follows:
///
///     ```
///     # use ink_lang as ink;
///     # #[ink::contract]
///     # mod flipper {
///         # #[ink(storage)]
///         # pub struct Flipper {
///         #     value: bool,
///         # }
///     impl Flipper {
///         #[ink(constructor)]
///         pub fn new(initial_value: bool) -> Self {
///             Flipper { value: false }
///         }
///         # #[ink(message)]
///         # pub fn message(&self) {}
///     }
///     # }
///     ```
///
/// - There must be at least one `#[ink(message)]` defined method.
///
///     Methods flagged with `#[ink(message)]` are special in that they are dispatchable
///     upon contract invocation. The set of ink! messages defined for an ink! smart contract
///     define its API surface with which users are allowed to interact.
///
///     An ink! smart contract can have multiple such ink! messages defined.
///
///     **Note:**
///
///     - An ink! message with a `&self` receiver may only read state whereas an ink! message
///       with a `&mut self` receiver may mutate the contract's storage.
///
///     **Example:**
///
///     Given the `Flipper` contract definition above we add some `#[ink(message)]` definitions
///     as follows:
///
///     ```
///     # use ink_lang as ink;
///     # #[ink::contract]
///     # mod flipper {
///         # #[ink(storage)]
///         # pub struct Flipper {
///         #     value: bool,
///         # }
///     impl Flipper {
///         # #[ink(constructor)]
///         # pub fn new(initial_value: bool) -> Self {
///         #     Flipper { value: false }
///         # }
///         /// Flips the current value.
///         #[ink(message)]
///         pub fn flip(&mut self) {
///             self.value = !self.value;
///         }
///
///         /// Returns the current value.
///         #[ink(message)]
///         pub fn get(&self) -> bool {
///             self.value
///         }
///     }
///     # }
///     ```
///
///     **Payable Messages:**
///
///     An ink! message by default will reject calls that additional fund the smart contract.
///     Authors of ink! smart contracts can make an ink! message payable by adding the `payable`
///     flag to it. An example below:
///
///     Note that ink! constructors are always implicitly payable and thus cannot be flagged
///     as such.
///
///     ```
///     # use ink_lang as ink;
///     # #[ink::contract]
///     # mod flipper {
///         # #[ink(storage)]
///         # pub struct Flipper {
///         #     value: bool,
///         # }
///     impl Flipper {
///         # #[ink(constructor)]
///         # pub fn new(initial_value: bool) -> Self {
///         #     Flipper { value: false }
///         # }
///         /// Flips the current value.
///         #[ink(message)]
///         #[ink(payable)] // You can either specify payable out-of-line.
///         pub fn flip(&mut self) {
///             self.value = !self.value;
///         }
///
///         /// Returns the current value.
///         #[ink(message, payable)] // ... or specify payable inline.
///         pub fn get(&self) -> bool {
///             self.value
///         }
///     }
///     # }
///     ```
///
///     **Controlling the messages selector:**
///
///     Every ink! message and ink! constructor has a unique selector with which the
///     message or constructor can be uniquely identified within the ink! smart contract.
///     These selectors are mainly used to drive the contract's dispatch upon calling it.
///
///     An ink! smart contract author can control the selector of an ink! message or ink!
///     constructor using the `selector` flag. An example is shown below:
///
///     ```
///     # use ink_lang as ink;
///     # #[ink::contract]
///     # mod flipper {
///         # #[ink(storage)]
///         # pub struct Flipper {
///         #     value: bool,
///         # }
///     impl Flipper {
///         #[ink(constructor)]
///         #[ink(selector = "0xDEADBEEF")] // Works on constructors as well.
///         pub fn new(initial_value: bool) -> Self {
///             Flipper { value: false }
///         }
///
///         # /// Flips the current value.
///         # #[ink(message)]
///         # #[ink(selector = "0xCAFEBABE")] // You can either specify selector out-of-line.
///         # pub fn flip(&mut self) {
///         #     self.value = !self.value;
///         # }
///         #
///         /// Returns the current value.
///         #[ink(message, selector = "0xFEEDBEEF")] // ... or specify selector inline.
///         pub fn get(&self) -> bool {
///             self.value
///         }
///     }
///     # }
///     ```
///
/// ## Interacting with the Contract Executor
///
/// The `ink_env` crate provides facitilies to interact with the contract executor that
/// connects ink! smart contracts with the outer world.
///
/// For example it is possible to query the current call's caller via:
/// ```
/// # ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
/// let caller = ink_env::caller::<ink_env::DefaultEnvironment>();
/// # let _caller = caller;
/// # Ok(())
/// # }).unwrap();
/// ```
///
/// However, ink! provides a much simpler way to interact with the contract executor
/// via its environment accessor. An example below:
///
/// ```
/// # use ink_lang as ink;
/// #
/// #[ink::contract]
/// mod greeter {
///     #[ink(storage)]
///     pub struct Greeter;
///
///     impl Greeter {
///         #[ink(constructor)]
///         pub fn new() -> Self {
///             let caller = Self::env().caller();
///             let message = format!("thanks for instantiation {:?}", caller);
///             ink_env::debug_println(&message);
///             Greeter {}
///         }
///
///         #[ink(message, payable)]
///         pub fn fund(&mut self) {
///             let caller = self.env().caller();
///             let value = self.env().transferred_balance();
///             let message = format!("thanks for the funding of {:?} from {:?}", value, caller);
///             ink_env::debug_println(&message);
///         }
///     }
/// }
/// ```
///
/// ## Events
///
/// An ink! smart contract may define events that it can emit during contract execution.
/// Emitting events can be used by third party tools to query information about a contract's
/// execution and state.
///
/// The following example ink! contract shows how an event `Transferred` is defined and
/// emitted in the `#[ink(constructor)]`.
///
/// ```
/// # use ink_lang as ink;
/// #
/// #[ink::contract]
/// mod erc20 {
///     /// Defines an event that is emitted every time value is transferred.
///     #[ink(event)]
///     pub struct Transferred {
///         from: Option<AccountId>,
///         to: Option<AccountId>,
///         value: Balance,
///     }
///
///     #[ink(storage)]
///     pub struct Erc20 {
///         total_supply: Balance,
///         // more fields ...
///     }
///
///     impl Erc20 {
///         #[ink(constructor)]
///         pub fn new(initial_supply: Balance) -> Self {
///             let caller = Self::env().caller();
///             Self::env().emit_event(Transferred {
///                 from: None,
///                 to: Some(caller),
///                 value: initial_supply,
///             });
///             Self { total_supply: initial_supply }
///         }
///
///         #[ink(message)]
///         pub fn total_supply(&self) -> Balance {
///             self.total_supply
///         }
///     }
/// }
/// ```
///
/// ## Example: Flipper
///
/// The below code shows the complete implementation of the so-called Flipper
/// ink! smart contract.
/// For us it acts as the "Hello, World!" of the ink! smart contracts because
/// it is minimal while still providing some more or less useful functionality.
///
/// It controls a single `bool` value that can be either `false` or `true`
/// and allows the user to flip this value using the `Flipper::flip` message
/// or retrieve the current value using `Flipper::get`.
///
/// ```
/// use ink_lang as ink;
///
/// #[ink::contract]
/// pub mod flipper {
///     #[ink(storage)]
///     pub struct Flipper {
///         value: bool,
///     }
///
///     impl Flipper {
///         /// Creates a new flipper smart contract initialized with the given value.
///         #[ink(constructor)]
///         pub fn new(init_value: bool) -> Self {
///             Self { value: init_value }
///         }
///
///         /// Flips the current value of the Flipper's bool.
///         #[ink(message)]
///         pub fn flip(&mut self) {
///             self.value = !self.value;
///         }
///
///         /// Returns the current value of the Flipper's bool.
///         #[ink(message)]
///         pub fn get(&self) -> bool {
///             self.value
///         }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn contract(attr: TokenStream, item: TokenStream) -> TokenStream {
    contract::generate(attr.into(), item.into()).into()
}

/// Marks trait definitions to ink! as special ink! trait definitions.
///
/// There are some restrictions that apply to ink! trait definitions that
/// this macro checks. Also ink! trait definitions are required to have specialized
/// structure so that the main [`#[ink::contract]`](`macro@crate::contract`) macro can
/// properly generate code for its implementations.
///
/// # Example: Definition
///
/// ```
/// use ink_lang as ink;
/// # type Balance = <ink_env::DefaultEnvironment as ink_env::Environment>::Balance;
///
/// #[ink::trait_definition]
/// pub trait Erc20 {
///     /// Constructs a new ERC-20 compliant smart contract using the initial supply.
///     #[ink(constructor)]
///     fn new(initial_supply: Balance) -> Self;
///
///     /// Returns the total supply of the ERC-20 smart contract.
///     #[ink(message)]
///     fn total_supply(&self) -> Balance;
///
///     // etc.
/// }
/// ```
///
/// # Example: Implementation
///
/// Given the above trait definition you can implement it as shown below:
///
/// ```
/// # use ink_lang as ink;
/// #
/// #[ink::contract]
/// mod base_erc20 {
/// #    // We somehow cannot put the trait in the doc-test crate root due to bugs.
/// #    #[ink_lang::trait_definition]
/// #    pub trait Erc20 {
/// #        /// Constructors a new ERC-20 compliant smart contract using the initial supply.
/// #        #[ink(constructor)]
/// #        fn new(initial_supply: Balance) -> Self;
/// #
/// #        /// Returns the total supply of the ERC-20 smart contract.
/// #        #[ink(message)]
/// #        fn total_supply(&self) -> Balance;
/// #    }
/// #
///     #[ink(storage)]
///     pub struct BaseErc20 {
///         total_supply: Balance,
///         // etc ..
///     }
///
///     impl Erc20 for BaseErc20 {
///         #[ink(constructor)]
///         fn new(initial_supply: Balance) -> Self {
///             Self { total_supply: initial_supply }
///         }
///
///         /// Returns the total supply of the ERC-20 smart contract.
///         #[ink(message)]
///         fn total_supply(&self) -> Balance {
///             self.total_supply
///         }
///
///         // etc ..
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn trait_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
    trait_def::analyze(attr.into(), item.into()).into()
}

/// Defines a unit test that makes use of ink!'s off-chain testing capabilities.
///
/// If your unit test does not require the existence of an off-chain environment
/// it is fine to not use this macro since it bears some overhead with the test.
///
/// Note that this macro is not required to run unit tests that require ink!'s
/// off-chain testing capabilities but merely improves code readability.
///
/// ## How do you find out if your test requires the off-chain environment?
///
/// Normally if the test recursively uses or invokes some contract methods that
/// call a method defined in `self.env()` or `Self::env()`.
///
/// An examples is the following:
///
/// ```no_compile
/// let caller: AccountId = self.env().caller();
/// ```
///
/// # Example
///
/// ```
/// use ink_lang as ink;
///
/// #[cfg(test)]
/// mod tests {
///     // Conventional unit test that works with assertions.
///     #[ink::test]
///     fn test1() {
///         // test code comes here as usual
///     }
///
///     // Conventional unit test that returns some Result.
///     // The test code can make use of operator-`?`.
///     #[ink::test]
///     fn test2() -> Result<(), ink_env::Error> {
///         // test code that returns a Rust Result type
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
    ink_test::generate(attr.into(), item.into()).into()
}

#[cfg(test)]
pub use contract::generate_or_err;