2

I am new to Solidity and Ethereum. I am at the stage where I have worked with basic HelloWorld and similar examples.

I have created a contract to generate a random number as follows

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract RandomNumber {

function generateRandomNumber(uint range) public view returns(uint) {
// As of 1-1-22 this contract is working okay at https://remix.ethereum.org/
// However I am having issues in running it through the Visual Studio Code IDE
return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty, msg.sender))) % range;

}

}

This contract is working when I deploy and run it at https://remix.ethereum.org/ enter image description here

However, If I try to deploy & run it within Visual Studio Code I get the following error

BN { negative: 0, words: [ 3, <1 empty item> ], length: 1, red: null }

enter image description here

Why should this happen? Any inputs are appreciated. I know I might be missing something basic here

Thanks in advance

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Nomad
  • 771
  • 4
  • 9
  • 19

3 Answers3

1

It's been a while since I did any solidity coding, but this one is easy.

I believe your contract is working fine.

BN { negative: 0, words: [ 3, <1 empty item> ], length: 1, red: null }

BN is "big number". Since a 256 bit uint doesn't work with a javascript "number" (floating point) type, the result is encoded in a BN data structure. (i.e. your random number is embedded in the words member of your returned object. (I think it's a literal 3 if I read that output right).

In your local javascript that tests your Solidity contract, just invoke toString on the BN object that is returned.

https://ethereum.stackexchange.com/questions/67087/how-to-use-bignumbers-in-truffle-tests

selbie
  • 100,020
  • 15
  • 103
  • 173
1

Even though you did not share your javacript code, I can tell that it is because you are not awaiting.

Calling contract code is asynchronous and you either call it with async.await or with then/catch

//in truffle console:

   randomContract=await RandomNumber.deployed()
   randomNumber=await randomContract.generateRandomNumber(rangeInteger)
   // this should print
   randomNumber.toString()
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • I understand. I am working this out in the Truffle console (Development environment). How do I use async / await in this context? – Nomad Jan 01 '22 at 17:48
  • Hi Yilmnaz, thanks for your help. It still does not work for me. Could you look at the question once again and possibly fine tune your answer? – Nomad Jan 02 '22 at 06:33
  • @Nomad I updated the answer. i did not finish the code correctly – Yilmaz Jan 02 '22 at 06:45
  • @Yilmnaz Thanks for your help. By just typing randomNumber at the end I get the result in BN. I have to convert this toString by using await as follows await randomNumber.toString() – Nomad Jan 02 '22 at 07:13
  • Another interesting thig i have noticed is that the Random Number generator I have written is not working for an environment that consists of Ganache, Visual Studio Code and Solidity. The number generated for a given range is the same. Only when I change the range does the random number change. The only reason for this to happen is that the block.timestamp and block.difficulty is not changing – Nomad Jan 02 '22 at 07:39
-1

Try to update the solidity library in Vs