Solidity Smart Contract Example: Simply Explained

Solidity Smart Contract Example: Simply Explained


Back to New at Nirolution Solidity Tutorial Blog

Solidity Smart Contract Example: Simply Explained

Solidity is a contract-oriented programming language for Ethereum. It is an interesting and exciting programming language that is becoming more and more popular. In this article, we will show you how to create a solidity Smart Contract.

At the beginning, we have to tell Solidity that we want to use it and of course which version we need.

“Solidity Smart Contract Example: Simply Explained” – Content:

  1. Solidity Smart Contract Example
  2. Name your Smart Contract
  3. Set Datatypes
  4. Create a Set and Get Function
  5. Create a Plausibility Check

Solidity Smart Contract Example

For this, we type in “pragma solidity” at the beginning and then the version we want to use.

pragma solidity ^0.4.0;

After that, we define a contract of type “Contract” and give him a name. Then we have to give our contract a name. Here you can choose any name you want. In our case our contract is called “MyFirstContract”.

Please note that we want to stick to the correct notations when creating variables, classes, methods, etc. right from the start. This means we distinguish between Camel Case and Pascal Case. PascalCase means that if we give our variables names, we start each word with a capital letter. This is only true for class names (e.g. our contract) and methods. As an example we can take our contract MyFirstContract, where each new word starts with an uppercase letter (another example would be ThatIsAnExample). In camelCase, each name begins with a small letter. However, this is only used for names for variables (e.g. uint) and arguments of methods. As example “uint aVariableDeclare”)

Name your Smart Contract

Then we have to determine what we want to include in our Smart Contract. So, for our solidity smart contract example we first need to add the name to our contract. For this, we can simply use “string name;” as in Java.

pragma solidity ^0.4.0;

contract MyFirstContract {

string name;
}

Set Datatypes

You can set this variable to private by modifying the access modifier “private”. This is not set at the beginning of the variable like with Java, but between the data type and the name of the variable. This will look like the following:

pragma solidity ^0.4.0;

contract MyFirstContract {

string private _name;
}

The convention for naming our private variables will have an underscore “_” right before the actual name, written in lowercase.

Furthermore, we need an age for our Smart Contract. For this, we used “uint private _age”. The syntax looks like this:

pragma solidity ^0.4.0;
 
contract MyFirstContract {

string private _name;
uint private _age;
}

Create a Set and Get Function

Because now all variables are private, they don’t have a real function yet. Therefore, we create a set and get function for “_name” and “_age”. Our two fields will be now called ‘property’. This means that when we later talk about properties, we mean fields with either a set or a get- function (or natural with both). But what do we achieve now? Now with the help of a get-function we can access our variable from the outside and for example display it on our screen.

However, this only refers to the output and not to the assignment. Now the set function comes into play. Over this we can get access to our properties. With certain plausibility checks, we can intercept impermissible values beforehand. For example our property “_age” may not be smaller than 0 or our string “_name” must contain at least x characters.

We can do this the same way as in JavaSkript. Therefore, we simply enter “function” into the console. Then the keyword “set” appears followed by the property we want to change, in our case “Name”. Then we have to declare a parameter within parentheses, just like with a method. This entered value (string) is then stored by the console. So in our case we create another variable named “newName” of the same type as “name” (Attention: use camelCase here). This is then assigned to our private variable within our set function.

This will look like this:

pragma solidity ^0.4.0;

contract MyFirstContract {

string private _name;
uint private _age;

  function setName(string newName) {
  
  _name = newName;
  }
}

So everything we type into the console will change the name of our contract because of the function “setName”.

So that we can now display the name on the screen, we write a get-function for it. This works like the set function. Therefor we write a get „followed by“ name. The only difference here is that we only have a return, because we only want to get the value displayed.

pragma solidity ^0.4.0;

contract MyFirstContract {

string private _name;
uint private _age;

  function setName(string newName) {
  
  _name = newName;
  }

  function getName() returns (string) {
  
  return _name;
  }
}

Now we do the whole thing for the variable Age. This should look like this:

pragma solidity ^0.4.0;

contract MyFirstContract {

string private _name;
uint private _age;

  function setName(string newName) {

  _name = newName;
  }

  function getName() returns (string) {

  return _name;
  }

  function setAge(unit newAge) {

  _age= newAge;
  }

  function getAge() returns (unit) {

  return _age;
  }
}

Create a Plausibility Check

Finally, we can add a plausibility check to the two SET functions. For the age we naively say that the input have to be higher than 0. For the name we want an input with at least one character and that it should be unequal to zero.

Now we have our two methods and can easily test if our simple Smart Contract works. We just have to press “Create” and if we have done everything right, we can enter the name as well as the age. It is important that you use quotation marks when entering strings.

You have now created your first very simple Smart Contract in Solidity.

If you want to be informed about the latest updates, follow us on Facebook, Pinterest and Steemit.

Stay current on your favorite topics

Leave a Reply

Close Menu