Convert.sol
1 pragma solidity >=0.6.0; 2 3 import "../interfaces/IERC20Details.sol"; 4 import "../utils/MoreMath.sol"; 5 import "../utils/SafeMath.sol"; 6 import "../utils/SignedSafeMath.sol"; 7 8 library Convert { 9 10 using SafeMath for uint; 11 using SignedSafeMath for int; 12 13 function to18DecimalsBase(address tk, uint value) internal view returns(uint) { 14 15 uint b1 = 18; 16 uint b2 = IERC20Details(tk).decimals(); 17 return formatValue(value, b1, b2); 18 } 19 20 function from18DecimalsBase(address tk, uint value) internal view returns(uint) { 21 22 uint b1 = 18; 23 uint b2 = IERC20Details(tk).decimals(); 24 return formatValue(value, b2, b1); 25 } 26 27 function formatValue(uint value, uint b1, uint b2) internal pure returns(uint) { 28 29 if (b2 < b1) { 30 value = value.mul(MoreMath.pow(10, (b1.sub(b2)))); 31 } 32 33 if (b2 > b1) { 34 value = value.div(MoreMath.pow(10, (b2.sub(b1)))); 35 } 36 37 return value; 38 } 39 40 function formatValue(int value, int b1, int b2) internal pure returns(int) { 41 42 if (b2 < b1) { 43 value = value.mul(int256(MoreMath.pow(10, uint256(b1.sub(b2))))); 44 } 45 46 if (b2 > b1) { 47 value = value.div(int256(MoreMath.pow(10, uint256(b2.sub(b1))))); 48 } 49 50 return value; 51 } 52 }