Deploying solidity contract in substrate parachain using Hyperledger Solang

Deploying solidity contract in substrate parachain using Hyperledger Solang

It can be challenging for developers to transition to a new language when working with different blockchains. Polkadot, for example, is considered a 3rd generation blockchain. Those experienced in developing with Solidity may find it difficult to switch to Rust, which is used in Substrate and Solana. Luckily, Hyperledger is working on an exciting project called Hyperledger Solang, which is a Solidity compiler written in Rust and helps generate contracts for Solana and Substrate from Solidity.

Pre-requisite:

  1. Hyperledger Solang (Installation Guide)

  2. Polkadot Wallet (Browser Extension)

Below is the solidity contract we are going to use for this demo, Score.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.0;    

contract Score {

    uint score = 5;

    function getScore() public view returns (uint) {
        return score;
    }

    function setScore(uint new_score) public  {
        score = new_score;
    }
}

To make sure we have Solang installed in our environment, let's run the command below. It should give us the Solang version v0.2.0.

solang --version

To compile your Solidity contract for the Substrate environment, simply run the following command,

solang compile --target substrate score.sol

You'll be able to generate two files, Score.contract and Score.wasm.

Create Polkadot accounts:

To get started, please follow this guide to create your accounts in the polkadot wallet,

https://support.polkadot.network/support/solutions/articles/65000098878-how-to-create-a-dot-account

Tesnet Tokens for deployment:

We are going to deploy our contracts in the Rococo Testnet, we need to have some ROC tokens for deploying the contract in the Rococo Testnet, follow the steps to get the ROC tokens,

  1. Go to Rococo faucet channel on matrix.

  2. To get free tokens, please copy your Account ID in the “Substrate” address format and head over to the Rococo faucet channel. Once there, please enter the command “!drip YOUR_WALLET_ADDRESS:1002” (make sure to include the “:1002” at the end of your address).

  3. A bot will respond shortly to confirm your balance. You can also check your balance by visiting the polkadot JS apps using this link.

Deploying Contracts on Testnet:

Once we have enough balance in the testnet, we'll be ready to deploy the contract!

  1. Go to contracts-UI page.

  2. Connect your polkadot wallet to that page and make sure 'Contracts (Rococo)' is selected.

  3. Upload your contract (.contract file) and click next

  4. You should see a demo run of the deployment and it should be successful and click next to do the actual deployment.

  5. You should see a final screen to upload and instantiate, click to proceed.

  6. Wallet will pop up to sign the transaction, give the password and proceed for signing the transaction.

  7. Finally, the contract deployed screen will be shown.