From 0659364e11ba1982f32c978c9f4db7951474f4b1 Mon Sep 17 00:00:00 2001 From: mingda Date: Mon, 11 Jan 2021 09:43:25 +0800 Subject: [PATCH] fix DODO math comment --- contracts/lib/DODOMath.sol | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/contracts/lib/DODOMath.sol b/contracts/lib/DODOMath.sol index 641b659..249e211 100644 --- a/contracts/lib/DODOMath.sol +++ b/contracts/lib/DODOMath.sol @@ -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