fix DODO math comment

This commit is contained in:
mingda
2021-01-11 09:43:25 +08:00
parent aba6b84683
commit 0659364e11

View File

@@ -27,7 +27,7 @@ library DODOMath {
let V1-V2=delta
res = i*delta*(1-k+k(V0^2/V1/V2))
i is the price of res-V trading pair
i is the price of V-res trading pair
support k=1 & k=0 case
@@ -77,16 +77,15 @@ library DODOMath {
// uint256 sqrt = (4 * k).mul(i).mul(delta).div(V1).add(DecimalMath.ONE2).sqrt();
uint256 sqrt;
uint256 ki = (4 * k).mul(i);
if(ki == 0 ) {
if (ki == 0) {
sqrt = DecimalMath.ONE;
}else if(ki * delta / ki == delta) {
} else if ((ki * delta) / ki == delta) {
sqrt = (ki * delta).div(V1).add(DecimalMath.ONE2).sqrt();
}else {
} else {
sqrt = (4 * k).mul(i).div(V1).mul(delta).add(DecimalMath.ONE2).sqrt();
}
uint256 premium = DecimalMath.divFloor(sqrt.sub(DecimalMath.ONE), k * 2).add(
DecimalMath.ONE
);
uint256 premium =
DecimalMath.divFloor(sqrt.sub(DecimalMath.ONE), k * 2).add(DecimalMath.ONE);
// V0 is greater than or equal to V1 according to the solution
return DecimalMath.mulFloor(V1, premium);
}
@@ -136,16 +135,17 @@ library DODOMath {
// if k==1
// Q2=Q1/(1+ideltaBQ1/Q0/Q0)
// temp = ideltaBQ1/Q0/Q0
// Q1-Q2 = Q1*(temp/(1+temp))
// Q2 = Q1/(1+temp)
// Q1-Q2 = Q1*(1-1/(1+temp)) = Q1*(temp/(1+temp))
// uint256 temp = i.mul(delta).mul(V1).div(V0.mul(V0));
uint256 temp;
uint256 idelta = i.mul(delta);
if(idelta == 0) {
if (idelta == 0) {
temp = 0;
}else if(idelta * V1 / idelta == V1) {
} else if ((idelta * V1) / idelta == V1) {
temp = (idelta * V1).div(V0.mul(V0));
}else {
temp = delta.mul(V1).div(V0).mul(i).div(V0);
} else {
temp = delta.mul(V1).div(V0).mul(i).div(V0);
}
return V1.mul(temp).div(temp.add(DecimalMath.ONE));
}
@@ -171,10 +171,11 @@ library DODOMath {
bAbs = bAbs.div(DecimalMath.ONE);
// calculate sqrt
uint256 squareRoot = DecimalMath.mulFloor(
DecimalMath.ONE.sub(k).mul(4),
DecimalMath.mulFloor(k, V0).mul(V0)
); // 4(1-k)kQ0^2
uint256 squareRoot =
DecimalMath.mulFloor(
DecimalMath.ONE.sub(k).mul(4),
DecimalMath.mulFloor(k, V0).mul(V0)
); // 4(1-k)kQ0^2
squareRoot = bAbs.mul(bAbs).add(squareRoot).sqrt(); // sqrt(b*b+4(1-k)kQ0*Q0)
// final res