You need to enable JavaScript to run this app. Truffle部署MasterChef.sol时如何传入合约类型构造参数
优惠活动
大模型
产品
解决方案
定价
更多

Truffle部署MasterChef.sol时如何传入合约类型构造参数

Truffle部署MasterChef合约时传入合约类型参数的操作方法

Solidity中的合约类型参数本质是合约地址,部署时直接传入对应合约的已部署链上地址即可,具体操作步骤如下:

  • 第一步:将CakeToken.sol、SyrupBar.sol、MasterChef.sol三个合约源文件全部放入Truffle项目的contracts目录下,执行truffle compile确保合约编译无报错。
  • 第二步:编写migrations/deploy_contracts.js部署脚本,参考代码如下:
// 引入三个合约的编译产物
const CakeToken = artifacts.require("CakeToken");
const SyrupBar = artifacts.require("SyrupBar");
const MasterChef = artifacts.require("MasterChef");

module.exports = function(deployer, network, accounts) {
  // 按依赖顺序依次部署合约
  deployer.deploy(CakeToken)
    .then(() => CakeToken.deployed())
    .then(cakeInstance => {
      // 部署SyrupBar,构造函数传入已部署的CakeToken地址
      return deployer.deploy(SyrupBar, cakeInstance.address)
        .then(() => SyrupBar.deployed())
        .then(syrupInstance => {
          // 部署MasterChef,传入5个构造函数参数,后三个参数按自身业务需求修改
          return deployer.deploy(
            MasterChef,
            cakeInstance.address,
            syrupInstance.address,
            "替换为你的dev接收地址",
            "替换为每区块释放的CAKE数量",
            "替换为奖励开启的起始区块号"
          )
        })
    })
};
  • 如果你已经提前部署过CakeToken和SyrupBar,不需要重复部署的话,可以直接通过CakeToken.at("已部署的Cake合约地址")获取合约实例,直接将两个已有合约地址传入MasterChef构造函数即可。
  • 部署前请确认你的truffle-config.js中已经正确配置币安智能链的RPC节点、部署账户私钥、gasLimit等参数,避免部署失败。

内容的提问来源于stack exchange,提问作者Azeem Lodhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 21:45:11