Solidity

Solidity Example Basic Smart Contract

Solidity Example Basic Smart Contract Main Tips

  • In this tutorial, we will go over a basic example of a smart contract made using Solidity.
  • To try out the code shown in this tutorial, it is suggested to use Remix.
  • It’s ok if you do not fully understand the example, as it will be explained in detail as we go on.

Solidity Example Basic Smart Contract

A contract in terms of Solidity is a data, that can be referred to as its state, and code, which can be referred to as its functions, collection, that resides on a specific address in the Ethereum blockchain.

Let’s try to create a simple contract that creates, sets the value for, and returns a variable called storageData.

Example

pragma solidity ^0.4.0;

contract SampleContract {
    uint storageData;

    function set(uint x) {
        storageData = x;
    }

    function get() constant returns (uint) {
        return storageData;
    }
}

 

Try on Remix Try live on Hosting

Let’s see what this code does in detail.


Solidity Example Basic Smart Contract Pragmas

The first line of code sets a pragma.

pragma is a keyword, which is used to instruct the compiler how the source code should be treated.

These instructions are what we refer to as pragmas.

In this case, the pragma specified that the source code is written for the Solidity version 0.4.0 and above (if it does not break functionality).


Solidity Example Basic Smart Contract Declare Variable

The first line inside the contract itself declares an unsigned integer (unsigned 256 bit integer) variable called storageData.

To specify that it will be an unsigned variable, we use the keyword uint before the name of the new variable.

This integer can be seen as a database slot, which is used by calling the appropriate functions to set and return the value.

In Ethereum’s case, this is always the owning contract.

Note: In Solidity, all identifiers are restricted to ASCII character set. The string values, however, can contain UTF-8 characters.

 

Warning: be careful with Unicode characters, since similarly looking characters may have different code points, resulting in them being encoded as different byte arrays.


Solidity Example Basic Smart Contract Set And Get

To modify and return the variable value, you will need to use the functions set and get.

To use either of these, you need to specify the type and name of the variable you want to use between parentheses (in this case it’s uint storageData).

In the case of set, next up is simply calling the name of the variable inside the functions and assigning a value.

In the case of get, you simply need to write the keyword return before your variable name.

The problem is though, that in this case just about anyone could call set again and set another value.

Luckily, the number is recorded into the Ethereum history for all time.

We will see how to impose restrictions in later tutorials.

Read previous post:
Chrome DevTools Elements

Chrome DevTools Elements Main Tips The Elements panel of the Chrome Developer Tools is used to inspect and modify the DOM and CSS. This part of...

Close