Search
⌃K

How To deploy your first smart contract on the XDC Network using Remix

Use Remix to deploy a Smart Contract.

🧭 Table of contents

📰 Overview

​Remix IDE is a blockchain development environment, which you can use to create and test smart contracts by levering an Ethereum Virtual Machine.

What you will learn

This guide aims to teach how to create a smart contract using Remix IDE and deploying it on XDC Network.

What you will do

  • Setup Remix IDE
  • Create an XDCPay Wallet and use faucet to test XDC tokens
  • Deploy contract on the XDC Network
  • Check the deployment status on xinfin.network.

🚀 Setting up the development environment

Remix is an online solidity IDE for compiling and deploying solidity code to EVM-compatible blockchains. To begin working on a new smart contract, we must first create a new file in the contracts folder on the left side of the view pane.

⚒️ Creating XDCPay Wallet for signing transactions

In order to get started deploying new contracts on XDC Mainnet and/or Apothem, we need to have an XDCPay wallet to sign our transactions and store XDC tokens.
  • First we have to install the chrome extension of XDCPay.
  • Open the Chrome extension after it has been successfully installed.
  • Agree to the Terms of Service.
  • Create a new XDCPay wallet by setting up a strong password or use an existing seed phrase 12 or 24-Word Mnemonic Phrase to recover your existing wallet here.
  • Keep the seed phrase safe. 🚨 Do not share the seed phrase with anyone or you can risk losing your assets and/or the ownership of your smart contracts! 🚨
  • Verify recovery phrase
  • Your XDCPay wallet has been successfully created.

⚒ Adding Testnet XDC to Development Wallet

Initially, your account would be empty, and it would require some XDC tokens to initiate blockchain transactions. You would use a faucet to fill your wallet with test XDC tokens for this. These tokens are worthless in and of themselves. They are simply used to test your contracts on the testnet in order to avoid losing your real money.
  • First, make a copy of your wallet address. Your wallet address would look like xdc057ac7de8ad6f21c7bb0dcc6a389ff9161b3b943. These account address are interchangeable with Ethereum network. We can access these accounts on Ethereum network by simply changing the initial xdc with 0x.
  • After that, navigate to the XDC faucet.
  • Enter your XDC account address and request for Test XDC here.
  • If your request is approved, you will be credited with the XDC in your wallet.
  • If you can't see the XDC in your wallet, make sure you're connected to the XDC Apothem Testnet or the XDC Mainnet.
  • If you are currently connected to the XDC Mainnet, switch to the XDC Apothem Testnet.

🍕 Writing our first Smart Contract

Lets create a simple Pizza.sol contract on Solidity, the Pizza contract should have:
  • a constructor where the deployer can define the pizza size,
  • a eatSlice method to consume slices available,
  • a bakeNewPizza method to refill all slices only if the previous pizza have been entirely eaten! 😋
Lets start by creating the Pizza.sol file:
Click on the New File button on the center navigation or in the left navigation pane within the contracts folder.
And write the following code to Pizza.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
​
contract Pizza {
uint256 public PIZZA_SIZE;
uint256 public slices;
​
constructor(uint256 _pizzaSize) {
PIZZA_SIZE = _pizzaSize;
slices = 0;
slices += PIZZA_SIZE;
}
​
function eatSlice() public {
require(slices > 0, "No Slices Left. Lets Bake a new Pizza!");
slices -= 1;
}
​
function bakeNewPizza() public {
require(slices == 0, "There still slices from a previous Pizza!");
slices += PIZZA_SIZE;
}
}
The first line of code // SPDX-License-Identifier: MIT specifies the License Permissions. This line is required for error-free compilation.
The line pragma solidity ^0.8.16; specifies the version used to write the solidity code. As we all know, Solidity is a rapidly evolving language, and there may be changes that will not work with a newer version of the Solidity compiler. As a result, we include this line to compile the code with a specific version. Pragmas are common instructions for compilers about how to treat the source code (e.g., pragma once).
The line contract Pizza {} is used in our smart contract to create a Solidity contract. Contracts in Solidity are similar to classes in object-oriented languages. A Solidity contract is a collection of code (its functions) and data (its state) that is stored at a specific address on the Ethereum blockchain. The line uint256 public PIZZA_SIZE; uint256 public slices; declares state variables of type uint256 called PIZZA_SIZE and slices. Both of these functions have a public property that allows you to access the current value of the state variable from outside of the contract.
We set initial value for PIZZA_SIZE in the constructor function and then place that value within the slices count.
Then we create a function eatSlice that decrements the value in the slices variable. We also have a function called bakeNewPizza where we increase the count of slices of the value of PIZZA_SIZE. Both these functions are of public type.

🍕 Compiling

Lets try compiling the Pizza.sol contract:
  • Open the Solidity Compiler in the left side navigation pane
    .
  • From the compiler option, select the compiler version v0.8.16.
  • Choose Language as Solidity.
  • Set the EVM version as the compiler default.
  • Next, select Compile Pizza.sol.
  • After successful compilation, it will show
    alt
    ​
  • Once our contract has been compiled, we can deploy it to the Apothem Test Network.

🍕 Deploying

To deploy on the XDC Apothem Testnet, you need to have enough funds to pay for gas fees on the address that is being used for development.
  • Navigate to Deploy and Transactions
    alt
    .
  • Choose Injected Web3 as the Environment.
  • Confirm the pop-up to add the account to Remix IDE now.
  • Next, choose the account to which you want to deploy the contract.
  • Choose the contract you want to use.
  • Add the number of pizza slices that you wish to create the pizza and deploy the contract.
  • A popup will appear, which you must confirm in order to create the transaction for contract deployment.

🔍 Veryfing Contracts on the Block Explorer

Once you have successfully deployed your smart contract to the blockchain, it might be interesting to verify your contract on XinFin Block Explorer.
First lets check the address our contract is deployed. Go to your wallet and get the most recent transaction details, then copy the transaction address.
Next, navigate to the XDC Block explorer and paste the transaction hash there.
From there, we need to get the transaction details as well as the To Address where the contract is deployed.
Here we have a Pizza contract deployed on XDC Apothem Testnet, we can search for our newly deployed contract on XinFin Block Explorer:
Verify 01
And click in the Verify And Publish Option.
We will be redirected to the Contract verification page where we need to fill out:
  • Contract Name: Pizza
  • Compiler: Check your Solidity file for Compiler Version
  • Contract Code: Just paste everything from your Pizza.sol file
Once everything is filled out, press Submit!
Verify 02
If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract:
Verify 03
In this page you can Read from, Write to, or simply read the information tied to your Smart Contract on the blockchain:
Verify 03

🍕 Interacting with your contract using Remix IDE

We can easily test our contract with the help of that contract address. To start interacting with you smart contracts you can start running,
  • Return to the Remix IDE.
  • Navigate to the deploy section, paste the contract address, and then click the At Address Button.
Verify 03
  • You will then be shown all of the different functions and variables that were available for us to see and use.
  • When you click that button after running our eatSlice() method, a transaction is started and its value is stored on the network. It should log a transaction confirmation (Or rejection) object
Verify 03
  • This transaction is immediately reflected in the corresponding block explorer, as seen here!​
​
For more information about Remix IDE, Please Visit Remix IDE Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook.\