dev/Solidity

LV.1 crypto zombies - 1 Making the Zombie Factory

_April 2021. 11. 25. 01:54

(1) Lesson Overview

DNA따라 캐릭터 조형이 바뀜을보여줌

nft는 실제로 이렇게 조형되서 예제를 이렇게 보여준듯


(2) Contracts

Solidity's code is encapsulated in contracts. A contract is the fundamental building block of Ethereum applications 

맞다

그래서 컨트랙만봐도 러그 50퍼는 피할수있다

이걸위해 나는 이 강의를 시작한것이다

 

-Version Pragma

 

All solidity source code should start with a "version pragma" — a declaration of the version of the Solidity compiler this code should use.

이걸이렇게 명시하다니 독특하다

보통은 메타데이터에 쓰면 컴파일러가 관리해주는데..

pragma solidity >=0.5.0 <0.6.0;

contract HelloWorld {

}

이런식으로 명시해야 헬로월드 컨트랙이 작동한다.

 


(3)State Variables & Integers

이제 데이터에 대해 배운다.

State variables are permanently stored in contract storage. This means they're written to the Ethereum blockchain. Think of them like writing to a DB.

데이터가 체인에영원히 남는다이거야.. 정말작은 nft파일은

이렇게 아예 이더리움체인 자체에 올릴수있다. 진정한 탈중앙화

 

이런 state variable의 데이터타입 중에는

uint가있다. 

The uint data type is an unsigned integer, meaning its value must be non negative.

기본형: uint256 짧은거 uint8, uint16, uint32

 

There's also an int data type for signed integers.

 

데이터타입 다 알려줘 감질나..

이렇게 데이터타입 세분화된이유는 코드 양에따라 이더리움 가스비가 다르기때문임 (이거 주워들은거라 확실치않음) =>코드양 최소화 마치 c++같군요


(4)Math

Addition: x + y
Subtraction: x - y,
Multiplication: x * y
Division: x / y
Modulus / remainder: x % y (for example, 13 % 5 is 3, because if you divide 5 into 13, 3 is the remainder)

그냥 수식

exponential operator
uint x = 5 ** 2; // equal to 5^2 = 25


(5)Structs

클래스대신 스트럭이라고 하는군요

struct Person {
  uint age;
  string name;
}

Structs allow you to create more complicated data types that have multiple properties.

객체랑은 다른듯함 객체는 속성과 동작인데 이건 속성(데이터)만 있는듯


(6) Arrays

fixed arrays and dynamic arrays

다이나믹만있으면 나는 행복합니다

// 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;
Person[] public people;

퍼블릭으로 만들면 Solidity will automatically create a getter method for it.

Other contracts would then be able to read from,(퍼블릭이니까) but not write to, this array. So this is a useful pattern for storing public data in your contract.


(7)Function Declarations

function eatHamburgers(string memory _name, uint _amount) public {

}

퍼블릭붙이는 위치가 독특하네.. variables뒤다.

플러터도 그러더니 솔리디티도 private는 변수명을 _로시작함

Note that we're specifying the function visibility as public. We're also providing instructions about where the _name variable should be stored- in memory.

그럼 memory 안붙이면 메모리에 안둔다는건지..?

 

  • By value, which means that the Solidity compiler creates a new copy of the parameter's value and passes it to your function. This allows your function to modify the value without worrying that the value of the initial parameter gets changed.
  • By reference, which means that your function is called with a... reference to the original variable. Thus, if your function changes the value of the variable it receives, the value of the original variable gets changed.

(8) Working With Structs and Arrays

그냥뻔함 새 struct만들어서 dynamic array에넣음


(9)Private / Public Functions

It's good practice to mark your functions as private by default, and then only make public the functions you want to expose to the world.


(10)More on Functions

return value,

Function modifiers

view function, meaning it's only viewing the data but not modifying it.

pure functions, which means you're not even accessing any data in the app.


(11)Keccak256 and Typecasting

Ethereum has the hash function keccak256 built in, which is a version of SHA3. A hash function basically maps an input into a random 256-bit hexadecimal number. A slight change in the input will cause a large change in the hash.

이걸 다양하게 쓸수있는데 랜덤만들때도 씀..

//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256(abi.encodePacked("aaaab"));
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256(abi.encodePacked("aaaac"));

주의: Secure random-number generation in blockchain is a very difficult problem. 보안문제 있을수있음

 

Typecasting

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);

살짝 c++의 악몽이..

 


12는 복습이라 생략

(13)Events

Events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.

 

리스너대신 events와 emit사용

// 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;
}

(14)Web3.js

Now we need to write a javascript frontend that interacts with the contract.
Ethereum has a Javascript library called Web3.js.
In a later lesson, we'll go over in depth how to deploy a contract and set up Web3.js.

 

아예 자바스크립트를 세트로가르치는군...