Hot answers tagged

18

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 because they are considered not to be precise enough. Ethereum contracts need to be 100% deterministic, and always run the same way on all hardware. Solidity ...


8

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 every integer type T that returns the smallest and largest value representable by the type. You can try it out with the following code. Note that the uint256 ...


4

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 wrapping behaviour. Checks for overflow are very common, so we made them the default to increase readability of code, even if it comes at a slight increase of ...


4

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 returns (uint256 result) { result = x / 2 + y / 2; if (x % 2 == 1 && y % 2 == 1) { result += 1; } } Update: I ended up optimising the ...


3

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 transaction, we have some apis like SendTransaction inside internal/ethapi/api.go When you do send transaction, you can set gas limit and gas price. Your balance is ...


3

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, uint64 variable2, uint64 variable3, uint64 variable4) { variable1 = uint64(combined); variable2 = uint64(combined >> 64); variable3 = ...


2

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 BigNumber conclusively for the purpose of interacting with contracts. If ethers.js doesn't support this type of input, then before passing variable x of type ...


2

Assuming genes.test.call(0x000063169218f348dc640d171b000208934b5a90189038cb3084624a50f7316c) is JavaScript, your problem is probably that the parameter is getting interpreted as a JavaScript number, which does not have enough precision to handle numbers as grand as the one you are using. The solidity call therefore gets fed a less precise number, which it ...


2

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 around (flips to the other end of the min/max) (https://en.wikipedia.org/wiki/Integer_overflow). So it's a Solidity feature (and actually quite common in ...


2

oraclize helped pragma solidity ^0.5.3; uint public degrees= 40; function getString() public view returns(string memory){ string memory a = "today is "; string memory b = uint2str(degrees); string memory c = "degrees outside"; string memory sentence = string(abi.encodePacked(...


2

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 115792089237316195423570985008687907853269984665640564039457584007913129639935 when interpreted as uint. I assume you are passing negative value to your function via ...


2

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, ...); }


2

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.


2

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 casting will cost gas, so it's more efficient to keep number as uint256, and do math calculation all in uint256, instead of reading a shorter bit number and cast it ...


2

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 add both expression will arrive at the same result. For other functions like sub and div make sure they are in the correct order. The last expression 5.add(7 + ...


2

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; } return number == x; } For completeness: function lowestOrderDigitIsX(uint256 number, uint256 x) public pure returns (bool) { return number % 10 =...


1

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; uint256 money; uint32 anotherTime; these variables will take up 3 slots of 256 bit: time will create a first 256 bit slot and take 32 bit in it, money will not ...


1

I suggest you change the way you built your uint256[13] to use uint256[] memory _value = new uint256[](13); instead. Then you should be able to use myFunc(_value). If your array does not always contain 13 elements, you can replace 13 with a variable, like so: uint256 _n = 3; uint256[] memory _value = new uint256[](_n); // populate your array


1

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 pad your result to be length 32, and allow it to be accepted by your function.


1

A problem with "short" uint/int type is that they aren't natively supported by the EVM so the compiler generates longer bytecode to operate them. Also compatibility with libraries and EIP. Many libraries, like OpenZeppelin, are written for uint256, also most EIP only use other types when explicitly needed.


1

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, address[] memory path) internal view returns (uint[] memory amounts) { require(path.length >= 2, 'PancakeLibrary: INVALID_PATH'); amounts = new uint[](path....


1

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 look something like const { BigNumber } = require("@ethersproject/bignumber"); it('should allow to mint a new nft', async () => { const ...


1

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 simply good habit to always use BigNumber. You are passing strings to your function. ‘1630972800’ is a string, not a number. My guess is that it gets ...


1

That's a very good question. It touches upon a detail that is probably not very well known, even though it's documented (Rational and Integer Literals): Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by using them together with a non-literal expression or by explicit conversion). This means that ...


1

You are using OraclizeLib library correct? If yes, this library has defined parseInt method inside of it. Then your function can be used like this without problems: function __callback(bytes32 myid, string result) public{ if (msg.sender != provable_cbAddress()) revert(); uint256 points = parseInt(result); balanceOf[pointsAllowance[myid]] = ...


1

When I try to run this code on remix it gave UnimplementedFeatureError: Not yet implemented - FixedPointType. Which you can see here This is not possible to use uint(3/30) as you are doing. Casting of any floating point using uint is not acceptable. The Fixed Point are not yet unuseable in solidity as it documentation says: Fixed point numbers are not ...


1

Looks like you're using an older ABI format (or at least an unconventional one). Change this: "internalType":"address" And this: "internalType":"uint256" To this: "name":"" UPDATE: It also seems that you have two balanceOf functions in your ABI, god knows why (maybe an attempt to support both formats). So to generalize the answer above, just use this ...


1

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 interprets incoming data. int, uint and bytes32 are all 32-byte words. So, any of those choices means the contract expects 256-bits. The casting of variable ...


1

The original workaround web3.toBigNumber(2).pow(256).minus(1) is no longer working for newer versions of web3. To pass in -1, try using web3.utils.toBN(2).pow(web3.utils.toBN(256)).sub(web3.utils.toBN(1));


1

Rob's answer is a good one. If you don't want to use events for some reason, you could: add your number to the uint array increment a counter Then if you wanted to retrieve all the numbers, you would use the counter as your index for iterating over the array.


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