Skip to content

Commit

Permalink
Merge pull request #881 from rndquu/feat/view-redeem
Browse files Browse the repository at this point in the history
feat: add getRedeemCollateralBalance() method
  • Loading branch information
gitcoindev committed Jan 24, 2024
2 parents b8ed0c3 + 608289e commit ef7fec9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/contracts/src/dollar/facets/UbiquityPoolFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ contract UbiquityPoolFacet is IUbiquityPool, Modifiers {
return LibUbiquityPool.getDollarPriceUsd();
}

/// @inheritdoc IUbiquityPool
function getRedeemCollateralBalance(
address userAddress,
uint256 collateralIndex
) external view returns (uint256) {
return
LibUbiquityPool.getRedeemCollateralBalance(
userAddress,
collateralIndex
);
}

//====================
// Public functions
//====================
Expand Down
11 changes: 11 additions & 0 deletions packages/contracts/src/dollar/interfaces/IUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ interface IUbiquityPool {
*/
function getDollarPriceUsd() external view returns (uint256 dollarPriceUsd);

/**
* @notice Returns user's balance available for redemption
* @param userAddress User address
* @param collateralIndex Collateral token index
* @return User's balance available for redemption
*/
function getRedeemCollateralBalance(
address userAddress,
uint256 collateralIndex
) external view returns (uint256);

//====================
// Public functions
//====================
Expand Down
20 changes: 17 additions & 3 deletions packages/contracts/src/dollar/libraries/LibUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ library LibUbiquityPool {
.div(1e18);
}

/**
* @notice Returns user's balance available for redemption
* @param userAddress User address
* @param collateralIndex Collateral token index
* @return User's balance available for redemption
*/
function getRedeemCollateralBalance(
address userAddress,
uint256 collateralIndex
) internal view returns (uint256) {
UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();
return
poolStorage.redeemCollateralBalances[userAddress][collateralIndex];
}

//====================
// Public functions
//====================
Expand Down Expand Up @@ -533,10 +548,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
31 changes: 31 additions & 0 deletions packages/contracts/test/diamond/facets/UbiquityPoolFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,37 @@ contract UbiquityPoolFacetTest is DiamondTestSetup {
assertEq(dollarPriceUsd, 1_000_000);
}

function testGetRedeemCollateralBalance_ShouldReturnRedeemCollateralBalance()
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
);

uint256 redeemCollateralBalance = ubiquityPoolFacet
.getRedeemCollateralBalance(user, 0);
assertEq(redeemCollateralBalance, 97.02e18);
}

//====================
// Public functions
//====================
Expand Down

0 comments on commit ef7fec9

Please sign in to comment.