Solidity Tutorials 1 "Contract introduces"

What's Solidity?

Solidity is a language if high leveled with it's syntax similar to that of JavaScript and designed to compile to code for the Ethereum Virtual Machine.
In this tutorial, you will learn the basic introduction to solidity, and also gain some knowledge of the Ethereum Virtual Machine in addition to its programming.
It is save to say that you can start using Solidity in your browser without any need to download anything.

The application supports complication.
Running the code or injecting into the blockchain, you will have to use a client like AlethZero.

Some Examples

Contract SimpleStorage

    uint storedData;  
    function set(uint x) {
        storedData = x; 
        }
        
        function get() constant returns (uint retVal) {
            return storedData;
            } 

unit storedData declares a state variable called storedData of type
uint (unsigned integer of 256 bits) whose position in storage is automatically allocated by the compiler.
The functions set and get can be used to modify or retrieve the value of the variable.

Sub currency Example

contract Coin {
    address minter;
    mapping (address => uint) balances;

    function Coin() { 
        minter = msg.sender;
    }

    function mint(address owner, uint amount) {
        if (msg.sender != minter) return;
        balances[owner] += amount;
        }

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

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

This contract introduces some new concepts.
One of them is the address type, which is a 160 bit value that does not allow any arithmetic operations.
Furthermore, the state variable balance is of a complex datatype that maps addresses to unsigned integers.

Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros.

The special function Coin is the constructor which is run during creation of the contract and cannot be called afterwards.

It permanently stores the address of the person creating the contract: Together with tx and block, msg is a magic global variable that contains some properties which allow access to the world outside of the contract.

The function queryBalance is declared constant and thus is not allowed to modify the state of the contract (note that this is not yet enforced, though).

In Solidity, return "parameters" are named and essentially create a local variable. So to return the balance, we could also just use balance = balances[addr]; without any return statement.

Next Lesson

Summary of Lessons

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?