Crowdsale test returns SafeMath Multiplication Overflow

:computer: Environment

I am using Truffle v5.0.29, and Solidity v 0.5.0

:memo:Details

When testing out my crowdsale, it returns errors saying “revert SafeMath: multiplication overflow – Reason given: SafeMath: multiplication overflow.”

:1234: Code to reproduce

const { balance, BN, constants, ether, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
const { ZERO_ADDRESS } = constants;

const { expect } = require('chai');

const Crowdsale = artifacts.require('EduChainCrowdsale');
const SimpleToken = artifacts.require('EduChain');

contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
  const rate = new BN(2*10^-9);
  const value = new BN(5000);
  const tokenSupply = 105000000;
  const expectedTokenAmount = rate.mul(value);


  it('requires a non-null token', async function () {
    await expectRevert(
      Crowdsale.new(rate, wallet, ZERO_ADDRESS),
      'Crowdsale: token is the zero address'
    );
  });

  context('with token', async function () {
    beforeEach(async function () {
      this.token = await SimpleToken.new(tokenSupply);
    });

    it('requires a non-zero rate', async function () {
      await expectRevert(
        Crowdsale.new(0, wallet, this.token.address), 'Crowdsale: rate is 0'
      );
    });

    it('requires a non-null wallet', async function () {
      await expectRevert(
        Crowdsale.new(rate, ZERO_ADDRESS, this.token.address), 'Crowdsale: wallet is the zero address'
      );
    });

    context('once deployed', async function () {
      beforeEach(async function () {
        this.crowdsale = await Crowdsale.new(rate, wallet, this.token.address);
        await this.token.transfer(this.crowdsale.address, tokenSupply);
      });

      describe('accepting payments', function () {
        describe('bare payments', function () {
          it('should accept payments', async function () {
            await this.crowdsale.send(value, { from: purchaser });
          });

          it('reverts on zero-valued payments', async function () {
            await expectRevert(
              this.crowdsale.send(0, { from: purchaser }), 'Crowdsale: weiAmount is 0'
            );
          });
        });

        describe('buyTokens', function () {
          it('should accept payments', async function () {
            await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
          });

          it('reverts on zero-valued payments', async function () {
            await expectRevert(
              this.crowdsale.buyTokens(investor, { value: 0, from: purchaser }), 'Crowdsale: weiAmount is 0'
            );
          });

          it('requires a non-null beneficiary', async function () {
            await expectRevert(
              this.crowdsale.buyTokens(ZERO_ADDRESS, { value: value, from: purchaser }),
              'Crowdsale: beneficiary is the zero address'
            );
          });
        });
      });

      describe('high-level purchase', function () {
        it('should log purchase', async function () {
          const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
          expectEvent.inLogs(logs, 'TokensPurchased', {
            purchaser: investor,
            beneficiary: investor,
            value: value,
            amount: expectedTokenAmount,
          });
        });

        it('should assign tokens to sender', async function () {
          await this.crowdsale.sendTransaction({ value: value, from: investor });
          expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(expectedTokenAmount);
        });

        it('should forward funds to wallet', async function () {
          const balanceTracker = await balance.tracker(wallet);
          await this.crowdsale.sendTransaction({ value, from: investor });
          expect(await balanceTracker.delta()).to.be.bignumber.equal(value);
        });
      });

      describe('low-level purchase', function () {
        it('should log purchase', async function () {
          const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
          expectEvent.inLogs(logs, 'TokensPurchased', {
            purchaser: purchaser,
            beneficiary: investor,
            value: value,
            amount: expectedTokenAmount,
          });
        });

        it('should assign tokens to beneficiary', async function () {
          await this.crowdsale.buyTokens(investor, { value, from: purchaser });
          expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(expectedTokenAmount);
        });

        it('should forward funds to wallet', async function () {
          const balanceTracker = await balance.tracker(wallet);
          await this.crowdsale.buyTokens(investor, { value, from: purchaser });
          expect(await balanceTracker.delta()).to.be.bignumber.equal(value);
        });
      });
    });
  });
});
2 Likes

Hi @Roodiger

The issue is the rate.

Apologies, when I did the rate calculation (How to calculate rate for a crowdsale?) I didn’t pick up the implication of the value of rate. const rate = new BN(2*10^-9);
2*10^-9 is less than one and is not an integer so is not a valid value to use.

If instead of decimals 5 you used decimals 18 then a valid rate can be calculated.

Using decimals of 18 and an initial rate of $0.01 per whole token:

If you want to issue “1 TKN for every 0.01 dollar (USD) in Ether”, we would calculate it as follows:

  • assume 1 ETH == $200
  • therefore, 10^18 wei = $200 or (20,000 cents)
  • therefore, 0.01 USD is 10^18 / 20000 , or 5 * 10^13 wei
  • we have a decimals of 18, so we’ll use 10^18 TKNbits instead of 1 TKN
  • therefore, if the participant sends the crowdsale 5 * 10^13 wei we should give them 10^18 TKNbits
  • therefore the rate is 5 * 10^13 wei === 10^18 TKNbits , or 1 wei = 2*10^4 TKNbits
  • therefore, our rate is 2*10^4

Testing this out:

  • If a participant paid 0.01 USD they should get 1 TKN or 10^18 TKNbits
  • 0.01 USD is 5 * 10^13 wei
  • Using the rate of 2*10^4
  • (5 * 10^13) * (2*10^4) = 10^18 TKNbits or 1 TKN

Hi @Roodiger

I wanted to check that the above made sense. Let me know if you have any questions.
Apologies again for not spotting this before.