From c9facee7c6aaa795cf46d3b56a5ef55d28b08ac5 Mon Sep 17 00:00:00 2001 From: 0xMithrandir <173684735+0xMithrandir@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:00:03 +0200 Subject: [PATCH] Add minDelegateTime in BFP createMarket (#2227) * Add minDelegateTime in createMarket * Add comment on minDelegateTime Co-authored-by: David Vuong * Add a setter function for minDelegateTime * Update utilization test --------- Co-authored-by: David Vuong --- .env.sample | 3 ++- .../interfaces/IMarketConfigurationModule.sol | 5 +++++ .../interfaces/IPerpMarketFactoryModule.sol | 2 ++ .../contracts/modules/MarketConfigurationModule.sol | 5 +++++ .../contracts/modules/PerpMarketFactoryModule.sol | 11 ++++++++++- markets/bfp-market/storage.dump.sol | 6 ++++++ markets/bfp-market/test/bootstrap.ts | 6 ++++-- .../test/integration/modules/OrderModule.test.ts | 5 +++++ .../PerpMarketFactoryModule.utilisation.test.ts | 13 ++++++++++++- 9 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.env.sample b/.env.sample index a4fc581e89..b07f2c3b49 100644 --- a/.env.sample +++ b/.env.sample @@ -4,4 +4,5 @@ INFURA_IPFS_ID= INFURA_IPFS_SECRET= ETHERSCAN_API_KEY= OVM_ETHERSCAN_API_KEY= -CANNON_REGISTRY_PRIORITY= \ No newline at end of file +CANNON_REGISTRY_PRIORITY= +REPORT_GAS= diff --git a/markets/bfp-market/contracts/interfaces/IMarketConfigurationModule.sol b/markets/bfp-market/contracts/interfaces/IMarketConfigurationModule.sol index 519a44d3be..d8c4727408 100644 --- a/markets/bfp-market/contracts/interfaces/IMarketConfigurationModule.sol +++ b/markets/bfp-market/contracts/interfaces/IMarketConfigurationModule.sol @@ -97,4 +97,9 @@ interface IMarketConfigurationModule is IBasePerpMarket { function getMarketConfigurationById( uint128 marketId ) external view returns (PerpMarketConfiguration.Data memory); + + /// @notice Updates the minimum delegation time for a market. + /// @param marketId Market to update + /// @param minDelegationTime Minimum delegation time in seconds + function setMinDelegationTime(uint128 marketId, uint32 minDelegationTime) external; } diff --git a/markets/bfp-market/contracts/interfaces/IPerpMarketFactoryModule.sol b/markets/bfp-market/contracts/interfaces/IPerpMarketFactoryModule.sol index d7614cdc3e..1eb854ec55 100644 --- a/markets/bfp-market/contracts/interfaces/IPerpMarketFactoryModule.sol +++ b/markets/bfp-market/contracts/interfaces/IPerpMarketFactoryModule.sol @@ -13,6 +13,8 @@ interface IPerpMarketFactoryModule is IMarket, IBasePerpMarket { struct CreatePerpMarketParameters { /// Name of the market to be created e.g, ETHPERP bytes32 name; + /// Minimum LP delegation time in seconds e.g. 86400 + uint32 minDelegateTime; } struct DepositedCollateral { diff --git a/markets/bfp-market/contracts/modules/MarketConfigurationModule.sol b/markets/bfp-market/contracts/modules/MarketConfigurationModule.sol index 7c2d96be03..ee1d839515 100644 --- a/markets/bfp-market/contracts/modules/MarketConfigurationModule.sol +++ b/markets/bfp-market/contracts/modules/MarketConfigurationModule.sol @@ -139,4 +139,9 @@ contract MarketConfigurationModule is IMarketConfigurationModule { ) external pure returns (PerpMarketConfiguration.Data memory) { return PerpMarketConfiguration.load(marketId); } + + /// @inheritdoc IMarketConfigurationModule + function setMinDelegationTime(uint128 marketId, uint32 minDelegationTime) external { + ISynthetixSystem(SYNTHETIX_CORE).setMarketMinDelegateTime(marketId, minDelegationTime); + } } diff --git a/markets/bfp-market/contracts/modules/PerpMarketFactoryModule.sol b/markets/bfp-market/contracts/modules/PerpMarketFactoryModule.sol index f9fad88815..6b05f81539 100644 --- a/markets/bfp-market/contracts/modules/PerpMarketFactoryModule.sol +++ b/markets/bfp-market/contracts/modules/PerpMarketFactoryModule.sol @@ -31,6 +31,8 @@ contract PerpMarketFactoryModule is IPerpMarketFactoryModule { address immutable SYNTHETIX_SUSD; address immutable ORACLE_MANAGER; + uint32 constant DEFAULT_MIN_DELEGATE_TIME = 24 hours; // 86400 seconds + constructor(address _synthetix) { SYNTHETIX_CORE = _synthetix; ISynthetixSystem core = ISynthetixSystem(_synthetix); @@ -71,7 +73,14 @@ contract PerpMarketFactoryModule is IPerpMarketFactoryModule { OwnableStorage.onlyOwner(); uint128 id = ISynthetixSystem(SYNTHETIX_CORE).registerMarket(address(this)); - + if (data.minDelegateTime > 0) { + ISynthetixSystem(SYNTHETIX_CORE).setMarketMinDelegateTime(id, data.minDelegateTime); + } else { + ISynthetixSystem(SYNTHETIX_CORE).setMarketMinDelegateTime( + id, + DEFAULT_MIN_DELEGATE_TIME + ); + } PerpMarket.create(id, data.name); PerpMarket.load().activeMarketIds.push(id); emit MarketCreated(id, data.name); diff --git a/markets/bfp-market/storage.dump.sol b/markets/bfp-market/storage.dump.sol index f469a54b77..9e1762929b 100644 --- a/markets/bfp-market/storage.dump.sol +++ b/markets/bfp-market/storage.dump.sol @@ -600,6 +600,7 @@ interface IPerpAccountModule { interface IPerpMarketFactoryModule { struct CreatePerpMarketParameters { bytes32 name; + uint32 minDelegateTime; } struct DepositedCollateral { address collateralAddress; @@ -724,6 +725,11 @@ contract PerpAccountModule { } } +// @custom:artifact contracts/modules/PerpMarketFactoryModule.sol:PerpMarketFactoryModule +contract PerpMarketFactoryModule { + uint32 internal constant DEFAULT_MIN_DELEGATE_TIME = 24; +} + // @custom:artifact contracts/storage/AddressRegistry.sol:AddressRegistry library AddressRegistry { struct Data { diff --git a/markets/bfp-market/test/bootstrap.ts b/markets/bfp-market/test/bootstrap.ts index 2a03e9d072..1d745a0756 100644 --- a/markets/bfp-market/test/bootstrap.ts +++ b/markets/bfp-market/test/bootstrap.ts @@ -148,6 +148,8 @@ export const bootstrap = (args: GeneratedBootstrap) => { marketId: () => BigNumber; }[] = []; + const minDelegateTime = 86400; // 1 day + let collaterals: PerpCollateral[]; let collateralsWithoutSusd: PerpCollateral[]; @@ -196,8 +198,8 @@ export const bootstrap = (args: GeneratedBootstrap) => { ); // Create market. - const marketId = await BfpMarketProxy.callStatic.createMarket({ name }); - await BfpMarketProxy.createMarket({ name }); + const marketId = await BfpMarketProxy.callStatic.createMarket({ name, minDelegateTime }); + await BfpMarketProxy.createMarket({ name, minDelegateTime }); // Configure market. await BfpMarketProxy.connect(getOwner()).setMarketConfigurationById({ diff --git a/markets/bfp-market/test/integration/modules/OrderModule.test.ts b/markets/bfp-market/test/integration/modules/OrderModule.test.ts index 52dcf3ceee..59bc68faf4 100644 --- a/markets/bfp-market/test/integration/modules/OrderModule.test.ts +++ b/markets/bfp-market/test/integration/modules/OrderModule.test.ts @@ -186,6 +186,11 @@ describe('OrderModule', () => { const { minDelegationD18 } = await Core.getCollateralConfiguration( stakedCollateral().address ); + + // increase the time for 1 day to pass the minimum delegation time period + const nowTime = (await provider().getBlock('latest')).timestamp; + await fastForwardTo(nowTime + SECONDS_ONE_DAY + 1, provider()); + const stakedCollateralAddress = stakedCollateral().address; await Core.connect(staker()).delegateCollateral( stakerAccountId, diff --git a/markets/bfp-market/test/integration/modules/PerpMarketFactoryModule.utilisation.test.ts b/markets/bfp-market/test/integration/modules/PerpMarketFactoryModule.utilisation.test.ts index 5e93a8f177..10e41c8faf 100644 --- a/markets/bfp-market/test/integration/modules/PerpMarketFactoryModule.utilisation.test.ts +++ b/markets/bfp-market/test/integration/modules/PerpMarketFactoryModule.utilisation.test.ts @@ -14,6 +14,7 @@ import { withExplicitEvmMine, } from '../../helpers'; import { calcUtilization, calcUtilizationRate } from '../../calculations'; +import { fastForwardTo } from '@synthetixio/core-utils/utils/hardhat/rpc'; describe('PerpMarketFactoryModule Utilization', () => { const bs = bootstrap(genBootstrap()); @@ -142,6 +143,11 @@ describe('PerpMarketFactoryModule Utilization', () => { const { minDelegationD18 } = await Core.getCollateralConfiguration( stakedCollateral().address ); + + // increase the time for 1 day to pass the minimum delegation time period + const nowTime = (await provider().getBlock('latest')).timestamp; + await fastForwardTo(nowTime + SECONDS_ONE_DAY + 1, provider()); + const stakedCollateralAddress = stakedCollateral().address; await Core.connect(staker()).delegateCollateral( stakerAccountId, @@ -217,6 +223,11 @@ describe('PerpMarketFactoryModule Utilization', () => { const { minDelegationD18 } = await Core.getCollateralConfiguration( stakedCollateral().address ); + + // increase the time for 1 day to pass the minimum delegation time period + const nowTime = (await provider().getBlock('latest')).timestamp; + await fastForwardTo(nowTime + SECONDS_ONE_DAY + 1, provider()); + const stakedCollateralAddress = stakedCollateral().address; await Core.connect(staker()).delegateCollateral( stakerAccountId, @@ -402,7 +413,7 @@ describe('PerpMarketFactoryModule Utilization', () => { } = pool(); // Some time passes - await fastForwardBySec(provider(), genNumber(1, SECONDS_ONE_DAY)); + await fastForwardBySec(provider(), SECONDS_ONE_DAY + 1); // Decrease amount of staked collateral on the core side. const stakedCollateralAddress = stakedCollateral().address; await withExplicitEvmMine(