Building Your First dApp on DeFiChain's MetaChain Layer: A Beginner's Guide

Jun 12, 2024

Creating a decentralized application (dApp) can seem a daunting task to those new to the blockchain space. 

However, it is surprisingly easier than many think. With the right guidance, anyone can start building their own projects on the blockchain. 

This guide will walk you through the process of developing an escrow application on DeFiChain's MetaChain (EVM) layer. 

We'll cover each step in detail, from setting up your development environment to deploying your dApp.

What is an Escrow Service in Blockchain?

An escrow service acts as a trusted third party that holds funds during a transaction between two parties, ensuring that both parties fulfill their obligations before releasing the funds. 

This is particularly useful for freelance work, where a client might pay for services rendered. It ensures that the service provider gets paid only after the work is verified.

Step 1: Setting Up Your Development Environment

To get started, we need to set up our development environment. This involves installing the necessary tools and libraries.

  1. Project Structure:
    • Frontend: This will be handled by the escrow-web folder and will include HTML, CSS, JavaScript, and React components.
    • Smart Contracts: Managed with Hardhat in the hardhat folder, containing Solidity contracts and scripts for deployment and testing.
  2. Smart Contracts:
    • Escrow Contract: Manages deposit, release, and refund of tokens.
    • Escrow Factory: Deploys new escrow contracts efficiently.
  3. Best Practices:
    • Write comprehensive tests for smart contracts to cover edge cases and better security.
    • Use OpenZeppelin Contract libraries for standard contracts to save time and ensure reliability.

Step 2: Setting Up the Project with Hardhat

Hardhat is a development environment that consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps.

Initialize the Project:

  • Open your terminal and run npx hardhat to set up the project structure. This command will create the necessary directories and configuration files for you.
  • Choose "Create a basic sample project" when prompted. This will set up a sample contract, test, and script to help you get started.
  1. Add OpenZeppelin Contracts:
    • Install OpenZeppelin libraries by running npm install @openzeppelin/contracts. These libraries provide reusable and secure Solidity code for common tasks like token transfers and ownership.
  2. Write the Smart Contract:
    • Imports: Import necessary OpenZeppelin contracts, such as ERC20, Ownable, and SafeERC20.
    • Functions: Define functions for deposit, release, and refund. Ensure each function includes necessary checks and emits relevant events.
    • Events: Create events to log transactions, such as Deposit, Release, and Refund.

Step 3: Testing the Contract

Hardhat comes with a testing framework that uses Mocha and Chai for writing tests.

  1. Write Unit Tests:
    • Create a test folder in your project directory and add test files for your contracts.
    • Write tests for each function in your contract, covering all possible scenarios and edge cases.
    • For complete test coverage, you can simply copy the test file from the project repo here.

       2. Run Tests:

  • Use Hardhat's testing tools to run the tests: npx hardhat test. This command will execute your tests and show the results in the terminal.

Step 4: Deploying the Contract

Once your contract is tested, it's time to deploy it to the blockchain.

  1. Compile the Contract:
    • Run npx hardhat compile to compile your contract. This step translates your Solidity code into bytecode and ABI (Application Binary Interface), which are necessary for deployment.
  2. Deploy with MetaMask:
    • Configure MetaMask to connect to the MetaChain testnet. You can do this by adding a new network in MetaMask settings and providing the testnet details.
    • Deploy the contract using a Hardhat deployment script. Create a script like scripts/deploy.js to handle the deployment logic.
  3. Verify Deployment:
    • Use the MetaChain explorer to verify the contract deployment. Search for the transaction hash to ensure the contract was deployed successfully.

Step 5: Building the Frontend

The frontend of your dApp is what users interact with. We'll use React to build a simple user interface.

  1. Set Up React Application:
    • Create a React app for the frontend interface using create-react-app: npx create-react-app escrow-web.
    • Structure the React app to include components for input fields, buttons, and display areas for contract interactions.
  2. Connect Wallet with RainbowKit:
    • Integrate RainbowKit to handle wallet connections. Install RainbowKit and dependencies by running npm install @rainbow-me/rainbowkit @rainbow-me/rainbowkit-wallets.
    • Implement wallet connect feature with MetaMask, allowing users to connect their wallet to interact with the dApp.
  3. User Interface:
    • Create a simple UI with inputs for client, resolver, and provider. Include forms to capture user input and buttons to trigger contract functions.
    • Implement functions to interact with the smart contract for deposit, release, and refund operations. Connect with the blockchain using web3.js or ethers.js.

Step 6: Optimization Tips

Gas optimization is essential when working with smart contracts, as it directly impacts transaction costs.

  1. Use Proxy Patterns:
    • Proxy patterns allow you to minimize gas costs by reusing contract logic across multiple instances.
  2. Optimize Contract Functions:
    • Reduce unnecessary computations and storage operations within your smart contracts to save gas costs.

Step 7: Deploying the dApp

After building and testing your dApp, it's time to deploy it for users to interact with.

  1. Deploy Frontend:
    • Host the React application using a static site service like Vercel or Netlify. Deploy the build output from npm run build.
    • Ensure the hosting service supports HTTPS and custom domain configurations.
  2. Test and Iterate:
    • Ensure all functionalities work seamlessly on the testnet. Test with different scenarios and user interactions.
    • Gather feedback from testers and make necessary adjustments to improve usability and performance.
  3. Move to Mainnet:
    • After thorough testing, deploy the contracts and frontend on the mainnet. Update configurations to point to mainnet addresses and endpoints.


By following the above steps and leveraging OpenZeppelin libraries, Hardhat, and React, you can efficiently build and deploy decentralized applications with enhanced security and user experience on DeFiChain’s metaChain Layer.


Decentralized finance enabled on Bitcoin. A blockchain dedicated to fast, intelligent and transparent financial services, accessible by everyone.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.