math overflow fix
This commit is contained in:
@@ -74,7 +74,8 @@ library DODOMath {
|
||||
// V0 = V1*(1+(sqrt-1)/2k)
|
||||
// sqrt = √(1+4kidelta/V1)
|
||||
// premium = 1+(sqrt-1)/2k
|
||||
uint256 sqrt = (4 * k).mul(i).mul(delta).div(V1).add(DecimalMath.ONE2).sqrt();
|
||||
uint256 sqrt = DecimalMath.multiMulWithDiv((4 * k).mul(i),delta,V1).add(DecimalMath.ONE2).sqrt();
|
||||
// uint256 sqrt = (4 * k).mul(i).mul(delta).div(V1).add(DecimalMath.ONE2).sqrt();
|
||||
uint256 premium = DecimalMath.divFloor(sqrt.sub(DecimalMath.ONE), k * 2).add(
|
||||
DecimalMath.ONE
|
||||
);
|
||||
@@ -128,7 +129,8 @@ library DODOMath {
|
||||
// Q2=Q1/(1+ideltaBQ1/Q0/Q0)
|
||||
// temp = ideltaBQ1/Q0/Q0
|
||||
// Q1-Q2 = Q1*(temp/(1+temp))
|
||||
uint256 temp = i.mul(delta).mul(V1).div(V0.mul(V0));
|
||||
uint256 temp = DecimalMath.multiMulWithDiv(i.mul(delta),V1,V0).div(V0);
|
||||
// uint256 temp = i.mul(delta).mul(V1).div(V0.mul(V0));
|
||||
return V1.mul(temp).div(temp.add(DecimalMath.ONE));
|
||||
}
|
||||
|
||||
|
||||
@@ -45,4 +45,10 @@ library DecimalMath {
|
||||
function reciprocalCeil(uint256 target) internal pure returns (uint256) {
|
||||
return uint256(10**36).divCeil(target);
|
||||
}
|
||||
|
||||
function multiMulWithDiv(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) {
|
||||
uint256 a = x.div(z); uint256 b = x.mod(z); // x = a * z + b
|
||||
uint256 c = y.div(z); uint256 d = y.mod(z); // y = c * z + d
|
||||
return a.mul(b).mul(z).add(a.mul(d)).add(b.mul(c)).add(b.mul(d).div(z));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ library SafeMath {
|
||||
return a / b;
|
||||
}
|
||||
|
||||
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
require(b > 0, "MOD_ERROR");
|
||||
return a % b;
|
||||
}
|
||||
|
||||
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
uint256 quotient = div(a, b);
|
||||
uint256 remainder = a - quotient * b;
|
||||
|
||||
Reference in New Issue
Block a user