Skip to content

Commit

Permalink
fix: limit AMO minter borrow amount
Browse files Browse the repository at this point in the history
  • Loading branch information
rndquu committed Jan 24, 2024
1 parent ba8e447 commit a36e77a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/contracts/src/dollar/libraries/LibUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,9 @@ library LibUbiquityPool {
// roundId
int256 answer, // startedAt
,
uint256 updatedAt,
uint256 updatedAt, // answeredInRound

) = // answeredInRound
priceFeed.latestRoundData();
) = priceFeed.latestRoundData();

// fetch number of decimals in chainlink feed
uint256 priceFeedDecimals = priceFeed.decimals();
Expand Down Expand Up @@ -592,6 +591,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);
Expand Down
37 changes: 37 additions & 0 deletions packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,43 @@ 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
);

// 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);
Expand Down

0 comments on commit a36e77a

Please sign in to comment.