Skip to main content

Solidity

Can be run using Remix, a browser-based IDE.

Code

Basics

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.7;

contract SimpleStorage {
//public automatically creates getter function for you
uint256 favNumber = 69;
People public person = People({favouriteNumber: 2, name: "Gavin"});

// mapping
mapping(string => uint256) public nameToFavouriteNumber;

// People type
struct People {
uint256 favouriteNumber;
string name;
}

// People Array, called people - dynamic array
People[] public people;

// People[3] public people - Max of 3 in the array

// can create your own getter function
function retrieve() public view returns(uint256){
return favNumber;
}

// view & pure functions don't require gas (pure functions don't allow viewing/ editing)
// unless you call it within a function that requires gas
// pure
function purefn() public view returns(uint256){
return favNumber;
}

function store(uint256 _favNumber) public {
favNumber = _favNumber;
}

function addPerson(string memory _name, uint256 _favouriteNumber) public {
People memory newPerson = People({favouriteNumber: _favouriteNumber, name: _name});
people.push(newPerson);

nameToFavouriteNumber[_name] = _favouriteNumber;
}
}


EVM Overview

EVM (Eth virtual machine) can access and store information in 6 different places.

For special types (Array, Struct and Mapping), must declare explicitly.

  1. Stack
  2. Memory - temp vars that can be modified
  3. Storage - permanent & can be modified
  4. Calldata - temp vars that cannot be modified
  5. Code
  6. Logs