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 to Pop Network
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 Network locally and on Paseo (Polkadot's Test Network)
interact with the deployed contract
Prerequisites
On completion of this tutorial, developers will be able to:
create, build, test, and deploy ink! smart contracts
deploy smart contracts to Pop Network locally and 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.
Make sure the Pop CLI tool has been installed correctly by running the pop
command in your terminal.
Getting Started
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! 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:
By default, when we generate an ink! smart contract using the command above, we get a template (the "flipper" template). It serves as a good starting point for contract development.
Pop CLI supports several templates. If you would like to know what templates are available you can run the following command to see a list: pop new contract --help
Let's look at our first bit of ink! code.
You will see the following in this directory:
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:
For Pop CLI versions <0.3.0
the pop build
command is pop build contract
Awesome, your contract is built!
With Pop CLI, you can also run your smart contract tests.
Run your ink! smart contract unit tests
Okay, so you contract builds, your contract passes the tests. We can now deploy the contract.
Deploy your contract locally
In order to deploy an ink! smart contract locally we need to spin up a local instance of Pop Network.
Pop Network is a parachain meaning that it runs on the Polkadot Relay chain.
To take a deep dive into how Parachains and the Relay chain work, go here.
We will use Pop CLI to spin up a live network locally on our machine.
In a separate directory, let's create the configuration file for our local test network.
./network.toml
We can now use the configuration file to spin up the network.
The first time you run this command it will take some time. Grab some coffee.
Once complete, you will get some output:
You now have the following running locally on your machine:
rococo-local
this is the Polkadot "Test" Relay chain
two validator nodes (alice & bob) to run the rococo-local Relay chain
asset-hub-rococo-local
this is the Asset Hub system parachain that manages assets in Polkadot
one collator node is running for this system chain
it is useful to have a second parachain like this one to test cross-chain capabilities
pop-devnet
this is the Pop Network parachain
one collator node is running for the Pop Network parachain
Confirm that the Pop Network parachain is producing blocks by opening the PolkadotJS link in your browser:
https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:4385#/explorer
Replace the port number with the port number for the Pop Network parachain that is outputted in your terminal
Cool. Now that we have Pop Network running we can now deploy our contract.
Keep the PolkadotJs App window open, specifically to see the recent events. After we deploy the contract, we will see the events displayed there.
You will need to know the RPC URL for Pop Network which was outputted in the terminal when you ran the pop up
command.
The contract has been deployed!
Save the contract address as we will be used in the next step to interact with the contract.
You can also confirm that the contract was deployed in the recent events in Polkadot JS:
Interacting with our contract
Now that the contract is live, we can start interacting with it.
Grab the contract address that was outputted from the previous step.
If you lost the address, you can always pull up PolkadotJs Apps and check the chain state for the contract address.
The result here is false
meaning that value
in the flipper storage is set to false
.
Let's try flipping it.
Notice the
-x
flag at the end of thepop call
command. This is needed because we are executing a transaction on the network and it will cost gas.
Did it work? Let's see if the value has been flipped.
The value
is now true
.
Awesome we now know how to make calls to our smart contract.
Running end-to-end tests of your contract
Make sure you are in the flipper directory:
If you look at the flipper ink! smart contract, you will notice it has end-to-end tests at the end of the lib.rs
file.
For ink! end-to-end testing, you will need to have a Substrate node with pallet contracts
for the ink! e2e tests to run against. Pop CLI will download the substrate-contracts-node binary for this purpose.
Run the e2e tests:
Passed!
To read more about ink! end-to-end testing:
Deploy on the Pop Network TestNet
Once you have battle-tested your smart contract using the armor of unit and e2e tests, you are ready to test your smart contract on a live network: the Pop Network TestNet.
Let's deploy.
The test network for Polkadot is Paseo.
You can find Pop Network running on Paseo here.
Use the Paseo faucet to fund Alice's Paseo account with some PAS tokens. PAS tokens are the equivalent of DOT on Polkadot.
Alice's account is: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
Go to the Paseo faucet and request some PAS tokens for Alice.
Since Pop Network uses the Relay chain's native token as its native token, we will need to get the PAS tokens that is in your account on Paseo into Pop Network so we can deploy the contract.
We will need to add Alice to our PolkadotJs Wallet extension. We can do that by using Alice's secret seed: 0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a
Remember this is for development purposes. In production you would already have an existing wallet to use.
Now that we have Alice's account in our PolkadotJs Wallet Extension, we can use the following for teleporting PAS tokens to Pop Network:
https://onboard.popnetwork.xyz
Cool! Now that you have some PAS tokens on Pop Network, we can deploy.
You can now take the contract address and check the chain state to confirm that the contract exists
Let's add the contract to our Contracts UI in PolkadotJS Apps. Click on "add an existing contract".
Add the contract address along with the flipper.contract
file found inside flipper/target/ink/flipper.contract
Save. You can now see your newly uploaded contract.
Done!
Resources
https://use.ink
Technical Support
Create a question and tag it with "
pop
"Share the StackExchange question in our Pop Support Telegram channel
Feedback
Take a minute to give us feedback on this tutorial so we can improve it!
Last updated