Deploy Smart Contracts

Last updated 24 days ago

Prerequisites

XDC01-docker-nodes already setup - if not refer to Setting up XDC Docker Nodes.

Tools: Docker, Docker Compose.

Wallet: Metamask (extension can be installed from the Metamask website).

Editor: Remix online IDE.

Unlocking the coinbase account

To deploy any contract on the private blockchain we need Ether. There is a pre-funded Coinbase account provided by XinFin for deploying smart contracts at a zero gas price.We can unlock the account by accessing the Geth JavaScript console provided for each node using the command

sudo docker exec -it PROJECT_NAME_STATIC_NODES_node_1_1 geth attach /qdata/dd/geth.ipc

This launches the console in the terminal were we can find the public key of the pre-funded account by using the command

personal.listAccounts

Then, we can unlock the account using the command

personal.unlockAccount(web3.eth.coinbase,””,15000)

Linking the chain with Metamask

Before importing our account, we should connect to our private network via Metamask. This can be done by networks drop down list box on the top and selecting the option “Custom RPC network”. Here we enter the IP address which was used while setting up the docker nodes and the RPC port number of the elected/leader node (can be obtained through the geth logs). The entered URL should be of the format

http://<IP_ADDRESS>:<RPC_PORT_NUMBER_OF_ELECTED_NODE>/

We have the private key encrypted in a keystore file for each node, we need a decrypting client. For decryption, we can follow the below procedure.

  1. Select the Keystore/JSON File option on the left side panel.

  2. Click SELECT WALLET FILE option and then select the keystore file in the following path inside the docker.

    XDC01-Docker-Nodes/static-nodes/qdata_/dd/keystore

  3. Once the file is uploaded, there will be a response - Your wallet is encrypted. Good! Please enter the password. Use an empty password in here and then click the unlock button.

  4. Wait for the redirected page, where we finally obtain the private key of the account.

Once we obtain the private key we can import the account using Import account option in Metamask and then entering the private key.

Deploying smart contracts

Open the Remix IDE in the same browser where Metamask plugin is installed.

Here, a new solidity contract can be created by clicking on the New contract icon on the top left corner of the IDE. Then we should enter the name of the contract followed by the extension .sol (for example HelloWorld.sol). This will create a new contract stored in the IDE cloud with the name you have specified. Then write the contract code in the main window.

Remix editor window

The contract code can be compiled by selecting the Compile tab on the right side panel and then clicking on the Start to compile option. This compiles and displays the errors in the code under the Compile tab in the right side panel.

Compile tab

To deploy the contract, move to the Run tab in the right side panel. Here, select the environment (the network where the contract is to be deployed) as Injected Web3 (Custom 2018). Once the environment is successfully connected, the single account imported in Metamask is auto-selected in the Account list, if it isn’t selected, select the account from the dropdown list. Set the Gas limit to any standard value and the Value to 0. Then select the smart contract that we had created from the empty dropdown list box and click Deploy.

Compile tab

Immediately, sends a prompt from Metamask to confirm the transaction for deploying the smart contract on the network. Here, change the Gas price to 0 (since the private network is designed to have contracts with 0 gas price) by selecting the Edit option and setting the gas price to 0.

Metamask confirmation tab

Then select confirm to confirm the gas payment for deploying the contract.

Executing the contract

The contract execution can be tested using remix. The functions of the deployed contract can be invoked using the specific buttons allocated for each function in the contract, where the outputs will be displayed below the buttons. Also, the logs can be accessed inside the command window inside the Remix IDE.

Compile tab

Account balances after deployment

The account balances can be checked using Metamask. Each transaction done after importing the account will be registered in the node's log.

References

Quorum network:

Raft consensus algorithm:

Constellation:

Smart contracts in Quorum: