第二遍走查 snapshot

This commit is contained in:
mingda
2020-11-29 17:38:13 +08:00
parent c52e7960b6
commit d90e031a30
17 changed files with 285 additions and 314 deletions

View File

@@ -10,7 +10,6 @@ pragma experimental ABIEncoderV2;
interface ICloneFactory {
function clone(address prototype) external returns (address proxy);
function clone2(address prototype,bytes32 salt) external returns (address proxy);
}
// introduction of proxy mode design: https://docs.openzeppelin.com/upgrades/2.8/
@@ -31,20 +30,4 @@ contract CloneFactory is ICloneFactory {
}
return proxy;
}
function clone2(address prototype, bytes32 salt) external override returns (address proxy) {
bytes20 targetBytes = bytes20(prototype);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(
add(clone, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
proxy := create2(0, clone, 0x37, salt)
}
return proxy;
}
}

View File

@@ -34,6 +34,8 @@ library PMMPricing {
RState R;
}
// ============ buy & sell ============
function sellBaseToken(PMMState memory state, uint256 payBaseAmount)
internal
pure
@@ -75,8 +77,6 @@ library PMMPricing {
receiveQuoteAmount = _RBelowSellBaseToken(state, payBaseAmount);
newR = RState.BELOW_ONE;
}
return (receiveQuoteAmount, newR);
}
function sellQuoteToken(PMMState memory state, uint256 payQuoteAmount)
@@ -109,8 +109,6 @@ library PMMPricing {
newR = RState.ABOVE_ONE;
}
}
return (receiveBaseAmount, newR);
}
// ============ R = 1 cases ============
@@ -118,7 +116,9 @@ library PMMPricing {
function _ROneSellBaseToken(PMMState memory state, uint256 payBaseAmount)
internal
pure
returns (uint256 receiveQuoteToken)
returns (
uint256 // receiveQuoteToken
)
{
// in theory Q2 <= targetQuoteTokenAmount
// however when amount is close to 0, precision problems may cause Q2 > targetQuoteTokenAmount
@@ -135,7 +135,9 @@ library PMMPricing {
function _ROneSellQuoteToken(PMMState memory state, uint256 payQuoteAmount)
internal
pure
returns (uint256 receiveBaseToken)
returns (
uint256 // receiveBaseToken
)
{
return
DODOMath._SolveQuadraticFunctionForTrade(
@@ -152,7 +154,9 @@ library PMMPricing {
function _RBelowSellQuoteToken(PMMState memory state, uint256 payQuoteAmount)
internal
pure
returns (uint256 receiveBaseToken)
returns (
uint256 // receiveBaseToken
)
{
return
DODOMath._GeneralIntegrate(
@@ -167,7 +171,9 @@ library PMMPricing {
function _RBelowSellBaseToken(PMMState memory state, uint256 payBaseAmount)
internal
pure
returns (uint256 receiveQuoteToken)
returns (
uint256 // receiveQuoteToken
)
{
return
DODOMath._SolveQuadraticFunctionForTrade(
@@ -184,7 +190,9 @@ library PMMPricing {
function _RAboveSellBaseToken(PMMState memory state, uint256 payBaseAmount)
internal
pure
returns (uint256 receiveQuoteToken)
returns (
uint256 // receiveQuoteToken
)
{
return
DODOMath._GeneralIntegrate(
@@ -199,7 +207,9 @@ library PMMPricing {
function _RAboveSellQuoteToken(PMMState memory state, uint256 payQuoteAmount)
internal
pure
returns (uint256 receiveBaseToken)
returns (
uint256 // receiveBaseToken
)
{
return
DODOMath._SolveQuadraticFunctionForTrade(
@@ -231,7 +241,7 @@ library PMMPricing {
}
}
function getMidPrice(PMMState memory state) internal pure returns (uint256 midPrice) {
function getMidPrice(PMMState memory state) internal pure returns (uint256) {
if (state.R == RState.BELOW_ONE) {
uint256 R = DecimalMath.divFloor(state.Q0.mul(state.Q0).div(state.Q), state.Q);
R = DecimalMath.ONE.sub(state.K).add(DecimalMath.mulFloor(state.K, R));