Learn Solidity: Basics of Solidity By Example

In this post, we will learn the Solidity language by going through two example. Then we will dig deeper into each & every aspect of Solidity & Blockchain-based development.

World is looking for a Certified Blockchain Expert Become one

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
A New Crypto Coin in Solidity
pragma solidity 0.4.8;

contract Coin {
* @title A Simple Subcurrency Example
* @author Toshendra Sharma
* @notice Example for the Solidity Course
* @dev This is only for demo the simple Coin example
address public minter;
uint public totalCoins;

event LogCoinsMinted(address deliveredTo, uint amount);
event LogCoinsSent(address sentTo, uint amount);

mapping (address => uint) balances;
function Coin(uint initialCoins) {
minter = msg.sender;
totalCoins = initialCoins;
balances[minter] = initialCoins;

/// @notice Mint the coins
/// @dev This does not return any value
/// @param owner address of the coin owner, amount amount of coins to be delivered to owner
/// @return Nothing
function mint(address owner, uint amount) {
if (msg.sender != minter) return;
balances[owner] += amount;
totalCoins += amount;
LogCoinsMinted(owner, amount);

function send(address receiver, uint amount) {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
LogCoinsSent(receiver, amount);

function queryBalance(address addr) constant returns (uint balance) {
return balances[addr];

function killCoin() returns (bool status) {
if (msg.sender != minter) throw;

As you can see in the above code, which is a simple smart contract written in Solidity for a new Coin. Here this Coin can be used to trace any digital asset which has a value.

Let’s go through the contract and understand it properly.

In line number 1 we have written the pragma which tells the compiler that this contract is written for solidity compiler version 0.4.8 or older. So if your compiler version is 0.4.9 this contract will through an error.

Then we have created a contract, specified the developer related comments, defined two public state variables minter & totalCoins of address & uint256 type respectively.

We then declared two events on line 14 & 15. Events will be used to inform the monitoring entities or wallets.

created a mapping aka array where address type will be the key & uint will be the value. We then created a constructor which will be called while initializing the contract. Then we defined the functions for the currency related operations & at last in line number 46, we created a kill function which will destroy the contract only if it is called by the minter (kind of admin).

You can then execute this contract as per the instruction in this article.

In this contract, we simply created a digital asset called “Coins” which is initialized with an initial supply. Here the executor will become the minter/admin. Then we wrote the function to send the Coins to an address & also to query the balance of an address. We have also written the function to create the new coins out of this air using the mint function.

Solidity follows the syntax of JavaScript hence most of the syntax is similar except at few places.

Next: Layout of the Solidity Source File