Solidity

[CryptoZombie] 1-2 Zombies Attack Their Victims

๐Ÿงก mapping

Solidity์—์„œ mapping์€ ํŒŒ์ด์ฌ์˜ dictionary์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. key-value ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

mapping (address => uint) public accountBalance;
mapping (uint => string) userIdToName;

 

๐Ÿงก Msg.sender

msg.sender๋Š” ๋ชจ๋“  ํ•จ์ˆ˜์—์„œ ์ด์šฉ ๊ฐ€๋Šฅํ•œ ์ „์—ญ๋ณ€์ˆ˜๋‹ค. ๋ง ๊ทธ๋Œ€๋กœ message sender๋ฅผ ๋œปํ•œ๋‹ค.

mapping (address => uint) favoriteNumber;

function setMyNumber(uint _myNumber) public {
  favoriteNumber[msg.sender] = _myNumber;
}

function whatIsMyNumber() public view returns (uint) {
  return favoriteNumber[msg.sender];
}

 

๐Ÿงก Require

Solidity์—์„œ require์€ ํŠน์ • ์กฐ๊ฑด์ด ์ฐธ์ด ์•„๋‹ ๋•Œ ํ•จ์ˆ˜ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ง€์‹œ์ž์ด๋‹ค. ์‚ด์ง assert ๋Š๋‚Œ๐Ÿ˜€

๊ทธ๋ฆฌ๊ณ , Solidity๋Š” ์ž์ฒด์ ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์—†์–ด์„œ ํ•ด์‹œ๊ฐ’์„ ๋น„๊ตํ•ด์•ผ ํ•œ๋‹ค. WA~

function sayHiToVitalik(string _name) public returns (string) {
  require(keccak256(_name) == keccak256("Vitalik"));
  return "Hi!";
}

 

๐Ÿงก Inheritance

๊ทธ๋ƒฅ.. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ํ”ํ•œ ์ƒ์† ์‹œ์Šคํ…œ์ด๋‹ค.

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

๋‹ค๋ฅธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ง€์‹œ์ž์ด๋‹ค.

import "./someothercontract.sol";

contract newContract is SomeOtherContract {

}

 

๐Ÿงก Storage vs Memory ๐Ÿค‘

๋งค์šฐ ๋งค์šฐ ๋งค์šฐ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋‹ค.

 

Solidity์—๋Š” ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์œผ๋กœ Storage์™€ Memory๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Storage๋Š” ๋ธ”๋ก์ฒด์ธ ์ƒ์—์„œ ์˜๊ตฌ์ ์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ณ€์ˆ˜์ด๊ณ  Memory๋Š” ์ž„์‹œ์ ์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ณ€์ˆ˜๋กœ, ์ปจํŠธ๋ž™ํŠธ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์™ธ๋ถ€ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์งˆ ๋•Œ ์‚ญ์ œ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Solidity๊ฐ€ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค. ์•„์ฃผ ์ข‹์•„์šฉ๐Ÿฅฐ except for struct

 

๊นŠ์€ ๋ณต์‚ฌ vs ์–•์€ ๋ณต์‚ฌ์˜ ์˜ˆ์ œ์ด๋‹ค.

contract SandwichFactory {
  struct Sandwich {
    string name;
    string status;
  }

  Sandwich[] sandwiches;

  function eatSandwich(uint _index) public {
    Sandwich storage mySandwich = sandwiches[_index];

    mySandwich.status = "Eaten!";
    
    Sandwich memory anotherSandwich = sandwiches[_index + 1];

    anotherSandwich.status = "Eaten!";

    sandwiches[_index + 1] = anotherSandwich;
  }
}

 

๐Ÿงก Access Modifier

Solidity์—๋Š” Internal๊ณผ External์ด๋ผ๋Š” ํ•จ์ˆ˜ ์ ‘๊ทผ ์ œ์–ด์ž๊ฐ€ ์žˆ๋‹ค.

 

Internal์€ ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋œ ์ปจํŠธ๋ž™ํŠธ๋ฅผ ์ƒ์†ํ•˜๋Š” ์ปจํŠธ๋ž™ํŠธ์—์„œ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

External์€ ์ปจํŠธ๋ž™ํŠธ ๋ฐ”๊นฅ์—์„œ๋งŒ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๊ณ  ์ปจํŠธ๋ž™ํŠธ ๋‚ด์— ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์˜ํ•ด ํ˜ธ์ถœ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

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

 

๐Ÿงก Interface

์ปจํŠธ๋ž™ํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ interface๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•.

contract NumberInterface {
  function getNum(address _myAddress) public view returns (uint);
}

 

๐Ÿงก ๋‹ค์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’ ์ฒ˜๋ฆฌํ•˜๊ธฐ

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

 

๐Ÿงก if

์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•„๋Š” ์กฐ๊ฑด๋ฌธ์ด๋‹ค.

function eatBLT(string sandwich) public {  
  if (keccak256(sandwich) == keccak256("BLT")) {
    eat();
  }
}

 

https://share.cryptozombies.io/ko/lesson/2/share/bjloed?id=Y3p8MTIwMjIz