In this chapter, we will learn how to deploy contract on Ethereum. Click on the Run menu option to deploy the contract. The following screen will appear.
The contract name is shown in the highlighted list box. Below this, you will notice the Deploy button, click on it to deploy the contract. The contract will be deployed on the Remix built-in Blockchain. You will be able to see the deployed contract at the bottom of the screen. You can see this in the highlighted portion of the screenshot below.
Notice, the presence of three method names in this highlighted region. Next, you will interact with the contract by executing the contract methods.
Once you write the complete contract code, compiling it in this IDE is trivial. Simply click on the Autocompile checkbox in the IDE as shown in the screenshot below −
Alternatively, you may compile the contract by clicking the button with the title “Start to compile”.
If there is any typo, fix it in the code window. Make sure the code is compiled fully without errors. Now, you are ready to deploy the contract.
We will name our contract MyContract as in the following declaration −
contract MyContract {
We will declare two variables as follows −
uint amount;
uint value;
The variable amount will hold the accumulated money sent by the contract executors to the contract creator. The value field will hold the contract value. As the executors execute the contract, the value field will be modified to reflect the balanced contract value.
In the contract constructor, we set the values of these two variables.
constructor (uint initialAmount, uint initialValue) public {
amount = 0;
value = 1000;
}
As initially, the amount collected on the contract is zero, we set the amount field to 0. We set the contract value to some arbitrary number, in this case it is 1000. The contract creator decides this value.
To examine the collected amount at any given point of time, we provide a public contract method called getAmount defined as follows −
function getAmount() public view returns(uint) {
return amount;
}
To get the balanced contract value at any given point of time, we define getBalance method as follows −
function getBalance() public view returns(uint) {
return value;
}
Finally, we write a contract method (Send). It enables the clients to send some money to the contract creator −
function send(uint newDeposit) public {
value = value - newDeposit;
amount = amount + newDeposit;
}
The execution of the send method will modify both value and amount fields of the contract.
Solidity is an object-oriented language especially developed for contract writing. It is a high-level language, which inherits traits from C++, Python, and JavaScript. The Solidity compiler compiles your source code into bytecode that runs on Ethereum Virtual Machine (EVM).
For quick understanding of the Solidity syntax, look at the sample code in the IDE.
The first line is a directive to the compiler. The second line starts the definition of the contract. Within the contract, you declare variables such as −
address chairperson;
You can also define structures such as Proposal and create an array of these structure items. Examine this in the code window.
You may then define a constructor which is invoked at the time of instantiating a contract.
constructor(uint8 _numProposals) public {
After the constructor, you will define several methods, which are the contract methods. In the sample contract, giveRightToVote is one such method having the following syntax −
function giveRightToVote(address toVoter) public {
The public keyword makes this method publicly invokable by any client who has access to the contract.
Likewise, the sample contract defines three more methods called delegate, vote, and winningProposal. Examine these for your own understanding of the Solidity syntax. These are the prerequisites to writing your own contract. Explaining the full syntax of Solidity is beyond the scope of this tutorial.
There are several tools available to develop and test contracts. One of the simplest tools is provided on the official Ethereum site itself. The tool is called Remix, we will use this for our contract development.
In the center window, you will see some default code, which is a sample Solidity code. You will type your contract code in this code editor. Your code may be auto-compiled. Upon successful compilation of the code, you will be able to run the code in the same IDE. When you execute the contract methods, the results will be displayed in the same IDE window. There are facilities to debug the code and to unit test your project. These can be seen in the menu bar at the top right hand side as shown in the IDE screenshot below. You will be using these options shortly.
A huge success of Bitcoin raised interest in the minds of several to create their own currencies. Looking at the advantages offered by Bitcoin – a digital currency, people wanted to use the concept of Blockchain in their own applications. People wanted to move out of their physical contracts to smart digital contracts where several issues like repudiation, transparency, security, etc. would be automatically addressed. The outcome of this effort resulted in the creation of Ethereum – a popular platform for creating distributed Blockchain applications that support smart contracts.
In this tutorial, you will learn how to create a distributed application (DAPP) on Ethereum platform. More specifically, you will learn how to write a contract, test it on a local Blockchain and finally deploy it on an external Blockchain for deep testing and commercial use. You will use Solidity, an object-oriented language for contract development. You will also use Remix, an open source IDE for developing and testing contracts. To deploy the tested contract on an external Blockchain, you will use Ganache. To interact with the contract you will need a client application. We will use MyEtherWallet to create a wallet for each such client. The contract creator will publish the contract. Any other client will look at the contact value by using the interface provided by the contract and send some money to the creator for executing a part of the contract.
Looking at the advantages offered by Bitcoin − a digital currency, people wanted to use the concept of Blockchain in their own applications. People wanted to move out of their physical contracts to smart digital contracts where several issues like repudiation, transparency, security, etc. would be automatically addressed. The outcome of this effort resulted in the creation of Ethereum − a popular platform for creating distributed Blockchain applications that support smart contracts.
Audience
This tutorial is designed for those who wish to gain some insight on how Ethereum works. After completing this tutorial, you will find yourself at a moderate level of expertise from where you can take yourself to the next level.
Prerequisites
Before proceeding with this course, we assume the reader has basic understanding in Web Development, JavaScript, Ajax-Requests, AngularJS, Gulp/Grunt and the Node Package Manager.
Since Bitcoin is a new emerging technology which is underway, unforeseen developments can make its existence and continuation difficult. Concerning its security and future, there are numerous questions which no one can answer. How far can we trust Bitcoins? Are they a bubble that is going to burst? Are they a passing phenomenon and a fad that would fizzle out over a period of time? Or are they going to stay put and perhaps dominate other currencies in future?
As of now, bitcoins are mostly unregulated, however this may change. Governments are worried about losing taxes and control over the currency. They may bring legislations to regulate bitcoin which may hugely impact the advantages that bitcoins have over other currencies. The volatility of bitcoin prices is one huge issue. The wild fluctuations in its index is sign of such volatility. In recent years, bitcoin prices have risen exponentially and after some corrections have dipped but still they are on the high side. Many expect that the price will further increase.
Favoring Growth Factors
The things that favor the growth of bitcoin adoption are as follows −
There are limited number of bitcoins.
The awareness about bitcoins is growing and so their acceptance and adoption.
The number of bitcoin transactions is increasing day by day.
A large number of wealthy people do not want government’s regulations on their wealth and would rather prefer storing in bitcoins.
Next halving is scheduled to occur in 2020. This will further decrease the rate of supply of bitcoins while bitcoin usage would have increased manifold by 2020. As of now, the number of bitcoin transactions is way behind the number of credit card transactions and the former has to significantly increase to realize the full potential of bitcoins.
Some of the issues which have to be tackled to help bitcoin’s growth are as follows −
Bitcoin transaction time or the time required to get confirmations is still on the high side as compared to credit or debit card transactions.
The security of Bitcoins has become a major issue. As the usage of Bitcoin is increasing, hacking of bitcoin wallets and even exchanges has been more widespread.
As of now Bitcoins are too technical for common people and are not so user friendly. It is difficult for people to understand why bitcoin prices are so volatile, why transaction time is so high and how they should safeguard their bitcoins.
Governments of several countries including India are discouraging legal use of Bitcoins as they understand that Bitcoin is a parallel financial system beyond their control. However, countries like Japan, Australia and several European countries have made Bitcoin legal as they realized that they cannot stop the usage of bitcoins. Some countries have banned bitcoin exchanges. People are using global exchanges to hide their transactions. Meanwhile India and China have been discouraging Bitcoin transactions. China has tried to ban all Bitcoin Exchanges in their country while India has not banned any exchange. Zebpay and Unocoin are Bitcoin Exchanges that are under operation in India. They require submission of KYC documents before executing any Buy or Sell transaction.
The following is a list of applications of bitcoins
Bitcoins are being used to buy goods and services as more and more stores across the world are accepting bitcoin payments.
Bitcoin transactions provide a customized level of anonymity and it is relatively difficult to trace their trail. So bitcoins are being used to transact anonymously.
International payments can be made easily and cheaply as bitcoins are not related to any country or subject to any government regulation.
There is the freedom of the fact that there is no need of permission from any authority for your transactions.
Bitcoins provide a way to transact securely online as they use very strong cryptographic algorithms.
Users and businesses like bitcoin payments because there are no credit card fees to pay.
Bitcoins can be as an investment, expecting that their value will appreciate significantly in future.
Bitcoins can be used to gamble on online sites like SatoshiDice, RoyalBitcoin, Bitzino, Peerbet, etc.
Bitcoins are being used to shop online as increasing numbers of vendors are allowing bitcoin transactions. Users now can make payments in bitcoins on their smartphones through bitcoin wallet apps.
Unlike credit card or bank payments, there is no need to provide personal information to complete the transactions. So the hassle of providing identity can be avoided.
In this chapter, we shall learn bitcoin glossary which describes over 50 bitcoin terms.
Address
A bitcoin address allows us to send and receive bitcoins on the bitcoin network. It is also the public key or address that is used to transact in bitcoins.
Altcoin
Altcoin is a group of ‘alternate’ cryptocurrencies other than bitcoins. Examples of altcoins include Ethereum, Litecoin and PPcoin.
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
Asic
An Application Specific Integrated Circuit (ASIC) is built specially to process the SHA- 256 hashing equations that are used in mining bitcoins.
Asic miner
An ASIC miner is the latest mining hardware used in bitcoin mining. It is used to calculate the SHA-256 equation faster than a CPU or a GPU. ASIC miners are custom-built and connect to the network through a wireless or Ethernet connection.
Bitcoin Price Index (BPI)
The Bitcoin Price Index, designed by Coin Desk, shows the average bitcoin prices across the top global currency exchanges.
Bitcoin Whitepaper
The Bitcoin Whitepaper dubbed as the Bible of the Bitcoin ecosystem, was submitted by the currency’s mysterious founder, Satoshi Nakamoto, in 2008. It gives a detailed description of the bitcoin protocol, and is a good reference material for newbies and experienced people alike.
Block Chain
This chain contains the records of all bitcoin ‘blocks’ that have been mined since the start of the currency. The chain is designed such that each block contains the hash of the preceding block, which makes the chain secure against counterfeit mining operations.
Block Reward
A reward is given to each miner who completes a transaction block. It can be in the form of coins or transaction fees; Bitcoin network currently rewards 25 coins for each completed block. Once the threshold of blocks has been mined (which currently stands at 210,000 blocks) the reward is halved; such an event as described above is called halving. The next halving is due to take place in 2020. Then the reward would become 12.5 coins for mining one block.
BTC
BTC is the abbreviation of bitcoin, similar to USD and GBP for US dollar and Great.
Bitcoin Client
This is the software program that connects a device, whether a desktop computer, laptop or mobile phone to the bitcoin network.
Confirmation
A confirmation of a transaction is its successful hashing into the block of a blockchain. It can take up to ten minutes, though larger transactions may require up to 6 confirmations.
Coloured Coins
Coloured coins is a proposed new feature of bitcoins that allows users to define their own attributes of the currency. It is intended that users could mark a bitcoin as a physical asset, which could then be exchanged as a token for other property.
Coinbase
The name of a bitcoin wallet operator that offers payment processing for merchants, and acts as an intermediary in bitcoin exchanges.
Coin Age
A coin’s age is calculated by the product of the currency amount and the period of time it has been owned.
Cryptocurrency
A cryptocurrency is considered legal tender by consensus and is secured by using cryptography based on mathematical formulas.
Cryptography
It is the field where math formulas and algorithms are used to create the codes that encrypt and decrypt information.
Double Spending
This is the criminal act of spending the same bitcoins more than once. The user completes a transaction using his bitcoins and then makes a second transaction with some other party using the same bitcoins. So confirmation is necessary to validate a transaction and prevent double spend. So zero-confirmation transactions are risky as they could involve double spending.
Dust Transaction
This is a transaction that has a record in the block chain but has very little worth. Steps are being taken to minimise the number of dust transactions that take place by introducing a minimum transaction amount.
ECDSA
ECDSA is the name of a code and an abbreviation for Elliptic Curve Digital Signature Algorithm. It is used in the Bitcoin protocol to sign transactions.
Escrow
An escrow is a kind of third party online wallet that stores funds securely during a transaction between two parties. It is used in cases where two parties cannot transact bitcoins till certain conditions are met, and want to ensure that their money is not ‘stolen’ digitally.
Faucet
A faucet is the method of mining a certain number of coins when launching a new cryptocurrency, and then giving these away in order to promote interest in the new currency. There are several bitcoin faucet sites that give away very small amounts of bitcoins to promote them
Fiat Currency
A Fiat currency is another name for token money used across the world that has been declared legal tender by governments and central banks and is not backed by a physical commodity.
Fork
A fork in a blockchain is said to occur when one group of miners starts hashing a different set of transaction blocks. It can also happen when a new version of the bitcoin client is introduced. A fork is deemed successful if it becomes the longer version of the chain.
Genesis Block
The original block in a chain.
GPU
This is a graphical processing unit, as found in standard PC graphics card. As GPUs are designed to process huge data at faster speeds in pixel-heavy computer games, they are also perfect for processing calculations required in cryptocurrency mining.
Hash
A hash is the mathematical processing done during bitcoin mining. It is a complex process that makes the currency secure and renders decryption very difficult and alteration of the output detectable.
Hash Rate
Hash rate counts the number of hash calculations done in a second. This generally indicates how fast and successful a mining operation is.
Input
Input shows where a bitcoin transaction has originated, and is generally a bitcoin address, unless it is a generation transaction meaning that the bitcoin has been newly-mined.
Litecoin
It is a type of alternate crypto currency that uses the Scrypt hashing formula.
Megahashes/SEC
It is the number of hashes per second measured in millions of hashes (a Megahash).
Market Order
A market order can be placed at an exchange when buying or selling bitcoins instantly, and at the prevailing market rate.
MBTC
A small amount: one thousandth of a bitcoin (0.001 BTC).
Micro-Transaction
Paying a very small amount as part of a transaction online, these are hard to execute under traditional payment systems. It is like paying for a bag of snack with a credit card.
Mining
Mining can be done by anyone who wants to mint some new bitcoins for his wallet. For this he should validate a block of outstanding transactions and solve cryptographic equations using some hashing algorithms.
Node
Every connected computer in the bitcoin network that relays transactions to other computers is called a node.
Orphan Block
Any block that was part of a discarded fork is known as an orphan block. This is not part of the valid blockchain.
Output
The output is the final address of a bitcoin transaction. It is quite possible that there can many outputs for a single transaction.
Paper Wallet
This is a physical record of public bitcoin addresses and their private keys. It can be a piece of paper, and presents a safer way to store bitcoins that cannot be hacked or corrupted.
Pool
A group of miners working in tandem is called a pool. These miners pool their work together to mine a block, and then share the reward accordingly. Mining pools improve the chances of successfully mining a block.
PP Coin
PP coin is sometimes known as peer coin or P2P coin. This is an altcoin that uses a ‘proof of stake’ calculation apart from proof of work for validation of work done.
Private Key
The security of private key is important in keeping bitcoins safe. The private key of an account is unique, and only the owner should know the private key. It is usually a string that signs a digital communication hashed with corresponding public key.
Proof Of Work
This calculation is used to give reward for mining work done in bitcoins. It does take a lot of time and effort to hash a block successfully, and this is considered as a proof of work which is rewarded appropriately.
Public Key
A public key is a bitcoin address, which is public or known or accessible to everyone. When a public key is hashed with a private key it makes a digital communication secure.
QR Code
A QR Code is a graphic that contains a data sequence. QR codes are scanned by mobile phones and other devices and are used in encoding bitcoin addresses and in facilitating bitcoin transactions.
Ripple
Ripple is a payment network on which users exchange any currency. Payments are done on an ‘IOU’ basis and are based on trust. The network consists of nodes and gateways operated by authorized people.
Satoshi
Satoshi, the name of the creator of bitcoin, is also the smallest denomination of bitcoin: 1 sat = 0.00000001 BTC.
Scrypt
A proof of work system meant for altcoin miners; it is relatively simple as compared to SHA-256; that is why altcoins using Scrypt are mined more than those using CPU and GPU set-ups.
Signature
When private and public keys are hashed together, they make a digital signature that authenticates the originating address of a bitcoin transaction.
SHA-256
It is the standard cryptographic equation that is used in the proof of work system of bitcoin mining.
SPV
The Simplified Payment Verification makes it possible for users to verify their transactions without downloading the massively-sized full block chain. Here users make by simply downloading the block headers only.
Transaction Block
The transaction block is the record of transactions which are collated and hashed, and then appended to the block chain.
Transaction Fee
Some bitcoin transactions will be charged a small fee when sent across the network. This fee is paid to the miner who has successfully hashed the block that contains that transaction.
µBTC
Another very small denomination of bitcoin; a µBTC is a ‘microbitcoin’
1 µBTC = 0.000001 BTC
Volatility
The fluctuations in price of bitcoin are defined as its Volatility.
Wire Transfer
A wire transfer is a method of transferring bitcoin currency to and from a bitcoin exchange. This transfer is done electronically, and can be secured to a bank account anywhere in the world.
Zero-Confirmation Transaction
It is a transaction where a vendor sells a product or service in return for a bitcoin payment, yet the transaction cannot yet be confirmed by a miner or added to the chain. This is where ‘double spending’ can happen.