Flattening Smart Contracts with Remix

Use Remix IDE to deploy and flatten an Openzeppelin 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

In this tutorial, you will learn how to set up Remix IDE and use it to build, test and deploy an OpenZeppelin smart contract on both the XDC Network mainnet and XDC Apothem testnet.

What you will do

  • Setup Remix IDE

  • Create a complex smart contract with dependencies (like OpenZeppelin)

  • Compile the smart contract

  • Test the smart contract

  • Deploy the smart contract

  • Flatten the smart contract

  • Verify the smart contract

  • Check the deployment status on xinfin.network

📰 About Openzeppelin

OpenZeppelin provides security products to build, automate, and operate decentralized applications. It is a library for secure smart contract development. Build on a solid foundation of community-vetted code. It provides implementation for a number of standards like ERC20 and ERC721.

🚀 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 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, our account would be empty, and we would require some XDC tokens to initiate blockchain transactions. We would use a faucet to fill our 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 OpenZeppelin Contract

The source code for the OpenZeppelin contract used in this tutorial is available within the tutorial. Here we will be creating an XRC721 token using the OpenZeppelin standards.

Lets start by creating the XRC721.sol file.

And write the shell of our smart contract by writing:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract XRC721 {
    
}

💵 OpenZeppelin

Inside our contract, we would be importing the scripts from OpenZeppelin Github repository. These form the foundation for our contract which is having all the code of different functions which needs to be implemented in our contract. We are also importing the Counters from OpenZeppelin Github repository which is used to keep account of the counter of the current tokenId.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";

contract XRC721 is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    
}

💵 Methods

We need to create the constructor that is a function called only once when the contract is deployed, where we can parse as arguments information such as the token name and symbol. We would also create another function createToken which will take an address and mint our created XRC721 NFT Token to that address:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";
// import "@openzeppelin/contracts/drafts/Counters.sol";

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";

contract XRC721 is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor(string memory name, string memory symbol) ERC721(name, symbol) {
    }

    function createToken(address tokenOwner) public returns (uint256) {
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _mint(tokenOwner, newItemId);
        return newItemId;
    }
}

And here we have implemented everything we needed to make our token compliant with the XRC721 Standard. Of course there are more features we can implement to this contract, such as the SafeMath library that replace naive mathematical operations for methods that will avoid underflows and overflows, and supply management methods such as mint and burn.

💵 Compiling and Deploying

Lets try compiling the XRC721.sol contract:

  • 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 XRC721.sol.

  • Once our contract has been compiled, we can deploy it to the Apothem Test Network.

For deployment on the XDC Apothem Testnet. In either case, you need to have enough funds to pay for gas fees on the address that is being used for development.

  • Choose Injected Web3 as the Environment.

  • Confirm the popup 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 Initial values regarding your token.

  • Press the "transact" button and a popup will appear, which we must confirm in order to create the transaction for contract deployment.

💵 Flattening our smart contract

After that we need to flatten out our smart contract so that it can be easily verified on our Block Explorer.

  • For that we would right click on our smart contract file and here we would see an option to flatten our smart contracts on the bottom of menu pane.

  • Clicking on the flatten button will open a dialog box which we need to accept and a new icon for flatter is added to our left navigation pane.

  • After that we move to the flattener tab and will see the option to select our smart contract which we need to flatten. Here we will select our XRC721.sol smart contract and it will give an option to save the flattened smart contract.

  • After flattening, we will be having the flattened file XRC721_flat.sol.

This flattened code would be used further when we are verifying our token.

🔍 Verifying Contracts on the Block Explorer

Once you have successfully deployed your smart contract to the blockchain, it might be interesting to verify you 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. Note that if you deployed your token to Apothem testnet then you will need to navigate to the Apothem Explorer, and if you deployed your token to XDC mainnet then you will need to navigate to the mainnet explorer.

From there, we need to get the transaction details as well as the To Address where the contract is deployed.

Here we have a XRC721 contract deployed on XDC Mainnet at the 0x53bA8Cb12EaF09E6B0b671F39ac4798A6DA7d660. This address is in the Ethereum standard but we can simply swap the 0x prefix for xdc and search for our newly deployed contract on XinFin Block Explorer:

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: XRC721

  • Compiler: Check your Remix IDE for Compiler Version

  • Contract Code: Just paste everything from your flattened contract file.

Once everything is filled out, press Submit!

If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract:

🔍 Interacting with your contract on the Block Explorer

With your XDCPay wallet, it is possible to interact with verified Smart Contracts on the XinFin Network Block Explorer. You can read from, write to, or simply read the information tied to your Smart Contract on the blockchain.

For that, we would first copy the address where our contract is deployed on the network.

After that we would need to go back to the Remix IDE and paste that deployed contract address in the At Address button text box and click on that button.

This would fetch the contract and all the functions related to our deployed contract and we can easily play around with those functions.

Now here we would be minting a new NFT token to our wallet.

Here we would be passing on our wallet address and then we click on the createToken button.

After clicking in createToken, we need to confirm the transaction on the XDCPay wallet:

We can check for the minted token by going to our XDCPay Wallet and clicking on the Tokens tab and click on the Add Token button.

Then we have to add the deployed contract address on the Token Address text field. This will automatically fetch the token symbol. Then click on the Add Token button.

Add our newly minted token is available in our wallet.

And we can check our successful transaction on the Block Explorer!


For more information about Remix IDE, Please Visit Remix IDE Documentation. For more information about XDC Network, Please Visit XDC Network Documentation on GitBook.

Last updated