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: — An Ethereum smart contract programming language. Solidity — An Ethereum development kit. Truffle Framework — A JavaScript package for interacting with the Ethereum network with an internet browser or Node.js. Web3.js Ether (ETH) — The currency of the . Ethereum network Optional tools: — a Chrome extension for crypto payments. MetaMask Cryptocurrency is merely one of limitless . 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. use-cases for a blockchain 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 ( ). Then open a terminal window and do: The tests in below will not work with versions earlier than node.js 8 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 as well as the . In your project directory, create a file in the auto-generated folder and name it . Add the following code that follows the ERC-20 Token Standard specification. Solidity DANGERS noted in the Token Standard contracts/ Token.sol This token is not mineable and there is a fixed supply. By default, there are , and each token can be divided into fractions up to 18 decimal places. The smallest fraction of a token is referred to as in Ethereum or in Bitcoin. You can change these numbers, the token name, and the token symbol to whatever you want ( ). 1 billion tokens Wei Satoshi 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 map is the data structure that holds all of the information regarding who owns tokens. The is the wallet owner’s public key, and the is the number of token Wei in the wallet. balances address unsigned 256 bit integer 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, 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 . ETH needs to be spent free to execute Test Your Code Before we deploy our contract, we should thoroughly test it. Truffle includes and documentation in their getting started guide. I’ve included sample integration tests with JavaScript (truffle-keys ). Solidity test JavaScript test 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 environment. truffle develop In order to migrate the contract to an Ethereum client using truffle, add this file to . This migration step is required for the tests to run. 2_deploy_contracts.js migrations/ To run the integration tests we will use the Truffle CLI: npm i ethereumjs-tx truffle developtruffle(develop)> test boots a test blockchain on your local machine. The test network has 10 default Ethereum key pairs that each have every time the server is started. These keys will call the functions in your Solidity contract to test their functionality. The truffle develop command 100 ETH The command will execute all of the tests in the folder. Explore the file to learn exactly what the tests are doing to your test wallets and test blockchain. test test/ integration.test.js 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 is one of several public test networks used for Ethereum development. Network connections can be in the file. Ropsten network configured truffle.js 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 to generate one. Select in the coin dropdown, and click the English button. this tool ETH 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 like referenced in the above truffle config file. ethereum_mnemonic ## 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 . 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. faucet truffle migrate --network ropsten This command will log the , be sure to save it! Migrate uses the flag and refers to the key in the networks object in . 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 for the contract address --network ropsten truffle.js Truffle Tutorial 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 button and input your contract address that was logged from the command. Add Token truffle migrate 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 out of the box. Let’s make that happen. crowdsale Crowdsales Now that you have installed Truffle, used the CLI, explored Solidity, and written some test code, we can unbox an existing Truffle project — . Let’s make a new directory and pull this project directly from using the Truffle CLI. Crowdsalable Ethereum Token my Github repo mkdir crowdsalable-eth-token && cd crowdsalable-eth-tokentruffle unbox git@github.com: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 command initializes on your machine - to execute transfers, view wallet balances, and launch the exciting crowdsale. truffle develop Broadcast Crowdsale Announcements The folder contains the web UI and also an extra bit of PubNub magic. When a crowdsale is launched, the owner has the option to all of their followers with the crowdsale details, so they can begin purchasing your token. app/ text message This functionality is powered by the ClickSend API and PubNub Functions. In order to enable this realtime updating feature, you must sign up for and . You insert your , , and where noted in and . Also edit the JavaScript array of to choose who receives an SMS. PubNub ClickSend api key publish key subscribe key app.js sms-handler.js Phone Numbers 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 . See for deploying function event handler code in 2 minutes. PubNub Admin Dashboard this tutorial 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 and , when in reality, these methods cannot be invoked by just anyone. For decorated Solidity functions, the invoker must be the owner of the contract and must sign their requests with their private key ( ). Also functions like 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. createCrowdsale transfer onlyOwner see _rawTransaction_ function in test/integration.test.js transfer web3.eth.sendRawTransaction(...) After you have deployed your , you are able to launch a crowdsale and to all of your followers when the contract opens. Input a name, click the button, and check your phone! PubNub Function send a mass SMS Launch The configuration of the crowdsale is specified in the script in the event handler. By default, the crowdsale is open forever, starts with to sell, and a buyer receives that they pay. app/js/app.js launchCrowdsale 100,000 TOK 3 TOK per ETH 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 and page. Best of luck to you in your blockchain adventures! events Meetup