Creating your own token for ICO

Share

ICO is a hot topic in the crypto world today. In this article, I will attempt to explain how to create your own token for an ICO project. 

First of all, you need to set up a private Ethereum testnet before you can proceed to create the token. After setting up the private network, you need to run it.(The detail steps for setting up a private Ethereum network is discussed in another article)

Next, you need to install the Ethereum wallet before you proceed. Follow the steps below to install the Ethereum Wallet.

  1. Install Ethereum Mist Wallet for Windows 10.
  2. Visit the link https://github.com/ethereum/mist/releases
  3. Download Ethereum-Wallet-win64-0-11-0.zip
  4. Create a folder Ethereum under the Program Files folder and extract the zip files there.

Launch the Ethereum wallet after successful installation. Also create an account in the wallet. 

Next, Open the Wallet app and then go to the Contracts tab as shown in the figure below:

Click on DEPLOY NEW CONTRACT and bring up the  Solidity Contract Source code text editor as shown the following figure:

Now type the following smart contract  code in the code editor.

pragma solidity ^0.4.24;

contract BestToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
     string public name;
     string public symbol;
     uint8 public decimals;

    /* Initializes contract with initial supply tokens to the creator of the contract */
 constructor(
     uint256 initialSupply,
     string tokenName,
     string tokenSymbol,
     uint8 decimalUnits
     ) public {
     balanceOf[msg.sender] = initialSupply;  // Give the creator all initial tokens
     name = tokenName;                        // Set the name for display purposes
     symbol = tokenSymbol;                   // Set the symbol for display purposes
     decimals = decimalUnits;               // Amount of decimals for display purposes
    }
    

    /* Send coins */
    function transfer(address _to, uint256 _value) public returns (bool success) {
     require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
     require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
     balanceOf[msg.sender] -= _value;                    // Subtract from the sender
     balanceOf[_to] += _value;                        // Add the same to the recipient
     return true;
    }
}

Don’t worry about the code first, I will explain them in another article.

If the code compiles without any error, you should see a “pick a contract” drop-down list on the right, as shown in the figure below:

Click Pick a contract button and select the “BestToken” contract. On the right column, you’ll see all the parameters you need to personalize your own token. You can tweak them as you like, I use the following parameters: 10,000 as the supply, BestCoin for the token name, “#” as the symbol and 2 decimal places. Your wallet should be looking like this:

Scroll to the end of the page and you’ll see an estimate of the computation cost of that contract and you can select a fee on how much Ether you are willing to pay for it. Any excess Ether you don’t spend will be returned to you so you can leave the default settings if you wish. Press “deploy”, type your account password and wait a few seconds for your transaction to be picked up.

Now click the DEPLOY button to deploy the smart contract, the output dialog is as follows:

Upon entering the password for your account and click send transaction, the contract is successfully deployed, as follows:

Now the new token Best Token is shown in your wallet, as follows:

Tokens are currencies and other fungibles built on the Ethereum platform. In order for accounts to watch for tokens and send them, you have to add their address to this list. Proceed to add BestCoin to the list, as shown in the following figure.

Now the new token BestCoin will be shown in the list, as shown in the figure below.  



Now you can send some funds from BestCoin to a wallet, as shown in the figure below:

Share

What is Merkle Tree in Blockchain?

Share

In my article ‘Blockchain in a Nutshell’, I have mentioned Merkle tree as one of the metadata in a block of the blockchain. What actually is the Merkle tree?

In computer science, the Merkle tree is a branching data structure that is used to store hashes of individual data in a large dataset. The purpose is to make the verification of the dataset efficient and fast. It is an anti-tamper mechanism to ensure that the large dataset has not been tampered with.

In blockchain,  the Merkle tree(also known as the hash tree) encodes the blockchain data in an efficient and secure manner. Every transaction occurring on the blockchain network is subjected to a hashing algorithm to produce a hash, as shown in the figure below. Therefore, every transaction has a hash associated with it.

As there are thousands of transactions stored on a particular block, it will be very time consuming if every node has to deal with hundreds of thousands of transaction across the blockchain, synchronization and mining will take a long time. To solve this issue, all the transactions hashes in the block are also hashed. As illustrated in the following figure, two hashes are hashed into a single hash.

