Introduction
In this series, I'm gonna write some record that I study with crypto zombie.
This article is in Solidity: Beginner to Intermediate Smart Contracts section Making the Zombie Factory lesson.
In this section, we're going to learn about some basic syntax and concept of solidity.
Contract
Contract is the fundamental of Ethereum smart contract application. It’s the starting point of application and all variables and function belongs to it.
The following code creates an empty contract :
contract ZombieFactory {
}
Version Pragma
All solidity code need to declare the solidity compiler version. This prevent the feature compiler break the current code.
The version pragma can set the range of solidity compiler version. For example, If we want to set version 0.5.0 to 0.6.0, It can be written as pragma solidity >=0.5.0 <0.6.0;
This is an example of contract and version pragma
pragma solidity >=0.5.0 <0.6.0;
contract ZombieFactory {
}
State variables
state variables are the permanently stored in contract storage. It seems like DB on the other system. In smart contract, we store the variable state on the Ethereum blockchain.
For example, to create the unsigned integer which has 16-digit number on contract, following the code:
pragma solidity >=0.5.0 <0.6.0;
contract ZombieFactory {
uint16 dnaDigits = 16;
}
In uint(unsigned integer) data type , you can declare the less bits of the variable like uint8, uint16, uint32 , etc.. But in general case, you can use just uint
keyword , alias of uint256, to declare it.
Math Operations
The Solidity math operations are quite straightforward. They are same as in most programming lang.
- Addition : x + y
- Subtraction : x - y
- Multiplication : x * y
- Division : x / y
- Modulus / remainder : x % y
- exponential : x^y or x**y
Struct
Struct is the complex datatype.
It can be declared by the code below:
struct Person {
uint age;
string name;
}
Arrays
Solidity has two kinds of array. fixed array and dynamic array”
// 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;
It also declare with struct:
Person[] people;
Due to the variable states are stored in blockchains block, the dynamic array can behaves like other systems Database. In struct dynamic array, the attribute of struct is equal to database columns, and the elements of array can be the row of database.
If there are any points to be aware of, To bear a mind that it doesn’t ensure the security.
Public Array
solidity can make the public array like the code below:
Person[] public people;
It means other contract can only read this array, not writable.
Function Declarations
In solidity, the function declaration in solidity looks like the following:
function eatHamburgers(string memory _name, uint _amount) public {
}
In this case, the name params stored in memory(computer memory). This is required for all refence types such as arrays, structs, mapping and strings. solidity has to specify the memory keyword to data store in memory.
To call eatHamburgers
function, we can use:
eatHamburgers("vitalik", 100);
Public / Private Functions
The public function is default on solidity. But it provide the private too.
uint[] numbers;
function _addToArray(uint _number) private {
numbers.push(_number);
}
The private function can only be called within other functions declared in the same contract. It is convention (but not required) to prefix the function name with an underscore (_) and specify the private keyword.
Return Values
To return the values from a function, the declaration is below:
string greeting = "What's up dog";
function sayHello() public returns (string memory) {
return greeting;
}
It’s the example that return the string type value.
Function modifiers
In solidity, It has Function Modifier concept. It works as auxiliary works of function. It provide the limitation or additional function to original function.
For example, the function modifiers “view” limit the function that cannot change the value of variable on smart contract.
view
function sayHello() public view returns (string memory) {
}
The function modifier view means that “only view can allow in this function”.
pure
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
The function modifier pure means that “not even accessing the data in app”. On example, the function just return value , depends on just function params.
Keccak256
Ethereum has the Keccak256 hash function. It can be used on generate the (semi)random uint data type.
//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256(abi.encodePacked("aaaab"));
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256(abi.encodePacked("aaaac"));
The keccak256 algorithm returns very differently string even though inputs are similar each other.
Typecasting
Solidity provide the typecasting. It can be able to convert the types when programming.
uint8 a = 5;
uint b = 6;
// throws an error because a * b returns a uint, not uint8:
uint8 c = a * b;
// we have to typecast b as a uint8 to make it work:
uint8 c = a * uint8(b);
As the code below, the uint256(uint type is alias of uint256) can be convert as uint8 to operate multiplication with a variable ,has uint8 datatype.
Events
Events are the way for communicate to your contact and frontend application. front-app can be listen event, and it can catch when the event emit.
In smart contract
// 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;
}
In frontend
YourContract.IntegersAdded(function(error, result) {
// do something with result
})