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.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 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: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.
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:
Next we can begin writing our Solidity contract. Take a few minutes to familiarize yourself with as well as the DANGERS . 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 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/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.
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.
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 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 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.
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 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.
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 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.
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 and . 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 . See for deploying function event handler code in 2 minutes.
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.
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.