Estimate Project

Smart-contracts in Ethereum blockchain

by | August 16th, 2018

Bitcoin and Ethereum are well known cryptocurrencies operating on blockchain technology. They both draw the attention of the world to their decentralized nature and obviously many people want to know more about these technologies. However while Bitcoin is just a digital currency,  Ethereum as a ledger technology is often used for creating programs and applications.

We decided to start with a few articles about the general structure of Ethereum, to describe the opportunities and benefits it gives and to follow step-by-step process of creating smart contracts.

Feel free to add your questions in comments below and we will answer them too.

Smart-contracts in Ethereum blockchain

Ethereum as a cryptocurrency is not a clone of Bitcoin. It offers infrastructure that provides  unique options, like creating and running smart contracts or new virtual cryptocurrencies .

You might have heard about ICO which states as “Initial coin offering”. It is an aspect of Ethereum that allows to create your own cryptocurrency and offer it on virtual internal market to be traded vs Ethereum and other cryptocurrencies issued there.

The smart contract is something like a program that is running on blockchain and can perform some meaningful actions according to its functionality.

Those functions can be the different ICO for investment data collection, cryptokitties from blockchain, numerous services for clear data storing of real estate or casino owners, sport bidding and other services that might be requested from Ethereum blockchain by your smart mind.

However you should know, that smart contracts are not exclusive feature for Ethereum only. On the hype period of blockchain technologies new platforms are being created so fast, promising to surpass the Ethereum in nearest future. They offer a possibility of creating the smart-contracts using different programming languages (C, Java etc); better scaling and net capacity for faster contract performance. Among such blockchains we can name NEO, Cardano, Waves. Even some older cryptocurrencies (like Zcash) plan to implement smart-contracts into their blockchains nearest future.

Currently we want to draw your attention to smart-contracts of Ethereum, so let’s get more specific.

Solidity is a programming language, used for writing smart-contracts for Ethereum. It is rather simple (typified JavaScript), able to consider the specific work of virtual machine of Ethereum blockchain.

Now let’s try to write demo contract to be performed on a blockchain – “Hello world”. We will need for that not only the language itself but a runtime as well. Ethereum has a free Web IDE for the development and testing smart-contracts named Remix. You can get to know it better following the link https://remix.ethereum.org

Loading the page you will see the example of smart-contract. Feel free to delete it as we are going to replace it with our own code.

Please insert this part of very complicated code, we will get back to it soon.

In a right panel we have available tabs «Compile», «Run», «Settings» etc. Currently we need first two.

«Compile» – checkmark «Autocompile» for recompiling the contract code right after its change or press the button “Start to compile” for manual source code compiling.

«Run» – is much interesting. We have available fields:

Environment

Environment — is a runtime of the contract, by default «JavaScript VM».

  • JavaScript VM –  is a blockchain emulator for debugging of your contract. It is fast and furious!
  • Injected Web3 — is a proxy between your browser and Ethereum blockchain. MetaMask for example.

MetaMask — is very cool thing. In essence, this is a usual Ethereum wallet, designed as a Chrome extension. In addition to the function of storing Ether, Metamask acts as a proxy for access to the Ethereum blockchain (both: test net for debugging smart-contracts (Rinkeby, Ropsten) and Ethereum mainnet). So it allows your web-application to interact with smart-contracts, performing some useful actions in blockchain.

The link for uploading MetaMask https://metamask.io/

  • Web3 Provider — is an own node of Ethereum blockchain (example, local wallet with a full Ethereum blockchain – Geth with RPC connection)

Account

Account — is a wallet with a test Ether available. 100 Ether on each! Yes, i am rich 🙂

Gas limit

Gas limit — amount of gas, that can be used for performing of smart-contract. Usually searching for Gas you will find only that it is a fuel for Ethereum and that’s it. But it’s not clear enough. However drawing an analogy with Bitcoin we can say that it is a kind of transaction commission or fee, but a very-very tricky one. It plays much more important role than just a fee for a transfer from one wallet to another. It is a payment for operation in blockchain.

