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
}
uint favriteNumber;
will be initialized by default even if you don't give it a value. (initialized with null value)
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function store(string memory _name) public {
}
uint[] numbers;
function _addToArray(uint _number) private {
numbers.push(_number);
}
When you create private function, you should add underscore in front of function name.
string greeting = "What's up dog";
function sayHello() public returns (string memory) {
return greeting;
}
function sayHello() public view returns (string memory) {}
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
struct Person {
unit age;
string name;
}
For more complex data type, you can use 'struct' like object in javascript
Person[] people;
Person buterin = Person(27, 'Vitalik');
// 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;
Person[] people;
Person buterin = Person(27, 'Vitalik');
people.push(buterin);
or
people.push(Person(27, 'Vitalik'))
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
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 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;
}