3

You can get a good estimation as to whether or not it is going to overflow, by: Counting the number of bits in the base Multiplying it by the exponent Checking whether or not the result is larger than 256 For example: function floorLog2(uint256 _n) internal pure returns (uint8) { uint8 res = 0; if (_n < 256) { // At most 8 iterations ...


3

The reason you are getting that error is because num is of type uint8, and the sub() function of SafeMath is looking for type uint256. Additionally you declare using SafeMath for uint;, which confirms that it only works with uint256 (as uint is an alias for uint256). There are two ways to fix it: 1) Make num a uint256 type. Your code would look as follows: ...


2

Currently when using the Truffle console, it loads in all your contract names as a convenience so that you can access your deployed contracts etc. This, however, causes an issue when there are contract names that conflict with Node native objects; in this case the native Buffer object. It would be kind of like if you had a contract named "String". ...


1

First of all, there is no single "SafeMath" library, but rather a number of different libraries with the same (or similar) names. Most widely used (though not necessary the best), is SafeMath library from OpenZeppelin. The most recent released version 0.2.4 starts with pragma solidity ^0.5.0;, so I believe, that it is compatible with Solidity 0.5.x.


1

It is not posible for all cases unless totalA equals totalB in which case you will have amountB = amountA. The division truncates the result discarding decimals digits, in your example the return value of convertBtoA should be 163813609331349736.27117520077928403. For most use cases small rounding errors are accepted, for example ERC20 and Ether both have ...


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