solidity TIL) 220402 배열,함수,data types

2022. 4. 2. 17:15코딩/solidity

 

1. Remix IDE를 통한 컨트랙트 배포

1) // SPDX-License-Identifier : GPL-30 

라이센스 명시를 안하면 에러가 난다. 

2) pragma solidity >= 0.7.0 < 0.9.0; 솔리디티 버전 명시 + Solidity는 객체지향형 프로그래밍 언어 

3) 컨트랙트 작성 

프로그래밍 언어를 처음 시작할 때 print문을 찍어보는게 국룰이지만 solidity는 print function이 없어서 log 형태로 출력해봐야 됨 

4) Compile > Deploy 후 해당 컨트랙트가 잘 배포가 되었는지를 확인해야함

내가 작성한 hi 라는 컨트랙트가 잘 배포되었으며 

단순히 hi 변수의 내용을 출력하는 것이 아닌(기존 프로그래밍 언어는 hi 라는 변수에 담긴 내용을 터미널에 출력해서 우리가 그 결과를 확인하는 것인데, 컨트랙트 배포가 결은 같으나 조금 다르게 느껴졌다. ) 

 

Hello 컨트랙트가 hi라는 변수에 담긴 내용 가서 확인하고 출력해주는 의미 

 

>> 그리고 JAVA나 대부분의 프로그래밍 언어에서는 접근제어자 데이터타입 변수명 = 값; 이렇게 써주는데 

solidity는 데이터타입을 먼저 써주고 그 다음이 접근제어자라는 것이.. 순서가 뒤바껴서 조금 헷갈리긴 하다.

 

 

 

2. data type 

address : 20바이트(이더리움의 address 크기)를 담을 수 있다. 모든 컨트랙트의 기반이 된다. 

address는 balance와 transfer이라는 멤버를 가지고 있다. 

balance 속성 이용 > address 잔고 조회 

transfer 함수 이용 > 다른 address에 ether을 보낼 수 있다. 

 

문자열 리터럴의 타입은 byte1~byte32로 암시적으로 변환될 수 있으며 적합한 크기라면 bytes와 string으로도 변환될 수 있다. 

// SPDX-License-Identifier:GPL-30

pragma solidity >=0.7.0 < 0.9.0; 

contract lec2 { 
    bool public a = true; 
    bool public b = false; 
    bool public c = true && false; 
    bool public d = true == false; 

    bytes4 public bt3 = 0x12345678; 
    bytes public bt2 = "Hello";

    address public addr = 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99;

    bool public b1 = !true; 

    int8 public it = 4; 
    uint256 public it2 = 300;
    //uint8 public it3 = 256; // 범위 넘어가서 TypeError: Type int_const 256 is not implicity convertible to expected type uint8.

}

 

3. 배열 

- storage는 블록체인 상에 영구적으로 저장되는 변수 

// SPDX-License-Identifier:GPL-30 

pragma solidity >= 0.7.0 < 0.9.0; 

// Solidity의 배열은 값을 꺼내도 인덱스와 값이 한 칸씩 앞으로 이동하는게 아니라 그 자리 그대로 남아있음
contract lec18 { 

    uint256[] public ageArray; //uint256 크기의 ageArray 이름을 가진 배열 선언 

    // view는 public과 같은 접근제한자 앞이나 뒤 어디든 붙이기 가능 
    // view는 storage state를 읽을 수 있지만 state 값 변경 불가능 
    // 위에서 ageArray의 length를 리턴하므로 당연히 storage state를 읽은 것이다. 그렇기 때문에 view를 넣어줘야 한다. 
    // 만약에 AgeLength에서 storage state를 바꾼다면 어떻게 될까? view 말고 아무것도 안써주면 된다 >> pure  
    function AgeLength() public view returns(uint256) { 
        return ageArray.length; 
    }


    // 나이 넣기 
    // 0->50 / 1->70/ Length:2 
    function AgePush(uint256 _age)public{ 
        ageArray.push(_age);
    }

    function AgeGet(uint256 _index)public view returns(uint256){
        return ageArray[_index];
    }

    // pop은 맨 위의 값을 꺼내 오는 것이므로 최신의 값을 삭제함. 배열 크기는 안줄어들음 
    function AgePop() public { 
        ageArray.pop();
    }

    function AgeDelete(uint256 _index)public { 
        delete ageArray[_index];
    }

    // 나이를 바꾸려면 인덱스 번호와 나이 필요 
    function AgeChange(uint256 _index, uint256 _age) public {
        ageArray[_index]=_age;
    }
  
}

 

 

solidity 함수 작성 방법 

https://solidity-kr.readthedocs.io/ko/latest/style-guide.html
https://solidity-kr.readthedocs.io/ko/latest/style-guide.html

 

함수 타입 

function (<parameter types>) {internal|external} [pure|constant|view|payable] [returns (<return types>)]

함수 타입이 아무것도 반환되지 않는다면 returns(<return types>) 이 부분을 전체 생략해야 한다.