Hot answers tagged

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 ...


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 =...


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

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 ...


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

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

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....


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