improve fee logic, make clamping logic more robust
This commit is contained in:
@ -1,20 +1,6 @@
|
||||
pragma solidity 0.5.13;
|
||||
|
||||
library Math {
|
||||
function add512(uint x0, uint x1, uint y0, uint y1) internal pure returns (uint z0, uint z1) {
|
||||
assembly { // solium-disable-line security/no-inline-assembly
|
||||
z0 := add(x0, y0)
|
||||
z1 := add(add(x1, y1), lt(z0, x0))
|
||||
}
|
||||
}
|
||||
function mul512(uint x, uint y) internal pure returns (uint z0, uint z1) {
|
||||
assembly { // solium-disable-line security/no-inline-assembly
|
||||
z0 := mul(x, y)
|
||||
let mm := mulmod(x, y, not(0))
|
||||
z1 := sub(sub(mm, z0), lt(mm, z0))
|
||||
}
|
||||
}
|
||||
|
||||
function min(uint x, uint y) internal pure returns (uint z) {
|
||||
return x <= y ? x : y;
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ library SafeMath {
|
||||
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
|
||||
}
|
||||
|
||||
function clamp128(uint y) internal pure returns (uint128 z) {
|
||||
z = y <= uint128(-1) ? uint128(y) : uint128(-1);
|
||||
function clamp112(uint y) internal pure returns (uint112 z) {
|
||||
z = y <= uint112(-1) ? uint112(y) : uint112(-1);
|
||||
}
|
||||
}
|
||||
|
||||
15
contracts/libraries/UQ112x112.sol
Normal file
15
contracts/libraries/UQ112x112.sol
Normal file
@ -0,0 +1,15 @@
|
||||
pragma solidity 0.5.13;
|
||||
|
||||
library UQ112x112 {
|
||||
uint224 constant Q112 = 2**112;
|
||||
|
||||
// encode a uint112 as a UQ112.112 fixed point number s.t. `y := y_encoded / 2**112` (i.e. with a Q112 denominator).
|
||||
function encode(uint112 y) internal pure returns (uint224 z) {
|
||||
z = uint224(y) * Q112; // never overflows
|
||||
}
|
||||
|
||||
// divide a UQ112.112 by a uint112 and return the result as a UQ112.112
|
||||
function qdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
|
||||
z = x / y;
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
pragma solidity 0.5.13;
|
||||
|
||||
// helpful links
|
||||
// https://en.wikipedia.org/wiki/Q_(number_format)
|
||||
// https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.md
|
||||
// https://github.com/gnosis/solidity-arithmetic
|
||||
|
||||
library UQ128x128 {
|
||||
uint constant Q128 = 2**128;
|
||||
|
||||
// we want to encode a uint128 `y` s.t. `y := y_encoded / 2**128` (i.e. with a Q128 denominator).
|
||||
// in other words, to encode `y` we simply multiply by `2**128`, aka Q104, and store this in a 208-bit slot.
|
||||
function encode(uint128 y) internal pure returns (uint z) {
|
||||
return uint(y) * Q128; // guaranteed not to overflow
|
||||
}
|
||||
|
||||
// we want to divide a UQ128.128 (the output of encode above) by an unencoded uint128 and return another
|
||||
// modified UQ128.128. to do this, it's sufficient to divide the UQ128.128 by the unencoded value.
|
||||
function qdiv(uint x, uint128 y) internal pure returns (uint z) {
|
||||
z = x / y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user