Skip to main content
28 votes
Accepted

Why write uint256 instead of uint if they're the same thing?

Yes, they are exact aliases. Some developers (including me) prefer to use uint256 because it is consistent with other uint data types, which also specify their size, and also because making the size ...
Edmund Edgar's user avatar
  • 16.9k
19 votes

Max/min values of standard data types

I assume you mean the integer types, because those are really the only types in Solidity that have a maximum and a minimum. Solidity does not support floating point types, and most likely will never ...
Jesbus's user avatar
  • 10.5k
18 votes
Accepted

How to convert a uint256 type integer into a bytes32?

Just use bytes32(u). It's as easy as that because they are both 2^256 (unlike bytes). No need for a separate function, but to clarify: function convert(uint256 n) returns (bytes32) { return ...
eth's user avatar
  • 85.8k
13 votes
Accepted

Max/min values of standard data types

Updated 2020 Solidity 0.6.8 introduced min and max keywords that can now natively tell you the min and max of an expected type. From the release page: Implemented type(T).min and type(T).max for ...
Shane Fontaine's user avatar
10 votes

Why write uint256 instead of uint if they're the same thing?

In addition to what @EdmundEdgar said, it's also better to be explicit when constructing method signature ID's. For example if doing bytes4(keccak('transfer(address, uint)')), you'll get a different ...
Miguel Mota's user avatar
  • 5,163
6 votes
Accepted

Convert number to bytes in ethersjs?

ethers also has a padding function where it will add the zeros to make it the correct length. It looks like this const oracleResponse = ethers.utils.hexZeroPad(ethers.utils.hexlify(1), 32) This will ...
Patrick Collins's user avatar
5 votes
Accepted

Why do reputable ERC721 contracts use uint256 so often?

Edited Answer The reason people like using uint256 for most calculation is that OpenZeppelin has a SafeMath library for uint256 that prevent number overflow for sol version < 0.8. Additional ...
Anton Cheng's user avatar
4 votes
Accepted

How to take the average of two uint256s without overflowing?

After posting the question, I realised I gave the answer myself. I simply have to check if both x and y are odd numbers and, if yes, add 1 to the result: function avg(uint256 x, uint256 y) external ...
Paul Razvan Berg's user avatar
4 votes
Accepted

Why Ethereum sets user balance to MaxBig256 whenever a contract query is performed?

In Ethereum, to execute operation in EVM costs you gas. One can change any state in the blockchain which is done through making transaction. In this case, you will be charged ether. To make ...
Md Rayhanul Masud Saom's user avatar
4 votes
Accepted

Why does uint 0 - 20 not cause an underflow?

That's because since solidity 0.8.0 the overflow / underflow are automatically checked : "Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous ...
hroussille's user avatar
  • 7,661
4 votes

Why uint8 for decimals in ERC20 tokens?

Well, nothing will mess up if you use uint8 for token decimal. The reason why most of the ERC20 smart contracts use uint8 for the decimal is that because the EIP-20 propose to use uint8 for decimals. ...
Dream Person's user avatar
3 votes
Accepted

How to set multiple unit64 values in a uint256 variable

