The Complete NFT Web Development Course - Zero To Expert - 섹션 7: Optional - Extra Solidity Practice For Beginners & Glossary

2022. 2. 20. 20:59· 스터디📖/블록체인
목차
  1. 55. General Introduction To Variables & Types
  2.  
  3. 57. Writing Solidity Variables
  4. 60. Introduction To Decision Making - If Statements in Solidity
  5.  
  6. 63. Arrays in Solidity - Pop Push & Length Methods
  7.  
  8. 64. Arrays in Solidity - Delete
  9. 65. Exercise - Maintain a Compact Array
  10. 66. Solution - Maintain a Compact Array
  11.  
  12. 67. What Are Abstract Contracts in Solidity

55. General Introduction To Variables & Types

Variables : Variables are ussed to store information to be referenced and manipulated in a computer program

Three main types of variables: Booleans, Integers, Strings

  • Boolean : bool - true/false
  • Integer : uint - Singed and unsigned integers of varying sizes
  • String : string - data values that are made up of ordered sequences of characters

 

57. Writing Solidity Variables

pragma solidity >= 0.7.0 < 0.9.0;

contract learnVariables {
    uint chocolateBar = 10;
    uint storeOwner = 300;
    bool lieDetector = true;
    string errorMessageText = "Error!";
}

했던 걸 반복하는 느낌인데.. 이거 했다고 awsome! 넌 이제 뭐든 할 수 있어! 이러니까 뭔가 ㅋㅋㅋㅋㅋ 점점 빨라지는 재생속도..

 

60. Introduction To Decision Making - If Statements in Solidity

If statement: The if statement is the fundamental control statement that allows Solidity to make decisions and execute statements conditionally.

contract DecisionMaking {
    uint orange = 5;

    function validateOranges() public view returns (bool) {
        if (orange == 5) {
            return true;
        } else {
            return false;
        }
    }
}

갑자기 public 뒤에 view가 붙어서 당황했는데 나중에 설명해준다고... 하지만 찾아봤지

https://www.tutorialspoint.com/solidity/solidity_view_functions.htm

View functions ensure that they will not modify the state. A function can be declared as view. The following statements if present in the function are considered modifying the state and compiler will throw warning in such cases.

상태를 변경하지 않을 경우에 사용한다고 한다. 실제로 저 안에서 orange에 다른 값을 줘봤더니 warning이 뜨더라는

 

63. Arrays in Solidity - Pop Push & Length Methods

Array is a data structure, which stores a fixed-size sequential collection of elements of the same type.

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

contract learnArrays {
    uint[] public myArr;
    uint[] public myArr2;
    uint[200] public myFixedSizeArr;

    // push() method adds one or more elements to the end of an array and returns the new length of the array
    function push(uint number) public {
        myArr.push(number);
    }

    // pop() method removes the last elemenet from an array and returns that value
    function pop() public {
        myArr.pop();
    }

    // length is a string property that is used to determine the length of a string
    function getLength() public view returns (uint) {
        return myArr.length;
    }
}

 

64. Arrays in Solidity - Delete

    // when you delete in your array the length remains the same
    function remove(uint index) public {
        delete myArr[index];
    }

myArr = [1, 2, 3] 이라고 할 때, remove(2) 를 호출하면 myArr = [1, 2, 0] 이 된다. 즉 value만 0으로 초기화되고 길이는 그대로인 것.

 

65. Exercise - Maintain a Compact Array

Exercise: create a function that can fully remove on item from an array

  1. Create an Empty array called changeArray
  2. Create a function called removeElement which sets the index argument of the array to the last element in the array
  3. remove the last index from that function with the pop method
  4. Create a function called test which pushes 1 2 3 4 into changeArray
  5. remove the element 2 from the array when the contract is called

쓰읍.. delete로는 데이터가 삭제가 안되니까, trick을 써서 삭제하고자 하는 인덱스의 value를 맨 마지막으로 옮겨서 pop으로 삭제하는 걸 구현해보라는데.. 이게.. 맞나..? 순서가.. 중요하지.. 않아...? 왜...?

66. Solution - Maintain a Compact Array

    uint[] public changeArray;

    function removeElement(uint i) public {
        changeArray[i] = changeArray[changeArray.length - 1];
        changeArray.pop();
    }

    function test() public {
        for (uint i=1; i<=4; i++){
            changeArray.push(i);
        }
    }

그래도 해보라니까 일단 했다. 찾아보니까 delete 함수밖에 없고 진짜 직접 만들어서 구현해야 하는 것 같다.
https://ethereum.stackexchange.com/questions/1527/how-to-delete-an-element-at-a-certain-index-in-an-array

 

