Skip to content

Latest commit

 

History

History
138 lines (95 loc) · 4.56 KB

2001.md

File metadata and controls

138 lines (95 loc) · 4.56 KB

HOME

Building a Smart Contract using Ink! and Astar

What Ink! is?

It's a domain-specific language (DSL) embedded within Rust programming language, designed specifically for writing WebAssembly (Wasm) smart contracts on Polkadot. By using Rust, developers benefit from its strong memory safety, advanced type system, and comprehensive development tools. Rust’s ownership model ensures that typical issues like null pointer dereferencing and buffer overflows are things of the past. Wasm offers fast execution speeds, platform independence, and enhanced security through sandboxed environments.

Why Astar Parachain?

Polkadot’s Relay Chain is designed for security and interoperability, not smart contract execution. To build a smart contract, you need a parachain like Astar, which supports the “contracts” pallet. For Cardano developers: this is like building on Cardano’s mainnet, but instead of deploying directly to the base layer, you deploy to a specialized chain (Astar) that handles smart contracts and connects to Polkadot’s shared security. You could spin your parachain and deploy your smart contracts on it, but we are following an easy path for educational purposes.

Step 1: Setup the environment

1.1 Install dependencies

# Install Rust & Cargo (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh

# Add WASM compilation target
rustup target add wasm32-unknown-unknown

# Install ink! CLI
cargo install --force --locked cargo-contract

1.2 Create a new ink! project my_first_dapp

cargo contract new my_first_dapp

cd my_first_dapp

Step 2: Write a simple smart contract (an incrementor)

2.1 Open lib.rs and replace it with this code

#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
mod my_first_dapp {
    #[ink(storage)]
    pub struct MyContract {
        value: i32,
    }

    impl MyContract {
        #[ink(constructor)]
        pub fn new(init_value: i32) -> Self {
            Self { value: init_value }
        }

        #[ink(message)]
        pub fn get(&self) -> i32 {
            self.value
        }

        #[ink(message)]
        pub fn increment(&mut self) {
            // Use checked_add to prevent overflow
            self.value = self.value
                .checked_add(1)
                .expect("Overflow in increment operation");
        }
    }
}

2.2 Explanation

  • #[ink(storage)]: Defines the contract’s state (like a Cardano datum).
  • #[ink(message)]: Public functions callable by users (similar to Cardano’s endpoints).
  • #[ink(constructor)]: Initializes the contract (like a Plutus validator script).

Step 3: Compile the Contract

cargo contract build

This generates:

  • my_first_dapp.wasm (compiled contract).
  • metadata.json (ABI for frontend interaction).

Step 4: Get SBY tokens (Shibuya Testnet)

  1. Connect to Shibuya Testnet:
  2. Use the Faucet:
    • Scroll to the SBY Token section
    • Click "Faucet" and request SBY free tokens
    • Note: SBY is the testnet token for Shibuya

Step 5 (option A - easy): Deploy with UseInk playground

4.1 Connect to Astar via UseInk

  1. Go to https://ui.use.ink/ →select Astar Shibuya
  2. Select Add new contract

4.2 Upload the Contract

  1. Click Upload new contract code.
  2. Set the name (e.g. my_first_dapp)
  3. Upload Contract Bundle (which is my_first_dapp.contract)
  4. Next and sign

4.3 Call the Contract

  1. Call get: Retrieve the current value.
  2. Call increment: Update the value (requires a small gas fee).

Step 5 (option B - facing PolkadotJS): Deploy directly to the parachain

4.1 Connect to Astar’s Testnet (Shibuya)

  1. Go to Polkadot-JS AppsShibuya Testnet.
  2. Navigate to Developer → Contracts.

4.2 Upload the Contract

  1. Click Upload & deploy code.
  2. Upload my_first_dapp.wasm and metadata.json.
  3. Set the initial value (e.g., 42) during deployment.

4.3 Call the Contract

  1. Call get: Retrieve the current value.
  2. Call increment: Update the value (requires a small gas fee).

HOME