From 4924ab0035521e70625d704791f5b260a4713327 Mon Sep 17 00:00:00 2001 From: korrrba <88761781+gitcoindev@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:53:19 +0100 Subject: [PATCH] feat: prevent duplicate collateral tokens (#903) * feat: prevent duplicate collateral tokens Resolves: https://github.com/sherlock-audit/2023-12-ubiquity-judging/issues/145 * chore: shorten test name to testAddCollateralToken_ShouldRevertIfCollateralExists As discussed during pull request review. --- .../src/dollar/libraries/LibUbiquityPool.sol | 23 +++++++++++++++++++ .../diamond/facets/UbiquityPoolFacet.t.sol | 11 +++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol b/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol index 407d90ba9..58b201fec 100644 --- a/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol +++ b/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol @@ -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 @@ -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; diff --git a/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol b/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol index 3a7bda5a5..9e4782f0c 100644 --- a/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol +++ b/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol @@ -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);