首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:找不到模块“bignumber.js”

错误:找不到模块“bignumber.js”
EN

Ethereum用户
提问于 2020-03-10 23:42:16
回答 1查看 1.7K关注 0票数 2

我正在尝试测试这个智能契约,但我似乎无法克服这个错误,我使用的是最新的松露套件v5.1.16和web3 v1.2.6。

KTokenSale.test.js

代码语言:javascript
复制
const BigNumber = web3.BigNumber;

require('chai')
  .use(require('chai-bignumber')(BigNumber))
  .should();

  const KToken = artifacts.require('KToken');
  const KTokenCrowdsale = artifacts.require('KTokenCrowdsale');

  contract('KTokenCrowdsale', function([_, wallet, investor1, investor2]) {
    beforeEach(async function () {
        this.name = 'TestToken';
        this.symbol = 'TT';
        this.decimals = 16;

        this.token = await KToken.new(
            this.name,
            this.symbol,
            this.decimals
            );

        this.rate = 850;
        this.wallet = wallet;

        this.crowdsale = await KToken.new(
            this.rate,
            this.wallet,
            this.token.address
            );
    });
  });

  contract('crowdsale', function() {
    it('tracks the rate', async function() {
        const rate = await this.crowdsale.rate();
        rate.should.be.bignumber.equal(this.rate);
    });

    it('tracks the wallet', async function() {
        const wallet = await this.crowdsale.wallet();
        wallet.should.equal(this.wallet);
    });

    it('tracks the token', async function() {
        const token = await this.crowdsale.token();
        token.should.equal(this.token.address);
    });
  });

正在测试的智能合同代码:

代码语言:javascript
复制
pragma solidity ^0.5.11;

import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/CappedCrowdsale.sol";

contract KTokenCrowdsale is Crowdsale, CappedCrowdsale {
    uint256 public investorMinCap = 7000000000000000000;
    uint256 public investorMaxCap = 350000000000000000000;
    mapping(address => uint256) public contributions;

    constructor(
        uint256 _rate,
        address payable _wallet,
        IERC20 _token,
        uint256 _cap
    )
    Crowdsale(_rate, _wallet, _token)
    CappedCrowdsale(_cap) public {}
}
EN

回答 1

Ethereum用户

回答已采纳

发布于 2020-03-10 23:56:01

web3.BigNumber on web3.js v0.xweb3.utils.BN on web3.js v1.x

请注意,bignumber.js处理非整数值,这意味着它可以:

  1. 接受非整数输入( Number类型或String类型)
  2. 生成非整数结果(通过函数‘div’和‘除以’)

这一点,虽然bn.js不处理非整数值,正如明确指出的那样:

注意:这个库不支持小数。

由于Solidity不支持非整数值,web3.js开发团队最终决定用BigNumber代替BN,而不是截断web3用户传递给web3函数的每个非整数值(从而使用户不知道代码中的潜在错误)。

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/80508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档