Your first ink! smart contract

Learn how to create and deploy your first ink! smart contract

Audience

Developers who want to learn how to:

  • use the Pop CLI tool to speed up development

  • deploy smart contracts on Pop

Learning Objectives

On completion of this tutorial, developers will be able to:

  • create, build, test, and deploy ink! smart contracts

  • deploy smart contracts to Pop on Paseo (Polkadot's Test Network)

  • interact with the deployed contract

Installing Pop CLI

Let's install the powerful Pop CLI tool which will be used throughout this tutorial.

cargo install --force --locked pop-cli

Make sure the Pop CLI tool has been installed correctly by running the pop command in your terminal.

pop --help

Getting Started

TL;DR;

Questions:

  1. What are ink! smart contracts?

  2. What are the benefits of using the ink! programming language?

  3. Why use ink! to develop smart contracts?

  4. What makes ink! so nice?

Take 10 minutes to skim over the ink! documentation and answer the above questions.

ink! contracts are smart(er)

ink! allows you to write smart contracts using Rust. That means you get all the benefits of the Rust programming language.

The ink! embedded domain-specific language (eDSL) retro-fits your Rust code with powerful features that enhance smart contract development. In doing so, ink! improves the developer ergonomics when it comes to using Rust for writing smart contracts.

Let's create a simple ink! smart contract using the "flipper" template:

pop new contract
┌   Pop CLI : Generate a contract

◇  Select a template type: 
│  Examples 

◇  Select the contract:
│  Standard 

◆  Where should your project be created?
│  ./flipper 

Flipper is a good starting point for those new to ink! smart contract development.

Let's look at our first bit of ink! code.

You will see the following in this directory:

flipper
  └─ lib.rs
  └─ Cargo.toml
  └─ .gitignore

Inside lib.rs you will find all the contract logic.

Take a moment to get familiar with the ink! programming language:

Building the ink! smart contract

Let's build flipper! Make sure you are inside the flipper directory and run the following command:

pop build

┌   Pop CLI : Building a contract

 [==] Checking clippy linting rules
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling syn v1.0.109
   Compiling equivalent v1.0.1
   Compiling hashbrown v0.14.3
   Compiling winnow v0.5.40
   ....
   Finished release [optimized] target(s) in 21.17s
   Running `target/ink/release/metadata-gen`
 [==] Generating bundle

│  Original wasm size: 21.3K, Optimized: 1.6K

│  The contract was built in RELEASE mode.

│  Your contract artifacts are ready. You can find them in:
│  /Users/bruno/src/flipper/target/ink

│    - flipper.contract (code + metadata)
│    - flipper.wasm (the contract's code)
│    - flipper.json (the contract's metadata)

└  Build completed successfully!

Awesome, your contract is built!

With Pop CLI, you can also run your smart contract tests.

Run your ink! smart contract unit tests

pop test contract

┌   Pop CLI : Building a contract

 [==] Checking clippy linting rules
   Compiling proc-macro2 v1.0.79
   ....
   Compiling flipper v0.1.0 (/Users/bruno/src/pop-cli/flipper)
    Finished test [unoptimized + debuginfo] target(s) in 8.06s
     Running unittests lib.rs (target/debug/deps/flipper-ff1505839f6873de)

running 2 tests
test flipper::tests::it_works ... ok
test flipper::tests::default_works ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests flipper

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

└  Unit testing complete

Okay, so you contract builds and passes the tests. We can now deploy the contract to Pop.

Deploy your contract

Once you have battle-tested your smart contract using unit and e2e tests, you are ready to test your smart contract on the Pop Testnet.

Before you deploy, make sure you have bridged some tokens from the Polkadot Relay chain to your Pop account. To learn how to do this, go here:

Deploy

Now that your account is funded on Pop, you can deploy your contract.

  1. Select "Add New Contract"

  2. Select "Upload New Contract Code"

Next, give the contract a name and upload the flipper/target/ink/flipper.contract file which contains the contract's Wasm code as well as the contract's metadata.

You can now interact with your contract!

Congrats! Your contract is deployed!

Resources

Technical Support

Feedback

Take a minute to give us feedback on this tutorial so we can improve it!

Last updated

Was this helpful?