Trying out Solidity (including 2 sample blockchain app tutorial)

in #blockchain6 years ago (edited)

This is my review on 4 hours on trying out solidity.

Disclaimer: This is just what I learn from Solidity in 4 hours, so some of the information might not be the best practise of the blockchain development

Solidity is a language that is being build on top of Ethereum Virtual Machine, where you can run your smart contract on top of Ethereum Network. I would say that this language is a static typing language, that need to be compiled into binary for it to work.

Solidity is inspired by C++ and JavaScript, the syntax are really similar to C family. I personally would say that Solidity is a higher level language because they encapsulate a lot of heavy lifting on making a blockchain app.

download.png
Solidity Image source

Here is an example of a simple blockchain app that I have made. (Tutorial is on solidity doc website)

Setup of project

Easiest way to start solidity is on Remix, an online IDE for you to play around Solidity.

image.png

I am using the school pc to try out all these.

The first program

A basic app to save a single data.

Codes

pragma solidity ^0.4.21;
contract hello {
    uint storedData;
    function set(uint d) public {
        storedData = d;
    }
    function get() public constant returns (uint) {
        return storedData;
    }
}

For solidity, the first line is to state which version of it.

contract hello {

Contract are just like a class in Object Oriented Programming.

uint storedData;

Create an unsigned interger variable called storedData.

    function set(uint d) public {
        storedData = d;
    }

this is a setter function just to save storedData

    function get() public constant returns (uint) {
        return storedData;
    }

this is a getter function just to get the storedData

Test the smart contract

Once the codes is done, what you need to do is to compile it. (Click on Compile), and in the Run tap, you can try out your smart contract.

In the run tab, there is 2 option, which are the function you had created previously. (since both function are declared as public, it is accessible outside)

image.png

put a value into set column, and then you can expect when you click on get, you will get the value.

Viola, you made a blockchain app!

image.png

All changes in the storedData variable is recorded into the blockchain, which can be shown on the terminal below. Therefore, all the changes that have made to the blockchain app, the data are immutable (meaning the data cannot be reversed).

Second app

pragma solidity ^0.4.21;
contract Coin {
    address public minter;
    mapping (address => uint) public balances;
    event Sent(address from, address to, uint amount);
    constructor() public {
        minter = msg.sender;
    }
    function mint(address receiver, uint amount) public {
        if(msg.sender != minter) return;
        balances[receiver] += amount;
    }
    function send(address receiver, uint amount) public {
        if(balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

In this blockchain app, what it did is that there is 4 function.

image.png

  • mint - is to add value to balance
  • send - send balance from current address to another address
  • balances - to check current balance of an address
  • minter - to get your current address

The code

    address public minter;
    mapping (address => uint) public balances;

The variable minter has a new type called address. address type is a 160-bit value which is suitable for storing addresses of contracts (which contains of alphabets and numbers). [1]
Since minter is public, it can be access outside of the contract.

For mapping, it is a data type in solidity, which is like a hash map in Ruby, when you pass in an address (probably minter), it will return you an unsigned integer.

    event Sent(address from, address to, uint amount);

In solidity, when an event is fired, it looks like this emit Sent(msg.sender, receiver, amount);. So, based on JavaScript context, it is just like an event that you setup, and you can make an event listener on the front end to indicate that that event has happen.

    constructor() public {
        minter = msg.sender;
    }

This constructor setup minter to be the sender's address.

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

As I said previously, mint means adding value to the balance.

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

For send function, it sends balance from sender to receiver. So the sender will have decrease in the amount, and the receiver will have increase in amount. The emit event is fired.

Test the smart contract

Same as previous, compiled the project and start to play with the Smart Contract.

  1. Try to use minter to get your address.
  2. Pasted your address into minter, and set the amount of balance you want to add in. [0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 12]
  3. Try to transfer the balance to another address.
  4. Check balance on the other address.

Done, you had done your first working cryptocurrency!

with 19 lines of codes, you had make a useable blockchain app!

Final thoughts

I made a blockchain app from scratch and it took me a lot of time setting up 4 classes (namely: Transaction, Block, Blockchain, P2pServer), which is a slightly tedious process.

Solidity and Ethereum solved these tedious work on setting up the Blockchain project, all you need to do is to write the Smart Contract to make it work. I guess it would save me more than a day to create similar feature from scratch.

In case you curious about building blockchain app from scratch, I am working on a video series of creating a Blockchain from scratch (Proof of Work)

current progress: 4/6

References

[1] http://solidity.readthedocs.io/en/v0.4.21/introduction-to-smart-contracts.html

Sort:  

恭喜你!您的这篇文章入选 @justyy 今日 (2018-04-18) 榜单 【优秀被错过的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。

Congratulations! This post has been selected by @justyy as today's (2018-04-18) 【Good Posts You May Miss】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^

Are you sure you take the right course of degree? It's seem you more suitable to become an expert in multiple programming languages.

Languages that I used

Low level: PIC assembly, C
High Level: C++, Java, JavaScript (TypeScript), Elixir, Swift, Go, MatLab, Python, Ruby

But I'm most comfortable with JavaScript, cuz it gets my job done most of the time. If solving algorithm, i prefer elixir.

Hello @superoo7 , I was designed to give advice to "steemit" users.

I recommend to increase this;

The most winning bid bot in the last 24 hours is ✅ "appreciator"

You can enter "steembottracker.com" to find more offers.

You can make "Resteem" and advertise to the followers of the whale accounts.

"Resteem Bot" for you;

@byresteem has 25.500 Followers + 7000 Sp + Upvote with min +55 accounts.

I am a bot, I can not answer the comment. I hope I could help. Good luck.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by superoo7 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.029
BTC 64133.95
ETH 3172.67
USDT 1.00
SBD 2.57