Ethereum — Developing with SmartContracts

Ethereum is a decentralized platform that runs smart contracts, applications that run exactly as programmed without possibility of downtime, fraud or third party interference.

Ethereum is the real application of blockchain technology. Developers have started using Bitcoins’s underlying technology, the Blockchain for creating new applications.

Ethereum vision is to create a platform where applications can built and run on a decentralized network.

In the current way of building and running App, we develop our App and deploy it in a data center run by big companies. These companies hosting our applications can figure out what is happening in our App.

But in the context of Ethereum, there is network of independent computers distributed over the world owned by common people like you and me. Every computer in the network contribute in their own to make the App run. As in the Bitcoin world, here also each of the computer receives a reward for their work because they paid for hardware, electricity, computing power. The monetization for running the dApps (Decentralized Apps) is in the form of digital currency called Ether.

Ether is the fuel that makes the dApps to run. Ether is the currency of computation in Ethereum. The units of interaction with Ether is called “gas” and has a dynamic price set by the network. You can check the cost here at the Ethereum dashboard (https://ethstats.net/)

Developing your first Smart Contract:

SmartContracts are the building block of the Ethereum project. Smart contracts are applications where the state of the application is stored in the blockchain. They can read other contracts, exchange ether and execute other smart contracts. Contracts will be executed as long as the network exist.

Let us consider a simple SmartContracts from the Ethereum tutorial

The above contract is implemented in Solidity which compiles to bytecode that can be executed in the Ethereum virtual machine.

Very simple implementation where the “greeter” contract is used to implement an “Hello World” sample. The “mortal” contract provides a way to terminate the contract once it has been finished, by sending a Kill command which would send back any funds in the contract back to the address that created it.

Now that we have defined the smart contract in Solidity, it needs to be compiled to the EVM bytecode. You can do it locally using a secure compiler or use the online compiler. In this sample we would use the Online Solidity compiler available here

Let us now enter the contract into the Solidity web compiler and get the compiled bytecode on the right.

In the above you can also get a JS snippet which you can run in the Ethereum JS console, titled “Web3 Deploy”.

Setting up the Ethereum Runtime Environment

You can install the Ethereum client environment (geth.exe) by following the instructions here. In this we have installed Windows client. With the Ethereum environment you can get started by syncing the Ethereum blockchain to the local machine by running the ‘geth’ command with no arguments on the shell. Syncing the blockchain is time consuming operation and takes a while.

In this sample, instead of downloading the blockchain using the geth command, buying ether to run the sample locally, decided to use the Ethereum test network. For logging into the Ethereum test network install the Ethereum Wallet

When you start the Ethereum Wallet, you would see the following screen where it is downloading the block chain and also connecting to the Peers in the network. This is equivalent to doing “geth” from the command line. Instead we are depending on the Ethereum wallet to download the blockchain.

To avoid buying Ether we are using the Ethereum Test network for running the sample. In the actual scenario, you would need to have Ether in your local wallet to be able to deploy the Smart contracts. You can read more about setting up Ethereum account and management here

Running the Smart Contract

For running the smart contract, we will use the Javascript runtime environment. Type ‘geth attach” in a new shell to start the Ethereum javascript runtime environment.

‘geth attach’ is the main entry point into the Ethereum JavaScript runtime with Ethereum extensions. You can get the balance Ether in your account by running the following command:

web3.fromWei(eth.getBalance(“ACCOUNT_ADDRESS”), “ether”); where ACCOUNT_ADDRESS is the address you got earlier.

For running the SmartContract that we have defined earlier, we need to copy the “Web3 deploy” javascript snippet from the Solidity web based compiler. Within a few minutes you would see that the Contract is mined along with the address at which the contract resides. You have now deployed the first Ethereum applications.

The other way of deploying the SmartContract would be using the Ethereum Wallet. In the Ethereum Wallet there is an option to deploy the contract directly:

Once the SmartContract is deployed, the Ethereum blockchain would process the transaction. After successful processing of the transaction you can execute the contract. You can look for your transactions using https://etherchain.org/ by entering the transaction hash. In the custom contracts section you would see the newly deployed “greeter” contract:

Now that the SmartContract is created and deployed, let us execute it. For executing the smart contract we would need the address and the interface definition.

When you run greeter.greet() in a new console window, it would fail with the error “unknown reference error” since we have not defined the “greeter” variable.

Let us instantiate the greeter instance using the following syntax — var greeter = eth.contract(interfaceDef).at(Address) where interfaceDef is the “interface” snippet the Solidity compiler produced earlier. You can also get it from the Ethereum wallet contract section. Address is the location or address where the contract was mined at. Now that we have instantiated the greeter let us run it by calling “greeter.greet()” and it prints the value that we have set earlier.

It is important to note that the “address” is now stored in the blockchain and accessible by anyone wanting to call that Smartcontract.

What is important to appreciate here is that you have access to the storage, computation across the peers in the network. It essentially also meant that you don’t need to build a distributed environment and instead using the peer nodes storage and computation capacity.

Deleting the Smart Contract

Finally once we are done with the using of our smart contract, we can delete it easily. If we don’t delete the contract, it would be unnecessarily available in the blockchain.

Deleting the contract involves sending a transaction to the network.

Conclusion

In this article, have just touched the very basics of Ethereum and Smart Contracts. The advantage of the Ethereum platform is that the Infrastructure, Identity, Security and Payments are all built into the platform.