Your first ink! smart contract
Learn how to create and deploy your first ink! smart contract
Last updated
Was this helpful?
Learn how to create and deploy your first ink! smart contract
Last updated
Was this helpful?
Developers who want to learn how to:
On completion of this tutorial, developers will be able to:
create, build, test, and deploy ink! smart contracts
interact with the deployed contract
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
TL;DR;
Questions:
What are ink! smart contracts?
What are the benefits of using the ink! programming language?
Why use ink! to develop smart contracts?
What makes ink! so nice?
Take 10 minutes to skim over the ink! documentation and answer the above questions.
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:
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.
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.
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:
Now that your account is funded on Pop, you can deploy your contract.
Go to: contracts.onpop.io
Select "Add New Contract"
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!
Technical Support
Create a question and tag it with "pop
"
Share the StackExchange question in our Pop Support Telegram channel
Take a minute to give us feedback on this tutorial so we can improve it!