Smart Contract 2

내용 요약

약간의 타입
  1. address : 주소 자료형. 주소를 받는다
  2. uint : 부호 없는 정수
  3. bool : true / false
생성자 함수 & 제어자 & 메서드
  1. constructor (함수) : 스마트 계약을 배포할 때 실행되는 함수
  2. modifier (제어) : 함수에 사용하는 에드온으로 추가적 논리를 작성
    1. require
    2. _; : 본문코드로의 시작을 알린다.
    3. 파이썬의 데코레이터를 만드는 느낌 같았다.
  3. returns (자료형) : 함수의 return 타입을 정함
  4. transfer : aaa.transfer라면 aaa에게 송금한다.
속성 & 키워드 & +a
  1. payable : 지불이 가능하다는 것
  2. public : 함수 외부에서도 함수에 접근 가능
  3. msg : message
    1. msg.sender : 배포자 (deploy)
    2. value : 전송하는 이더의 양
  4. mapping (키 타입 => value 타입)접근제한자(ex: public) 이름 : map을 만듦

Will 만들기

필요한 것 - solidity 자료형

필요항목 자료형
재산 소유자 or 주소 address
소유자의 사망여부 bool
재산 uint
받을 사람 addres payable[]
  • Address : 주소 타입. 유저의 고유 아이디 또는 배포된 스마트 컨트랙트의 아이디

  • uint{숫자} : 바이트 기준으로 정수의 타입을 지정할 수 있다

solidity의 생성자 함수 & 속성 키워드

constructor
solidity 스마트 계약을 배포할 떄 실행되는 함수
  • public : 계약 밖에서도 함수를 호출 할 수 있다.
  • payable : 대상이 이더를 보내고 받을 수 있게 한다.

      // 1.
        constructor() payable public {
          owner = msg.sender,
          ...
        }
      // 2.
        address payable[] ArryName
    

  • msg : message
    • sender : 발신자 (호출 & 실행) / 계약을 배포한 사람의 주소
      onwer = msg.sender  // owner가 누구건간에 address를 대표한다.
      // 유서라면 : 유언장 작성자겠죠?
      // 계약을 호출하고 실행하는 사람 = owner
      
    • value : 전송하는 eth 의 양

solidity의 제어자

modifier : 함수에 사용하는 애드온으로 추가적 논리를 생성

  • require : if문 같은 조건문을 입력
  • _ : 다음 함수로 이어진다는 뜻 같음

배열

[]배열이름
크게 다른게 없다.
address payable[] ArryName 이렇게 명시해줘도 ok

Mapping ()

js 의 객체 같은 것?

  mapping( 키의 타입 => 값의 타입) 매핑 이름
  // 화살표 꼭 써야합니다.

사용 :

  • 매핑이름[키 값] = 값
pragma solidity >= 0.7.0 < 0.9.0;

contract AddressWallets {

    address payable[] investorWallets;
    address owner;
    mapping(address => uint) investors;

    function payInvestors(address payable wallet, uint amount) public {
        investorWallets.push(wallet);
        investors[wallet] = amount;
    }

    function checkInvestors() public view returns (uint256) {
        return investorWallets.length;
    }
}

will 끝 이 아님!

1. mapping에 익숙해지자
2. 함수, 타입, payable 이해하기
3. constructor 개념 숙지하기
4. modifier 개념 숙지하기

for ; 자바스크립트랑 동일합니다.

for (타입 init; condition; increment ) {
  code...
}

: private : 비공개임.

transfer

돈을 이체한다.

transfer은 항상 금액을 인수로 가진다.

pragma solidity ^0.8.4;

contract Will {
    // 계약에서 호출 가능한 변수
    address owner; // 소유자 주소
    uint fortune; // 금액 : 정수
    bool deceased; // 사망 여부

    constructor() payable public {
        owner = msg.sender; // msg sender represents address being called
        fortune = msg.value; //msg value tells us how much ether is being sent
    }

    // create modifier so the only person who can call the contract is the owner
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    // only allocate funds if friend's gramps is deceased
    // 제어자임. 아래에서 사용. 함수의 실행 조건을 말한다. 파이썬의 데코레이터?
    modifier mustBeDeceased {
        require(deceased == true);
        _;
    }

    // list of family wallets 배열입니다.
    address payable[] familyWallets;

    // map through inheritance // 매핑
    mapping(address => uint) inheritance;


    // 각 주소에 유산을 할당 : 소유자만 이 함수를 실행 시킬 수 있다.
    function setInheritance(address payable wallet, uint amount) public onlyOwner {
        familyWallets.push(wallet);
        inheritance[wallet] = amount;
    }



    // 가족 구성원 지갑 주소에 각각 지급
    function payout() private mustBeDeceased {
        // for 루프 생성
        for (uint i = 0; i <familyWallets.length; i++) {
            // 계좌의 i 인덱스에 familWallet[i], 즉 amount를 전송한다.
            familyWallets[i].transfer(inheritance[familyWallets[i]]);
        }
    }

    function deceased() public onlyOwner {
        isDeceased = true;

    }
}

예…

이렇게 아주 간단한 스마트 계약을 하나 배포해봤습니다..

추가 자료

Solidity 공식 문서

4월부터 깃에서 겨울잠을 자버린 불쌍한 will 게시글…