0

不知道是否应该指定事件或如何使用 balanceOf 创建函数。无法通过 Truffle 测试。

您好,这是我的第一个智能合约,旨在创建 ERC20 代币。在没有任何语法问题的情况下与 Dapp 大学一起完成这样的过程,我无法通过松露测试来检查总供应量是否以良好的方式编码。我尝试了不同的方法,请查看代码。错误如下,经过 $truffle 测试:使用网络“开发”。

正在编译 ./contracts/TestToken.sol... 正在编译 ./contracts/Migrations.sol...

合约:TestToken 1) 在部署时设置总供应量

未发出任何事件

0 通过 (67ms) 1 失败

1) 合约:TestToken 在部署时设置总供应量:TypeError: tokenInstance.balanceOf is not a function at Context。(test/Test.js:27:40) 在 process.internalTickCallback (internal/process/next_tick.js:77:7)

.sol 代码:

pragma solidity ^0.4.24;

contract TestToken {
uint256 public totalSupply;

constructor (uint256 _initialSupply) public {
    totalSupply = _initialSupply;
    // allocate the initial supply
}
}

test.js 代码:(工作到第 7 行 [错误开始于 , adminBalance = (...)])

var TestToken = artifacts.require("./TestToken.sol"); 

contract('EracoinToken', (accounts) => { 
var tokenInstance;
it('sets the total supply upon deployment', async function() { 
    const tokenInstance = await TestToken.deployed() 
    , _initialSupply = 100
    , totalSupply = await tokenInstance.totalSupply() 
    , adminBalance = await 
tokenInstance.balanceOf(accounts[0]) 
    assert.equal(await totalSupply.toNumber(), 
_initialSupply, 'Total supply should be _initialSupply'); 
    assert.equal(await adminBalance.toNumber(), 
_initialSupply, 'Initial supply should be allocated to admin 
account!'); 
}); 
}); 

迁移部署 .js 代码:

var TestToken = artifacts.require("./TestToken.sol");
let _initialSupply = 100;

module.exports = function(deployer) {
  deployer.deploy(TestToken, _initialSupply);
};

如果我运行 test.js until , totalSupply = await (...) 它工作得很好。Terminal 表示,它会在部署时设置总供应量。在 test.js 的第 7 行之后,我希望终端会说,总供应量应该是 _initialSupply 并且初始供应量应该分配给管理员帐户!

肯定代码有问题 - 我是初学者。请给我一个线索或帮助我修复代码。

4

1 回答 1

0

根据ERC20 代币接口,您的代币合约需要实现以下内容:

function balanceOf(address tokenOwner) public view returns (uint balance);

您的测试将无法尝试执行,因为您的智能合约tokenInstance.balanceOf(accounts[0])中没有任何功能。balanceOf

至于事件,您将看到 ERC20 代币接口提供以下内容:

event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

您可以从上面链接中的实现中看到应该发出这些事件的位置。

于 2018-12-21T17:10:11.220 回答