GuiGou12358/astar-tutorials: Tutorials to write Smart Contracts in Rust and Ink! https://github.com/GuiGou12358/astar-tutorials
You are a world-class software engineer. You are particularly good at explaining code in a thorough but simple way. Explain the given code. Be thorough, but explain it in a simple way that anyone could understand.
Respond as a well-formatted markdown file that is organized into sections. Make sure to use code blocks.
Explain this code:
#![cfg_attr(not(feature = "std"), no_std)]
#[ink::contract]
mod incrementer {
#[ink(storage)]
pub struct Incrementer {
value: i32,
}
#[ink(event)]
pub struct Incremented {
by: i32,
new_value: i32,
who: AccountId,
}
impl Incrementer {
#[ink(constructor)]
pub fn new(init_value: i32) -> Self {
Self { value: init_value }
}
#[ink(message)]
pub fn inc(&mut self, by: i32) {
self.value += by;
ink::env::debug_println!("increment by {}, new value {}", by, self.value);
let signer = self.env().caller();
self.env().emit_event(Incremented{ by, new_value: self.value, who: signer });
}
#[ink(message)]
pub fn get(&self) -> i32 {
self.value
}
}
}
The given code is written in Rust and is using a library named "ink". Ink is a Rust Smart Contract Language, built on top of Rust language, it provides additional features that make it easy and fast to write smart contracts for several blockchains, including Substrate. Ink defines some custom attributes, such as "#[ink::contract]", "#[ink(storage)]", "#[ink(event)]", and "#[ink(constructor)]", to simplify and enhance smart contract development in Rust.
The code has a struct named "Incrementer", which is a smart contract, and it has one variable named "value" which is an integer, and is public so can be accessed outside of the contract.
#[ink(storage)]
pub struct Incrementer {
value: i32,
}
The code has an event named "Incremented", which will be emitted every time the value variable gets incremented using an "inc" method. The payload of this event has three variables: "by" the number by which the value got incremented, "new_value" the new value of the "value" variable after incrementing, "who" the AccountId of the person who called the "inc" message.
#[ink(event)]
pub struct Incremented {
by: i32,
new_value: i32,
who: AccountId,
}
The code has "#[ink::contract]" at the beginning of the smart contract which indicates that this Rust struct is Ink Smart Contract. And then "#[cfg_attr(not(feature = "std"), no_std)]" make sure the code can be compiled for non-standard configurations to be valid.
#![cfg_attr(not(feature = "std"), no_std)]
#[ink::contract]
mod incrementer {
The code has a public function name "new", which is a constructor, this constructor initiates the value of the "value" variable of the Struct/Smart contract, it takes one parameter as an integer.
#[ink(constructor)]
pub fn new(init_value: i32) -> Self {
Self { value: init_value }
}
The code has a public function named "inc", this increments the value of the "value" variable by taking an integer as its parameter. Along with incrementing the value, it also logs the current value and who called the function using the Event named "Incremented" by calling the emit_event() function.
#[ink(message)]
pub fn inc(&mut self, by: i32) {
self.value += by;
ink::env::debug_println!("increment by {}, new value {}", by, self.value);
let signer = self.env().caller();
self.env().emit_event(Incremented { by, new_value: self.value, who: signer });
}
The code has a public function named "get", which is a getter function and is used to get the current value of the "value" variable.
#[ink(message)]
pub fn get(&self) -> i32 {
self.value
}