Skip to content

Commit

Permalink
Add minDelegateTime in BFP createMarket (#2227)
Browse files Browse the repository at this point in the history
* Add minDelegateTime in createMarket

* Add comment on minDelegateTime

Co-authored-by: David Vuong <[email protected]>

* Add a setter function for minDelegateTime

* Update utilization test

---------

Co-authored-by: David Vuong <[email protected]>
  • Loading branch information
0xMithrandir and davidvuong authored Aug 21, 2024
1 parent 53dabcd commit c9facee
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ INFURA_IPFS_ID=
INFURA_IPFS_SECRET=
ETHERSCAN_API_KEY=
OVM_ETHERSCAN_API_KEY=
CANNON_REGISTRY_PRIORITY=
CANNON_REGISTRY_PRIORITY=
REPORT_GAS=
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 10 additions & 1 deletion markets/bfp-market/contracts/modules/PerpMarketFactoryModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions markets/bfp-market/storage.dump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ interface IPerpAccountModule {
interface IPerpMarketFactoryModule {
struct CreatePerpMarketParameters {
bytes32 name;
uint32 minDelegateTime;
}
struct DepositedCollateral {
address collateralAddress;
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions markets/bfp-market/test/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export const bootstrap = (args: GeneratedBootstrap) => {
marketId: () => BigNumber;
}[] = [];

const minDelegateTime = 86400; // 1 day

let collaterals: PerpCollateral[];
let collateralsWithoutSusd: PerpCollateral[];

Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c9facee

Please sign in to comment.