Storage.sol smart contract explained in Remix IDE

In this tutorial, we will understand the working of one of the simplest contracts in solidity which is a default contract in Remix IDE.

Above you can see the storage.sol contract. let’s discuss in detail about it.

pragma solidity >=0.4.22 <0.7.0;
At line one we define the version of solidity which we will be using in this contract by using the ‘pragma’ keyword. Here version must be greater than or equal to 0.4.22 and smaller than 0.7.0.
Without defining the version you will get the error in IDE.

/** * @title Storage * @dev Store & retrieve value in a variable */
Comments as usual in every language. (Noting new) 🙂

contract Storage {
In solidity, we define the contract using the ‘contract’ keyword. It is the same as you define a class in java using the ‘class‘ keyword, forwarding contract name i.e. ‘Storage‘ in above contract.

uint256 number;
At line number nine we have defined an unsigned integer type variable which is of 256 bits in size naming ‘number‘.

Besides this, we also have a string, bytes, int, address, bool variables in solidity, and which can be of size 8, 16, 32 … up to 256 bits.

function store(uint256 num) public {
number = num;
Now we define a function named ‘store‘ it takes ‘num’ as a parameter which is of type uint256. This function is a public function i.e. can be accessed publically or by anyone.
Inside the function body, we are assigning the value of num to the global variable (storage variable in case of solidity) ‘number‘.

function retrieve() public view returns (uint256) {
return number;
Here is another function name ‘retrieve‘ which is also a public function. It does not take any parameter as input but returns a uint256 type of value.
Note: uint256 or uint both are similar.
The above function is of ‘view‘ type mean it does not update the state of the contract i.e. it does not update variables defined in the storage space i.e. ‘number‘ in this case, although it’s just returning the value stored in variable ‘number’.
In the body, we just return the number variable as defined in the function’s declaration.

Now, fire your Remix IDE and try to run the contract.

Create your own cryptocurrency

In this tutorial we gonna learn how we can create our own cryptocurrency backed by ethereum blockchain. Basically we will be building a cryptotoken i.e. ERC20 token which behaves like any cryptocurrency and is backed by one. In our case it is ethers.

Before we begin let’s understand what ERC is?
ERC stands for Ethereum request for comments. These are the standards made by creators for ethereum which defines the rules, functions, properties while developing smart contracts on ethereum blockchain. There are many ERC’s such as ERC721, ERC223, ERC777 other than ERC20 ( which is used for building fungible tokens on ethereum) similarly ERC721 is used to build non-fungible assets.

Our main focus for this tutorial is on ERC20 token. So let’s have a look over ERC20 token interface.

Above we can see the interface for our ERC20 token, These functions defined the standard for a basic ERC20 token, containing five functions and two events. These are as follow –

  1. balanceof
  2. transfer
  3. allowance
  4. transferFrom
  5. approve
  6. Transfer (Event)
  7. Approve (Event)

balanceof – This function will provide us the balance/number of tokens owned by the particular address or the owner of that token.

transfer – Transfer function as name suggest is used to transfer your owned tokens to other address. It takes the address of the sender and the amount of token as arguments.

approve – So here comes the most important function which makes many use cases easy to implement in smart contracts. Suppose there are three persons A, B, and C. Now B wants to send the tokens to C. But the problem is that B does not have enough balance in his/her wallet. So he is waiting for his payment which A has to sent as part of their freelancing project.
Now what A does is approve B to spend 100 tokens (which is B’s fee for their freelancing project) on his behalf to anyone B wanted.

create cryptocurrency of your own

Now, A permitted B to spend 100 tokens in his account to anyone B wanted. This is done through approve function.

transferFrom – As A has permitted B to transfer 100 tokens from A’s balance. Now using transferFrom function B can transfer tokens directly to C’s account or He/She can transfer it to his/her account than to C account by calling transfer function.

create cryptocurrency of your own

allowance – Using allowance function anyone can check the allowance provided by someone on their behalf to someone, as in above case A gives to B. We will learn about it further in more details.

Transfer/Approve – Both are events which are triggered when any of the function containing event is called.

Now, Let’s start building our token.

For reference you can see the above contract for ERC20 token in solidity. Now let’s begin.

First of all we have SafeMath class for secure arithmetic operations as defined by openzeppelin.
And then we have defined our interface for ERC20 token as discussed above. After that you can see a contract name Owned, which is usefull to transfer the ownership of contract. We will discuss about this contract in some other tutorial.
Let’s come to our main contract MyERC20Token, which defines all the functions in our ERC20 interface. This is the main contract which makes are cryptotokens and make them act like one.

Have a look to the contract and try to understand whatever you can. Here our MyERC20Token contract is inheriting other three contracts as you can see at line 68.
Now we have defined few variables in our contract listed below.

Variables defined in our SKA token contract.
string public symbol; –> Defines the symbol for our token (SKA)
string public name; –> Defines the name of our token (SKA’s token)
uint8 public decimals; –> Defines the decimals (18) [Max value 18 as 1 eth = 10^18 wei]
uint public _totalSupply; –> Defines the total supply for our token. (10000000000000000000000000)
mapping(address => uint) balances; –> Mapping to store the amount of token for particular address.
mapping(address => mapping(address => uint)) allowed; –> Nested mapping to store the allowance provided by particular address to other addresses.

After this you can see we have define a constructor to initialise the value of all variables while deploying the smart contract.

totalSupply() and balanceOf() are view type functions which provide us the total supplied tokens and the balance of tokens for a particular address.

Below is the transfer function is used to transfer the amount of tokens from sender to receiver. Let’s understand how it works.

transfer() function in MyERC20Token contract
function transfer(address to, uint tokens) public override returns (bool success) {We define our transfer function taking address of receiver and number of tokens to transfer as input and it will return success (true/false) as return value.
require(balances[msg.sender] >= tokens);This will check if the balance in senders account is greater than the tokens he/she wants to transfer and if not it will not initiate a transaction i.e. it will be failed.
balances[msg.sender] = safeSub(balances[msg.sender], tokens);Here we are subtracting the token value from sender’s account. (msg.sender is the address of caller and safeSub is defined in our SafeMath contract.)
balances[to] = safeAdd(balances[to], tokens);After subtracting the token quantity from sender’s account we are crediting it to receivers account by adding the required amount i.e. ‘tokens’ variable.
emit Transfer(msg.sender, to, tokens);Here we are emitting the event which will be used to get response at front-end using web3.js library.
return true;If all well it will return true. (Required only while testing on remix.)

As discussed above we will be using approve function to provide the access of specific amount of our token to other person.

approve() function in MyERC20Token contract.
function approve(address spender, uint tokens) public override returns (bool success) {Approve function will take the address of spender (B) and amount of token for which access is to given. (A is caller of function)
allowed[msg.sender][spender] = tokens;Here nested mapping is acting like 2D array and msg.sender is A and spender is B. (As A needs to give access to B to spend x tokens on behalf on him/her)
emit Approval(msg.sender, spender, tokens);Here we are emitting the event which will be used to get response at front-end using web3.js library.
return true;
} If all well it will return true. (Required only while testing on remix.)

Now let’s come to transferFrom() function which will be used by B to spent x tokens from account A. (assuming A has already approve B to spend x tokens from his/her account.)

transferFrom() function in MyERC20Token contract.
function transferFrom(address from, address to, uint tokens) public override returns (bool success) {It takes from (address of A), to (address of C) and number of tokens i.e. ‘tokens’ as parameters. And here msg.sender will be caller i.e. B.
require(allowed[from][to] >= tokens);If not approved or token amount is larger than approved amount, transaction will not be initiated i.e. it will be failed.
balances[from] = safeSub(balances[from], tokens);Now token amount is subtracted from senders account i.e. A (caller is B).
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);Same amount is deducted from the approval mapping as B already used that amount.
balances[to] = safeAdd(balances[to], tokens);After deducting amount from senders and approval mapping we added the amount to receivers address.
emit Transfer(from, to, tokens);Here we are emitting the event which will be used to get response at front-end using web3.js library.
return true;If all well it will return true. (Required only while testing on remix.)

Let’s come to our last function allowance() which is a simple view type function and it will display the amount of token approved by A to B.

This is all about ERC20 token as of now, try fire your remix IDE and deploy the contract and understand its functionality.

Be Happy and Safe !!!