website logo
WebsiteGithubDiscordTwitterTelegram
⌘K
Get started
Welcome to peaq
Introduction to peaq
Our mission
Our vision
Learn
Networks overview
Economics
Tokens & Token Utility
Why Polkadot
Tech stack
Source code
Important links
Security Assessment Reports
Builders
Get started
Build
Integrate
Get inspired
Grants & Funding
Ecosystem and Community Initiatives
User Interfaces
peaq control
peaq App
Node operators
Run a node
Tokens & Earn
Agung tokens (Faucet)
Wallets
Docs powered by archbee 
25min

Substrate Smart Contract

1. Running a Substrate Smart Contracts Node OR Peaq Network Node

After successfully installing substrate-contracts-node, you can start a local development chain by running:

Text
|



Or Peaq Network Node:

Text
|


You should start seeing blocks being produced by your node in your terminal.

2. Canvas UI or Polka.js front end

you can etiher upload the contract to Canvas UI or to our peaq front end.

Also Canvas UI can be connected to Substrate Node or Peaq Network node.

Canvas UI: Go to the hosted version of Canvas UI to interact with your node. You first need to configure the UI to connect to it:

  • Click on the dropdown selector at bottom left corner.
  • Choose the Local Node


Polka.js Front end:

This is the second option to upload a contract.

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fwss.agung.peaq.network#/explorer

3.Build Smart Contract Code

Flipper: For tesing you can use flipper contract also. Just checkout the latest flipper contract from https://github.com/paritytech/ink/tree/master/examples/flipper and build it.

RBAC: https://github.com/peaqnetwork/RBAC/tree/dev check out the latest code in local

Run the following command to compile your smart contract in the RBAC project directory:

Text
|


Once the code is built, you would see target folder in your local under RBAC folder.


Document image


Now that we have generated the Wasm binary from our source code and connected to a local node, we want to deploy this contract onto our Substrate blockchain.

Smart contract deployment on Substrate is a little different than on traditional smart contract blockchains.

Whereas a completely new blob of smart contract source code is deployed each time you push a contract on other platforms, Substrate opts to optimize this behavior. For example, the standard ERC20 token has been deployed to Ethereum thousands of times, sometimes only with changes to the initial configuration (through the Solidity constructor function). Each of these instances take up space on the blockchain equivalent to the contract source code size, even though no code was actually changed.

In Substrate, the contract deployment process is split into two steps:

  1. Putting your contract code on the blockchain
  2. Creating an instance of your contract

With this pattern, contract code like the ERC20 standard can be put on the blockchain one single time, but instantiated any number of times. No need to continually upload the same source code over and waste space on the blockchain.

1. Upload Contract Code to Canvas UI or Polka.js peaq front end

Option 1- Canvas UI:

Here we will upload the contract code and instantiate one copy of the contract on the blockchain ( which is usually why we upload the contract code in the first place):

  • Click the Upload & Instantiate Contract button.
  • Choose an Instantiation account (e.g. ALICE).
  • Give the contract a descriptive Name (e.g. RBAC Contract).
  • Drag the RBAC.contract or flipper.contract file that contains the bundled Wasm blob and metadata into the drag & drop area. You will see the UI parse the metadata and showing what functions the contract contains.
  • Click the Constructor Details
Document image


Option 2- Polka.js Front end:

  • Go to https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fwss.agung.peaq.network#/explorer
  • Click Developer Menu → Contract
  • Click the Upload & Deploy code
  • Upload the RBAC.contract or flipper.contract file that contains the bundled Wasm

Then you can see the contract and can intereact with it.

Document image


2. Instantiate a Contract on the Blockchain

Smart contracts exist as an extension of the account system on the blockchain. Thus creating an instance of this contract will create a new Account

Document image


Id which will store any balance managed by the smart contract and allow us to interact with the contract.

Now a screen displays the information that represents our smart contract. We are going to instantiate a copy of the smart contract:

  • Give the contract instance a Contract Name (e.g. The First Flipper or RBAC).
  • Accept the default options for the contract Instantiation Constructor.
  • Accept the default options Endowment of 1000 Units to pay the storage rent, and Max Gas Allowed of 200000.
  • Click on Instantiate
Document image


Note:* As mentioned earlier, contract creation involves creation of a new account. As such, you must be sure to give the contract account at least the existential deposit defined by your blockchain. We also need to be able to pay the contract's rent (endowment). If we consume all of this deposit, the contract will become a tombstone. We can always refill the contract's balance and keep it on chain.*

