• docs.soliditylang.org/en/v0.8.13/
  • Solidity는 정적타입
    • 동적 타입: 메모리 할당을 동적으로 수행할 수 있다. (C++ - malloc.h)
    • 정적 타입: 변수의 메모리 크기를 처음 선언하고 수정할 수 없다. (int a[100] : 100개의 원소만 가질 수 있는 array)
  • Remix: Smart Contract를 작성하고, 배포하고, 실행할 수 있는 IDE
    • localhost 파일 import 할 수 있다.

    • contracts: solidity 파일 저장하는 폴더

    • scripts: solidity 파일을 배포하는 폴더

    • tests: 테스트 코드 저장하는 폴더

    • artifacts: metadata를 저장하는 경로

    • compiler, language, abi 등 저장

    • Owner: test 환경

    • bytecode: byte 데이터 + opcodes(byte code가 실제로 의미하는 것)

    • Solidity Compiler: Web 상에서의 Solidity Compiler 파일을 설치하고 쓸 수 있다.

    • YUL: Assembly언어에 가까운 언어

    • Solidity 언어 안에 YUL을 함께 써서 contract를 작성할 수 있다.

    • Virtual Machine: Dynamic Gas비 같은 경우 EVM 버전을 바꿔서 Soft Fork해서 활용한다.

    • Compiler Configuration

      • optimization: bytecode를 실행할 떄 gas비가 안드는 쪽으로 코드를 수정한다.
        • optimization이 많이 수행될 수록 bytecode는 길어진다. bytecode가 길어지면 deploy할 때 gas비가 많이 들고, execute할 때 gas비가 적게 든다.
        • compile은 artifacts 설정에 따라 compile 된다.
      • Deploy(새로운 contract를 blockchain에 올리는 행위) & Run Transaction (Contract를 실행하여 state를 변경하거나 view function을 활용하는 행위)
        • account: 실험할 수 있게 해주는 account
        • Environment: 실제 블록체인과 비슷한 환경으로 Local 환경에 contract를 deploy한다.
        • Call data: state가 변하면 gas 비가 소모
        • 외부에서 view function만 call 하면 gas 비가 소모되지 않는다.
  • Smart Contract는 Legacy에 대해 중요하게 생각한다. (이미 검증이 되어 있는 코드이기 떄문에)
    • Overflow 사례
      • 다른 Compiler와 달리 evm은 v0.8 전까지는 library를 import해서 overflow를 검사해야만 했다.
      • v0.8 이후에는 다행히 다른 Compiler처럼 내장되어 활용할 수 있게 되었다.
    • OpenZeppelin 등의 검증된 legacy 코드 사용을 추천한다.
      • 새로운 코드는 bug에 취약할 수 있기 떄문 (bug bounty program!)
        • 만약 legacy 코드 중에 취약점이 있다면 legacy 코드 기반의 코드 전체가 취약점에 노출될 위험이 있다.
        • 토큰 bridge 웜홀 프로토콜의 결함을 악용해 약 3616억원의 손실 발생 사례
        • https://www.cctvnews.co.kr/news/articleView.html?idxno=232095
    • audit 업체는 모든 contract의 code를 커버해주지는 못한다.
      • 단, 업체의 tool로 simulation 한 결과를 분석해서 취약점을 알려주는 역할
    • user-defined types: 구조체
      • 이미 있는 data type을 조합하여 새로운 type 생성
      • 공통된 element를 가지는 struct를 생성
  • Language Description
    • Layout : Solidity Contract의 definition 정의
    • pragma : Compiler 타입에 대해 명시
    • SPDX License Identifier
    • Decentralized되어 있기 때문에 Open Source로 개발하게 되고, 이에 대한 License를 명시해야 한다.
      • blockchain에 같이 올라간다.
      • pragmas, Version Pragma, Experimental Pragma, SMTChecker: skip
      • ABI Coder Pragma : 이전 버전에서는 명시했어야했지만, 현재 버전은 필요 없음.
        • Importing other Source Files
        • import "filename" (Python, Typescript에서 동일)
        • Comments : 주석처리
  • Structure of a Contract
    • Contracts == Class
      • State Variables, Functions, Function Modifiers, Events, Errors, Struct Types, Enum Types
      • Function은 Class의 method 개념이랑 동일
      • Function Modifier는 Python Decorator와 비슷하게 동작 https://schoolofweb.net/blog/posts/파이썬-데코레이터-decorator/
      • Events: Logging과 비슷
      • Enum Types: python enumerate와 비슷, 데이터 선언 방식
        • State Variables
          • permanently stored in contract storage
          • 영원히 데이터가 blockchain에 저장되므로, 개인정보는 state variable에 저장하지 않는 규약이 있다.
        • Functions
          • contract 내부, 외부 모두에 define될 수 있다.

          • Function Modifier

          • modifier 객체 내에 "_" 위치에 modifier 객체를 call한 함수의 logic이 수행된다.

            • 예시

              modifier onlySeller() {
              	require(msg.sender == seller, "Only seller can call this.");
              	_;
              }
              
              function abort() public view onlySeller{ //Modifier usage
              					// ...}
              
          • Events: type에 대한 정의와 output에 대한 정의로 선언할 수 있다.

            • logging에 활용
            • emit "Events 객체()"
          • Error: Error 객체

            • Error를 호출할 때는 revert를 사용한다.

            • revert를 사용하면 transaction이 즉시 종료된다.

              • 예시
              error NotEnoughFunds(uint requested, uint available);
              revert NotEnoughFunds(amount, balance);
              
          • StructTypes: 여러 개의 predefined 된 data type의 집합

          • Enum Types: 배열의 각 원소에 값이 자동 부여된다.

            • enum State { created, Locked, Inactive }
            • created에는 0이 저장, Locked에는 1이 저장, Inactive에는 2가 저장된다.
  • Types
  • Payable: Mainnet에 coin을 transfer하려면 기본적으로 address는 payable이어야 한다.
  • address(this) : contract 내부에서만 쓸 수 있는 표현 // 해당 contract의 주소를 의미
  • transfer/send : 특정 계좌로 코인/토큰을 송금한다. => 현재는 잘 사용하지 않음. Gas fee가 고정되어 있기 때문에
  • call: bytecode를 실행시키는 함수
  • Input Data: Input Value 기록
  • Internal Transaction: Transaction 과정에 대한 detail이 저장
  • Event log: Event 명시
  • delegate call <<Call>>
  • Gas Option: 하나의 Contract에서 다른 Contract에 gas를 부여할 수 있다.
  • Contract Type: Payable이다. address type과 동일
  • Fixed-size byte arrays
  • Padding
  • Dynamically-size byte array