An Ethereum Contract in 30 Minutes

Culture of Innovation Jan 21, 2019

Many of us came to know about blockchain technology due to the boom of Bitcoin Price in the last few years. Initially, Bitcoin was all about cryptocurrency and slowly entrepreneurs started using it for other business problems and developing solutions around them.

In mid-2015 another blockchain “Ethereum” has been invented by Canadian-Russian Programmer Vitalik Buterin which provide a suitable platform to many new decentralized applications (dApps). Ethereum’s success is mainly attributed to its implementation of smart contracts.

What Are Smart Contracts in Ethereum?

“A smart contract is a set of promises, specified in the digital form, including protocols within which the parties perform on these promises.”  – Nick Szabo, 1996

In this article, we will discuss how to write, deploy and interact with smart contract using ethereum remix IDE (Integrated Development Environment) on the Ropsten test net.

Things to get started

  1. Remix: Solidity IDE – Develop Smart Contracts for the Ethereum
  2. Metamask – Bring Ethereum to your Browser
  3. Ropsten Test Network – Testnet for Ethereum Transactions

Step 1: Setup the Environment

We are going to use an online tool Remix – Solidity IDE in this tutorial. It’s an easy and free tool for creating and deploying smart contracts.

Open the tool and remove pre-loaded smart contracts name as ballot.sol and ballot_test.sol.

An Ethereum Contract In 30 Minutes

Login to Metamask and select Ropsten test network for deploying the contract. If you haven’t installed MetaMask yet, you can start here.

An Ethereum Contract In 30 Minutes

Step 2: Write a Smart Contract

After removing pre-loaded smart contracts from the browser, click on “+” icon and create new smart contract file Test.sol for writing the code.

An Ethereum Contract In 30 Minutes

Once the file is created, open it for writing the smart contract. In this tutorial, we will be designing a smart contract to create multiple ERC tokens for any real-life asset. The smart contract will allow a person to tokenize his asset and issue N number of tokens against the asset.

Main Components of the source code are pragma solidity ^0.4.24

It means the source code is written for Solidity Version 0.4.24 and above and doesn’t break functionality.

struct Token
{ uint256 tokenId;
address tokenOwner;
uint256 tokenAmount;
string tokenType;
string tokenMetadata;}

It represents the structure of the issued token by this smart contract.

  • Uint256 tokenId – the ID of issued token.
  • address tokenOwner – Wallet address of the owner who is issuing the tokens
  • uint256 tokenAmount – The number of tokens issued by the owner
  • tokenType – Each user can define his own token type
  • tokenMetadata – Contain the specific details of the asset.

function CreateToken(uint256 idForToken, address owner, uint256 amount, string tokenType,
string metadata) public payable returns (uint256 _id) { tokensMapById[idForToken] = Token(idForToken, owner, amount, tokenType, metadata);
if (tokensMapByOwner[owner].length == 0) {uniqueOwners.push(owner);} tokensMapByOwner[owner].push(tokensMapById[idForToken]); return idForToken;}

This function lets you tokenize your asset and issue N number of tokens and system automatically update issued tokens in owner’s wallet address.

function Balance(address _owner) public view returns (uint256 _balance) {
uint256 totalAmount = 0;
for(uint256 i = 0; i < tokensMapByOwner[_owner].length; i = SafeMath.add(i, 1)) {
totalAmount = SafeMath.add(totalAmount, tokensMapByOwner[_owner][i].tokenAmount);} return totalAmount; //
return SafeMath.div(totalAmount, tokensMapByOwner[contractInstanceOwner][0].tokenAmount);}

This function lets you check the balance of issued tokens by wallet address.

Your Smart contract is now ready so let’s compile it.

An Ethereum Contract In 30 Minutes

Step 3: Deploy Smart Contract on Ropsten Test Network

Click on run in the tool. Then select Injected Web3 Ropsten Under Environment and other tab shows the wallet address and the balance in the account.

An Ethereum Contract In 30 Minutes

Select the smart contract from drop-down menu >> Deploy the contract. Remix tool also shows the live status of all events. As currently, it showing deployed smart contract is pending in Metamask.

An Ethereum Contract In 30 Minutes

>> Open Metamask Chrome Extension >> Confirm the Transaction.

An Ethereum Contract In 30 Minutes

Click on the contract deployment and it shows the contract successfully deployed on testnet and you can also verify the transaction on the Ethereum network.

An Ethereum Contract In 30 Minutes
An Ethereum Contract In 30 Minutes

Step 4: Interact With The Smart Contract

Let’s issue some tokens for an asset. Expand the “CreateToken” tab in the smart contract. >> Fill Information >> Transact

An Ethereum Contract In 30 Minutes

>> Open Metamask Chrome Extension >> Confirm the Transaction.

An Ethereum Contract In 30 Minutes

Transaction successful. We successfully created 1M tokens as you can see in the decoded input tab.

An Ethereum Contract In 30 Minutes

Let’s check the balance of the owner and we should be able to see 1M tokens in his wallet address.

An Ethereum Contract In 30 Minutes
An Ethereum Contract In 30 Minutes
An Ethereum Contract In 30 Minutes

That’s it, guys. There you have your first smart contract, deployed on the blockchain. In this article, we designed a smart contract for token issuance, deployed on blockchain and interacted within the smart contract using Remix – Solidity IDE.

What’s the biggest thing you’re struggling with right now that we as a technology consulting company can help you with? Feel free to reach out to us at info@jalantechnologies.com. We hope our assistance will help!

Tags

Mohit Kumar

Solving problems using technology.

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.