SimpleStorage

Important

The below exercises will be completed within REMIX. Navigate to: https://remix.ethereum.org

Video Tutorial

1 Define the compiler version, line 1

pragma solidity 0.4.24;

2 Create the SimpleStorage contract, line 3

contract SimpleStorage {}

3 Compile and deploy, view the deloyed contract instance within Remix

4 Add a first storage variable, storedData, line 4

uint256 storedData;

5 Compile and deploy, view the deloyed contract instance

Note

Is the storage variable, storedData, available in the interface?

6 Update the storage variable’s visibility to public, line 4

uint256 public storedData;

7 Compile and deploy, view deloyed contract instance

Note

Is the storage variable, storedData, available in the interface now?

Important

Note the changes made between 4 and 7 and the impact of the visibility modification.

  • The difference between default(internal) visibility and public visibility.

8 Create the SimpleStorage contract’s first function to set the value of the storage variable, line 6-8

function set(uint256 x) {
    storedData = x;
}

9 Compile and deploy the contract again, test the set function

  • Read storedData
  • Call set to update the value of storedData, note default visibility
  • Read storedData, did the value change successfully?
  • Expand the transactional data within the evm console and investigate

10 Change the visibility of storedData to private, line 4

uint256 private storedData;

Note

Storage variable is no longer accessible, let’s write a function to fix that!

11 Create a function to get the value of storedData, line 10-12

function get() returns (uint256) {
    return storedData;
}

12 Compile and deploy, test the get function

Note

Could you get the value of storedData? What did the get function return? Was gas consumed? Was a transaction sent? Or a call?

13 Update the get function’s mutability, line 10

function get() view returns (uint256) {
    return storedData;
}

14 Compile and deploy, test the set and get functions

  • Get the initial value, what was returned this time? a transaction or a call?
  • Set the value
  • View it has changed
  • Investigate the evm console transactional details along the way

The final solution may be found here

Important

All done? We recommend reviewing the complementary video series found here.