0

Hi I am trying to run the following tests

var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var expect = chai.expect;

var Bet = artifacts.require("./contracts/Bet");

const betAmountInEth = "0.25";
const wrongBetAmountInEth = "0.15";
const agreedUponBetAmount = web3.utils.toWei(betAmountInEth, "ether");
const wrongBetAmount = web3.utils.toWei(wrongBetAmountInEth, "ether");

contract("Bet", function(accounts) {
  const betOriginator = accounts[0];
  const betTaker = accounts[1];
  const badActor = accounts[2];
  const originatorBet = 4;
  const takerBet = 5;

  const originatorBalanceBeforeBet = web3.eth.getBalance(betOriginator);
  const takerBalanceBeforeBet = web3.eth.getBalance(betTaker);

  let bet;

  it("We should be able to start a bet by setting a guess and sending the bet amount that the contract was initialized with", async function() {
    const tx = await bet.createBet(originatorBet, {
      from: betOriginator,
      value: agreedUponBetAmount
    });
    expect(tx).to.exist;

    const betEvent = tx.logs[0].args;
    expect(betEvent).to.exist;
    expect(betEvent.gameStatus.toNumber()).to.equal(1);
    expect(betEvent.originatorStatus.toNumber()).to.equal(4);
    expect(betEvent.originatorAddress).to.not.equal(
      "0x0000000000000000000000000000000000000000"
    );
    expect(betEvent.originatorGuess.toNumber()).to.equal(0); //Hides until the end
    expect(betEvent.takerAddress).to.equal(
      "0x0000000000000000000000000000000000000000"
    );
    expect(betEvent.takerStatus.toNumber()).to.equal(0);
    expect(betEvent.takerGuess.toNumber()).to.equal(0);
    expect(betEvent.betAmount.toNumber()).to.equal(Number(agreedUponBetAmount));
    expect(betEvent.actualNumber.toNumber()).to.equal(0);
    expect(betEvent.pot.toNumber()).to.equal(0);
  });

Here are the events emitted during the test

 Events emitted during test:
    ---------------------------

Bet.BetStatus(
  gameStatus: Bet.GameStatusEnum.STATUS_STARTED (type: enum Bet.GameStatusEnum),
  originatorStatus: Bet.BetStatusEnum.STATUS_PENDING (type: enum Bet.BetStatusEnum),
  originatorAddress: 0x5cA881342Ab93763Fd2a8D66AE72A2554276590d (type: address),
  originatorGuess: 0 (type: uint256),
  takerAddress: 0x0000000000000000000000000000000000000000 (type: address),
  takerStatus: Bet.BetStatusEnum.STATUS_UNKNOWN (type: enum Bet.BetStatusEnum),
  takerGuess: 0 (type: uint256),
  betAmount: 250000000000000000 (type: uint256),
  actualNumber: 0 (type: uint256),
  pot: 0 (type: uint256)
)


---------------------------
2) We should be able to take a bet by setting a guess and sending the bet amount that the contract was initialized with

Events emitted during test:
---------------------------

Bet.BetStatus(
  gameStatus: Bet.GameStatusEnum.STATUS_COMPLETE (type: enum Bet.GameStatusEnum),
  originatorStatus: Bet.BetStatusEnum.STATUS_LOSE (type: enum Bet.BetStatusEnum),
  originatorAddress: 0x5cA881342Ab93763Fd2a8D66AE72A2554276590d (type: address),
  originatorGuess: 4 (type: uint256),
  takerAddress: 0xaD5A36fBEB78252d61d325E800b5085eA33645d7 (type: address),
  takerStatus: Bet.BetStatusEnum.STATUS_WINNER (type: enum Bet.BetStatusEnum),
  takerGuess: 5 (type: uint256),
  betAmount: 250000000000000000 (type: uint256),
  actualNumber: 7 (type: uint256),
  pot: 500000000000000000 (type: uint256)
)

The errors I am encountering:

1) Contract: Bet
       We should be able to start a bet by setting a guess and sending the bet amount that the contract was initialized with:
     Error: Number can only safely store up to 53 bits
      at assert (C:\Users\Michael\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\bn.js\lib\bn.js:6:1)
      at BN.toNumber (C:\Users\Michael\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\bn.js\lib\bn.js:506:1)
      at Context.<anonymous> (test\testBet.js:70:31)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  2) Contract: Bet
       We should be able to take a bet by setting a guess and sending the bet amount that the contract was initialized with:
     Error: Number can only safely store up to 53 bits
      at assert (C:\Users\Michael\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\bn.js\lib\bn.js:6:1)
      at BN.toNumber (C:\Users\Michael\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\bn.js\lib\bn.js:506:1)
      at Context.<anonymous> (test\testBet.js:95:31)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

I have found examples of people running into similar issues online and a workaround suggested was to downgrade truffle to v4 but ideally, I would like to keep truffle v5

0

Don't downgrade to Truffle 4.x if you're already on Truffle 5.x.

The former simply ignores that issue, so it would be like sticking your head in the sand.

The value of Number.MAX_SAFE_INTEGER is indeed 53 bits (2^53 - 1).

So you should get rid of all the toNumber conversions in your code.

For more details how to use big numbers in truffle tests, see this answer.

Proper disclosure - it was written by me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.