Let’s go further with analogies!

In essence, Gas is an Ether. The minimal element of Gas is called «wei». Let’s call it a drop of petrol that runs our car (blockchain).

1 Ether — equals not too much and not too little, but 1,000,000,000,000,000,000 wei !

Please enjoy the list of abbreviations:

Unit Wei Value Wei
wei

1 wei                    

1
Kwei (babbage)

1e3 wei

1,000
Mwei (lovelace)

1e6 wei

1,000,000
Gwei (shannon)

1e9 wei

1,000,000,000
microether (szabo)

1e12 wei

1,000,000,000,000
milliether (finney)

1e15 wei

1,000,000,000,000,000
ether

1e18 wei

1,000,000,000,000,000,000

 

So, Gas is Ether. What is next? There are new terms appearing, like “Gas limit” and “Value”. They exist to keep the operations under control, including smart-contract operations.

Example: We have a smart contract. On a startup stage it is performed by miners until the operation will be confirmed and transaction will be included to blockchain.

But! I am a foxy-hacker – i create the smart-contract and write the code like:

Obviously, it is an infinite loop and completing of such a smart-contract can bring our Ethereum network down forever. RIP. Sly and clever Ethereum creator – Vitalii Buterin – came up with such decision. Each “setter” operation, that changes the data in blockchain (in our case it is the operation “x++”) applies for a fee and spends our Gas. If have not enough Gas for a lifetime operation it just breaks and your Gas goes directly to miners. This is kind of protection from the useless operations.

Even more – if you smart-contract is written incorrectly or if it stores much data – you will spend your Gas again, as each operation (roughly speaking each row in your code) cost you money.

Gas limit – is a maximum amount of Gas that you smart-contract may use for performing the operations. If you specify too little – the fulfilling of the contract will be suspended and none of the records will appear in blockchain. If you specify too much – it’s ok, operation will be completed successfully and the remaining Gas after the charge will be refunded back to your account.

Gas Price

“Gas price” – is an option which allow to push your transaction in Ethereum network faster and more expensive (or more slowly but cheaper). This indicator is dynamic and may change depending on the network load.

In total, the payment for your transaction (transferring costs, performing of smart-contracts, uploading of smart-contracts) can be calculated using a simple formula:

Used Gas limit * Gas Price = Payment for the transaction

if you are looking for the optimal balance between the speed and the value – please check the service https://ethgasstation.info/

And now, let’s get back to our code on Remix, where we started the test contract “Hello world”

We chose the runtime, wallet, press Compile and then press the Deploy button. So our test of smart-contract is here!

If we press the «getData» – we will get the value from the smart-contacts (the row “Hello world”). Here is our contract itself!

Own Ethereum based token

Many people want to create their own token, based on ERC20 inspired with a growing popularity of different ICO and give a try to collect some money for their ideas and projects. It’s absolutely real and here are some hints.

First of all, ERC20 — is a standard requirements of smart-contract creating that provides a certain set of variables, methods and events, different combinations of which gives that meaning of the “own token”

Methods

function totalSupply() constant returns (uint256 totalSupply)

function balanceOf(address _owner) constant returns (uint256 balance)

function transfer(address _to, uint256 _value) returns (bool success)

function transferFrom(address _from, address _to, uint256 _value) returns (bool success)

function approve(address _spender, uint256 _value) returns (bool success)

function allowance(address _owner, address _spender) constant returns (uint256 remaining)

 

Events

event Transfer(address indexed _from, address indexed _to, uint256 _value)

event Approval(address indexed _owner, address indexed _spender, uint256 _value)

The standard was approved a little while ago, however if you ignore it, your token might not be accepted for the exchange, and you will have to update it accordingly.

We will not consider the full algorithm of the token creating and the code, however let us offer you few useful links. They will help you to create your own Gera- token within one evening!

Links:

  1. Creating an ERC20 compatible token
  2. The project with a list of git-snippets for the creating of the considered and secure smart-contract 

We hope this information is useful for you, in our next article we will speak about deploying the smart-contract into the blockchain.

Tags: , , , ,