You are currently viewing Solidity Heredity Explained: How to use Solidity Best!

Solidity Heredity Explained: How to use Solidity Best!


Back to New at Nirolution Solidity Tutorial Blog

Solidity Heredity Simply Explained

Now we have created a simple Smart Contract with a few get and set functions. Now we can extend our simple contract by inheritance. This will give us a general extension of functionality from one contract to the other. With Solidity Heredity, we don’t have to define any additional functions in our own contract, we can simply access functions from other Smart Contracts.

“Solidity Heredity Simply Explained” – Content:

  1. Solidity Heredity Simply Explained
  2. Create a Bank Account
  3. Create a Deposit Function
  4. Create a withdraw function
  5. Create a balance function
  6. Create a check value function
  7. Create a plausibility check
  8. Our contracts inherit from Bank

Solidity Heredity Simply Explained

We now want to inherit “MyFirstContract” from our first contract. This will make it the so-called upper class. All classes or contracts inherited from it will automatically become its subclass. The subclasses inherit all methods, properties and fields from the superclass without having to create them again.

With solidity heredity we do not have to define any additional functions in our own contract, we can simply access functions with of the superclasses.

How does it all work now?

Based on our simple Smart Contract “MyFirstContract” we now create a new contract in Solidity called “Bank”. So that our “MyFirstContract” contract can inherit the functions from “Bank”, we need the “is” function. This will look like this:

pragma solidity ^0.4.0;

contract MyFirstContract is Bank {

}

Create a Bank Account

Now we have to create our Smart Contract “Bank”. This works like our bank account. Depending on the amount of money deposited, our bank account has a certain value, a possibility to make deposits, and we should be able to withdraw the money.

First we create the Smart Contract “Bank” with an indefinite value. For this indefinite value we create a new private field of the type “uint” and call it “_value”. We use this data type because we only want positive values and it would not make sense to deposit a negative amount. Such cases can also be intercepted with the help of a plausibility check.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;

}

Create a Deposit Function

After that, we need a function to deposit money into the account. The function now gets a parameter of type “unit” from the outside (i.e. by an input from the console), which we declare with “amount”. The following will happen: If you now want to deposit money, the function “Deposit” gets a certain value from the outside. Within this function we now perform the following calculation.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;

  function deposit(unit amount) {

  _value += amount;
  }
}

The value of the bank account is the sum of all deposits we have made. Therefore, we use “value+=amount;”.

Create a withdraw function

Now we have to be able to withdraw money. Therefore we write a “withdraw” function.

pragma solidity ^0.4.0;

contract Bank {
  
uint private _value;

  function deposit(unit amount) {

  _value += amount;
  }

  function withdraw(unit amount) {

  _value -= amount;
  }
}

This function actually works exactly like our “deposit” function, but we deduct the paid amount from our account within the function.

The value of the account is reduced by the value “amount”.

Create a balance function

Now we still need a function to be able to get our current account balance. Therefore, we create the function “Balance” which should return us an integer value. Here we do not use the data type “uint”, because with our credit card we are able to get negative values.

This function is created as follows: First we write the keyword “function”, because it is a function. Then we give the function a name, in our case “Balance”. Immediately after that there are 2 empty round brackets. If we still had parameters, which the function would get from outside (see function “Deposit” or “Withdraw”), we would have to define them within the round brackets. Finally there is the return of the function, which is done with the keyword “returns” and then the data type that we want to be returned.

Within the function we simply return the value “value” calculated in our above functions.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;

  function deposit(unit amount) {
  
  _value += amount;
  }

  function withdraw(unit amount) {
  
  _value -= amount;
  }

  function balance() returns(unit) {
  
  return _value;
  }
}

The function should show us the current value of our bank account.

Create a check value function

So that nobody withdraws money, which he does not have, we must create a function at the end that checks it. Therefore, we write a function “CheckValue” which should return us a “Boolean”. This data type has two states, true or false.

The function has the same structure as our “Balance” function, but this time we get a Boolean back.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;

  function deposit(unit amount) {
  
  _value += amount;
  }

  function withdraw(unit amount) {
  
  _value -= amount;
  }

  function balance() returns(unit) {
 
  return _value;
  }

  function checkValue(uint amount) returns(bool) {
 
  return _value >= amount;
  }
}

Create a plausibility check

Now we have to change our “Withdraw” function so that you can only withdraw money if you have enough in your account. We create a plausibility check for this. These always work according to the same principle. Since we only want to perform a function if all checks were successful, we always write our check at the beginning of a function. With the help of an “if-Statement” we can write our function “CheckValue” in parentheses after the “if”. This function now gets the amount we want to withdraw as parameter, so the value in “amount” is passed to the function. If the check is OK, a “true” is returned and we process the code within the “If-Statement”. Alternatively “CheckValue(amount) == true” would also work. However, if the function returns “false”, the calculation would not be executed and the bank account would remain untouched.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;

  function deposit(unit amount) {
  
  _value += amount;
  }

  function withdraw(unit amount) {
  
    if (checkValue(amount)) {
    
    _value -= amount;
    }
  }  

  function balance() returns(unit) {
  
  return _value;
  }

  function checkValue(uint amount) returns(bool) {
  
  return _value >= amount;
  }
}

Our contracts inherit from Bank

Our virtual bank account is now ready. We can now deposit and withdraw money. Since our Smart Contract “MyFirstContract” inherits from “Bank”, it now also has the functions “deposit”, “withdraw” and “balance”.

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