These hashes are not stored in a sequential order on the block, rather in the form of a tree-like structure such that each hash is linked to its parent following a parent-child tree-like relation. The hashing will go on until it produces a singular hash,  the Merkle root. This Merkle root is the hash of the block and it is stored on the header of the block. The process is illustrated in the following diagram:

The Merkle Tree structure will enable the quick verification of blockchain data and quick movement of large amounts of data from one computer node to the other on the peer-to-peer blockchain network.

Share

How to achieve Proof of Work?

Share

Mining involves the process of producing a hash whose value is less than the target value. When this hash has been found, it is called a valid hash and hence proof of work is achieved.

The mining algorithm uses a counter known as the nonce to generate the hash using the SHA256 cryptographic function. A hash algorithm always produces the same arbitrary length data given the same inputs. It is impossible to compute the same hash with two different inputs. It is also impossible to predict the output of any given data in advance.

The value of nonce is initialised to 0. Mining is finding the nonce, the only input that changes every time we run the hash function. The goal is to find a value for the nonce that will result in hash lower than the target. So, the mining node might need to try billions or trillions of nonce values before it gets a valid hash. As you can see, mining is like playing the slot machine, there is no way to predict when can you strike a jackpot.

It is very easy to prove that the nonce found indeed produces a valid hash. All the information are available, everyone in the network can run the hash function and confirm if the hash is valid or not. Because it is also impossible to predict what the nonce will be, it also acts as a proof that the miner has indeed achieved Proof-of-Work.

Calculation of the Valid Hash

