don't stop believing

Contract 개발 환경 (Remix; Browser-Solidity) 본문

Ethereum

Contract 개발 환경 (Remix; Browser-Solidity)

Tongchun 2018. 8. 26. 17:28

Browser-Solidity를 이용한 Contract 개발 환경을 구성해 보겠습니다.


https://remix.ethereum.org


Browser-Solidity(Remix)는 Solidity 언어의 기여자(Contributor)가 개발한 Solidity 언어 전용 웹 브라우저 기반 통합 개발 환경입니다. 웹 브라우저에서 계약 코드 작성, 컴파일, 이더리움 노드에 배포, 계약 메서드의 실행 등 일반적으로 필요한 작업을 수행할 수 있습니다.


Browser-Solidity를 이용하는 방법은 두 가지입니다. 하나는 인터넷에 공개돼어 있는 사이트(https://remix.ethereum.org)에 접속해 온라인으로 사용하는 방법과 다른 하나는 깃허브에서 Zip 파일을 내려 받아 오프라인으로 이용하는 방법입니다.


Browser-Solidity를 깃허브에서 다운받아 설치해 보겠습니다.

참고로 최신 Browser-Solidity의 github url은 https://github.com/ethereum/remix-ide입니다.

https://github.com/ethereum/browser-solidity 는 이전 버전의 repository 입니다.


Browser-Solidity 최신 버전인 Remix는 npm으로 간단하게 설치할 수 있습니다.

(https://docs.npmjs.com/getting-started/installing-node)


npm이 설치되어 있다면 아래처럼 진행합니다.

$ npm install remix-ide -g

위 명령으로 remix-ide가 설치되었다면 이제 실행합니다.

$ remix-ide
setup notifications for /Users/tongchunkim/Documents/Test_Ethereum/remix-ide
Shared folder : /Users/tongchunkim/Documents/Test_Ethereum/remix-ide
Starting Remix IDE at http://localhost:8080 and sharing /Users/tongchunkim/Documents/Test_Ethereum/remix-ide
Sun Aug 26 2018 17:13:05 GMT+0900 (대한민국 표준시) Remixd is listening on 127.0.0.1:65520

remix-ide 명령을 실행하면 8080 포트로 웹 서버가 실행됩니다.

브라우저를 열고 http://127.0.0.1:8080로 접속합니다.


remix를 통해 이더리움 노드(로컬 테스트넷)에 접속해 보겠습니다.

먼저 geth의 rpc 서버가 실행되어 있아야 합니다.


geth --networkid 45 --nodiscover --maxpeers 0 --datadir /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/ --mine --minerthreads 1 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpccorsdomain "*" --rpcapi "admin, db, eth, debug, miner, net, shh, txpool, personal, web3" --unlock 0,1 --password /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/password --verbosity 6 console 2>> /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/geth.log


remix의 우측 창에서 Run > Environment 값을 Web3 Provider로 변경합니다.

그러면 창이하나 뜨고 geth RPC 서버 IP를 입력합니다. 저는 http://192.168.0.5:8545로 입력했습니다.

(http://localhost:8545 도 접속이 잘 됩니다.)


OK를 클릭하면 Geth RPC 서버와 연결하며 이상이 없다면 account 정보도 변경됩니다.

계속 mining을 돌렸더니 Ether도 많이 쌓였네요.


이제 HelloWorld를 찍어봅시다.

remix의 왼쪽 창에서 + 버튼을 누르고 HelloWorld.sol 파일을 생성합니다.

그리고 아래와 같이 solidity 코드를 작성합니다.

pragma solidity ^0.4.8;

contract HelloWord {
	
	// 상태 변수 선언
	string public greeting;

	// 생성자
	function HelloWorld(string _greeting) {
		greeting = _greeting;
	}

	// 메서드 선언
	function setGreeting(string _greeting) {
		greeting = _greeting;
	}

	function say() constant returns(string) {
		return greeting;
	}
}


Deploy의 입력창에 "Hello World"를 입력하고 Deploy를 클릭합니다.

그럼 중앙 하단에 있는 로그창에 Creating of HelloWorld pending... 이 표시되며 mining으로 블록이 생성되면 Transaction에 기록됩니다.


그런데 X 가 나오네요. txIndex가 0으로 오고있습니다.



에러가난 로그의 세부 정보는 아래와 같습니다.


뭐가 문제인지는 확인해 봐야겠습니다.

오른쪽 창에 Deployed Contracts가 보입니다.



HelloWorld at 0x8f5...9bc58 (blockchain)을 클릭하면 상태변수와 메서드를 볼 수 있습니다.

먼저 greeting을 클릭하면 0:string: Hello World가 나타납니다. say도 클릭하면 동일하게 0:string: Hello World가 나옵니다.


setGreeting 옆의 input 창에 "Hello Tongchun"을 입력하고 setGreeting을 클릭하면 Transaction to Helloworld.setGreeting pending ... 로그가 나옵니다. 마찬가지로 mining으로 블록이 생성되면 Transaction이 기록되면서 contract를 생성합니다.




이제 기존 contract에 접근해 보겠습니다.

먼저 접근하려는 contract의 블록 주소를 복사합니다. Deployed Contracts에서 blockchain 몊의 복사 이미지(Copy value to Clipboard)를 클릭하면 복사할 수 있습니다.

제가 확인하려는 Contract의 블록 주소는 0x8f5a08222816956149af0674b8c581238179bc58 입니다.


remix가 실행되고 있는 브라우저를 새로고침하거나 재실행해 초기화 해줍니다. 그리고 앞서 진행한 것과 같이 Environment를 Web3 Provider로 지정하고 http://localhost:8545에 접속합니다.


At Address 옆의 입력창에 블록 주소를 붙여넣고 At Address를 클릭합니다.

그럼 Deployed Contracts에 블록에 있는 HelloWorld Contract가 나옵니다. greeding과 say를 클릭하면 블록의 Transaction에 기록된 Hello Tongchun이 리턴됩니다.



Deploy 할때 로그에 에러로 나오는 건 확인해봐야 겠지만 Transaction에 Contract를 배포하고 다시 불러오는 것은 정상적인 것 같습니다.

여기까지 remix (구 Browser-Solidity) 개발환경 확인이었습니다.


Comments