Truffle에서 migration 사용하기
한 환경에서 배포를 다양하게 해야할 경우
migration을 사용하여서 배포가 가능하다.
https://trufflesuite.com/docs/truffle/reference/truffle-commands/
Truffle Commands - Truffle Suite
Truffle Commands This section will describe every command available in the Truffle application. Usage All commands are in the following form: truffle [options] Passing no arguments is equivalent to truffle help, which will display a list of all commands an
trufflesuite.com
truffle migrate [--reset] [--f <number>] [--to <number>] [--network <name>] [--compile-all] [--verbose-rpc] [--dry-run] [--interactive] [--skip-dry-run] [--describe-json]
이런식으로 그냥 migrate를 할 경우 전체가 배포되게 된다.
하지만 옵션을 설정할 경우 특정 파일만 배포할 수 있게 된다.
Options:
- --reset: Run all migrations from the beginning, instead of running from the last completed migration.
- --f <number>: Run contracts from a specific migration. The number refers to the prefix of the migration file.
- --to <number>: Run contracts to a specific migration. The number refers to the prefix of the migration file.
- --network <name>: Specify the network to use, saving artifacts specific to that network. Network name must exist in the configuration.
- --compile-all: Compile all contracts instead of intelligently choosing which contracts need to be compiled.
- --verbose-rpc: Log communication between Truffle and the Ethereum client.
- --dry-run: Fork the network specified and only perform a test migration.
- --skip-dry-run: Skip the test migration performed before the real migration.
- --interactive: Prompt to confirm that the user wants to proceed after the dry run.
- --describe-json: Prints additional status messages.
일단
--f 를 넣고 숫자를 지정하면 해당 숫자부터 시작이 되고
--to 를 넣고 숫자를 지정하면 해당 숫자까지 진행이 된다.
--network를 넣고 실행을 하면 truffle-config.js에 정의되어 있는 네트워크에 맞게 배포가 된다.
base) klaytn % truffle migrate --f 2 --to 2
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'ganache'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
실행해본 결과 잘 되는것을 확인할 수 있었다.
일단 배포의 경우 단일 배포를 가정하여 모든 배포 스크립트를 쪼개서 사용하기로 하였다.
그리고, 각 실행하는 함수에 대한 스크립트를 작성할 예정이다.
migration을 조금 더 잘 작성하면 배포 스크립트의 경우 다양하게 만들 수 있을 것으로 판단하였다.
var a, b;
deployer.then(function() {
// Create a new version of A
return A.new();
}).then(function(instance) {
a = instance;
// Get the deployed instance of B
return B.deployed();
}).then(function(instance) {
b = instance;
// Set the new instance of A's address on B via B's setA() function.
return b.setA(a.address);
});
이런식으로 배포를 진행할 시 한번에 여러개의 함수들을 모두 배포할 수 있다고 한다.
이걸 이용해서 전체 배포 스크립트를 만들어봐야겠다.
그리고 동기/비동기 처리도 가능하다고 한다.
module.exports = async function(deployer) {
// deploy a contract
await deployer.deploy(MyContract);
//access information about your deployed contract instance
const instance = await MyContract.deployed();
}