Skip to content

Commit

Permalink
feat: prevent duplicate collateral tokens (#903)
Browse files Browse the repository at this point in the history
* feat: prevent duplicate collateral tokens

Resolves: sherlock-audit/2023-12-ubiquity-judging#145

* chore: shorten test name to testAddCollateralToken_ShouldRevertIfCollateralExists

As discussed during pull request review.
  • Loading branch information
gitcoindev authored Feb 27, 2024
1 parent 49ab47b commit 4924ab0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/contracts/src/dollar/libraries/LibUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,24 @@ library LibUbiquityPool {
return poolStorage.collateralAddresses;
}

/**
* @notice Check if collateral token with given address already exists
* @param collateralAddress The collateral token address to check
*/
function collateralExists(
address collateralAddress
) internal view returns (bool) {
UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();
address[] memory collateralAddresses = poolStorage.collateralAddresses;

for (uint256 i = 0; i < collateralAddresses.length; i++) {
if (collateralAddresses[i] == collateralAddress) {
return true;
}
}
return false;
}

/**
* @notice Returns collateral information
* @param collateralAddress Address of the collateral token
Expand Down Expand Up @@ -630,6 +648,11 @@ library LibUbiquityPool {
address chainLinkPriceFeedAddress,
uint256 poolCeiling
) internal {
require(
!collateralExists(collateralAddress),
"Collateral already added"
);

UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();

uint256 collateralIndex = poolStorage.collateralAddresses.length;
Expand Down
11 changes: 11 additions & 0 deletions packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,17 @@ contract UbiquityPoolFacetTest is DiamondTestSetup {
assertEq(info.redemptionFee, 20000);
}

function testAddCollateralToken_ShouldRevertIfCollateralExists() public {
uint256 poolCeiling = 50_000e18;
vm.startPrank(admin);
vm.expectRevert("Collateral already added");
ubiquityPoolFacet.addCollateralToken(
address(collateralToken),
address(collateralTokenPriceFeed),
poolCeiling
);
}

function testRemoveAmoMinter_ShouldRemoveAmoMinter() public {
vm.startPrank(admin);

Expand Down

0 comments on commit 4924ab0

Please sign in to comment.