How To Launch Your Own Production-ready Cryptocurrency

Written by adam.bavosa | Published 2018/02/14
Tech Story Tags: ethereum | cryptocurrency | blockchain | build-your-own-crypto | crypto

TLDRvia the TL;DR App

This article covers blockchain programming for cryptocurrency, testing the code, and deploying to the blockchain. Learn how to create wallets and a simple user interface for smart contract execution.

The way we do money is changing. What can you do to keep up? Blockchain and cryptocurrency have been around for years, but only in the past several months has the conversation spread far beyond arcane internet forums and tech company break rooms.

Maybe it’s time for you to make your own cryptocurrency, to make your own medium of exchange for goods and services. That may sound complicated! But it’s simpler than you would expect — thanks to the great strides the decentralization community has made and continues to innovate with.

Bank of the Future

A cryptocurrency replaces today’s bank. Banks keep track of the amount of money you have in an account. Instead of trusting a single institution to keep track of this for you, you can trust a massive computer network made up of anyone and everyone to keep track, publicly. The collective computers in this network confirm every single transaction of currency that ever happened and ever will happen. This public consensus is the assurance that people rely on when using cryptocurrency for payment.

Your own cryptocurrency can be the token that you accept for business — kind of like tokens in an arcade. This currency can be brought into existence today. The first step is to choose a big, decentralized computer network that is constantly confirming the legitimacy of new additions to its blockchain. Let’s go with Ethereum.

Smart Contract Programming

Ethereum is a decentralized computing platform for executing smart contracts. These are programs that run code in a transparent environment, without the possibility of third-party tampering. A user’s private key is used to create a fundamentally unique signature for every request that executes a smart contract (see this wiki if you are not familiar with asymmetrical cryptography).

The Ethereum community has a sizable following of open source software engineers that are relentlessly developing amazing tools for the greater good of humanity. To make your own cryptocurrency on the Ethereum network, you need these four tools:

  • Solidity — An Ethereum smart contract programming language.
  • Truffle Framework — An Ethereum development kit.
  • Web3.js — A JavaScript package for interacting with the Ethereum network with an internet browser or Node.js.
  • Ether (ETH) — The currency of the Ethereum network.

Optional tools:

  • MetaMask — a Chrome extension for crypto payments.

Cryptocurrency is merely one of limitless use-cases for a blockchain. The brilliant community rallying around Solidity makes it attractive to invest effort in building decentralized apps with Ethereum. First let’s build your own cryptocurrency to start learning blockchain development.

Developing the ERC-20 Token

The Ethereum community has established some standards regarding the functionality of smart contracts, including tokens. The cryptocurrency token in this tutorial is based on the ERC-20 Token Standard.

If you don’t have node.js, install it now (The tests in below will not work with versions earlier than node.js 8). Then open a terminal window and do:

npm install -g trufflemkdir MyToken && cd MyTokentruffle initnpm init -ynpm install -E zeppelin-solidity

This installs the Truffle CLI, creates a project directory for your token, and installs an open source Solidity library called OpenZeppelin.

Next we can begin writing our Solidity contract. Take a few minutes to familiarize yourself with Solidity as well as the DANGERS noted in the Token Standard. In your project directory, create a file in the auto-generated contracts/ folder and name it Token.sol. Add the following code that follows the ERC-20 Token Standard specification.

This token is not mineable and there is a fixed supply. By default, there are 1 billion tokens, and each token can be divided into fractions up to 18 decimal places. The smallest fraction of a token is referred to as Wei in Ethereum or Satoshi in Bitcoin. You can change these numbers, the token name, and the token symbol to whatever you want (see the above constructor function).

Once this code is deployed to the main Ethereum network, anyone who wants to send or receive your token will execute the code in this contract. The balances map is the data structure that holds all of the information regarding who owns tokens. The address is the wallet owner’s public key, and the unsigned 256 bit integer is the number of token Wei in the wallet.

Read vs. Write

Read and Write operations for the blockchain have two easy rules: reads are free, writes are not. Since we are using the Ethereum network, ETH needs to be spent by the caller when doing blockchain additions. Additions are made by functions that change state, like transferring tokens from one wallet to another. This can be contrasted with read functions, such as viewing the balance of a wallet. Read functions add no new data to the blockchain and they are always free to execute.

Test Your Code

Before we deploy our contract, we should thoroughly test it. Truffle includes Solidity test and JavaScript test documentation in their getting started guide. I’ve included sample integration tests with JavaScript (truffle-keys here).

test/integration.test.js — node.js 8 or later

The tests here are not exhaustive, but they are a good starting point for an engineer that is learning Solidity. This test file can only be run inside of the truffle develop environment.

In order to migrate the contract to an Ethereum client using truffle, add this file 2_deploy_contracts.js to migrations/. This migration step is required for the tests to run.

To run the integration tests we will use the Truffle CLI:

npm i ethereumjs-tx

truffle developtruffle(develop)> test

The truffle develop command boots a test blockchain on your local machine. The test network has 10 default Ethereum key pairs that each have 100 ETH every time the server is started. These keys will call the functions in your Solidity contract to test their functionality.

The test command will execute all of the tests in the test/ folder. Explore the integration.test.js file to learn exactly what the tests are doing to your test wallets and test blockchain.

Deploying

After you are comfortable with, Solidity, network gas prices, and writing your own tests, you can put your token on a public test network. The Ropsten network is one of several public test networks used for Ethereum development. Network connections can be configured in the truffle.js file.

