Solidity Basics part 2

Yechan Jeon·2022년 1월 13일
0

Eth dev

목록 보기
2/2
post-thumbnail

Basic Types

pragma solidity >=0.6.0 <0.9.0;

contract SimpleStorage {
    uint256 fovoriteNumber = 7; // max 256
    bool favoriteBool = false;
    string favoriteString = "String";
    int256  favoriteInt = -5; // max 256
    address favoriteAddress = 0x7449E23a7DBC2c54Dc6cBb376a71968f09857cC9;
    bytes32 favoriteBytes = "cat"; // max 32
}
  1. uint : unsigned interger, you can give bit size to type like 'uint8' , 'uint256'. uint only can handle non-negative interger.
  2. bool
  3. string : utf-8 encoded available
  4. int: can include bit size and negative interger
  5. address : address of blockchain account
  6. byte: convert value to byte, can include byte size

Default initialized

uint favriteNumber; will be initialized by default even if you don't give it a value. (initialized with null value)

Function

  • parameter
    In parameter, you should clarify type of parameter. The common convention of naming parameter is give underscore in front of parameter name
function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }
  • reference type
    By the way, Types such as arrays, structs, mappings, and strings are reference type. It means that if your function changes the value of the variable it recieves, the value of the original variable gets changed.
function store(string memory _name) public {
}
  • public / private
    In Solidity, Function is public by default.
    However, if the function doesn't have any purpose of being exposed to other contracts, you should be use private keyword.
uint[] numbers;

function _addToArray(uint _number) private {
  numbers.push(_number);
}

When you create private function, you should add underscore in front of function name.

  • return
    To return function, add 'returns' keyword and return type in parenthesis. Don't forget if you return reference type, you should add 'memory key word'
string greeting = "What's up dog";

function sayHello() public returns (string memory) {
  return greeting;
}
  • view / pure keyword
    with view keyword, function only working with reading
    function sayHello() public view returns (string memory) {}
    with pure keyword, Not accesssing any data in the app, only depends on parameter
function _multiply(uint a, uint b) private pure returns (uint) {
  return a * b;
}

Structs Types

struct Person {
	unit age;
  	string name;
}

For more complex data type, you can use 'struct' like object in javascript

  • Add new Structs instance
    Refers to above struct 'people', it will be like this:
Person[] people;
 
Person buterin = Person(27, 'Vitalik');

Array Types

// Array with a fixed length of 2 elements:
uint[2] fixedArray;
// another fixed Array, can contain 5 strings:
string[5] stringArray;
// a dynamic Array - has no fixed size, can keep growing:
uint[] dynamicArray;
  • types of array
    There are two types of array in solidity. Fixed and dynamic.
    If you want dynamic, just give empty square brackets.
    If you want fixed, give square brackets that includes max item of array.

  • Define array in struct
    You also can create array of Struct

Person[] people; //dynamic array

State variable are stored in blockchain permanently , so it is useful when you keep a variable like balance
If you want to expose data of arrays to other contracts, use public keyword
Person[] public people;

  • Add item to array
    To add item, use push() method('push' adds item at the end of array)
Person[] people;
 
Person buterin = Person(27, 'Vitalik');
people.push(buterin);

or
people.push(Person(27, 'Vitalik'))

Keccak256 and Typecasting

  1. keccak256
    This is built-in hash function for Ethereum. This maps an input to a random 256-bit hexadecimal number.
    !important, keccak256 expects a single parameter of type bytes. So you should pack any parameters before calling
    keccak256(abi.encodePacked("value"));
    with above execution, "value" -> 81afeeaff0ed5cee7d05a21078399c2f56226b0cd5657062500cef4c4e736f85

  2. Typecasting
    If you trying to operate between different type, you should cast type one of those variables.

uint8 a = 5;
uint b = 6;
uint8 c = a * uint8(b); // because b is 'uint' type, can't process calculation with 'uint8' type.

Events

Events is quite a similar concept with javascript event.
They are way for your contract to communicate that something happened on the blockchain to your front-end.
Listener can listen what events send.

// declare the event
event IntegersAdded(uint x, uint y, uint result);

function add(uint _x, uint _y) public returns (uint) {
  uint result = _x + _y;
  // fire an event to let the app know the function was called:
  emit IntegersAdded(_x, _y, result);
  return result;
}
profile
방황했습니다. 다시 웹 개발 하려고요!

0개의 댓글