dev/Solidity

LV.1 crypto zombies - 2 Zombies Attack Their Victims

_April 2021. 11. 26. 01:36

생각보다 강의가 길다?


(1)Mappings and Addresses

new data types: mapping and address.

이더주소 자체가 데이터타입이라니..!

Address 

an address is owned by a specific user (or a smart contract).

The Ethereum blockchain is made up of accounts, which you can think of like bank accounts. An account has a balance of Ether (the currency used on the Ethereum blockchain), and you can send and receive Ether payments to other accounts, just like your bank account can wire transfer money to other bank accounts.

 

Mapping

A mapping is essentially a key-value store for storing and looking up data.

// For a financial app, storing a uint that holds the user's account balance:
mapping (address => uint) public accountBalance;
// Or could be used to store / lookup usernames based on userId
mapping (uint => string) userIdToName;

In the first example, the key is an address and the value is a uint, and in the second example the key is a uint and the value a string.


(2)Msg.sender

there are certain global variables that are available to all functions. One of these is msg.sender, which refers to the  address of the person (or smart contract) who called the current function.

Note: In Solidity, function execution always needs to start with an external caller. A contract will just sit on the blockchain doing nothing until someone calls one of its functions. So there will always be a msg.sender.


(3)Require

if else대신 require을 쓴다.. 

the function will throw an error and stop executing if some condition is not true:

function sayHiToVitalik(string memory _name) public returns (string memory) {
  // Compares if _name equals "Vitalik". Throws an error and exits if not true.
  // (Side note: Solidity doesn't have native string comparison, so we
  // compare their keccak256 hashes to see if the strings are equal)
  require(keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked("Vitalik")));
  // If it's true, proceed with the function:
  return "Hi!";
}

솔리디티 스트링 비교없는거 몹시 신박하다

비교를위한 암호화..

 

근데 참이아니면 뭘리턴한다는거임? 리턴할거없어서 에러날거같은뎅

 


(4)Inheritance

솔리디티는 예제도 "도지"다

투더문~~


(5)Import

이거야뭐.. 설마 파이썬처럼 제가 직접 넣어야할까요?

에디터가 넣어줄거라 굳게믿습니다


(6)Storage vs Memory (Data location)

In Solidity, there are two locations you can store variables — in storage and in memory.

 

Storage refers to variables stored permanently on the blockchain. Memory variables are temporary, and are erased between external function calls to your contract. Think of it like your computer's hard disk vs RAM.

 

기본적으로 건드릴필요는없다

Most of the time you don't need to use these keywords because Solidity handles them by default. State variables (variables declared outside of functions) are by default storage and written permanently to the blockchain, while variables declared inside functions are memory and will disappear when the function call ends.

 

언제 건드리냐면 : structs and arrays

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

  Sandwich[] sandwiches;

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

    // ^ Seems pretty straightforward, but solidity will give you a warning
    // telling you that you should explicitly declare `storage` or `memory` here.

    // So instead, you should declare with the `storage` keyword, like:
    Sandwich storage mySandwich = sandwiches[_index];
    // ...in which case `mySandwich` is a pointer to `sandwiches[_index]`
    // in storage, and...
    mySandwich.status = "Eaten!";
    // ...this will permanently change `sandwiches[_index]` on the blockchain.

    // If you just want a copy, you can use `memory`:
    Sandwich memory anotherSandwich = sandwiches[_index + 1];
    // ...in which case `anotherSandwich` will simply be a copy of the 
    // data in memory, and...
    anotherSandwich.status = "Eaten!";
    // ...will just modify the temporary variable and have no effect 
    // on `sandwiches[_index + 1]`. But you can do this:
    sandwiches[_index + 1] = anotherSandwich;
    // ...if you want to copy the changes back into blockchain storage.
  }
}

(7)More on Function Visibility

In addition to public and private, Solidity has two more types of visibility for functions: internal and external.

internal is the same as private, except that it's also accessible to contracts that inherit from this contract. 

external is similar to public, except that these functions can ONLY be called outside the contract — they can't be called by other functions inside that contract. We'll talk about why you might want to use external vs public later.

 


(8)Interacting with other contracts

오 이런게된다고..

first we need to define an interface.

공개된 다른플젝 인터페이스 긁어와서 init할때 이더 주소 넣으면 된다.

근데 fav address라고 되어있어서 그 플젝주소넣어야하는지 홀더주소 넣어야하는지 잘모르겠음


(9)Handling Multiple Return Values

난 멀티플리턴되는게 좋더라.. 클래스로 커버안되는 그런게있음