If you do not have an Ethereum wallet of your own, I suggest you make one now. If you do not have a mnemonic and wallet, use this tool to generate one. Select ETH in the coin dropdown, and click the English button. Save the mnemonic somewhere safe and secure!

It’s a good idea to make separate wallets for test and main networks, so it’s less likely that you’ll have an ETH wasting accident. Add your Ethereum key set mnemonic to your environment variables as ethereum_mnemonic like referenced in the above truffle config file.

## bashexport ethereum_mnemonic="candy maple cake...."

If you do not have test ETH in your wallet on the Ropsten network, install the MetaMask Chrome extension to get some for free from a faucet. In order to execute a transaction in your token contract, you need to spend some ETH on fees — work that is done in the network on your behalf.

truffle migrate --network ropsten

This command will log the contract address, be sure to save it! Migrate uses the --network flag and refers to the ropsten key in the networks object in truffle.js. An object for the main Ethereum network can be included like the Ropsten object. I excluded it from my truffle box code for safety reasons. See the "live" connection object in this Truffle Tutorial for the Main Ethereum network.

Once your token contract is deployed, you can review your token balance using the MetaMask Chrome extension. In the MetaMask UI, select the Ropsten Test Network from the dropdown picker on the top left, then click the tokens tab. Click the Add Token button and input your contract address that was logged from the truffle migrate command.

MetaMask can transfer token, or you can create your own contract invoking UI with Web3.js. The token contract above is exciting to see for ICO developers, but it isn’t able to do the newsworthy crowdsale out of the box. Let’s make that happen.

Crowdsales

Now that you have installed Truffle, used the CLI, explored Solidity, and written some test code, we can unbox an existing Truffle project — Crowdsalable Ethereum Token. Let’s make a new directory and pull this project directly from my Github repo using the Truffle CLI.

mkdir crowdsalable-eth-token && cd crowdsalable-eth-tokentruffle unbox [email protected]:ajb413/crowdsalable-eth-token.git

This Truffle project has a more robust implementation of an Ethereum token. The token name and symbol can be changed by the owner after the contract is already deployed. The owner can also configure and open new crowdsales at any time.

The truffle box comes with a development mode UI for executing contracts on the local Ethereum client. The UI uses the 10 static wallets that the truffle develop command initializes on your machine - to execute transfers, view wallet balances, and launch the exciting crowdsale.

Broadcast Crowdsale Announcements

The app/ folder contains the web UI and also an extra bit of PubNub magic. When a crowdsale is launched, the owner has the option to text message all of their followers with the crowdsale details, so they can begin purchasing your token.

This functionality is powered by the ClickSend API and PubNub Functions. In order to enable this realtime updating feature, you must sign up for PubNub and ClickSend. You insert your api key, publish key, and subscribe key where noted in app.js and sms-handler.js. Also edit the JavaScript array of Phone Numbers to choose who receives an SMS.

excerpt from app/js/app.js

excerpt from app/pubnub-functions/sms-handler.js — Deploy this to PubNub Functions!

The PubNub Function event handler must be deployed in the PubNub Admin Dashboard. See this tutorial for deploying function event handler code in 2 minutes.

A Development UI with Web3.js

Next we run the local Ethereum client the same way we did earlier.

cd crowdsalable-eth-tokennpm itruffle develop

## Truffle development blockchain and console are booted

truffle(develop)> compiletruffle(develop)> migratetruffle(develop)> test

  • Leave the truffle console running
  • Open a new command line window
  • Navigate to the same project directory
  • Run the development UI with:

npm run dev

> crowdsalable-eth-token dev /crowdsalable-eth-token> webpack-dev-server

Project is running at http://localhost:8080/

Next, open the UI in your browser and explore the key features. We can:

  • Launch a new crowdsale
  • Transfer token from wallet to wallet
  • Purchase token from a crowdsale using ETH
  • Check the balance of any wallet

The nature of the development environment allows anyone to invoke functions like createCrowdsale and transfer, when in reality, these methods cannot be invoked by just anyone. For onlyOwner decorated Solidity functions, the invoker must be the owner of the contract and must sign their requests with their private key (see _rawTransaction_ function in test/integration.test.js). Also functions like transfer will only be able to send token from the wallet that the private key belongs to. This will be more restrictive on the main network for the right reasons.

web3.eth.sendRawTransaction(...)

After you have deployed your PubNub Function, you are able to launch a crowdsale and send a mass SMS to all of your followers when the contract opens. Input a name, click the Launch button, and check your phone!

The configuration of the crowdsale is specified in the app/js/app.js script in the launchCrowdsale event handler. By default, the crowdsale is open forever, starts with 100,000 TOK to sell, and a buyer receives 3 TOK per ETH that they pay.

Use the default wallet public keys listed at the bottom of the UI for transfers, purchases, and balance checks. Remember, on this network, the wallets each have 100 fake ETH to play with.

If you’ve made it this far, you now have some command over blockchain and PubNub. To recap, we covered:

  • What cryptocurrencies are
  • How to build an Ethereum token that is compliant with the standard
  • How to transfer the token and invoke contract methods
  • How to test all of the contract code
  • How to create a crowdsale
  • How to broadcast an announcement to all of your followers with PubNub

The true power of blockchain is yet to be realized because the technology is only recently picking up widespread interest. PubNub will stay involved. For live demonstrations of blockchain programming, check out our events and Meetup page. Best of luck to you in your blockchain adventures!


Published by HackerNoon on 2018/02/14