Last active
July 6, 2022 07:28
-
-
Save sbin0819/2029990c459ed676d09af4a1df27788a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Chapter 1 | |
Web3.js | |
## basic | |
### ์ปจํธ๋ํธ | |
```solidity | |
pragma solidity ^0.4.19; | |
contract HelloWorld { | |
} | |
``` | |
### ๊ตฌ์กฐ์ฒด | |
```solidity | |
struct Person { | |
uint age; | |
string name; | |
} | |
``` | |
### ํจ์ | |
```solidity | |
uint[] numbers; | |
function _addToArray(uint _number) private { | |
numbers.push(_number); | |
} | |
``` | |
### ํจ์ ์ ์ด์ | |
- ์ํ๋ฅผ ๋ณํ์ํค์ง ์์ `view` | |
- ํจ์๊ฐ ์ฑ์์ ์ด๋ค ๋ฐ์ดํฐ๋ ์ ๊ทผํ์ง ์๋ ๊ฒ `pure` | |
### ์ด๋ฒคํธ | |
```solidity | |
event IntegersAdded(uint x, uint y, uint result); | |
function add(uint _x, uint _y) public { | |
uint result = _x + _y; | |
// ์ด๋ฒคํธ๋ฅผ ์คํํ์ฌ ์ฑ์๊ฒ add ํจ์๊ฐ ์คํ๋์์์ ์๋ฆฐ๋ค: | |
IntegersAdded(_x, _y, result); | |
return result; | |
} | |
``` | |
```js | |
YourContract.IntegersAdded(function(error, result) { | |
// ๊ฒฐ๊ณผ์ ๊ด๋ จ๋ ํ๋์ ์ทจํ๋ค | |
}) | |
``` | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Chapter 2 | |
## ๋งคํ๊ณผ ์ฃผ์ | |
`mapping`, `adress` | |
- ์ฃผ์ | |
์ด๋๋ฆฌ์ ๋ธ๋ก์ฒด์ธ์ ์ฃผ์ | |
- ๋งคํ | |
๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋๋ค๋ฅธ ๋ฐฉ๋ฒ | |
```solidity | |
// ๊ธ์ต ์ฑ์ฉ์ผ๋ก, ์ ์ ์ ๊ณ์ข ์์ก์ ๋ณด์ ํ๋ uint๋ฅผ ์ ์ฅํ๋ค: | |
mapping (address => uint) public accountBalance; | |
// ํน์ userID๋ก ์ ์ ์ด๋ฆ์ ์ ์ฅ/๊ฒ์ํ๋ ๋ฐ ๋งคํ์ ์ธ ์๋ ์๋ค | |
mapping (uint => string) userIdToName; | |
``` | |
๋งคํ์ ๊ธฐ๋ณธ์ ์ผ๋ก ํค-๊ฐ (key-value) ์ ์ฅ์๋ก, ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ฒ์ํ๋ ๋ฐ ์ด์ฉ๋๋ค. ์ฒซ๋ฒ์งธ ์์์์ ํค๋ address์ด๊ณ ๊ฐ์ uint์ด๋ค. ๋๋ฒ์งธ ์์์์ ํค๋ uint์ด๊ณ ๊ฐ์ string์ด๋ค. | |
## msg.sender | |
์๋ฆฌ๋ํฐ์๋ ๋ชจ๋ ํจ์์์ ์ด์ฉ ๊ฐ๋ฅํ ํน์ ์ ์ญ ๋ณ์๋ค์ด ์์ง. ๊ทธ ์ค์ ํ๋๊ฐ ํ์ฌ ํจ์๋ฅผ ํธ์ถํ ์ฌ๋ (ํน์ ์ค๋งํธ ์ปจํธ๋ํธ)์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ msg.sender์ด์ง. | |
์ฐธ๊ณ : ์๋ฆฌ๋ํฐ์์ ํจ์ ์คํ์ ํญ์ ์ธ๋ถ ํธ์ถ์๊ฐ ์์ํ๋ค. ์ปจํธ๋ํธ๋ ๋๊ตฐ๊ฐ๊ฐ ์ปจํธ๋ํธ์ ํจ์๋ฅผ ํธ์ถํ ๋๊น์ง ๋ธ๋ก์ฒด์ธ ์์์ ์๋ฌด ๊ฒ๋ ์ ํ๊ณ ์์ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๋ ํญ์ msg.sender๊ฐ ์์ด์ผ ํ๋ค. | |
```solidity | |
mapping (address => uint) favoriteNumber; | |
function setMyNumber(uint _myNumber) public { | |
// `msg.sender`์ ๋ํด `_myNumber`๊ฐ ์ ์ฅ๋๋๋ก `favoriteNumber` ๋งคํ์ ์ ๋ฐ์ดํธํ๋ค ` | |
favoriteNumber[msg.sender] = _myNumber; | |
// ^ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ตฌ๋ฌธ์ ๋ฐฐ์ด๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋์ ๋์ผํ๋ค | |
} | |
function whatIsMyNumber() public view returns (uint) { | |
// sender์ ์ฃผ์์ ์ ์ฅ๋ ๊ฐ์ ๋ถ๋ฌ์จ๋ค | |
// sender๊ฐ `setMyNumber`์ ์์ง ํธ์ถํ์ง ์์๋ค๋ฉด ๋ฐํ๊ฐ์ `0`์ด ๋ ๊ฒ์ด๋ค | |
return favoriteNumber[msg.sender]; | |
} | |
``` | |
์ด ๊ฐ๋จํ ์์์์ ๋๊ตฌ๋ setMyNumber์ ํธ์ถํ์ฌ ๋ณธ์ธ์ ์ฃผ์์ ์ฐ๊ฒฐ๋ ์ฐ๋ฆฌ ์ปจํธ๋ํธ ๋ด์ uint๋ฅผ ์ ์ฅํ ์ ์์ง. | |
msg.sender๋ฅผ ํ์ฉํ๋ฉด ์๋ค๋ ์ด๋๋ฆฌ์ ๋ธ๋ก์ฒด์ธ์ ๋ณด์์ฑ์ ์ด์ฉํ ์ ์๊ฒ ๋์ง. ์ฆ, ๋๊ตฐ๊ฐ ๋ค๋ฅธ ์ฌ๋์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด ํด๋น ์ด๋๋ฆฌ์ ์ฃผ์์ ๊ด๋ จ๋ ๊ฐ์ธํค๋ฅผ ํ์น๋ ๊ฒ ๋ฐ์๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๋ค๋ ๊ฒ์ด๋ค. | |
## require | |
```solidity | |
function sayHiToVitalik(string _name) public returns (string) { | |
// _name์ด "Vitalik"์ธ์ง ๋น๊ตํ๋ค. ์ฐธ์ด ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฉ์์ง๋ฅผ ๋ฐ์ํ๊ณ ํจ์๋ฅผ ๋ฒ์ด๋๋ค | |
// (์ฐธ๊ณ : ์๋ฆฌ๋ํฐ๋ ๊ณ ์ ์ ์คํธ๋ง ๋น๊ต ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์์ง ์๊ธฐ ๋๋ฌธ์ | |
// ์คํธ๋ง์ keccak256 ํด์๊ฐ์ ๋น๊ตํ์ฌ ์คํธ๋ง ๊ฐ์ด ๊ฐ์์ง ํ๋จํ๋ค) | |
require(keccak256(_name) == keccak256("Vitalik")); | |
// ์ฐธ์ด๋ฉด ํจ์ ์คํ์ ์งํํ๋ค: | |
return "Hi!"; | |
} | |
``` | |
## ์์ | |
`is` | |
```solidity | |
contract Doge { | |
function catchphrase() public returns (string) { | |
return "So Wow CryptoDoge"; | |
} | |
} | |
contract BabyDoge is Doge { | |
function anotherCatchphrase() public returns (string) { | |
return "Such Moon BabyDoge"; | |
} | |
} | |
``` | |
## import | |
## storage vs memory | |
Storage๋ ๋ธ๋ก์ฒด์ธ ์์ ์๊ตฌ์ ์ผ๋ก ์ ์ฅ๋๋ ๋ณ์๋ฅผ ์๋ฏธํ์ง. Memory๋ ์์์ ์ผ๋ก ์ ์ฅ๋๋ ๋ณ์๋ก, ์ปจํธ๋ํธ ํจ์์ ๋ํ ์ธ๋ถ ํธ์ถ๋ค์ด ์ผ์ด๋๋ ์ฌ์ด์ ์ง์์ง์ง. ๋ ๋ณ์๋ ๊ฐ๊ฐ ์ปดํจํฐ ํ๋ ๋์คํฌ์ RAM๊ณผ ๊ฐ์ง. | |
`์ํ ๋ณ์(ํจ์ ์ธ๋ถ์ ์ ์ธ๋ ๋ณ์)`๋ ์ด๊ธฐ ์ค์ ์ `storage`๋ก ์ ์ธ๋์ด ๋ธ๋ก์ฒด์ธ์ ์๊ตฌ์ ์ผ๋ก ์ ์ฅ๋๋ ๋ฐ๋ฉด, `ํจ์ ๋ด์ ์ ์ธ๋ ๋ณ์`๋ `memory`๋ก ์๋ ์ ์ธ๋์ด์ ํจ์ ํธ์ถ์ด ์ข ๋ฃ๋๋ฉด ์ฌ๋ผ์ง์ง. | |
*ํ์ง๋ง ์ด ํค์๋๋ค์ ์ฌ์ฉํด์ผ ํ๋ ๋๊ฐ ์์ง. ๋ฐ๋ก ํจ์ ๋ด์ ๊ตฌ์กฐ์ฒด์ _๋ฐฐ์ด_์ ์ฒ๋ฆฌํ ๋์ง:* | |
```solidity | |
contract SandwichFactory { | |
struct Sandwich { | |
string name; | |
string status; | |
} | |
Sandwich[] sandwiches; | |
function eatSandwich(uint _index) public { | |
// Sandwich mySandwich = sandwiches[_index]; | |
// ^ ๊ฝค ๊ฐ๋จํด ๋ณด์ด๋, ์๋ฆฌ๋ํฐ๋ ์ฌ๊ธฐ์ | |
// `storage`๋ `memory`๋ฅผ ๋ช ์์ ์ผ๋ก ์ ์ธํด์ผ ํ๋ค๋ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ๋ฐ์ํ๋ค. | |
// ๊ทธ๋ฌ๋ฏ๋ก `storage` ํค์๋๋ฅผ ํ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ ์ธํด์ผ ํ๋ค: | |
Sandwich storage mySandwich = sandwiches[_index]; | |
// ...์ด ๊ฒฝ์ฐ, `mySandwich`๋ ์ ์ฅ๋ `sandwiches[_index]`๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ์ด๋ค. | |
// ๊ทธ๋ฆฌ๊ณ | |
mySandwich.status = "Eaten!"; | |
// ...์ด ์ฝ๋๋ ๋ธ๋ก์ฒด์ธ ์์์ `sandwiches[_index]`์ ์๊ตฌ์ ์ผ๋ก ๋ณ๊ฒฝํ๋ค. | |
// ๋จ์ํ ๋ณต์ฌ๋ฅผ ํ๊ณ ์ ํ๋ค๋ฉด `memory`๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค: | |
Sandwich memory anotherSandwich = sandwiches[_index + 1]; | |
// ...์ด ๊ฒฝ์ฐ, `anotherSandwich`๋ ๋จ์ํ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ๋ ๊ฒ์ด ๋๋ค. | |
// ๊ทธ๋ฆฌ๊ณ | |
anotherSandwich.status = "Eaten!"; | |
// ...์ด ์ฝ๋๋ ์์ ๋ณ์์ธ `anotherSandwich`๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ผ๋ก | |
// `sandwiches[_index + 1]`์๋ ์๋ฌด๋ฐ ์ํฅ์ ๋ผ์น์ง ์๋๋ค. ๊ทธ๋ฌ๋ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค: | |
sandwiches[_index + 1] = anotherSandwich; | |
// ...์ด๋ ์์ ๋ณ๊ฒฝํ ๋ด์ฉ์ ๋ธ๋ก์ฒด์ธ ์ ์ฅ์์ ์ ์ฅํ๊ณ ์ ํ๋ ๊ฒฝ์ฐ์ด๋ค. | |
} | |
} | |
``` | |
## ํจ์ ์ ๊ทผ ์ ์ด์ ๋ ์์๋ณด๊ธฐ | |
- internal | |
- external | |
```solidity | |
contract Sandwich { | |
uint private sandwichesEaten = 0; | |
function eat() internal { | |
sandwichesEaten++; | |
} | |
} | |
contract BLT is Sandwich { | |
uint private baconSandwichesEaten = 0; | |
function eatWithBacon() public returns (string) { | |
baconSandwichesEaten++; | |
// eat ํจ์๊ฐ internal๋ก ์ ์ธ๋์๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค | |
eat(); | |
} | |
} | |
``` | |
## ์ธํฐํ์ด์ค | |
```solidity | |
contract LuckyNumber { | |
mapping(address => uint) numbers; | |
function setNum(uint _num) public { | |
numbers[msg.sender] = _num; | |
} | |
function getNum(address _myAddress) public view returns (uint) { | |
return numbers[_myAddress]; | |
} | |
} | |
``` | |
```solidity | |
contract NumberInterface { | |
function getNum(address _myAddress) public view returns (uint); | |
} | |
``` | |
## ์ธํฐํ์ด์ค ํ์ฉ | |
```solidity | |
contract MyContract { | |
address NumberInterfaceAddress = 0xab38... | |
// ^ ์ด๋๋ฆฌ์์์ FavoriteNumber ์ปจํธ๋ํธ ์ฃผ์์ด๋ค | |
NumberInterface numberContract = NumberInterface(NumberInterfaceAddress) | |
// ์ด์ `numberContract`๋ ๋ค๋ฅธ ์ปจํธ๋ํธ๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ค. | |
function someFunction() public { | |
// ์ด์ `numberContract`๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ์ปจํธ๋ํธ์์ `getNum` ํจ์๋ฅผ ํธ์ถํ ์ ์๋ค: | |
uint num = numberContract.getNum(msg.sender); | |
// ...๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์ `num`์ผ๋ก ๋ฌด์ธ๊ฐ๋ฅผ ํ ์ ์๋ค | |
} | |
} | |
``` | |
## ๋ค์์ ๋ฐํ๊ฐ ์ฒ๋ฆฌํ๊ธฐ | |
```solidity | |
function multipleReturns() internal returns(uint a, uint b, uint c) { | |
return (1, 2, 3); | |
} | |
function processMultipleReturns() external { | |
uint a; | |
uint b; | |
uint c; | |
// ๋ค์๊ณผ ๊ฐ์ด ๋ค์ ๊ฐ์ ํ ๋นํ๋ค: | |
(a, b, c) = multipleReturns(); | |
} | |
// ํน์ ๋จ ํ๋์ ๊ฐ์๋ง ๊ด์ฌ์ด ์์ ๊ฒฝ์ฐ: | |
function getLastReturnValue() external { | |
uint c; | |
// ๋ค๋ฅธ ํ๋๋ ๋น์นธ์ผ๋ก ๋๊ธฐ๋ง ํ๋ฉด ๋๋ค: | |
(,,c) = multipleReturns(); | |
} | |
``` | |
```solidity | |
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; | |
// ์ฌ๊ธฐ์ if ๋ฌธ ์ถ๊ฐ | |
_createZombie("NoName", newDna); | |
} | |
function feedOnKitty(uint _zombieId, uint _kittyId) public { | |
uint kittyDna; | |
(,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId); | |
// ์ฌ๊ธฐ์ ์๋ ํจ์ ํธ์ถ์ ๋ณ๊ฒฝ: | |
feedAndMultiply(_zombieId, kittyDna); | |
} | |
} | |
``` | |
## if ๋ฌธ | |
์๋ฐ์คํฌ๋ฆฝํธ if ๋ฌธ๊ณผ ๋์ผ | |
```solidity | |
function eatBLT(string sandwich) public { | |
// ์คํธ๋ง ๊ฐ์ ๋์ผ ์ฌ๋ถ๋ฅผ ํ๋จํ๊ธฐ ์ํด keccak256 ํด์ ํจ์๋ฅผ ์ด์ฉํด์ผ ํ๋ค๋ ๊ฒ์ ๊ธฐ์ตํ์ | |
if (keccak256(sandwich) == keccak256("BLT")) { | |
eat(); | |
} | |
} | |
``` | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ์๋ฆฌ๋ํฐ ๊ณ ๊ธ๊ฐ๋ | |
## ๊ฐ์ค(Gas) | |
### ๊ฐ์ค - ์ด๋๋ฆฌ์ DApp์ด ์ฌ์ฉํ๋ ์ฐ๋ฃ | |
ํจ์๋ฅผ ์คํํ๋ ๋ฐ์ ์ผ๋ง๋ ๋ง์ ๊ฐ์ค๊ฐ ํ์ํ์ง๋ ๊ทธ ํจ์์ ๋ก์ง(๋ ผ๋ฆฌ ๊ตฌ์กฐ)์ด ์ผ๋ง๋ ๋ณต์กํ์ง์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ค. ๊ฐ๊ฐ์ ์ฐ์ฐ์ ์๋ชจ๋๋ ๊ฐ์ค ๋น์ฉ(gas cost)์ด ์๊ณ , ๊ทธ ์ฐ์ฐ์ ์ํํ๋ ๋ฐ์ ์๋ชจ๋๋ ์ปดํจํ ์์์ ์์ด ์ด ๋น์ฉ์ ๊ฒฐ์ ํ๋ค. ์๋ฅผ ๋ค์ด, storage์ ๊ฐ์ ์ฐ๋ ๊ฒ์ ๋ ๊ฐ์ ์ ์๋ฅผ ๋ํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๋น์ฉ์ด ๋๋ค. ์๋ค ํจ์์ ์ ์ฒด ๊ฐ์ค ๋น์ฉ์ ๊ทธ ํจ์๋ฅผ ๊ตฌ์ฑํ๋ ๊ฐ๋ณ ์ฐ์ฐ๋ค์ ๊ฐ์ค ๋น์ฉ์ ๋ชจ๋ ํฉ์น ๊ฒ๊ณผ ๊ฐ๋ค. | |
### ๊ฐ์ค๋ ์ ํ์ํ๊ฐ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment