📌 개발 환경
- nodejs
- truffle
- npm -g install truffle@5.0.2
- ganache-cli
- 블록체인을 쉽게 Mock을 해놓은 테스트해놓은 app
- vscode - solidity extension
- metamask
📌 truffle 세팅
폴더 생성한 뒤 해당 폴더에서 truffle init
- contracts
- 스마트 컨트렉트 관련 파일들을 작성
- migrations
- 배포 관련 스크립트를 작성
- test
- solidity, json을 이용해서 만드는 테스트가 있는데, 보통 json을 이용해서 만드는 테스트로 사용한다.
📦 Compile
pragma solidity >= 0.4;
contract Lottery{
}
contracts 폴더 안에 Lottery.sol 파일 생성 후 위 코드 작성
- truffle compile
compile을 하게 되면 builld 폴더가 생성된다.
해당 파일안에는 bytecode와 배포에 필요한 메타 데이터 등이 생성되게 된다.
📦 Migrations
migrations 폴더 안에 있는 initial 파일 복사해서 새로 만든다.
artifacts.require("migrations")는 build 폴더 안에 있는 migrations를 가져오게 된다.
그래서 위의 코드는 migrations안에 있는 byte코드를 가져와서 deploy를 하는 코드이다.
📚 deployer
스마트 컨트랙트를 배포하기 위해서는 이더리움이 있는 주소가 있어야 한다. truffle-config.js 에서 내가 사용할 주소를 설정하고, 해당 주소가 deployer에게 injection이 된다.
const Lottery = artifacts.require("Lottery");
module.exports = function(deployer) {
deployer.deploy(Lottery);
};
- 위의 코드에서 Migration → Lottery로 바꿔서 배포하자.
📦 배포
배포를 하기 위해서는 blockchain network가 있어야 한다.
해당 네트워크로는 ganache를 사용하겠다.
- ganache-cli -d -m tutorial
- tutorial 이라는 이름을 사용하자.
📚 truffle-config
어디다가 배포를 할 지 설정해주어야 한다.
development를 주석 해제 해주자.
그런 다음 아래 명령어를 입력하자.
- truffle migrate
다시 한번 배포 할 때는 그냥 truffle migrate를 다시 사용하는게 아니고, --reset 옵션을 주어서 truffle migrate --reset 으로 배포를 해주는게 좋다.
build를 처음부터 하게 해준다고 생각하면 될 것 같다.
📦 Migrations.sol
> smartContract의 버전관리를 해주는 역할
몇 번째 deploy까지 사용했는지 해당 파일에 기록해둔다.
해당 completed 변수에 저장해둔다.
Migrations 파일은 건들지 않는 것이 좋다.
📌 Truffle을 활용한 스마트 컨트렉트 상호작용
📦 Lottery.sol
pragma solidity >= 0.4;
contract Lottery{
address public owner;
constructor() public{
owner = msg.sender;
}
function getSomeValue() public pure returns (uint256 value){
return 5;
}
}
- truffle migrate --reset
📦 truffle console
콘솔창에서 현재 프로젝트로 이동 뒤,
- truffle console
- geth console을 열어서 접근하는 것과 똑같다.
- web3에는 다양한 함수들이 존재한다.
- 우리가 많이 사용할 것은 web3.eth이다.
- 그래서 eth = web3.eth 로 변수에 넣어주자.
📚 예시 : eth.getAccounts()
📚 Lottery.address
- 위의 주소
- Lottery.json > networks > address 의 값을 가져온 것
📚 사용
- Lottery.deployed().then(function(instance){lt=instance})
- lt라는 변수 안에 해당 lottery instance가 들어간 것을 확인할 수 있다.
📌 Truffle을 활용한 테스트
📦 lottery.test.js
test > lottery.test.js 생성
const Lottery = artifacts.require("Lottery");
contract('Lottery',function([deployer,user1,user2]){
beforeEach(async()=>{
console.log('before each')
})
it('Basic test',async()=>{
console.log("basic test")
})
})
- truffle test
- test 폴더 하위의 모든 테스트 파일들이 실행된다.
- truffle test test/lottery.test.js
- 단위테스트 . 파일 지정
📦 lottery.test.js - v2
const Lottery = artifacts.require("Lottery");
contract('Lottery',function([deployer,user1,user2]){
let lottery;
beforeEach(async()=>{
console.log('before each')
lottery = await Lottery.new()
})
it('Basic test',async()=>{
console.log("basic test")
let owner = await lottery.owner();
let value = await lottery.getSomeValue();
console.log(`Owner : ${owner}`)
console.log(`value : ${value}`)
assert.equal(value,5)
})
})
- assert를 사용할 수 있다.
이 블로그 글은 아래 인프런 강좌를 참조하여 작성하였습니다.
Ethereum 실전! 초보자를 위한 Lottery Dapp 개발
→ ( https://www.inflearn.com/course/ethereum-dapp/dashboard )
'SSAFY 6기 > 📌blockchain' 카테고리의 다른 글
인프런 nft 강좌 #3 - react 개발 (0) | 2022.03.14 |
---|---|
인프런 nft 강좌 #2 - react 세팅 (0) | 2022.03.14 |
인프런 nft 강좌 #1 스마트 컨트랙트 작성 (0) | 2022.03.10 |
댓글