Skip to content

Commit

Permalink
Feat/ink basic (#14)
Browse files Browse the repository at this point in the history
* feat: add ink basic

* add ink basic content

* add ink contract architecture

* add testing content

* add cross contract call

* feat: update cross contract call

* add contract template

* feat add why ink

* add introduction pallet contract

* update docs
  • Loading branch information
CocDap authored Sep 8, 2024
1 parent 96dd850 commit 9c652e1
Show file tree
Hide file tree
Showing 26 changed files with 1,407 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"client_libraries": "Thư viện client",
"evm_compatible_layer_substrate": "Tích hợp EVM trong Substrate",
"solidity_development_on_moonbeam": "Solidity Development On Moonbeam",
"introduction_to_ink": "Giới thiệu ink!",
"ink_basic":"Ink! căn bản",
"rakiapp": {
"title": "Polkadot Bootcamp - Video ↗",
"type": "page",
Expand Down
Binary file added pages/assets/call_flip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/call_get.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/cross_contract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/deploy_ink_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/event_ink.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/feature_ink.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/ink.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/ink_application.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/ink_how_it_work.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/ink_rust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/pallet_contract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/upload_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/upload_contract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added pages/ink_basic.mdx
Empty file.
6 changes: 6 additions & 0 deletions pages/ink_basic/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"contract_template": "Contract Template",
"contract_ink_architecture":"Cấu trúc cơ bản của ink! smart contract",
"cross_contract_call":"Tương tác cross-contract",
"testings": "Cách viết tests trong ink!"
}
248 changes: 248 additions & 0 deletions pages/ink_basic/contract_ink_architecture.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# Cấu trúc cơ bản của ink! smart contract


## Định nghĩa ink! contract
+ Sử dụng `#[ink::contract]`

```rust
#[ink::contract]
mod <tên module> {
...
}
```

## Cách định nghĩa on-chain storage
+ Sử dụng `#[ink::storage]`


```rust
#[ink(storage)]
pub struct <tên contract> {
// Các trường dữ liệu on-chain
}
```

### Ví dụ 1 : Hỗ trợ hầu hết các kiễu dữ liệu nguyên thuỷ Rust

```rust
#[ink(storage)]
pub struct MyContract {
// Store a bool
my_bool: bool,
// Store some number
my_number: u32,
// Store some String
my_string: String,
// Store some u32 in a vec
my_vector: Vec<u32>,
}
/* --snip-- */
```

### Ví dụ 2: Substrate Types như AccountId, Hash, Balance

```rust
#[ink(storage)]
pub struct MyContract {
// Store some AccountId
my_account: AccountId,
// Store some Balance
my_balance: Balance,
// Store some Hash
my_hash: Hash,
}
/* --snip-- */
```

### Ví dụ 3: Mapping

```rust
#[ink(storage)]
#[derive(Default)]
pub struct Mappings {
/// Mapping from owner to number of owned token.
balances: Mapping<AccountId, Balance>,
/// Mapping from owner to aliases.
names: Mapping<AccountId, Vec<String>>,
}
```


### Ví dụ 4: Enum và Struct

Lưu ý import 2 thư viện này ở `Cargo.toml`
```rust
[dependencies]
...
scale-info = { version = "2.6", default-features = false, features = ["derive"]}
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
```

+ Định nghĩa Enum
```rust
#[derive(scale::Encode, scale::Decode, Debug)]
#[cfg_attr(feature = "std", derive(StorageLayout, scale_info::TypeInfo))]
pub enum Status {
/// An auction has not started yet.
NotStarted,
/// We are in the starting period of the auction, collecting initial bids.
OpeningPeriod,
}
```


```rust
mod MyContract {
use ink::prelude::string::String;
use ink::prelude::vec::Vec;

// Định nghĩa struct lưu thông tin Status
#[derive(scale::Encode, scale::Decode, Debug)]
#[cfg_attr(feature = "std", derive(StorageLayout, scale_info::TypeInfo))]
pub struct Auction {
/// Branded name of the auction event.
name: String,
/// Some hash identifying the auction subject.
subject: Hash,
/// Auction status.
status: Status, // Enum: Usage shown in next section
/// Candle auction can have no winner.
/// If auction is finalized, that means that the winner is determined.
finalized: bool,
/// vector
vector: Vec<u8>,
}

#[ink(storage)]
pub struct MyContract {
// Store Auctions in a vec
auctions: Vec<Auction>,
}
}

```

## Hàm WRITE (&mut self)

+ Cấu trúc:
```rust
fn <tên hàm> (&mut self, ...) {
...
}
```


+ Ví dụ
```rust
impl MyContract {

#[ink(message)]
pub fn my_setter(&mut self, new_value: u32) {
self.my_number = new_value;
}
}
```

## Hàm READ (&self)

+ Cấu trúc:
```rust
fn <tên hàm> (&self, ...) {
...
}
```



+ Ví dụ

```rust
impl MyContract {

#[ink(message)]
pub fn my_setter(&mut self, new_value: u32) {
self.my_number = new_value;
}
}
```


## Events


![](../assets/event_ink.png)

+ Định nghĩa event

```rust
#[ink(event)]
pub struct Released {
value: Balance,
to: AccountId,
}
```

+ Emit event

```rust
#[ink(message)]
pub fn release(&mut self) -> Result<(), Error> {
...

self.env().emit_event(Released {
value: releasable,
to: self.beneficiary,
});

Ok(())
}
```

## Errors

+ Định nghĩa Errors

```rust
#[derive(Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum Error {
InvalidBeneficiary,
ZeroReleasableBalance,
}
```

+ Return errors

```rust
#[ink(message)]
pub fn release(&mut self) -> Result<(), Error> {
let releasable = self.releasable_balance();
if releasable == 0 {
return Err(Error::ZeroReleasableBalance)
}
...
Ok(())
}
```


## Gas trong ink!
Gas là tài nguyên sử dụng khi thực hiện tương tác với smart contract
Bao gồm:
+ không gian lưu trữ proof (for storing data in the contract's storage)
+ thời gian tính toán (for executing the contract and its logic)

-> Trong Substrate: `Gas = (refTime, proofSize)`

-> Gas -> người thực hiện sẽ trả 1 lượng phí (phí lưu trữ)

### refTime

Lượng thời gian tính toán có thể được sử dụng cho việc thực thi, tính bằng picogiây

### proofSize

+ proofSize is the size of the proof that individual parachains send to the Polkadot or Kusama relay chain to allow re-executing their block for validation (this is called Proof of Validity).
+ Phrased differently: Layer-1 chains send a proof of validity to a Layer-0 chain to validate the block. This Proof of Validity contains everything necessary to execute the block -- the code of each contract that is executed plus the storage each contract reads and writes


Loading

0 comments on commit 9c652e1

Please sign in to comment.