Table of Content

Using Chain IDE for Compiling and Deploying the Smart Contracts

Lesson Objectives

By the end of this lesson, you will be able to:

  • Write smart contracts using the Solidity Programming lanuage.
  • Use Smart Contract Templates by Chain IDE for development.
  • Use Chain IDE for compiling and deploying smart contracts on BNB Chain.
  • Interacting with deployed smart contract using Chain IDE.

Overview

In the previous module, we outlined how to write a smart contract using the Solidity programming language for use on the BNB Smart Chain network. Remember that, BNB Smart Chain is the component of the BNB Chain ecosystem that is equipped with the smart contract programmability and hence any smart contract or dapps are in essence deployed on the BNB Smart Chain. In this lesson, we will outline the steps to compile and deploy a simple storage smart contract generated by templates from Chain IDE on the BNB Smart Chain Testnet.

Demo

In this hands-on guide, readers are encourged to perform these tasks along for a better understanding. Before starting off with the practical demo, it is necessary to make sure you have the following software requisites installed. Chain IDE is an online web IDE and hence does not require any installation.

Software Prerequisites

What is Chain IDE?

ChainIDE is a chain agnostic, cloud-based IDE for creating decentralized applications. It enhances the development cycle through pre-configured plugins that save users' time and effort. This is a beginner guide on creating a simple smart contract and deploying it to the BNB Smart Chain. If you have any questions, feel free to ask them in the ChainIDE Discord.

Overview of the Demo

For this demo lesson, we will first write a simple storage smart contract that stores a number value. We will also list steps on how to compile and deploy this smart contract on the BNB Smart Chain using the ChainIDE. Following are general steps for compiling and deploying a simple smart contract on BNB Smart Chain include:

  1. Setting up a Wallet
  2. Write down a Smart Contract
  3. Compile the Smart Contract
  4. Deploy the Smart Contract
  5. Create a Flattened File using Flattener Library
  6. Verify the Smart Contract
  7. Contract Interaction

Setting up a Wallet

Install Web3 Wallet

When deploying a smart contract to a blockchain or when making a transaction to a deployed smart contract, a gas fee must be paid, and for that, we need to use a crypto/Web3 wallet like Binance Wallet or MetaMask. You can download the Binance Wallet from here. If you want to continue with MetaMask Wallet, click here.

Adding BNB Smart Chain Test Network to MetaMask Wallet

Visit ChainIDE, create a project, and click on the "Connect Wallet button" in the upper right corner, select the "Injected Web3 Provider" button, and then click the "MetaMask" to connect to the MetaMask wallet ("BNB Chain Mainnet" is the main network, and "BNB Chain Testnet" is the test network, click on the "BNB Chain Testnet" and it will be added to your MetaMask wallet. connect-metamask-to-chainide

Enabling the BNB Smart Chain Test Network to Binance Wallet

If you want to continue with Binance Wallet, install Binance Wallet. After installing Binance Wallet, you need to enable "Show Test Networks" and switch to the "BNB Smart Chain Test Network".

<img src="https://d3gvnlbntpm4ho.cloudfront.net/Using+ChainIDE+BNB+Smart+Chain/16.png" alt="img" style={{zoom:"50%"}}/>

Obtaining Test BNB tokens

Once BNB Smart Chain Test Network has been added to MetaMask, navigate to the BNB Smart Chain Faucet to receive test tokens. Tokens are needed to pay for gas fees to deploy and interact with the smart contract. On the faucet page, paste your MetaMask wallet address. Then, click submit and the faucet will send you some test BNBs. obtain-test-bnb-tokens

Writing Storage Smart Contract

You need to write down all the required functions that you want to implement in your storage smart contract. A general storage smart contract has the following functions:

  • store(): store value in variables
  • retrieve(): returns the stored value

The ChainIDE team has prepared a simple storage smart contract that includes all the required functions; you may use this built-in template and add/delete functions according to your requirements.

Visit the ChainIDE site and click on "Try Now".

try-chainide

Then, click on "New Project" and select "BNB Chain", and "Storage".

new-project-chainide

Now, you can see the template contract, Storage.sol, that includes all the required functions.

Compile a Storage Smart Contract

After you have completed your smart contract, it is time to compile it. To compile, navigate to the "Compile", module, choose an appropriate compiler version according to your source code, and press the "Compile" button. An ABI and bytecode for the source code generate upon successful compilation. If there are some errors in your source code, they will be displayed under the output panel in the "Logger module". You may need to carefully read the error, resolve it accordingly and compile the contract again.

Note down the compiler version and the license for your source code as it would be needed when you verify your smart contract on the BNB Smart Chain Test Network.

compile-smart-contract

Deploy a Storage Smart Contract

After successful compilation, it's time to deploy your compiled storage smart contract to the BNB Smart Chain Test Network. For that, you need to have a MetaMask installed, the BNB Smart Chain Test Network added to your wallet, and some testnet tokens to pay for the transaction fee.

Navigate to the "Deploy & Interaction" module and choose the smart contract that you want to deploy among the compiled smart contracts and click the "deploy" button. For this tutorial, the Storage smart contract will be deployed.

deploy-smart-contract

Create a Flattened File using Flattener Library

To verify a smart contract that imports other smart contracts, we need to create a flattened file, a flattened file including all the source code of imported contracts in a single file. To create a flattened file, you need to add a "Flattener" plug-in.

flattener-plugin

Once the Flatterner plug-in is activated, you'll be able to access it as a separate module as shown in the figure below. Choose the compiled file, and click on the flatten button to create a flattened file, once the flattened file is created, it will be automatically copied to the clipboard, you may paste it to a file and save it for later usage.

flatten-smart-contract

If you want to save the flattened file, click the save button, and a flattened file will be saved in the current repository.

save-flattened-file

The saved flattened file can be accessed under the explorer module.

view-in-explorer

Verify a Smart Contract

To verify a smart contract, you need to visit BNB Smart Chain Explorer and search for the deployed smart contract using the contract address.

flatten-smart-contract

Click on the "verify and publish" link shown under the contract section.

verify-publish-mart-contract

Once you click on the verify and publish link, you will be asked for the following:

  • Contract Address: The address of a deployed smart contract that you want to verify
  • Compiler Type: Either you want to verify a single file or multiple files
  • Compiler Version: The compiler version that you used to compile the smart contract
  • License: Open-source license type that you used for your source code

bsc-scan-verify-publish

After that, you need to paste the flattened file that you created in step 5, and your smart contract will be verified.

contract-source-code-details

If there are no issues with your smart contract, it would be verified, and you'll be able to see an image similar to the one that is shown below.

solidity-source-code

contract-details Congratulations, you have successfully deployed your smart contract to the blockchain and verified it, now it's time to interact with your deployed smart contract.

Contract Interaction

After successful deployment and verification. All the functions in the deployed smart contract can be seen in the "INTERACT" panel. In our scenario, we have two functions, Store() that is used to store the value to the blockchain, and Retrieve() to retrieve stored data from the blockchain.

interact-with-smart-contract

Table of Content