Skip to content

Commit

Permalink
🐴 Add stability pool whitelist (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse authored May 6, 2024
1 parent 6c0c7ee commit cb1b11a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions contracts/Interfaces/IStabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface IStabilityPool is IDeposit {
error StabilityPool__AdminContractOnly(address sender, address expected);
error StabilityPool__VesselManagerOnly(address sender, address expected);
error StabilityPool__ArrayNotInAscendingOrder();
error StabilityPool__AddressNotCollateralWhitelisted(address _address);

// --- Functions ---

Expand Down
10 changes: 10 additions & 0 deletions contracts/StabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ contract StabilityPool is ReentrancyGuardUpgradeable, UUPSUpgradeable, TrinityBa
* Skipping a collateral forfeits the available rewards (can be useful for gas optimizations)
*/
function provideToSP(uint256 _amount, address[] calldata _assets) external override nonReentrant {
for(uint256 i = 0; i < _assets.length; i++) {
if(!IAdminContract(adminContract).getIsAddressCollateralWhitelisted(_assets[i], msg.sender)) {
revert StabilityPool__AddressNotCollateralWhitelisted(_assets[i]);
}
}
_requireNonZeroAmount(_amount);

uint256 initialDeposit = deposits[msg.sender];
Expand All @@ -281,6 +286,11 @@ contract StabilityPool is ReentrancyGuardUpgradeable, UUPSUpgradeable, TrinityBa
*/

function withdrawFromSP(uint256 _amount, address[] calldata _assets) external {
for(uint256 i = 0; i < _assets.length; i++) {
if(!IAdminContract(adminContract).getIsAddressCollateralWhitelisted(_assets[i], msg.sender)) {
revert StabilityPool__AddressNotCollateralWhitelisted(_assets[i]);
}
}
(address[] memory assets, uint256[] memory amounts) = _withdrawFromSP(_amount, _assets);
_sendGainsToDepositor(msg.sender, assets, amounts);
}
Expand Down
5 changes: 4 additions & 1 deletion test/trinity/GasCompensationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ const deploy = async (treasury, distributor, mintingAccounts) => {

validCollateral = await adminContract.getValidCollateral()

for(const account of mintingAccounts) {
for (const account of mintingAccounts) {
await adminContract.setLiquidatorWhitelisted(account, true)
for (const collateral of validCollateral) {
await adminContract.setAddressCollateralWhitelisted(collateral, account, true)
}
}
}

Expand Down
27 changes: 26 additions & 1 deletion test/trinity/StabilityPoolTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

for(const account of mintingAccounts) {
for (const account of mintingAccounts) {
await adminContract.setLiquidatorWhitelisted(account, true)
for (const collateral of validCollateral) {
await adminContract.setAddressCollateralWhitelisted(collateral, account, true)
}
}
}

Expand Down Expand Up @@ -125,6 +128,16 @@ contract("StabilityPool", async accounts => {
})

describe("Providing", async () => {
it("provideToSp(): reverts if not whitelisted", async () => {
try {
await stabilityPool.provideToSP(200, [alice], { from: alice })
assert.isFalse(tx.receipt.status)
} catch (err) {
assert.include(err.message, "revert")
assert.include(err.message, `StabilityPool__AddressNotCollateralWhitelisted("${alice}")`)
}
})

it("provideToSP(): increases the Stability Pool balance", async () => {
await _openVessel(erc20, (extraDebtTokenAmt = 200), alice)
await stabilityPool.provideToSP(200, validCollateral, { from: alice })
Expand Down Expand Up @@ -740,6 +753,8 @@ contract("StabilityPool", async accounts => {
it("provideToSP(): passing wrong address to asset list has no impact", async () => {
await openWhaleVessel(erc20, (icr = 10), (extraDebtTokenAmt = 1_000_000))
// first call won't revert as there is no initial deposit
await adminContract.setAddressCollateralWhitelisted(alice, whale, true)

await stabilityPool.provideToSP(dec(199_000, 18), [alice], { from: whale })
await stabilityPool.provideToSP(dec(1_000, 18), [alice], { from: whale })
await stabilityPool.withdrawFromSP(dec(1_000, 18), [alice], { from: whale })
Expand Down Expand Up @@ -767,6 +782,16 @@ contract("StabilityPool", async accounts => {
})

describe("Withdrawing", async () => {
it("withdrawFromSP(): reverts if not whitelisted", async () => {
try {
await stabilityPool.withdrawFromSP(200, [alice], { from: alice })
assert.isFalse(tx.receipt.status)
} catch (err) {
assert.include(err.message, "revert")
assert.include(err.message, `StabilityPool__AddressNotCollateralWhitelisted("${alice}")`)
}
})

it("withdrawFromSP(): reverts when user has no active deposit", async () => {
await _openVessel(erc20, 100, alice)
await _openVessel(erc20, 100, bob)
Expand Down
6 changes: 4 additions & 2 deletions test/trinity/VesselManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

for(const account of mintingAccounts) {
await adminContract.setAddressCollateralWhitelisted(erc20.address, account, true)
for (const account of mintingAccounts) {
for (const collateral of validCollateral) {
await adminContract.setAddressCollateralWhitelisted(collateral, account, true)
}
await adminContract.setLiquidatorWhitelisted(account, true)
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/trinity/VesselManager_RecoveryModeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

for(const account of mintingAccounts) {
await adminContract.setAddressCollateralWhitelisted(erc20.address, account, true)
for (const account of mintingAccounts) {
await adminContract.setLiquidatorWhitelisted(account, true)
for (const collateral of validCollateral) {
await adminContract.setAddressCollateralWhitelisted(collateral, account, true)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

for(const account of mintingAccounts) {
for (const account of mintingAccounts) {
await adminContract.setLiquidatorWhitelisted(account, true)
for (const collateral of validCollateral) {
await adminContract.setAddressCollateralWhitelisted(collateral, account, true)
}
}
}

Expand Down

0 comments on commit cb1b11a

Please sign in to comment.