
Subscribe to KevinWang
Share Dialog
Share Dialog
<100 subscribers
<100 subscribers
Hi all, I’m a developer working in web2. I want to learn web3 knowledge and become a developer in web3. In this artical, I will show my solidity learning stage.
And this is my crypto zombie. You can click this link to see it.
https://share.cryptozombies.io/zh/lesson/1/share/kevin?id=Y3p8NjIzMTk2
In this section, I will show my contract and the knowledge behind the code.
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
At the top of the contract, there is a version of solidity.
pragma solidity ^0.4.19;
The version can help complier check if it’s version is match the code.
uint dnaDigits = 16;
Here is the type in solidity.
The value of booleans are constants true and false.
There are int and uint to distinguish signed or un signed.
And there are int8, int16, int24… to int256, the step is 8. So as uint.
int and uint is the aliases of int256 and uint256.
strings are written by quotes like “hello”
There are basic operators in solidity like + - * / %.
Especially, ** means exponentiation.
struct Zombie {
string name;
uint dna;
}
In solidity, we can define our own type by struct.
And we can also define member variables in the struct.
Zombie[] public zombies;
uint id = zombies.push(Zombie(_name, _dna)) - 1;
If we need to define array, we can use [] behind the type like this.
The array variable contains the function push. Which can add an element in to the array and return the length of array now.
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
Function is a block of codes which can be executed.
The declaration of func is
function (<parameter types>) {internal|external} [pure|view|payable] [returns (<return types>)]
pure function can not achieve any variable in the contract.
and view function can only watch but can’t change the variable of contract.
event NewZombie(uint zombieId, string name, uint dna);
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
NewZombie(id, _name, _dna);
}
If we want do something when the solidity was executed, we can use event.
The javascript can listening the event in contract.
Hi all, I’m a developer working in web2. I want to learn web3 knowledge and become a developer in web3. In this artical, I will show my solidity learning stage.
And this is my crypto zombie. You can click this link to see it.
https://share.cryptozombies.io/zh/lesson/1/share/kevin?id=Y3p8NjIzMTk2
In this section, I will show my contract and the knowledge behind the code.
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
At the top of the contract, there is a version of solidity.
pragma solidity ^0.4.19;
The version can help complier check if it’s version is match the code.
uint dnaDigits = 16;
Here is the type in solidity.
The value of booleans are constants true and false.
There are int and uint to distinguish signed or un signed.
And there are int8, int16, int24… to int256, the step is 8. So as uint.
int and uint is the aliases of int256 and uint256.
strings are written by quotes like “hello”
There are basic operators in solidity like + - * / %.
Especially, ** means exponentiation.
struct Zombie {
string name;
uint dna;
}
In solidity, we can define our own type by struct.
And we can also define member variables in the struct.
Zombie[] public zombies;
uint id = zombies.push(Zombie(_name, _dna)) - 1;
If we need to define array, we can use [] behind the type like this.
The array variable contains the function push. Which can add an element in to the array and return the length of array now.
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
Function is a block of codes which can be executed.
The declaration of func is
function (<parameter types>) {internal|external} [pure|view|payable] [returns (<return types>)]
pure function can not achieve any variable in the contract.
and view function can only watch but can’t change the variable of contract.
event NewZombie(uint zombieId, string name, uint dna);
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
NewZombie(id, _name, _dna);
}
If we want do something when the solidity was executed, we can use event.
The javascript can listening the event in contract.
No activity yet