Skip to content

Commit

Permalink
Merge pull request #11 from DODOEX/delete-TWAP-module
Browse files Browse the repository at this point in the history
delete-TWAP-module
  • Loading branch information
Attens1423 authored Jan 10, 2024
2 parents 4fc9e6d + 4679f22 commit e9ca972
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 98 deletions.
7 changes: 3 additions & 4 deletions contracts/GasSavingPool/impl/GSP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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, always false, keep the same interface with old version pool
*/
function init(
address maintainer,
Expand Down Expand Up @@ -64,9 +64,8 @@ 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_ is always false
_IS_OPEN_TWAP_ = false;

string memory connect = "_";
string memory suffix = "GSP";
Expand Down
10 changes: 0 additions & 10 deletions contracts/GasSavingPool/impl/GSPStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -127,12 +123,6 @@ contract GSPStorage is ReentrancyGuard {
R = uint256(state.R);
}

/// @notice Return the adjusted mid price
/// @return midPrice The current mid price
function getMidPrice() public view returns (uint256 midPrice) {
return PMMPricing.getMidPrice(getPMMState());
}

/// @notice Return the total mt fee maintainer can claim
/// @dev The total mt fee is represented in two types: in base token and in quote token
/// @return mtFeeBase The total mt fee in base token
Expand Down
16 changes: 0 additions & 16 deletions contracts/GasSavingPool/impl/GSPVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +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 ============
/**
* @notice Set the reserves of the pool, internal use only
Expand All @@ -103,7 +89,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();
}

/**
Expand All @@ -121,7 +106,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
Expand Down
46 changes: 0 additions & 46 deletions test/GSP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,52 +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
address MAINTAINER = 0x95C4F5b83aA70810D4f142d58e5F7242Bd891CB0;
Expand Down
22 changes: 0 additions & 22 deletions test/PMMPricing.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit e9ca972

Please sign in to comment.