Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: limit AMO minter borrow amount #882

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -622,6 +622,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(
gitcoindev marked this conversation as resolved.
Show resolved Hide resolved
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
Loading