티스토리 뷰
좀비 만들수 있게 됐다.
이제 좀비 군단을 만들어서.. 공격도 하는걸 배우나보다..
레슨 2 시작..
pragma solidity >=0.5.0 <0.6.0;
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;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string memory _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
// start here
emit NewZombie(id, _name, _dna);
}
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string memory _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
우선 mapping을 배웠다. python에서 dictionary 같은거다..
이제 msg.sender 에 대해 배우는데, msg.sender는 현재 함수를 호출한 사람 또는 컨트랙트의 주소를 나타내는 전역변수다.
Solidity의 모든 함수는 누군가 불러 줘야 실행이 된다. 가만히 앉아있다 이 친구들은 누가 불러주기 전까지는..
아주 조신하고 참한 함수들인 것이다.
msg.sender는 여기서 그 "누구" 를 알려주는 역할이다.
그리고 다음번엔 require도 배운다. 보니까 solidity는 순차적으로 라인바이라인 실행되는 코드인갑다.
pragma solidity >=0.5.0 <0.6.0;
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;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string memory _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
emit NewZombie(id, _name, _dna);
}
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string memory _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
// Start here
여기서 ownerZombieCount 매핑에서 msg.sender의 값이 0인 조건을 충족하면 _createZombie 함수가 실행된다.
이건 require 조건으로 감싸준다.
다음으로 inheritance도 배운다. 상속 재밌게 하네. BabyDoge가 Dodge를 상속하면 코드가
contract BabyDodge is Dodge {}
이렇게 된다. ㅋㅋ
그리고 storage랑 memory도 배우는데 storage는 블록체인 위에 쓰는거고, memory는 휘발되는거다.
그리고 함수 가시성에 대해서도 배운다. 기존에 public, private만 알려줬는데 이번에는 internal, external에 대해서도 알려준다.
private과 public은 우리가 아는 그대로다. public은 어디서든 호출 가능한 함수다. 컨트랙트 내부, 외부, 다른 컨트랙트에서도 호출할 수 있다.
private은 이 함수가 선언된 현재 컨트랙트 안에서만 호출이 된다.
internal은 private이랑 비슷하지만, 상속 관계에 있는 자식 컨트랙트에서도 호출이 된다.
external같은 경우는 컨트랙트 외부에서만 호출이 가능하다. 컨트랙트 내부에서 호출하는건 불가능하다.
pragma solidity >=0.5.0 <0.6.0;
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;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string memory _name, uint _dna) internal {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
emit NewZombie(id, _name, _dna);
}
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string memory _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
여기보면 원래 _createZombie가 private 함수였는데, 상속받는 자식 컨트랙트에서 쓰임이 필요해서 internal로 바꾼거다.
그리고 Interface 선언해서 다른 컨트랙트와 소통하는 방법도 배운다.
pragma solidity >=0.5.0 <0.6.0;
import "./zombiefactory.sol";
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
contract ZombieFeeding is ZombieFactory {
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
KittyInterface kittyContract = KittyInterface(ckAddress);
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
_createZombie("NoName", newDna);
}
// define function here
}
이렇게 Lesson2 를 마쳤당...
https://share.cryptozombies.io/en/lesson/2/share/mar?id=Y3p8NjU3MTQ1
My CryptoZombie OGURL just ate a CryptoKitty and leveled up!
I just completed CryptoZombies Lesson 2, and added a reanimated CryptoKitty to my zombie army. Come take a look! Thanks @CryptoZombiesHQ.
share.cryptozombies.io
- Total
- Today
- Yesterday
- prettier
- web2web3비교
- gili
- 세상만사새옹지마
- 크립토좀비
- vscode
- Flutter
- 찾아봄
- web3
- 공부좀열심히해라
- 이더리움
- 길리여행
- 패들보트
- 플러터
- DART
- 선라이즈 패들보트
- 김영한
- til
- 터틀포인트
- 티스토리챌린지
- 테스트넷
- contains
- 세폴리아
- 스파르타코딩클럽
- 솔리디티
- setState
- web2
- 길리
- zksync
- 오블완
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 |