Create a token issuance Ethereum contract in 30 minutes

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.

“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


Remix: Solidity IDE – Develop Smart Contracts for the Ethereum

https://remix.ethereum.org


Metamask – Bring Ethereum to your Browser 

https://metamask.io/


Ropsten Test Network – Testnet for Ethereum Transactions 

https://ropsten.etherscan.io/


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.

screenshot 2019-01-21 at 1.45.13 pm

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

Screenshot 2019-01-21 at 1.55.05 PM.png

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.

screenshot 2019-01-21 at 1.46.27 pm

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.

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.


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.

Open Metamask Chrome Extension >> Confirm the Transaction.

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.

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

Open Metamask Chrome Extension >> Confirm the Transaction.

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

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


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.

Any questions? Please add them in the comments or email me at mohit@jalantechnologies.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s