From 7462b7e1c39f8736e56236caac4aed16374455ad Mon Sep 17 00:00:00 2001 From: Skye Date: Tue, 9 Jan 2024 16:54:42 +0800 Subject: [PATCH] delete-TWAP-module https://github.com/sherlock-audit/2023-12-dodo-gsp-judging/issues/39 https://github.com/sherlock-audit/2023-12-dodo-gsp-judging/issues/80 --- contracts/GasSavingPool/impl/GSP.sol | 6 +-- contracts/GasSavingPool/impl/GSPStorage.sol | 4 -- contracts/GasSavingPool/impl/GSPVault.sol | 15 ------- contracts/lib/PMMPricing.sol | 12 ------ git | 1 + test/GSP.t.sol | 45 --------------------- test/PMMPricing.t.sol | 22 ---------- 7 files changed, 3 insertions(+), 102 deletions(-) create mode 160000 git diff --git a/contracts/GasSavingPool/impl/GSP.sol b/contracts/GasSavingPool/impl/GSP.sol index ea5afd9..6b15ca5 100644 --- a/contracts/GasSavingPool/impl/GSP.sol +++ b/contracts/GasSavingPool/impl/GSP.sol @@ -29,7 +29,7 @@ contract GSP is GSPTrader, GSPFunding { * @param mtFeeRate The rate of mt fee, with 18 decimal * @param i The oracle price, possible to be changed only by maintainer * @param k The swap curve parameter - * @param isOpenTWAP Use TWAP price or not + * @param isOpenTWAP Useless, just for compatible */ function init( address maintainer, @@ -64,9 +64,7 @@ contract GSP is GSPTrader, GSPFunding { _MT_FEE_RATE_ = mtFeeRate; // _MAINTAINER_ is set when initialization, the address receives the fee _MAINTAINER_ = maintainer; - _IS_OPEN_TWAP_ = isOpenTWAP; - // if _IS_OPEN_TWAP_ is true, _BLOCK_TIMESTAMP_LAST_ is set to the current block timestamp - if (isOpenTWAP) _BLOCK_TIMESTAMP_LAST_ = uint32(block.timestamp % 2**32); + _IS_OPEN_TWAP_ = false; // GSP does not open TWAP by default string memory connect = "_"; string memory suffix = "GSP"; diff --git a/contracts/GasSavingPool/impl/GSPStorage.sol b/contracts/GasSavingPool/impl/GSPStorage.sol index 421993c..b7b9907 100644 --- a/contracts/GasSavingPool/impl/GSPStorage.sol +++ b/contracts/GasSavingPool/impl/GSPStorage.sol @@ -32,10 +32,6 @@ contract GSPStorage is ReentrancyGuard { // _BASE_RESERVE_ and _QUOTE_RESERVE_ are the current reserves of the GSP uint112 public _BASE_RESERVE_; uint112 public _QUOTE_RESERVE_; - // _BLOCK_TIMESTAMP_LAST_ is used when calculating TWAP - uint32 public _BLOCK_TIMESTAMP_LAST_; - // _BASE_PRICE_CUMULATIVE_LAST_ is used when calculating TWAP - uint256 public _BASE_PRICE_CUMULATIVE_LAST_; // _BASE_TARGET_ and _QUOTE_TARGET_ are recalculated when the pool state changes uint112 public _BASE_TARGET_; diff --git a/contracts/GasSavingPool/impl/GSPVault.sol b/contracts/GasSavingPool/impl/GSPVault.sol index 7dfeca1..3b0a4de 100644 --- a/contracts/GasSavingPool/impl/GSPVault.sol +++ b/contracts/GasSavingPool/impl/GSPVault.sol @@ -78,19 +78,6 @@ contract GSPVault is GSPStorage { return _QUOTE_TOKEN_.balanceOf(address(this)) - uint256(_QUOTE_RESERVE_) - uint256(_MT_FEE_QUOTE_); } - // ============ TWAP UPDATE =========== - /** - * @notice Update the twap price, internal use only - * @dev The twap price is updated when _IS_OPEN_TWAP_ is true - */ - function _twapUpdate() internal { - uint32 blockTimestamp = uint32(block.timestamp % 2**32); - uint32 timeElapsed = blockTimestamp - _BLOCK_TIMESTAMP_LAST_; - if (timeElapsed > 0 && _BASE_RESERVE_ != 0 && _QUOTE_RESERVE_ != 0) { - _BASE_PRICE_CUMULATIVE_LAST_ += getMidPrice() * timeElapsed; - } - _BLOCK_TIMESTAMP_LAST_ = blockTimestamp; - } // ============ Set States ============ /** @@ -103,7 +90,6 @@ contract GSPVault is GSPStorage { require(baseReserve <= type(uint112).max && quoteReserve <= type(uint112).max, "OVERFLOW"); _BASE_RESERVE_ = uint112(baseReserve); _QUOTE_RESERVE_ = uint112(quoteReserve); - if (_IS_OPEN_TWAP_) _twapUpdate(); } /** @@ -121,7 +107,6 @@ contract GSPVault is GSPStorage { if (quoteBalance != _QUOTE_RESERVE_) { _QUOTE_RESERVE_ = uint112(quoteBalance); } - if (_IS_OPEN_TWAP_) _twapUpdate(); } /// @notice Sync the reserves of the pool diff --git a/contracts/lib/PMMPricing.sol b/contracts/lib/PMMPricing.sol index e6a0d01..30ed16f 100644 --- a/contracts/lib/PMMPricing.sol +++ b/contracts/lib/PMMPricing.sol @@ -251,16 +251,4 @@ library PMMPricing { ); } } - - function getMidPrice(PMMState memory state) internal pure returns (uint256) { - if (state.R == RState.BELOW_ONE) { - uint256 R = DecimalMath.divFloor(state.Q0 * state.Q0 / state.Q, state.Q); - R = DecimalMath.ONE - state.K + (DecimalMath.mulFloor(state.K, R)); - return DecimalMath.divFloor(state.i, R); - } else { - uint256 R = DecimalMath.divFloor(state.B0 * state.B0 / state.B, state.B); - R = DecimalMath.ONE - state.K + (DecimalMath.mulFloor(state.K, R)); - return DecimalMath.mulFloor(state.i, R); - } - } } diff --git a/git b/git new file mode 160000 index 0000000..a54a84b --- /dev/null +++ b/git @@ -0,0 +1 @@ +Subproject commit a54a84b333adbecf7bc4483c0e36ed5878cac17b diff --git a/test/GSP.t.sol b/test/GSP.t.sol index 415b094..eb8b9da 100644 --- a/test/GSP.t.sol +++ b/test/GSP.t.sol @@ -70,51 +70,6 @@ contract TestGSPVault is Test { assertEq(sha256(abi.encodePacked(str)), sha256(abi.encodePacked("5615deb7"))); } - function testTwapUpdate() public { - // set is_open_twap to true - address MAINTAINER = 0x95C4F5b83aA70810D4f142d58e5F7242Bd891CB0; - address BASE_TOKEN_ADDRESS = DAI; - address QUOTE_TOKEN_ADDRESS = USDC; - uint256 LP_FEE_RATE = 0; - uint256 MT_FEE_RATE = 10000000000000; - uint256 I = 1000000; - uint256 K = 500000000000000; - bool IS_OPEN_TWAP = true; - gsp.init( - MAINTAINER, - BASE_TOKEN_ADDRESS, - QUOTE_TOKEN_ADDRESS, - LP_FEE_RATE, - MT_FEE_RATE, - I, - K, - IS_OPEN_TWAP - ); - - // transfer some tokens to USER - vm.startPrank(DAI_WHALE); - dai.transfer(USER, BASE_RESERVE + BASE_INPUT); - vm.stopPrank(); - vm.startPrank(USDC_WHALE); - usdc.transfer(USER, QUOTE_RESERVE + QUOTE_INPUT); - vm.stopPrank(); - // User buys shares - vm.startPrank(USER); - dai.transfer(address(gsp), BASE_RESERVE); - usdc.transfer(address(gsp), QUOTE_RESERVE); - gsp.buyShares(USER); - dai.transfer(address(gsp), BASE_INPUT); - gsp.sellBase(USER); - uint32 blockTimestampBefore = gsp._BLOCK_TIMESTAMP_LAST_(); - // Time elapse - vm.warp(block.timestamp + 500000); - usdc.transfer(address(gsp), QUOTE_INPUT); - gsp.sellQuote(USER); - uint32 blockTimestampAfter = gsp._BLOCK_TIMESTAMP_LAST_(); - vm.stopPrank(); - assertTrue(gsp._IS_OPEN_TWAP_() == true); - assertTrue(blockTimestampAfter > blockTimestampBefore); - } function testInitFail() public { // Init params diff --git a/test/PMMPricing.t.sol b/test/PMMPricing.t.sol index c2271ca..c135425 100644 --- a/test/PMMPricing.t.sol +++ b/test/PMMPricing.t.sol @@ -27,15 +27,6 @@ contract PMMPricingTestHelper { return (receiveBaseAmount, newR); } - - function getMidPrice(PMMPricing.PMMState memory state) - external - pure - returns (uint256) - { - uint256 result = PMMPricing.getMidPrice(state); - return result; - } } contract PMMPricingTest is Test { @@ -143,17 +134,4 @@ contract PMMPricingTest is Test { assertTrue(newR == PMMPricing.RState.BELOW_ONE); assertTrue(receiveBaseAmount == state.B - state.B0); } - - function testGetMidPrice() public { - state.i = 1e18; - state.K = 1e18; - state.B = 5e18; - state.Q = 5e18; - state.B0 = 6e18; - state.Q0 = 6e18; - uint256 midPrice; - state.R = PMMPricing.RState.BELOW_ONE; - midPrice = helper.getMidPrice(state); - assertEq(midPrice, 694444444444444444); - } } \ No newline at end of file