Your smart contract code is okay, but this is probably simpler: pragma solidity 0.5.7; contract Uncombine { function getVariables(uint256 combined) external pure returns (uint64 variable1, ...
user19510's user avatar
  • 28k
3 votes
Accepted

Is it better practice to use smaller uint sizes when applicable?

It really depends on the way you implement it. EVM allocates you new memory slots in 256 bit chunks and in order that the state variables are declared. So if you declare them like that: uint32 time; ...
Kostiantyn Kurbatskyi's user avatar
3 votes
Accepted

Does casting a Keccak256 hash to a uint256 increase likelihood of a collision?

A keccak256 string has a length of 256 bits. Obviously, uin256 also has 256 bits. Since they both have the same length, there shouldn't be any problems when converting them even back and forth. I ...
Lauri Peltonen's user avatar
2 votes

Why write uint256 instead of uint if they're the same thing?

I hate to say RTFM, but this one was too trivial. "uint and int are aliases for uint256 and int256, respectively." --https://solidity.readthedocs.io/en/v0.4.21/types.html#value-types
Fred Douglis's user avatar
2 votes
Accepted

Ethers.js invalid number value while deploying contract

const tokenWeiPrice = (answers.priceEur / ethEur) * Math.pow(10, 18); Problem #1: 10 ^ 18 > Number.MAX_SAFE_INTEGER Problem #2: tokenWeiPrice is not necessarily an integer. You should use ...
goodvibration's user avatar
2 votes
Accepted

bytes32/uint256 conversion

Assuming genes.test.call(0x000063169218f348dc640d171b000208934b5a90189038cb3084624a50f7316c) is JavaScript, your problem is probably that the parameter is getting interpreted as a JavaScript number,...
Edmund Edgar's user avatar
  • 16.9k
2 votes
Accepted

Solidity. Return a Sruct that lives inside an array

You forgot to name your variable. You only gave it a type. Try this: function getStudent(uint id) ... { return (students[id].name, students[id].ethAddress, ...); }
user19510's user avatar
  • 28k
2 votes

Solidity - preventing negative uint parameters/overflow

Since web3 apparently doesn't require functions with uint parameters to have positive values Not true. uint is always a positive number, by definition. I suspect the issue is about how the contract ...
Rob Hitchens's user avatar
  • 55.2k
2 votes
Accepted

Mapping with iterating uint as identifier - multiple transactions in the same block

No, it doesn't matter whether transactions are in the same block or not. Each transaction is processed on its own, sequentially. The code you propose is fine, though an array may make more sense.
user19510's user avatar
  • 28k
2 votes

Passing '-1' to a solidity function which takes a 'uint256' argument as input

I'm unsure if web3 truffle do some magic of their own but I'd guess no, because they don't need to interpret the values themselves. Anyway, if you test this in Remix you will also see that it wraps ...
Lauri Peltonen's user avatar
2 votes

converting uint256 to string

oraclize helped pragma solidity ^0.5.3; uint public degrees= 40; function getString() public view returns(string memory){ string memory a = "today is "; ...
CryptoPuff's user avatar
2 votes
Accepted

variable declared as uint but accepting negative values instead of throwing errors

Both, uint and int are 256-bit long in Solidity, so arbitrary sequence of 256 bits may be interpreted in both ways. For example, sequence of 256 "ones" is -1 when interpreted as int, and is ...
Mikhail Vladimirov's user avatar
2 votes
Accepted

How to get the first digit of a uint256

Substitute x for whichever number you want to check for. function highestOrderDigitIsX(uint256 number, uint256 x) public pure returns (bool) { while (number >= 10) { number /= 10; }...
Richard Horrocks's user avatar
2 votes
Accepted

How to add three numbers using safemath?

Writing 5.add(7) is a syntax sugar for add(5, 7). So first expression 5.add(7.add(8)) is add(5, add(7, 8)) and the second one 5.add(7).add(8) is add(add(5, 7), 8). In the particular case of SafeMath's ...
Ismael's user avatar
  • 30.6k
2 votes

Updating values that are packed inside a uint256

To make this a bit easier to visualize, let's say you have four numbers and each takes 2 bits. 8 bits in total. Your 8 bit integer might look for example like this: 10 10 11 01. In decimal, the ...
Lauri Peltonen's user avatar
2 votes
Accepted

How can we assign bytes to uint256 variables in solidity?

You can assign smaller integers to larger ones. A byte is uint8. Not semantically, but from the bits points of view.
Mikko Ohtamaa's user avatar
1 vote
Accepted

How to send uint256 and Address types to Smart contract in JavaScript web3/ethers?

It seems like you are trying to use the wrong getAmountsOut function: // performs chained getAmountOut calculations on any number of pairs function getAmountsOut(address factory, uint amountIn, ...
Paulus's user avatar
  • 410
1 vote
Accepted

Error: overflow

JavaScript can natively only deal with integers of size 2^53 - 1. So to deal with the big numbers in Solidity, you can use the BigNumber library. Assuming you're using truffle's test suite, that would ...
Alex Otsu's user avatar
  • 478
1 vote

compare uint from datestamp always return true

There are a few issues with your code. You should always use BigNumber when using integers. JavaScript can't handle big numbers. Even if you know the numbers will never be too big, in my opinion it's ...
Lauri Peltonen's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible