部署合约
通过 Javascript 脚本将我们的合约文件部署到 YOUChain 网络,此命令与 migrate 命令作用相同。
youbox deploy
1、一个部署文件示例:
注意:此处合约编译后的文件会存储在 ./build/contracts 目录下,请不要手动修改这些文件
const Migrations = artifacts.require("Migrations");
module.exports = function (deployer) {
deployer.deploy(Migrations);
};
如果合约依赖 Library,且 Library 中包含 external 或 public 方法,可参考:
const ConvertLib = artifacts.require("ConvertLib");
const MetaCoin = artifacts.require("MetaCoin");
module.exports = function (deployer) {
deployer.deploy(ConvertLib);
deployer.link(ConvertLib, MetaCoin);
deployer.deploy(MetaCoin);
};
此处也可以使用 Promise 来执行部署任务。
// Deploy A, then deploy B, passing in A's newly deployed address
deployer.deploy(A).then(function() {
// 或输出一些日志
return deployer.deploy(B, A.address);
});
所有部署需要使用 module.exports 导出,且导出函数需要 deployer 对象作为参数。
2、另外,还可根据网络判断不同的部署流程,需要添加 network 参数:
module.exports = function(deployer, network) {
// Add data if we're not deploying to the mainNet.
if (network != "mainNet") {
deployer.exec("add_extra_data.js");
}
}
3、还可添加 accounts 参数,获取可用账户列表,这个账户列表是 YOUChain 客户端或 youchain.provider 中的账户:
module.exports = function(deployer, network, accounts) {
// return list of account
console.log('accounts: ' + accounts);
}
4、deployer 部署器用法
- deployer.deploy(contracts, args...)
// 部署单个合约
deployer.deploy(A, arg1, arg2);
// 部署多个合约
deployer.deploy([
[A, arg1, arg2, ...],
B,
[C, arg1]
]);
- deployer.link(library, contracts)
// 连接一个合约
deployer.link(libA, A);
// 连接多个合约
deployer.link(libA, [A, B, C]);
- deployer.autolink()
// 自动关联合约依赖的所有库, 前提:所有依赖库已部署
deployer.deploy([libA, libB]);
deployer.autolink();
- deployer.then()
// Promise 语法
deployer.deploy(A).then(function(){
return deployer.deploy(B, A.address);
})
- deployer.exec()
// 可以执行外部脚本
deployer.exec('/path/to/file.js');