From 46d9d6245568affe4f175a65d3bbefd1e6e6095e Mon Sep 17 00:00:00 2001 From: rndquu <119500907+rndquu@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:21:47 +0300 Subject: [PATCH] fix: limit AMO minter borrow amount (#882) * fix: limit AMO minter borrow amount * test: assert free collateral amount --- .../src/dollar/libraries/LibUbiquityPool.sol | 6 +++ .../diamond/facets/UbiquityPoolFacet.t.sol | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol b/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol index 2d8bf83a2..3ca5ea12a 100644 --- a/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol +++ b/packages/contracts/src/dollar/libraries/LibUbiquityPool.sol @@ -606,6 +606,12 @@ library LibUbiquityPool { "Collateral disabled" ); + // ensure the pool is solvent (i.e. AMO minter borrows less than users want to redeem) + require( + collateralAmount <= freeCollateralBalance(minterCollateralIndex), + "Not enough free collateral" + ); + // transfer IERC20(poolStorage.collateralAddresses[minterCollateralIndex]) .safeTransfer(msg.sender, collateralAmount); diff --git a/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol b/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol index 5e89b4e7a..18bd805c2 100644 --- a/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol +++ b/packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol @@ -653,6 +653,44 @@ contract UbiquityPoolFacetTest is DiamondTestSetup { ubiquityPoolFacet.amoMinterBorrow(1); } + function testAmoMinterBorrow_ShouldRevert_IfThereIsNotEnoughFreeCollateral() + public + { + vm.prank(admin); + ubiquityPoolFacet.setPriceThresholds( + 1000000, // mint threshold + 1000000 // redeem threshold + ); + + // user sends 100 collateral tokens and gets 99 Dollars (-1% mint fee) + vm.prank(user); + ubiquityPoolFacet.mintDollar( + 0, // collateral index + 100e18, // Dollar amount + 99e18, // min amount of Dollars to mint + 100e18 // max collateral to send + ); + + // user redeems 99 Dollars for 97.02 (accounts for 2% redemption fee) collateral tokens + vm.prank(user); + ubiquityPoolFacet.redeemDollar( + 0, // collateral index + 99e18, // Dollar amount + 90e18 // min collateral out + ); + + // get free collateral amount, returns 2.98e18 + uint256 freeCollateralAmount = ubiquityPoolFacet.freeCollateralBalance( + 0 + ); + assertEq(freeCollateralAmount, 2.98e18); + + // Dollar AMO minter tries to borrow more collateral than available after users' redemptions + vm.prank(address(dollarAmoMinter)); + vm.expectRevert("Not enough free collateral"); + ubiquityPoolFacet.amoMinterBorrow(freeCollateralAmount + 1); + } + function testAmoMinterBorrow_ShouldBorrowCollateral() public { // mint 100 collateral tokens to the pool collateralToken.mint(address(ubiquityPoolFacet), 100e18);