首页 > 解决方案 > ReferenceError:迁移solidity文件时无法在初始化之前访问'TokenFarm'

问题描述

intial_deploy_contract.js 文件

const DappToken = artifacts.require('DappToken.sol')
const DaiToken = artifacts.require('DaiToken.sol')
const TokenFarm = artifacts.require('TokenFarm.sol')

module.exports = async function(deployer, network, accounts) {
  //Deploy Mock DAI Token
  await deployer.deploy(DaiToken)
  const daiToken = await DaiToken.deployed()

  //Deploy Dapp Token
  await deployer.deploy(DappToken)
  const dappToken = await DappToken.deployed()

  //Deploy TokenFarm
  await deployer.deploy(TokenFarm, dappToken.address, daiToken.address)
  const TokenFarm = await TokenFarm.deployed()

  //Transfer all tokens to TokenFarm(1 million)
  await dappToken.transfer(tokenFarm.address, '1000000000000000000000000')

  //Transfer 100 Mock DAI tokens to investor
  await daiToken.transfer(accounts[1], '100000000000000000000')

 };'

TokenFarm.sol 文件

 pragma solidity ^0.5.0;

 import "./DappToken.sol";
 import "./DaiToken.sol";

 contract TokenFarm {
   //All code goes here...
   string public name = "Dapp Token Farm";
   DappToken public dappToken;
   DaiToken public daiToken;

  constructor(DappToken _dappToken, DaiToken _daiToken) public {
      //Storing reference to the DappToken and DaiToken
      dappToken = _dappToken;
      daiToken = _daiToken;
  }
}

该文件成功编译,但是当使用truffle migrate --reset 迁移时,它显示 ReferenceError 为

ReferenceError:在 module.exports (C:\Users\NIHAL SINGH\Desktop\defi_tutorial\migrations\2_deploy_contracts.js:15:25) 在 processTicksAndRejections (internal/process/task_queues.js:93:5) 初始化之前无法访问“TokenFarm” ) 在 Migration._load (C:\Users\NIHAL SINGH\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:55:1)
在 Migration.run (C:\Users \NIHAL SINGH\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:171:1)
在 Object.runMigrations (C:\Users\NIHAL SINGH\AppData\Roaming\npm\ node_modules\truffle\build\webpack:\packages\migrate\index.js:150:1)
在 Object.runFrom (C:\Users\NIHAL SINGH\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1) 在 Object.runAll (C:\Users\ NIHAL SINGH\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1) 在 Object.run (C:\Users\NIHAL SINGH\AppData\Roaming\npm\node_modules \truffle\build\webpack:\packages\migrate\index.js:79:1) 在 runMigrations (C:\Users\NIHAL SINGH\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\ lib\commands\migrate.js:269:1)

标签: blockchainsoliditytruffle

解决方案


您已经在这一行定义了TokenFarm(大写)T

const TokenFarm = artifacts.require('TokenFarm.sol')

然后你试图在这条线上重新分配它

const TokenFarm = await TokenFarm.deployed()

解决方案:分配给另一个const名称(例如tokenFarm- 小写T),而不是重新分配已经存在的TokenFarm.

const tokenFarm = await TokenFarm.deployed()

推荐阅读