(The numbers are based on block #540909)

The formula to calculate the current target of the block is

Current target= maximum target / difficulty

Maximum target is set to

0x00000000FFFF0000000000000000000000000000000000000000000000000000

This is a hexadecimal number. After conversion to a decimal number

Maximum target= 26959946667150639794667015087019630673637144422540572481103610249215

Difficulty is (as given in the block)

7019199231177.17

Therefore,

Current target=(26959946667150639794667015087019630673637144422540572481103610249215)/7019199231177.17

= 3.84089×10^54

The hash of the block is

00000000000000000000ef17668e407e78c5a247f731b1138ad16f5bf79f1c0d

After converted to a decimal number, the value is as follows:

89454716205495239548871016846060264708718561584946189

The hash value is approximately   8.95×10^51   

Clearly, the hash value is less than the current target, therefore it is a valid hash.

Share

Technical Explanation of Mining

Share

In this article, let’s examine the technical aspects of crypto mining. In the blockchain, every block has a previous block except the very first block or the genesis block. Miners are competing to validate a new block by solving a complex mathematical puzzle. To explain in details, let’s take a look at the latest bitcoin mined block, block #540909 at the time of writing this article.

Notice that the block height is is 540909, which means there are 540909 blocks in the bitcoin blockchain.

Let’s call the successful miner for this block Mr.John. Before John successfully mines block #540909, he was actually competing with other miners in mining the previous block #540908. However, he lost in the contest and block #540908 was mined by a fellow miner. As soon as block #540908 was mined, he needs to quickly update his blockchain and starts mining for a new unvalidated block, known as the candidate block.

In actual fact, while John’s computer(also known as a node)  was searching for the Proof of Work for the previous block, it was also searching for new transactions. Those new transactions are added to the memory pool or transaction pool.  The memory pool is a node’s temporary storage area for transaction data. This is where transactions wait until they can be included in a new block and validated.

In constructing the candidate block, John’s node starts gathering the transactions in the transaction pool. It removes the transactions already present in the previous block if there are any. The block is called a candidate block because it doesn’t have a valid Proof of Work yet.

As you can see that block #540909 has 1696  transactions inside it. This was the number of transactions present in John’s transaction pool when he created his candidate block. The mining process can be illustrated in the following figure.

In the mining process, John’s node is creating a coinbase transaction. This transaction is to create some bitcoins and deposit them into John’s wallet as a reward for finding a valid Proof of Work. This transaction is different from the other ones because the bitcoins in the reward are created out of nothing. They do not come from someone’s wallet. Besides that, John’s node also calculates the transaction fees in the block.

John reward by mining this block is as follows;

Total Reward =  Reward for mining block + transactions fees

                    = 12.5 BTC+0.15289664 BTC

                   = 12.65289664 BTC

The details of the transaction is as follows:

You can see the No Inputs (Newly Generated Coins) statement. It is because coinbase transactions do not come from anyone’s wallet, so they cannot have any inputs. You only have the winning miner’s wallet address here.

Share

Blockchain in a Nutshell

Share

    A blockchain is a distributed and a decentralized digital ledger that can be used to record transactions and other data across numerous computers in a peer-to-peer network. We can also define a blockchain as a distributed encrypted database like a spreadsheet that is duplicated thousands of times across a network of computers. This network is designed to regularly update this spreadsheet. It is a subset of distributed ledger technologies.

    Centralized Network  vs  Decentralized Network

    Each node is a computer(desktop, laptop, handphone, mining rig etc) that is connected to the network

    A blockchain comprises a chronological chain of blocks. A block refers to a set of transactions that are bundled together and appended to the blockchain.  Every node in the network stores a copy of the distributed ledgers, or a chain of blocks, as shown in the figure below.

    A block in the blockchain primarily consists of four pieces of metadata, as follows:

    • The reference to the previous block
    • The proof of work, also known as a nonce
    • The timestamp
    • The Merkle tree root for the transactions included in the block.

    What is Proof of work?

    On the Bitcoin blockchain network, the miner nodes bundle unconfirmed and valid transactions into a block. Each block contains a given number of transactions. In the Bitcoin network, every miner must solve a cryptographic puzzle to propose the next block. This process is known as ‘proof of work’.  Proof-of-work is a mechanism that existed before the first blockchain was created. It prevents abuses by requiring a certain amount of computing work. That amount of work is key to prevent spamming and tampering. Spamming is no longer worth it if it requires a lot of computing power.

    What is Timestamping?

    Timestamping is an important feature of blockchain technology. Each block is timestamped, with each new block referring to the previous block using the cryptographic hash. Combined with cryptographic hashes, this timestamped chain of blocks provides an immutable record of all transactions in the blockchain, as shown in the figure below:


    Notice that the hash and the previous hash must be the same.

    What is Hash?

    A hash or hash value is the result of a hash function. A hash function takes an input of any length, perform some algorithmic transformation, and produces an alphanumeric value of a predetermined length. The input could be a spreadsheet file, a music file, video file, image file, financial statement, invoice, contract etc.

    A hash value comprises  a 256 bits(64 characters) random letters and numbers , as follows: “4373c7fb1437035365d9228c77eca2cfd240523e274163e78c1eba11effd8b38”.

    A hash has the following properties

    • A given input has a precisely predictable output of a specified length, usually, but not necessarily much shorter than the input.
    • Even if the input is only slightly changed, the output differs dramatically.
    • If the hash function is of the cryptographic variety, it is very difficult, if not practically impossible, to infer the original input, given only the output. The degree of difficulty/impossibility depends on the strength of the encryption used.

    Hashes are used to represent the current state of the blockchain. The input is the entire state of the blockchain. It means all the transactions that have taken place so far and the resulting output hash represents the current state of the blockchain. The hash is used to agree between all parties that the state is the same.

    Hash is for validation purpose. Data on the blockchain is “hashed” in each block. Each block is linked with the previous bock via the hash value. If someone tampers with a block, everyone will know the block is corrupted. Therefore. It preserves the integrity and immutability of the blockchain.

    What is a Digital Signature?

    Digital signatures, like real signatures, are a way to prove that somebody is who they say they are, except that we use cryptography or math, which is more secure than handwritten signatures that can be easily forged. A digital signature is a way to prove that a message originates from a specific person and no one else, like a hacker.

    Digital signatures are used today all over the Internet. Whenever you visit a website, you are using SSL, which uses digital signatures to establish trust between you and the server. This means that when you visit Facebook.com, your browser can check the digital signature that came with the web page to verify that it indeed originated from Facebook and not some hacker.

    Generating a key pair is analogous to creating an account on the blockchain, but without having to actually register anywhere. Besides that, every transaction that is executed on the blockchain is digitally signed by the sender using their private key. This signature ensures that only the owner of the account can move money out of the account.

    The Role of Hashing and Digital Signature in Blockchain

    To bring it all together, blockchain could not exist without hashing and digital signatures. Hashing provides a way for everyone on the blockchain to agree on the current world state, while digital signatures provide a way to ensure that all transactions are only made by the rightful owners. We rely on these two properties to ensure that the blockchain has not been corrupted or compromised.


    Share