Skip to content

Commit

Permalink
fix: limit AMO minter borrow amount (#882)
Browse files Browse the repository at this point in the history
* fix: limit AMO minter borrow amount

* test: assert free collateral amount
  • Loading branch information
rndquu authored Jan 24, 2024
1 parent ef7fec9 commit 46d9d62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/contracts/src/dollar/libraries/LibUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 46d9d62

Please sign in to comment.