67. What Are Abstract Contracts in Solidity

Abstract Contracts

Abstract Contract is one which contains at least one function without any implementation.

Such a contract is used as a base contract.

Generally an abstract contract contains both implemented as well as abstract functions.

Derived contract will implement the abstract function and use the existing functions as and when required.

// base
abstract contract X {
    function y() public view virtual returns(string memory);
}

// derive
contract Z is X {
    function y() public override view returns(string memory) {
        return "hello";
    }
}

위의 코드를 deploy하면 alert가 뜨는데, contract를 Z contract로 바꾼 후 deploy하면 된다.

 

abstract contract에 대해 더 검색해봤는데, 이전 버전에서는 abstract라는 keyward를 따로 붙이지 않았던 듯 하다. 그래서 당황하다가 그냥 최근 docs를 보고 해결.. 아무튼 요즘엔 붙이는 게 맞는 것 같다. (아니 근데 안 붙이는 버전이 2020이던데 정말 빠르다..)

https://docs.soliditylang.org/en/v0.8.12/contracts.html?highlight=abstract#abstract-contracts

저작자표시 (새창열림)

'스터디📖 > 블록체인' 카테고리의 다른 글

The Complete NFT Web Development Course - Zero To Expert - 섹션 10: Building NFT Smart Contracts - First Steps  (0) 2022.02.24
The Complete NFT Web Development Course - Zero To Expert - 섹션 9: Set Up NFT Project Configuration & Architecture  (0) 2022.02.23
The Complete NFT Web Development Course - Zero To Expert - 섹션 6: Optional - Crash Course Solidity (Programming For Complete Beginners) Part II  (0) 2022.02.19
The Complete NFT Web Development Course - Zero To Expert - 섹션 5: Optional - Crash Course Solidity (Programming For Complete Beginners) Part I  (0) 2022.02.18
The Complete NFT Web Development Course - Zero To Expert - 섹션 4: What is the ERC721 NFT Standard  (0) 2022.02.18
  1. 55. General Introduction To Variables & Types
  2.  
  3. 57. Writing Solidity Variables
  4. 60. Introduction To Decision Making - If Statements in Solidity
  5.  
  6. 63. Arrays in Solidity - Pop Push & Length Methods
  7.  
  8. 64. Arrays in Solidity - Delete
  9. 65. Exercise - Maintain a Compact Array
  10. 66. Solution - Maintain a Compact Array
  11.  
  12. 67. What Are Abstract Contracts in Solidity
'스터디📖/블록체인' 카테고리의 다른 글
  • The Complete NFT Web Development Course - Zero To Expert - 섹션 10: Building NFT Smart Contracts - First Steps
  • The Complete NFT Web Development Course - Zero To Expert - 섹션 9: Set Up NFT Project Configuration & Architecture
  • The Complete NFT Web Development Course - Zero To Expert - 섹션 6: Optional - Crash Course Solidity (Programming For Complete Beginners) Part II
  • The Complete NFT Web Development Course - Zero To Expert - 섹션 5: Optional - Crash Course Solidity (Programming For Complete Beginners) Part I
호프
호프
호프
Untitled
호프
전체
오늘
어제
  • 분류 전체보기 (341)
    • 오류😬 (4)
    • 스터디📖 (96)
      • 웹 개발 기초 (8)
      • Spring (20)
      • ML, DL (30)
      • Node.js (22)
      • React (0)
      • 블록체인 (12)
      • Go (3)
      • Javascript (1)
    • 알고리즘💻 (153)
      • 그리디 (23)
      • Bruteforce&Backtracking (16)
      • DP (17)
      • 이분탐색&정렬&분할정복 (17)
      • 누적합&투포인터 (6)
      • 스택&큐&덱 (19)
      • 그래프(DFS&BFS) (19)
      • 트리 (7)
      • 우선순위큐&다익스트라 (11)
      • 벨만포드&플로이드와샬 (8)
      • map&set&number theory (5)
      • 기타 (5)
    • 프로젝트 (3)
      • 캡스톤 디자인 프로젝트 (3)
    • 블록체인🔗 (3)
      • Solana (2)
      • 개발 (0)
      • Harmony (1)
    • ASC (6)
    • CS (73)
      • 데이터베이스 (12)
      • 클라우드컴퓨팅 (21)
      • 운영체제 (11)
      • 컴퓨터네트워크 (14)
      • 블록체인응용 (15)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 복습

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.1
호프
The Complete NFT Web Development Course - Zero To Expert - 섹션 7: Optional - Extra Solidity Practice For Beginners & Glossary
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.