(Part 1) Ethereum Solidity Development - Getting Started + Lower Level Explanation (PT 1)

in #utopian-io6 years ago

Repository

https://github.com/igormuba/EthereumSolidityClass/tree/master

What Will I Learn?

  • Ethereum variables and functions
  • Lower level memory vs storage vs stack
  • Deploy on a test environment (free)

Requirements

  • Browser
  • Visual Studio Code

Difficulty

  • Basic

Tutorial Contents

Setting up the environment for the whole series

First, you need to setup your development environment.

Think about long term, where I want to get with this course, I recommend you to use the metamask browser add-on.
You can get it here
https://metamask.io/

Also, I use Visual Studio code because it has many handy plugins, but you can also use Ethereum's browser IDE.

To get Visual Studio Code go here
https://code.visualstudio.com/
And add to it an addon called Solidity. To do so, after you install and open the Visual Studio Code go to the tab extensions (CTRL+SHIFT+X), find the extension called solidity which is the most widely used and install it

To use the browser IDE, that should also work fine, just go to
http://remix.ethereum.org/

Solidity version

It is highly recommended that when you are developing for the Ethereum virtual machine you set the solidity version you want to use.
If you are not sure what is the most recent version, when you open the online IDE (Remix) you will see on the sample code what is the most recent version

Basic sintax

Solidity is very similar to Javascript, with a few tweaks.

To declare variables you must cast the type of the variable and it's scope followed by the variable name.
For example

string private stringVarible = "Variable name";

For setting integer variables you must explicitly tell the virtual machine if the variable is signed or unsigned, unsigned means it can only be positive (can't have negative sign).
For example

uint private unsignedInteger = 10;

Setters and Getters

I am using specifically Solidity 5 in this tutorial to highlight a few substantial changes. Most tutorials you will find on the internet might cover an earlier version of the language, if you try to code like they teach, you will probably face a few errors and you will be stuck on an earlier syntax that, though easier to write and read, gives you less power.

The importance of using the most up to date syntax is that you can LITERALLY SAVE MONEY as the developers are always working on making the language less resource intensive, because computer resources from the EVM (Ethereum Virtual Machine) literally cost money (Ethereum) to execute.

Don't worry though, we will be using a development environment that is free and even shows you an estimative of costs so when you develop your contracts you can try and see what data structures are the cheapest.

Starting from Solidity 5 onwards you must make the location of the variables explicit, refer to the change doc at
https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html

There is a very in depth guide of how and when to use it here
https://medium.com/coinmonks/ethereum-solidity-memory-vs-storage-which-to-use-in-local-functions-72b593c3703a

But to sum it up: storage variables are permanent and CAN NOT BE CRATED INSIDE FUNCTIONS, so in functions when we create a variable we are using memory variables.

So a simple setter for the string variable would be

function setStringVariable(string memory newVariable) public {
        stringVariable = newVariable;
    }

The first keyword is the same as JavaScript and then we give the name of the function, then as an argument for the setter we give the type string and we must make it explicit that it is a memory variable and is called newVariable, and then we say it is public.

A getter would look like

function getStringVariable() public view returns (string memory){
        return stringVariable;
    }

Again, we declare the first two words as if JavaScript but we must explicitly tell later that it is a public function that creates a view that is returned from the Ethereum Virtual Machine, and that it returns especifically a string that is in memory

Where this stuff goes

Before we continue, you must be aware that all variables and functions must be inside a contract. Ethereum Viirtual Machine deal with everything as contracts, so let us wrap our varaibles and functions inside a contract and name it

pragma solidity ^0.5.0;


contract firstClassContract{
    string private stringVariable = "Variable name";
    int private unsignedInteger = 10;

    function setStringVariable(string memory newVariable) public {
        stringVariable = newVariable;
    }

    function getStringVariable() public view returns (string memory){
        return stringVariable;
    }

}

Why do I need to tell it explicitly that it is a memory variable if I know that a function can't create a storage variable?

Because there is a third place in the Ethereum Virtual Machine where variables can be stored, it is the stack, it is the cheapest storage but is the smallest of the 3. You want to use it when you know for sure the size of your variable.

You can read a more in depth look at data sizes in
https://blog.zeppelin.solutions/ethereum-in-depth-part-2-6339cf6bddb9

But in short, stack items have a size of 256bits

This is more lower level stuff but I highly recommend you to read about lower level Ethereum development. In my tutorials I will cover lower level stuff the most I can but I might miss something (I was never good at computer architecture and operating systems at college)

How to deploy and test (FOR FREE)

I have been writing so far with the Visual Studio code for systax highlight (makes development much easier), the code looks like

Here is how to test it on Remix

Paste the code inside the browser IDE

On the right side of the screen click on Start to compile

Set the environment to JavaScript VM and click on the pink button that says deploy

If you don't do the above you will automatically use the Ethereum blockchain and will have to spend Ether to pay for real transactions.

Then below we can see and interact with the deployed contracts

Sort:  

Thank you for your contribution @igormuba.
We have been analyzing your tutorial and we suggest the following points:

  • We suggest that you use comments in the sections of your code. Comments are very important for readers to understand well what you are developing.

This tutorial is very interesting thanks for your work in constructing this tutorial.

Happy New Year :)

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @igormuba!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @igormuba!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.030
BTC 60115.50
ETH 3192.77
USDT 1.00
SBD 2.45