My Ether Wallet(MEW) 사용법, 트러플(Truffle) 환경에서 ERC-20 토큰을 테스트넷에 배포하기
2021. 10. 20. 09:59ㆍBlockchain/Truffle
기본 세팅
- 가나슈와 메타마스크 연결
- truffle init
- truffle-config.js 수정 // 주석 해제
작업 순서
- 환경 설정
- 코드 작성
- 가나슈 배포(local)
- 테스트넷 배포(테스트넷 이더리움)
My Ether Wallt (MEW)
- MEW란?
* 메타마스크와 같은 지갑, 온라인 브라우저(사이트)로 구현되어 있음
* 스마트 컨트랙트에 대한 메서드나 내용들을 볼 수 있는 기능이 있음
* 프론트단이 없어도 결과 확인 가능 // postman과 비슷한듯
- 사용법
- 사이트 접속 (링크 : http://www.myetherwallet.com)
- 메타마스크 연결하기
- 연결 완료 확인
작업 시작
- npm init // root 디렉토리에서 작업
- npm install openzeppelin-solidity
- /node_modules/openzeppelin-solidity/contracts/token/ERC20/ 디렉토리 확인
- truffle version // 트러플 및 솔리디티 버젼 확인
- truffle-config.js에서 솔리디티 버젼 수정
- truffle version // 수정 된 솔리디티 버젼 확인
- truffle create contract IngToken
truffle create migration IngToken - IngToken.sol 코드 작성
// SPDX-License-Identifier: MIT pragma solidity >= 0.4.22 < 0.9.0; contract IngToken { string public name = "INGOO TOKEN"; string public symbol = "ING"; uint256 public decimals = 18; uint256 public totalSupply = 10000 * (10 ** decimals); // ** == 제곱 string text = "hello world 11"; constructor() public { } }
- 1634691029_ing_token.js 코드 수정
const IngToken = artifacts.require("IngToken"); module.exports = function (deployer) { deployer.deploy(IngToken); };
- truffle compile
truffle migrate - 배포 된 IngToken의 contract address 값 빼놓기
- ABI 가져오기
/build/contracts/IngToken.json 파일 안에 3번째~65번째 줄까지 복사
- Contract > Interact with Contract 선택 > 아래와 같이 입력 후 Interact 클릭
- 새로운 팝업창 뜨면서 리스트에 IngToken.sol에 적었던 변수 내용이 출력 됨
하지만 /* string text = "hello world 11"; */ 이 변수는 출력되지 않음, public 처리가 되지 않았기 때문
아래와 같이 테스트 가능
- IngToken.sol 코드 수정
// SPDX-License-Identifier: MIT pragma solidity >= 0.4.22 < 0.9.0; import '../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol'; contract IngToken is ERC20 { string public _name = "INGOO TOKEN"; string public _symbol = "ING"; // uint256 public _decimals = 18; uint256 public _totalSupply = 10000 * (10 ** uint256(decimals())); // ** == 제곱 string text = "hello world 11"; constructor() ERC20(_name, _symbol) { _mint(msg.sender, _totalSupply); // 토큰 생성 끝 } }
- /build/contract에 있는 기존 json 파일 삭제 후 새로 컴파일과 배포 진행
truffle comiple
truffle migrate - 새로 배포 된 IngToken의 contract address값 빼놓기
ABI도 새로 가져오기
/build/contracts/IngToken.json 파일 안에 3번째~65번째 줄까지 복사 - MEW에 해당 값 입력 후 Interact
- 메타마스크 import token으로 새 토큰 생성
- IngToken.sol 코드 수정
// SPDX-License-Identifier: MIT pragma solidity >= 0.4.22 < 0.9.0; import '../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol'; // Ownable.sol : 배포자만 실행 할 수 있게 해주는 라이브러리 import '../node_modules/openzeppelin-solidity/contracts/access/Ownable.sol'; // Pausable.sol : 메서드를 호출하면 해당 메서드를 사용 할 수 있음 // pause() : 컨트랙트 동작을 잠시 멈춤 // unpause () : 컨트랙트 동작을 잠시 실행 import '../node_modules/openzeppelin-solidity/contracts/security/Pausable.sol'; contract IngToken is ERC20, Ownable, Pausable { string public _name = "INGOO TOKEN"; string public _symbol = "ING"; // uint256 public _decimals = 18; uint256 public _totalSupply = 10000 * (10 ** uint256(decimals())); // ** == 제곱 string text = "hello world 11"; constructor() ERC20(_name, _symbol) { _mint(msg.sender, _totalSupply); // 토큰 생성 끝 } // onlyOwner로 배포자만 실행 할 수 있게 처리 function pause() public onlyOwner { _pause(); } function unpause() public onlyOwner { _unpause(); } }
- /build/contract에 있는 기존 json 파일 삭제 후 새로 컴파일과 배포 진행
truffle comiple
truffle migrate - 새로 배포 된 IngToken의 contract address값 빼놓기
ABI도 새로 가져오기
/build/contracts/IngToken.json 파일 안에 3번째~65번째 줄까지 복사
- 메타마스크에서 가나슈 1번 인덱스의 PRIAVET KEY로 계정 추가 후
테스트를 위해 추가 된 계정에 500 ING 보내기 - MEW에 해당 값 입력 후 Interact
- Pause 오류 발생 // 배포 먼저 진행함
- 링크 접속 후 회원가입 진행 (링크 : https://infura.io)
- 새 프로젝트 생성
- 프로젝트 선택 후 [SETTINGS] 탭 클릭
- truffle-config.js 파일 수정
- 메타마스크 회원 가입 시 받았던 시드 문구 필요
시드문구 기억 안날 경우 아래 참고 - root 디렉토리(truffle-config.js와 같은 경로)에 .secret 파일 생성 후 시드 문구 붙여넣기
- /build/contract에 있는 기존 json 파일 삭제 후 새로 컴파일과 배포 진행
truffle comiple // 컴파일 오류 발생 truffle migrate // 테스트넷에 배포하기 때문에 시간 소요 - 이더스캔 접속 (링크 : https://etherscan.io/)
- 검색창에 migrate 결과물 중 transaction hash값 입력 후 검색