fbpx

A Blockchain and A.I First Innovation Hub.

3rd Floor, Plaza 2000, Mombasa Rd.
+254 716 490 808
[email protected]
+254 716 490 808
3rd Floor, Plaza 2000, Mombasa Rd
[email protected]
Solidity-blockchain

Solidity Blockchain Coding: The Many different Languages You Need!

Finally, we come to Solidity. For anyone who wants learn how to make DAPPs (Decentralized Applications) or get into the ICO game, learning Solidity is an absolute must. Here we are going to give you a basic overview. Solidity was developed by Gavin Wood, Christian Reitwiessner, Alex Beregszaszi, Yoichi Hirai and several former Ethereum core contributors to enable writing smart contracts on blockchain platforms such as Ethereum.

Solidity is a purposefully slimmed down, loosely-typed language with a syntax very similar to ECMAScript (Javascript). There are some key points to remember from the Ethereum Design Rationale document, namely that we are working within a stack-and-memory model with a 32-byte instruction word size, the EVM (Ethereum Virtual Machine) gives us access to the program “stack” which is like a register space where we can also stick memory addresses to make the Program Counter loop/jump (for sequential program control), an expandable temporary “memory” and a more permanent “storage” which is actually written into the permanent blockchain, and most importantly, the EVM requires total determinism within the smart contracts.

So, before we continue, let’s checkout a basic Solidity contract example. (Codes taken from github).

 

Let’s run a simple while loop in solidity:

contract BasicIterator 

{ 

address creator; // reserve one "address"-type spot 

uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array 

function BasicIterator() 

{ 

creator = msg.sender;

uint8 x = 0; 

//Section 1: Assigning values

while(x < integers.length) { 

integers[x] = x;  

x++; 

} } 

function getSum() constant returns (uint) { 

uint8 sum = 0; 

uint8 x = 0; 


//Section 2: Adding the integers in an array.

while(x < integers.length) { 
sum = sum + integers[x];

 x++; 
} 

return sum; 

} 

// Section 3: Killing the contract

function kill() 

{ 

if (msg.sender == creator) 

{ 

suicide(creator); 

}

}

}

So, let’s analyse.

Section 1: Assigning Values

In the first step we are filling up an array called “integers” which takes in 10 8-bit unsigned integers.  The way we are doing it is via a while loop. Let’s look at what is happening inside the while loop.

while(x < integers.length) {

integers[x] = x;

x++;

}

Remember, we have already assigned a value of “0” to the integer x. The while loop goes from 0 to integers.length. Integers.length is a function which returns the max capacity of the array. So, if we decided that an array will have 10 integers, arrayname.length will return a value of 10. In the loop above, the value of x goes from 0 – 9 (<10) and assigns the value of itself to the integers array as well. So, at the end of the loop, integers will have the following value:

 

0,1,2,3,4,5,6,7,8,9.

 

Section 2: Adding the array content

Inside the getSum() function we are going to add up the contents of the array itself. The way are going to do it is by repeating the same while loop as above and using the variable “sum” to add the contents of the array.

 

Section 3: Killing the contract

This function kills the contract and sends the remaining funds in the contract back to the contract creator.

When asked about what was the inspiration and motivation behind creating solidity, Dr. Gavin Woods said this:

 

“It [Solidity] was meant to be a sophisticated tool for developing contracts that could ultimately give both developers and users good information on what the code did. To help this along, I devised NatSpec, a contract-friendly documentation format, and made that a first-class citizen in Solidity. I also proposed a formal proofing language subset (not yet implemented) in order to maximise the kinds of correctness guarantees that could be made.

 

I introduced events as a first class citizen into the Solidity language in order to provide a nice abstraction for LOGs similar in form to function calls. Inspiration for that came from the Qt meta-object system’s “signals”.

One later feature that Christian R. and I figured out together was function modifiers; that allows attributes placed as part of a function signature to make some modifications to the apparent function body. Being a very declarative means of expression, it’s an idiom that falls nicely into the contract-oriented programming space.”

 

Blockchain Coding: Conclusion

In this and previous articles we have only covered 4 languages for blockchain coding that are used in developing in and around the blockchain. In reality there are many many more languages that you can potentially use (Java, Go). If you are a programmer, then the possibilities for you are truly endless. As the world becomes more and more decentralized and blockchain becomes more and more mainstream, the future for you is definitely limitless.