When you click Instantiate, and in the next confirmation screen Sign & Submit, you should see the extrinsic contracts.instantiateWithCode is called, and a flurry of events appear including the creation of a new account (system.NewAccount) and the instantiation of the contract (contracts.Instantiated):

Document image


Calling your Contract

Now that your contract has been fully deployed, we can start interacting with it! Flipper only has two functions, flip() and get() so we will show you what it's like to play with both of them. Click the Execute button under the contract after you instantiate the Flipper contract in the previous step.

1. get() function

Take a look at our contract's default() function, we set the initial value of the Flipper contract value to No when we instantiated the contract. Let's check that this is the case.

In the Message to Send section, select the "get(): bool" message and accept the default values for the other options.

Press "Call" and confirm that it returns the value false:

Document image


Note

You might be wondering "Why did we need to specify gas when reading a value from a contract?" If you notice right above the "Call" button is a dropdown select box that allows you to "Send as RPC call" or "Send as transaction". For a read-only request like this, we can simply use an RPC call which will simulate a transaction, but not actually storing anything on-chain. Thus, you will still need to specify the right amount of gas to cover your "virtual fee". But don't worry, nothing will be charged when making a call this way. 🙂

2. flip() function

So let's make the value turn true now!

The alternative message to send with the UI is flip(). Again, accept the default values for the other options.

You will notice that the flip() message defaults to a transaction call.

Document image


If the transaction was successful, we should then be able to go back to the get() function and see our updated storage:

Document image


Woohoo! You deployed your first smart contract!

3. Moving Forward

We will not go over these setup and deployment steps again, but you can use them throughout the tutorial to deploy certain contract on-chain.

The rest of the tutorial will have template code which you will use to walk through the different steps of contract development. Each template comes with a fully designed suite of tests that should pass if you programmed your contract correctly. Before you move on from a section, make sure that you run:

Text
|


and that the tests all execute successfully without any warnings.

You need not deploy your contract between each section, but if we ask you to deploy your contract, you will need to follow these same steps you have done with the Flipper contract.

Troubleshooting

Here are solutions to some of the common problems you may come across:

1. Unexpected Epoch Change

There is a known issue with the Substrate block production (BABE) on a running chain. If you stop your node for too long (closing the terminal, putting your computer to sleep, etc...), you will get the following error:

Text
|


To solve this you will need to restart your node with: canvas --dev --tmp. At that point, you will need to re-deploy any contracts and re-do any steps that you may have done before on your node. As long as you keep your node running, you should face no issues.

2. Old Contracts in Local Storage

Canvas UI uses its own local storage to track the contracts that you have deployed. This means that if you deploy a contract using the UI, and then purge your Canvas node, you will be prompted to reset your local storage and please do so. And then re-deploy any contracts and re-do any steps that you may have done before on your node.

3. Send as Transaction vs Send as RPC

When interacting with contracts using the Canvas UI, you have the option to submit your call as a transaction or as a RPC:

Document image


When you send as a transaction, it should be exactly as you expect. A transaction is submitted to the contract, a fee is deducted from your account, and the state of your blockchain can change. In these situations, no value is returned from your contract call, only a "Success" or "Failed" extrinsic message along with any events it may emit.

However, there may be some calls that you want to "test", rather than actually submit a transaction, or you may want to peek at the value that would be returned if you called the contract function. For these scenarios, you submit an RPC call, which will run all of your contract logic, but not actually submit a transaction or update the chain state. However, you will still need to specify the right amount of gas to cover your "virtual fee". But don't worry, nothing will be charged when making a call this way. :)

Did this page help you?
Yes
No
UP NEXT
Solidity Smart Contract
Docs powered by archbee 
TABLE OF CONTENTS
1. Running a Substrate Smart Contracts Node OR Peaq Network Node
Or Peaq Network Node:
2. Canvas UI or Polka.js front end
3.Build Smart Contract Code
1. Upload Contract Code to Canvas UI or Polka.js peaq front end
2. Instantiate a Contract on the Blockchain
Calling your Contract
1. get() function
2. flip() function
3. Moving Forward
Troubleshooting
1. Unexpected Epoch Change
2. Old Contracts in Local Storage
3. Send as Transaction vs Send as RPC