Skip to content

Commit

Permalink
delete-TWAP-module
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyewwww committed Jan 9, 2024
1 parent ad9d148 commit 7462b7e
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 102 deletions.
6 changes: 2 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, just for compatible
*/
function init(
address maintainer,
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 0 additions & 4 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
15 changes: 0 additions & 15 deletions contracts/GasSavingPool/impl/GSPVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 ============
/**
Expand All @@ -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();
}

/**
Expand All @@ -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
Expand Down
12 changes: 0 additions & 12 deletions contracts/lib/PMMPricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions git
Submodule git added at a54a84
45 changes: 0 additions & 45 deletions test/GSP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 7462b7e

Please sign in to comment.