Examples

After deploying your smart contract on MetaChain, the next step is to create a user-friendly interface for users to interact with it. This guide provides instructions and example code to help developers set up a web portal for their smart contract.

Prerequisites

  • A deployed smart contract on MetaChain.

  • Basic knowledge of web development (HTML, CSS, JavaScript).

  • Familiarity with web3.js or ethers.js libraries.

Step-by-Step Guide

1. Setting Up the Development Environment

  • Create a new directory for your web portal project.

    mkdir my-dapp-portal
    cd my-dapp-portal
  • Initialize a new npm project.

    npm init -y
  • Install necessary libraries.

    npm install web3 bootstrap

2. HTML Structure

Create an index.html file with the following basic structure:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My dApp Portal</title>
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <!-- Your UI components go here -->
    </div>
    <script src="node_modules/web3/dist/web3.min.js"></script>
    <script src="app.js"></script>
</body>
</html>

3. Connecting to MetaChain

In a new file named app.js, initialize web3 and connect to MetaChain:

const web3 = new Web3(Web3.givenProvider || "YOUR_METACHAIN_NODE_URL");
const contractAddress = "YOUR_SMART_CONTRACT_ADDRESS";
const contractABI = []; // Your contract's ABI

const contractInstance = new web3.eth.Contract(contractABI, contractAddress);

4. Creating UI Components

Add UI components in index.html to interact with your smart contract. For example, if your contract has a function to set and get a value:

<input type="text" id="inputValue" placeholder="Enter a value">
<button onclick="setValue()">Set Value</button>
<button onclick="getValue()">Get Value</button>
<p>Current Value: <span id="currentValue"></span></p>

5. Interacting with the Smart Contract

In app.js, add functions to interact with your smart contract:

async function setValue() {
    const value = document.getElementById('inputValue').value;
    const accounts = await web3.eth.getAccounts();
    contractInstance.methods.setValue(value).send({ from: accounts[0] });
}

async function getValue() {
    const value = await contractInstance.methods.getValue().call();
    document.getElementById('currentValue').innerText = value;
}

6. Styling and Enhancements

  • Use CSS frameworks like Bootstrap to style your web portal.

  • Add error handling and user feedback mechanisms, such as loading spinners or success/error messages.

Creating a dapp web portal for your smart contract enhances user experience and accessibility. By following this guide, developers can set up a basic interface for users to interact with their smart contracts on MetaChain seamlessly.

Tip: While this guide provides foundational examples, tailoring them to your specific needs is encouraged. For additional logic or clarifications, don't hesitate to engage with our team or the broader community. We're here to help!

Last updated