반응형

💡Hello World!
모든 언어의 시작은 Hello World 찍어보기부터!
오늘은 솔리디티 언어로 간단하게 Hello World를 반환하는 컨트랙트를 작성해보고자 한다.
➤ Step 1. http://remix.ethereum.org/ 에 접속한다.

➤ Step 2. helloWorld.sol 파일을 생성한다.
➤ Step 3. SPDX 라이센스와 pragma를 설정한다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
➤ Step 4. contract 키워드를 사용해 helloWorld 컨트랙트를 만든다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract helloWorld {}
➤ Step 5. renderHelloWorld 함수를 만든다. 접근 수준은 public 으로 설정한다. renderHelloWorld 함수는 "Hello World!" 라는 문자열을 반환하는 함수이다. 따라서 returns 의 형태를 string으로 설정한다. 스토리지에 영구적으로 저장하지 않기 때문에 데이터 저장 위치는 memory로 지정한다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract helloWorld {
function renderHelloWorld () public returns (string memory){}
}
➤ Step 6. 함수 안에 "Hello World!" 를 리턴하는 구문을 작성한다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract helloWorld {
function renderHelloWorld () public returns (string memory){
return "Hello World!";
}
}
리턴문은 다음과 같이 작성할 수도 있다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract helloWorld {
// 함수가 실행되는 동안 greeting 변수를 사용할 수 있다.
// 함수가 끝까지 실행되면 greeting 변수의 값을 반환합니다.
function renderHelloWorld () public returns (string memory greeting){
greeting = "Hello World!";
}
}
➤ Step 7. renderHelloWorld 함수는 스토리지를 읽거나 쓰지 않기 때문에 pure 키워드를 추가한다.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract helloWorld {
// 함수가 실행되는 동안 greeting 변수를 사용할 수 있다.
// 함수가 끝까지 실행되면 greeting 변수의 값을 반환합니다.
function renderHelloWorld () public pure returns (string memory greeting){
greeting = "Hello World!";
}
}
실행결과

반응형
'블록체인 > WEB3 개발' 카테고리의 다른 글
Ganache/Ganache-cli 설치하기, Ganache와 Remix 연동, Ganache Network에 스마트 컨트랙트 배포 (0) | 2022.07.15 |
---|---|
Geth를 사용해 스마트 컨트랙트 빌드하기 (0) | 2022.07.13 |
Ropsten 테스트넷에 컨트랙트 배포하기 (0) | 2022.07.12 |
Remix에 메타마스크(MetaMask) 연결하기 (0) | 2022.07.12 |
로컬 컴퓨터에 Remix 코드 저장하기 - Remixd (0) | 2022.07.12 |
댓글