5. Create smart contract

A step-by-step guide showing how to add a smart contract to the Porta blockchain

Requirement: You have completed the installation checklist found here: https://docs.porta.network/developer-guide/smart-contracts/installation-checklist

Guide

We start by creating a new flipper smart contract but please be advised, this is only for example purposes. We assume at this point you will have built your own smart contract and be ready to upload that to the Porta blockchain.

Create a new smart contract

cargo contract new flipper

The above command created a new project folder named flipper. Enter that folder now:

cd flipper

Test the contract to confirm everything is working as expected

cargo +nightly test

Build the contract

cargo +nightly contract build

If you receive an error about wasm-opt version, please refer to the link below to solve it.

If you did not receive an error, or you did but fixed it using the above link, you are now able to proceed.

The command (cargo +nightly contract build) will build for the ink! Project:

  • a Wasm binary

  • a metadata file (which contains the contract's ABI)

  • a .contract file which bundles both.

If all goes well, you should see a target folder which contains these files:

The .contract file is used to deploy your contract to the Porta blockchain.

What is an ABI?

You may be familiar with contract ABI from Ethereum based contracts, that provide front-end Dapps the means to communicate to the contract on-chain. They essentially describe the structure of the contract including its functions and variables within a JSON object, making it particularly simple for JavaScript based apps to integrate.

Connect to Porta blockchain through PolkadotJS Apps

https://polkadot.js.org/apps/#/contracts

Ensure you are on Developer > Contracts

Click: Upload & deploy code

Click and drag the flipper.contract file onto the input field

The screen will now show this

Choose:

  • The constructor to initialise the contract and any attributable settings

  • The amount of funds to give to the token (known as the endowment). Ensure the endowment amount is over 100 tokens (because 100 tokens is the minimum number of token allowed for an account to exist on the Porta blockchain).

  • The maximum gas fee the contract should use.

Congratulations. Your smart contract is deployed on the blockchain and you are now part of a growing community.

You can now move onto step 6: Interact with your smart contract on the Porta blockchain.