Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sip-332: update pool collateral configuration logic #1776

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions protocol/synthetix/contracts/interfaces/IPoolModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ interface IPoolModule {
*/
event SetMinLiquidityRatio(uint256 minLiquidityRatio);

/**
* @notice Allows collaterals accepeted by the system to be accepeted by the pool by default
* @param poolId The id of the pool.
* @param disabled Shows if new collateral's will be dsiabled by default for the pool
*/
event PoolCollateralDisabledByDefaultSet(uint128 poolId, bool disabled);

/**
* @notice Creates a pool with the requested pool id.
* @param requestedPoolId The requested id for the new pool. Reverts if the id is not available.
Expand Down Expand Up @@ -118,6 +125,13 @@ interface IPoolModule {
PoolCollateralConfiguration.Data memory newConfig
) external;

/**
* @notice Allows collaterals accepeted by the system to be accepeted by the pool by default
* @param poolId The id of the pool.
* @param disabled If set to true new collaterals will be disabled for the pool.
*/
function setPoolCollateralDisabledByDefault(uint128 poolId, bool disabled) external;

/**
* @notice Retrieves the MarketConfiguration of the specified pool.
* @param poolId The id of the pool whose configuration is being queried.
Expand Down Expand Up @@ -186,16 +200,6 @@ interface IPoolModule {
*/
function setMinLiquidityRatio(uint256 minLiquidityRatio) external;

/**
@notice Shows if a given collateral type is enabled for deposits and delegation in a given pool.
* @param poolId The id of the pool for to check the collateral for.
* @param collateral The address of the collateral.
*/
function isDelegationEnabledByPool(
uint128 poolId,
address collateral
) external view returns (bool enabled);

/**
@notice returns a pool minimum issuance ratio
* @param poolId The id of the pool for to check the collateral for.
Expand Down
23 changes: 12 additions & 11 deletions protocol/synthetix/contracts/modules/core/PoolModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ contract PoolModule is IPoolModule {
emit PoolConfigurationSet(poolId, newMarketConfigurations, msg.sender);
}

/**
* @inheritdoc IPoolModule
*/
function setPoolCollateralDisabledByDefault(uint128 poolId, bool disabled) external override {
Pool.Data storage pool = Pool.loadExisting(poolId);
Rickk137 marked this conversation as resolved.
Show resolved Hide resolved
Pool.onlyPoolOwner(poolId, msg.sender);

pool.collateralDisabledByDefault = disabled;

emit PoolCollateralDisabledByDefaultSet(poolId, disabled);
}

/**
* @inheritdoc IPoolModule
*/
Expand Down Expand Up @@ -228,17 +240,6 @@ contract PoolModule is IPoolModule {
emit PoolNameUpdated(poolId, name, msg.sender);
}

/**
* @inheritdoc IPoolModule
*/
function isDelegationEnabledByPool(
uint128 poolId,
address collateral
) external view override returns (bool) {
return
!Pool.loadExisting(poolId).collateralConfigurations[collateral].collateralTypeDisabled;
}

/**
* @inheritdoc IPoolModule
*/
Expand Down
3 changes: 0 additions & 3 deletions protocol/synthetix/contracts/modules/core/VaultModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ contract VaultModule is IVaultModule {
// Check if the collateral is enabled here because we still want to allow reducing delegation for disabled collaterals.
CollateralConfiguration.collateralEnabled(collateralType);

// Check if delegation is enabled for the pool
Pool.load(poolId).checkDelegationEnabled(collateralType);

Account.requireSufficientCollateral(
accountId,
collateralType,
Expand Down
28 changes: 11 additions & 17 deletions protocol/synthetix/contracts/storage/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ library Pool {
uint256 maxCollateral
);

/**
* @notice Thrown when attempting delegate a pool disabled collateral
*/
error PoolCollateralIsDisabled(address collateral, uint128 poolId);

bytes32 private constant _CONFIG_SET_MARKET_MIN_DELEGATE_MAX = "setMarketMinDelegateTime_max";

struct Data {
Expand Down Expand Up @@ -131,6 +126,14 @@ library Pool {
uint64 __reserved2;
uint64 __reserved3;
mapping(address => PoolCollateralConfiguration.Data) collateralConfigurations;
/**
* @dev A switch to make the pool opt-in for new collateral
*
* By default it's set to false, which means any new collateral accepeted by the system will be accpeted by the pool.
*
* If the pool owner sets this value to true, then new collaterals will be disabled for the pool unless a maxDeposit is set for a that collateral.
*/
bool collateralDisabledByDefault;
}

/**
Expand Down Expand Up @@ -534,8 +537,9 @@ library Pool {
uint256 maxDeposit = self.collateralConfigurations[collateralType].maxDepositD18;

if (
maxDeposit > 0 &&
self.vaults[collateralType].currentCollateral() + collateralAmountD18 > maxDeposit
(self.collateralDisabledByDefault && maxDeposit == 0) ||
(maxDeposit > 0 &&
self.vaults[collateralType].currentCollateral() + collateralAmountD18 > maxDeposit)
) {
revert PoolCollateralLimitExceeded(
self.id,
Expand All @@ -545,14 +549,4 @@ library Pool {
);
}
}

/**
* @notice Shows if a given collateral type is enabled for deposits and delegation in given pool.
* @param collateralType The address of the collateral.
*/
function checkDelegationEnabled(Data storage self, address collateralType) internal view {
if (self.collateralConfigurations[collateralType].collateralTypeDisabled) {
revert PoolCollateralIsDisabled(collateralType, self.id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ library PoolCollateralConfiguration {

struct Data {
uint256 maxDepositD18;
bool collateralTypeDisabled;
uint256 issuanceRatioD18;
}
}
1 change: 0 additions & 1 deletion protocol/synthetix/test/integration/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export function bootstrapWithMockMarketAndPool() {
.Core.connect(owner)
.setPoolCollateralConfiguration(r.poolId, r.collateralAddress(), {
maxDepositD18: bn(1000000000),
collateralTypeDisabled: false,
issuanceRatioD18: bn(1),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ describe('IssueUSDModule', function () {
await systems()
.Core.connect(owner)
.setPoolCollateralConfiguration(poolId, collateralAddress(), {
collateralTypeDisabled: false,
maxDepositD18: bn(10),
issuanceRatioD18: bn(6),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { bootstrap } from '../../../bootstrap';
describe('PoolConfigurationModule', function () {
const { signers, systems } = bootstrap();

let owner: Ethers.Signer, user1: Ethers.Signer;
let owner: Ethers.Signer, user1: Ethers.Signer, user2: Ethers.Signer;

let receipt: Ethers.providers.TransactionReceipt;

Expand All @@ -23,7 +23,7 @@ describe('PoolConfigurationModule', function () {

describe('PoolConfigurationModule', function () {
before('identify signers', async () => {
[owner, user1] = signers();
[owner, user1, user2] = signers();
});

describe('when some pools are created', function () {
Expand Down Expand Up @@ -183,6 +183,25 @@ describe('PoolConfigurationModule', function () {
});
});
});

describe('when the owner tries to disable/enable new collaterals by default', () => {
it('setPoolCollateralDisabledByDefault is restricted to the pool owner', async () => {
await assertRevert(
systems().Core.connect(user2).setPoolCollateralDisabledByDefault(1, true),
`Unauthorized("${await user2.getAddress()}")`,
systems().Core
);
});

it('pool owner disables new collaterals by default', async () => {
const tx = await systems()
.Core.connect(user1)
.setPoolCollateralDisabledByDefault(1, true);

receipt = await tx.wait();
await assertEvent(receipt, 'PoolCollateralDisabledByDefaultSet(1, true)', systems().Core);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -714,37 +714,21 @@ describe('PoolModule Admin', function () {
.Core.connect(user2)
.setPoolCollateralConfiguration(thirdPoolId, collateralAddress(), {
maxDepositD18: bn(10),
collateralTypeDisabled: false,
issuanceRatioD18: bn(0),
}),
`Unauthorized("${await user2.getAddress()}")`,
systems().Core
);
});

it('collateral is enabled by default for the pool', async () => {
await assert.equal(
await systems().Core.isDelegationEnabledByPool(thirdPoolId, collateralAddress()),
true
);
});

it('disable the collateral by pool owner', async () => {
await systems()
.Core.connect(user1)
.setPoolCollateralConfiguration(thirdPoolId, collateralAddress(), {
maxDepositD18: bn(10),
collateralTypeDisabled: true,
issuanceRatioD18: bn(2),
});
});

it('collateral is disabled for the pool', async () => {
await assert.equal(
await systems().Core.isDelegationEnabledByPool(thirdPoolId, collateralAddress()),
false
);
});
});

describe('set pool collateral issuance ratio', async () => {
Expand Down Expand Up @@ -773,7 +757,6 @@ describe('PoolModule Admin', function () {
.Core.connect(user2)
.setPoolCollateralConfiguration(thirdPoolId, collateralAddress(), {
maxDepositD18: bn(10),
collateralTypeDisabled: false,
issuanceRatioD18: bn(2),
}),
`Unauthorized("${await user2.getAddress()}")`,
Expand All @@ -793,7 +776,6 @@ describe('PoolModule Admin', function () {
.Core.connect(user1)
.setPoolCollateralConfiguration(thirdPoolId, collateralAddress(), {
maxDepositD18: bn(10),
collateralTypeDisabled: false,
issuanceRatioD18: bn(2),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ describe('VaultModule', function () {
.Core.connect(user1)
.setPoolCollateralConfiguration(fakeVaultId, collateralAddress(), {
maxDepositD18: bn(10),
collateralTypeDisabled: true,
issuanceRatioD18: bn(0),
});
});
Expand All @@ -357,7 +356,9 @@ describe('VaultModule', function () {
depositAmount.div(50),
ethers.utils.parseEther('1')
),
`PoolCollateralIsDisabled("${collateralAddress()}", "${fakeVaultId}")`,
`PoolCollateralLimitExceeded("${fakeVaultId}", "${collateralAddress()}", "${depositAmount
.div(50)
.toString()}", "${bn(10).toString()}")`,
systems().Core
);
});
Expand All @@ -367,7 +368,6 @@ describe('VaultModule', function () {
.Core.connect(user1)
.setPoolCollateralConfiguration(fakeVaultId, collateralAddress(), {
maxDepositD18: bn(1000000),
collateralTypeDisabled: false,
issuanceRatioD18: bn(0),
});
});
Expand All @@ -391,7 +391,6 @@ describe('VaultModule', function () {
.Core.connect(owner)
.setPoolCollateralConfiguration(poolId, collateralAddress(), {
maxDepositD18: depositAmount.div(2),
collateralTypeDisabled: false,
issuanceRatioD18: bn(0),
});
});
Expand Down Expand Up @@ -454,7 +453,6 @@ describe('VaultModule', function () {
.Core.connect(owner)
.setPoolCollateralConfiguration(poolId, collateralAddress(), {
maxDepositD18: depositAmount.mul(10),
collateralTypeDisabled: false,
issuanceRatioD18: bn(0),
});
});
Expand Down
Loading