Skip to content

Commit

Permalink
feat: upgrade flipper to ink!4
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Feb 27, 2023
1 parent e6dd43a commit 2bcf30a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ This tutorial targets developers with no experience in ink! and a **basic** leve
- Basic Rust knowledge. [Learn Rust](https://www.rust-lang.org/learn).

### What will we do?
In this tutorial we will implement the most basic contract: [Flipper](https://github.com/paritytech/ink/blob/v3.3.0/examples/flipper/lib.rs) in ink!.
In this tutorial we will implement the most basic contract: [Flipper](https://github.com/paritytech/ink/blob/v4.0.0/examples/flipper/lib.rs) in ink!.

### What will we use?
- [ink! 3.3.0](https://github.com/paritytech/ink/tree/v3.3.0)
- [ink! 4.0.0](https://github.com/paritytech/ink/tree/v4.0.0)

### What will you learn?
- Anatomy of an ink! contract
Expand Down
52 changes: 21 additions & 31 deletions docs/build/wasm/from-zero-to-ink-hero/flipper-contract/flipper.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ sidebar_position: 2
---

# Flipper Contract

This is step-by-step explanation of ink! smart contract by using the most simple app, which is flipper. You will understand the basic structure of ink! smart-contract.

## What is Flipper?

Flipper is a very basic smart contract. It has only one boolean in the storage (`true` or `false`), and when you flip, the value will be changed in to the other.

## Preparation

Please refer to [Prerequisites](./flipper-contract.md)

## Flipper Smart Contract

In a folder run:

```bash
Expand All @@ -23,20 +27,6 @@ $ cd flipper/
$ cargo contract build #build flipper app
```

💡 If you get an error saying:
```bash
ERROR: cargo-contract cannot build using the "stable" channel. Switch to nightly.
```
Please try:
```bash
$ rustup default nightly
```
to switch default Rust toolchain to nightly, or
```
$ cargo +nightly contract build
```
to use nightly toolchain explicitly.

Then, you get the full package and code for Flipper.
Let’s dive into the structure.

Expand All @@ -52,16 +42,14 @@ Let’s dive into the structure.
```rust
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod flipper {

/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[ink(storage)]
pub struct Flipper {
pub struct Flipper {
/// Stores a single `bool` value on the storage.
value: bool,
}
Expand Down Expand Up @@ -104,9 +92,6 @@ mod flipper {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;

/// Imports `ink_lang` so we can use `#[ink::test]`.
use ink_lang as ink;

/// We test if the default constructor does its job.
#[ink::test]
fn default_works() {
Expand Down Expand Up @@ -162,22 +147,24 @@ mod flipper {
```

This annotates a struct that represents the **contract's internal state.** ([details](https://use.ink/macros-attributes/storage)):

```rust
#[ink(storage)]
```

Storage types:

- Rust primitives types
- `bool`
- `u{8,16,32,64,128}`
- `i{8,16,32,64,128}`
- `String`
- `bool`
- `u{8,16,32,64,128}`
- `i{8,16,32,64,128}`
- `String`
- Substrate specific types
- `AccountId`
- `Balance`
- `Hash`
- `AccountId`
- `Balance`
- `Hash`
- ink! storage type
- `Mapping`
- `Mapping`
- Custom data Structure [details](https://use.ink/datastructures/custom-datastructure)

This means the contract(called Flipper) stores a single `bool` value on the storage.
Expand All @@ -190,6 +177,7 @@ pub struct Flipper {
```

### Callable Functions

This is when the contract is deployed and is responsible for **bootstrapping the initial contract state** into the storage, ([more details](https://use.ink/macros-attributes/constructor)).

```rust
Expand All @@ -214,7 +202,7 @@ pub fn default() -> Self {
}
```

This marks a function as **publicly dispatchable**, meaning that it is exposed in the contract interface to the outside world, ([more details](https://use.ink/4.0.0-alpha.1/macros-attributes/message)). Note that all public functions must use the `#[ink(message)]` attribute.
This marks a function as **publicly dispatchable**, meaning that it is exposed in the contract interface to the outside world, ([more details](https://use.ink/macros-attributes/message)). Note that all public functions must use the `#[ink(message)]` attribute.

```rust
#[ink(message)]
Expand All @@ -234,11 +222,11 @@ pub fn get(&self) -> bool {
}
```

💡 If you are simply *reading* from the contract storage, you only need to pass `&self`. But if you want to *modify* storage items, you will need to explicitly mark it as mutable `&mut self`.
💡 If you are simply _reading_ from the contract storage, you only need to pass `&self`. But if you want to _modify_ storage items, you will need to explicitly mark it as mutable `&mut self`.

```rust
impl Flipper {

#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
Expand Down Expand Up @@ -301,3 +289,5 @@ impl Flipper {

Here is the guide how to deploy your contract. Once you deploy it, you can interact with the contracts there:
[deploy using Polkadot UI](https://docs.astar.network/docs/wasm/sc-dev/polkadotjs-ui/).

In this [wasm-flipper](https://github.com/AstarNetwork/wasm-flipper) repository, it explains how to compile and deploy with swanky, and you can interact with contract from UI.

0 comments on commit 2bcf30a

Please sign in to comment.