Contract-Development

使用 Truffle 工具的合約函式呼叫錯誤“TypeError: Cannot read property ‘isMinter’ of undefined”

  • July 5, 2019

我在 truffle env 中部署了一個 ERC20Mintable 令牌(.sol 文件是ERC20Mintable.sol)。

2_mintable.js
=============
  Deploying 'ERC20Mintable'
  -------------------------
  > transaction hash:    0x02c7b8e1dbde226304614ceeea48e1d4eb123bd1a70e73b1955d8718f3d49e08
  > Blocks: 0            Seconds: 0
  > contract address:    0x25CcB91c6643a0679591F888c3a16B12745b9594
  > block number:        3
  > block timestamp:     1562326476
  > account:             0x6D04f90886E0651381c174B2752C6c0F06626a54
  > balance:             99.96399902
  > gas used:            1496633
  > gas price:           20 gwei
  > value sent:          0 ETH
  > total cost:          0.02993266 ETH
  > Saving migration to chain.
  > Saving artifacts
  -------------------------------------
  > Total cost:          0.02993266 ETH

2_mintable.js 程式碼是:

const ERC20Mintable = artifacts.require("ERC20Mintable.sol");
module.exports = function(deployer) {
 deployer.deploy(ERC20Mintable);
};

我想檢查帳戶是否

$$ 0 $$是 minter,呼叫函式如下:

let instance = await ERC20Mintable.deployed()//It can display correct contract info.
let accounts = await web3.eth.getAccounts()
instance.MinterRole.isMinter(accounts[0])

有如下錯誤資訊,如何解決問題,非常感謝!

truffle(develop)> instance.MinterRole.isMinter(accounts[0])
evalmachine.<anonymous>:0
instance.MinterRole.isMinter(accounts[0])
                   ^

TypeError: Cannot read property 'isMinter' of undefined
   at evalmachine.<anonymous>:0:21
   at sigintHandlersWrap (vm.js:285:15)
   at Script.runInContext (vm.js:127:14)
   at runScript (/usr/local/lib/nodejs/node-v10.15.3-linux-x64/lib/node_modules/truffle/build/cli.bundled.js:368533:21)
   at Console.interpret (/usr/local/lib/nodejs/node-v10.15.3-linux-x64/lib/node_modules/truffle/build/cli.bundled.js:368548:21)
   at ReplManager.interpret (/usr/local/lib/nodejs/node-v10.15.3-linux-x64/lib/node_modules/truffle/build/cli.bundled.js:369389:18)
   at bound (domain.js:395:14)
   at REPLServer.runBound [as eval] (domain.js:408:12)
   at REPLServer.onLine (repl.js:639:10)
   at REPLServer.emit (events.js:189:13)

閱讀ERC20Mintable.sol文件的頂部(您已在自己的問題頂部連結):

contract ERC20Mintable is ERC20, MinterRole ...

契約ERC20MintableIS MinterRole(與 HAS 相比MinterRole)。

這意味著ERC20Mintable繼承了所有(非私有)函式和變數MinterRole

因此,您應該直接呼叫該函式:

instance.isMinter

引用自:https://ethereum.stackexchange.com/questions/72602