• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Codemotion Magazine

We code the future. Together

  • Discover
    • Events
    • Community
    • Partners
    • Become a partner
    • Hackathons
  • Magazine
    • Backend
    • Frontend
    • AI/ML
    • DevOps
    • Dev Life
    • Soft Skills
    • Infographics
  • Talent
    • Discover Talent
    • Jobs
    • Manifesto
  • Companies
  • For Business
    • EN
    • IT
    • ES
  • Sign in
ads

Giacomo BarbieriMarch 18, 2020

Creating the interface of a smart contract

Blockchain
Blockchain
facebooktwitterlinkedinreddit
Table Of Contents
  1. Introduction
  2. Smart contract: building a simple interface

Introduction

In previous articles in this series, we learned how a basic contract is made, and discovered that Ethereum Studio is a simple and easy way to develop and deploy on a testnet. We demonstrated how to build a simple web interface using the Web3.js Javascript framework to handle user interaction via web browser. With that in mind, in this chapter we will go through an example of a web page, and in the following (and final) chapter, we’ll see how web3 is actually implemented in order to connect the smart contract to a web page.

This article is part four of a series on how to create an ERC-20 token. If you want to begin from scratch, please read part one, part two and part three of this series first (here’s part five).

Recommended article
blockchain, avascan, avalanche, defikingdom, subnet
May 17, 2022

How to Deploy a Subnet on Avalanche Blockchain: The Case of DeFi Kingdom

Codemotion

Codemotion

Blockchain

Smart contract: building a simple interface

Web3.js is the Javascript framework that allows users to interact with the Blockchain. To implement a smart contract with an interactive interface, we must build a simple web page.

We begin by creating an HTML file:

<!DOCTYPE html>
<html lang="en">
// here lies the smart contract interface
</html>Code language: HTML, XML (xml)

Then, we need to configure the metadata in the header. We load jQuery and Web3.js packages:

<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
    <script type="text/javascript" src="https://unpkg.com/web3@0.20.5/dist/web3.min.js"></script>
</head>Code language: HTML, XML (xml)

Then we can actually design the interface. In this example, we are going to create a user interface that has two sections: the first will let users create an address with an attached amount of token, whilst the second will check the balance of an address.

Here is the first section:

<body>
    <div id="create" class="tabcontent">
        <h2>Create new coins</h2>
        <div class="input-group">
            <h3>Address</h3>
            <input type="text" id="create-address" placeholder="Enter address" />
        </div>
        <div class="input-group">
            <h3>Amount</h3>
            <input type="number" id="create-amount" placeholder="Enter amount..." />
        </div>
        <div class="input-group">
            <button class="btn" id="button-create">Send</button>
        </div>
    </div>Code language: HTML, XML (xml)

And here is the second section:

    <div id="balances" class="tabcontent">
        <h2>Check balance of wallet</h2>
        <div class="input-group">
            <h3>Address</h3>
            <input type="text" id="balance-address" placeholder="Enter address..." />
        </div>
        <div class="input-group">
            <button class="btn" id="button-check">Check balance</button>
        </div>
    </div>

    <h1 class="text message">Balance: <span id="message"></span></h1>
</body>Code language: HTML, XML (xml)

It’s important to note that there’s not even a single script within the HTML code. That’s because the contract connects with the page through a .js file that builds objects from the smart contract.

In the <div> elements, one can see the id of every element: these will later be called by the .js core to match the attributes in the smart contract.

Here’s the complete HTML page:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
    <script type="text/javascript" src="https://unpkg.com/web3@0.20.5/dist/web3.min.js"></script>
</head>

<body>

    <div id="create" class="tabcontent">
        <h2>Create new coins</h2>
        <div class="input-group">
            <h3>Address</h3>
            <input type="text" id="create-address" placeholder="Enter address..." />
        </div>
        <div class="input-group">
            <h3>Amount</h3>
            <input type="number" id="create-amount" placeholder="Enter amount..." />
        </div>
        <div class="input-group">
            <button class="btn" id="button-create">Send</button>
        </div>
    </div>

    <div id="balances" class="tabcontent">
        <h2>Check balance of wallet</h2>
        <div class="input-group">
            <h3>Address</h3>
            <input type="text" id="balance-address" placeholder="Enter address..." />
        </div>
        <div class="input-group">
            <button class="btn" id="button-check">Check balance</button>
        </div>
    </div>

    <h1 class="text message">Balance: <span id="message"></span></h1>
</body>

</html>Code language: HTML, XML (xml)

So, to recap:

  • The logic of the token is described in the .sol file, in Solidity;
  • The token logic is built (initialized) in a .js file, that ‘prepares’ the smart contract to be interactive in a HTML page;
  • The HTML page visualizes the objects rendered with the .js file.

Of course, the HTML page can and should be built with a CSS file as well (which can be put in the same folder as the HTML page). Coding the stylesheet of the interface is beyond the scope of this series, so we won’t show it here, but the page can be customized as much as one wants.

So, we have the token logic and we have designed the HTML page. We must now code the .js core file that acts as a bridge between the two. That’s the focus of the final chapter in this series.

Related Posts

What is Web3 and What New Capabilities Does it Bring?

Codemotion
April 19, 2022

Guide on Blockchain Software Development

Evangeline Reece
November 5, 2021
blockchain developer

Blockchain Developers’ Dream Job: What Are the Main Areas of Development?

Lorenzo Ermigiotti
December 7, 2020
cryptocurrency

Exploring Primecoin: An Alternative Cryptocurrency Project

Imran Bashir
November 16, 2020
Share on:facebooktwitterlinkedinreddit

Tagged as:Ethereum HTML Smart Contracts

Giacomo Barbieri
Blogger with over 6 years of experience in blogs and newspapers, passionate about AI, 5G and blockchain. Never-ending learner of new technologies and approaches, I believe in the decentralized government and in the Internet of Money.
Career advice for developers
Previous Post
Design Tokens and Cross Platform Coherence – Part 3
Next Post

Footer

Discover

  • Events
  • Community
  • Partners
  • Become a partner
  • Hackathons

Magazine

  • Tech articles

Talent

  • Discover talent
  • Jobs

Companies

  • Discover companies

For Business

  • Codemotion for companies

About

  • About us
  • Become a contributor
  • Work with us
  • Contact us

Follow Us

© Copyright Codemotion srl Via Marsala, 29/H, 00185 Roma P.IVA 12392791005 | Privacy policy | Terms and conditions