로컬에서 이더리움 네트워크를 구성하고 테스트해보자
npm install -g truffle
mkdir test
cd test
truffle init
truffle init을 하면 폴더가 생성되는데
truffle develop
truffle console로 입장할 수 있고, exit로 나올 수 있다.
truffle compile
truffle migrate
이미 배포된 컨트랙트를 수정하고 다시 배포하려면
truffle migrate --reset
deploy로 배포만 할 뿐 아니라 web3를 이용해 추가적인 작업도 실행할 수 있다.
deployer.deploy()만 해도 배포는 되지만 배포 후에 트랜잭션, 컨트랙트의 정보를 확인하고 싶으면 아래오 같이 확인해볼 수 있다.
const Web3 = require('web3');
let web3 = new Web3('http://127.0.0.1:7545');
//컨트랙트를 참조함
var contract = artifacts.require("ContractName");
module.exports = async function(deployer) {
// 컨트랙트 배포
await deployer.deploy(contract);
// 이 밑으로는 현재 코드에서는 필요한 부분은 아님
// 초기 설정에 필요한 컨트랙트 정보 가져오기
const ContractInfo = await contract.deployed().then((el)=>{
return {abi : el.abi,
address: el.address}
//그냥 return el을 하면 디테일하게 볼 수 있다.
});
//서버 계정 주소 저장
const accounts = await web3.eth.getAccounts();
const serverAddress = accounts[0]
};
truffle test
테스트는 solidity로 작성할 수도, js로 작성할 수도 있다.
//test.sol
pragma solidity ^0.8.10;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/contractFile.sol";
contract Test {
function test() public {
ContractName temp = new ContractName();
Assert.equal(테스트할 값, 나와야하는 결과값, 메시지);
//예시
//Assert.equal(temp.name(), "EAToken", "value equal test");
}
}
//test.js
var contract = artifacts.require("ContractName");
contract("ContractName", function () {
it ("is_greet_works_well", async function () {
const instance = await contract.deployed();
await instance.testFunctuion(parameters);
assert.equal(테스트할 값, 나와야하는 결과값, 메시지);
})
})
npm install -E openzeppeling-solidity
설치 후
import "@openzeppelin/contracts/token/ERC20/ERC20/sol"
등과 같은 방식으로 임포트하여 사용할 수 있다.
npm install -g @remix-project/remixd
설치 후
remixd -s __dirname --remix-ide https://remix.ethereum.org
실행 후 remix에서 workspace를 로컬로 설정해주면 로컬 환경을 remix로 열어볼 수 있다.
이 때 자동으로 동기화되기때문에 추가로 작성한 내용이 사라질 수도 있다는 것을 명심하자.