Skip to content

Commit

Permalink
feat: fixing CI and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FibrinLab committed Dec 30, 2023
1 parent ea50c85 commit 65d892d
Show file tree
Hide file tree
Showing 7 changed files with 1,228 additions and 35 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions packages/contracts/src/dollar/facets/FuseRariAmoStrategyFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {LibFuseRariAmoStrategy} from "../libraries/LibFuseRariAmoStrategy.sol";
import {Modifiers} from "../libraries/LibAppStorage.sol";

contract FuseRariAmoStrategyFacet is Modifiers {
function init(
function initialize(
address _dollar,
address[] memory _initialUnitrollers,
address[] memory _initialFusePools,
address _amoMinterAddress,
uint256 _globalDollarCollateralRatio
) external {
LibFuseRariAmoStrategy.init(
LibFuseRariAmoStrategy.initialize(
_dollar,
_initialUnitrollers,
_initialFusePools,
Expand All @@ -29,12 +29,12 @@ contract FuseRariAmoStrategyFacet is Modifiers {
return LibFuseRariAmoStrategy.showAllocations();
}

function dollarBalances()
function dollarBalancesStrategy()
external
view
returns (uint256 dollarVal, uint256 collatVal)
{
return LibFuseRariAmoStrategy.dollarBalances();
return LibFuseRariAmoStrategy.dollarBalancesStrategy();
}

function allPoolAddresses() external view returns (address[] memory) {
Expand Down Expand Up @@ -163,8 +163,8 @@ contract FuseRariAmoStrategyFacet is Modifiers {
/* ========== Burns and givebacks ========== */

// Burn unneeded or excess DOLLAR. Goes through the minter
function burnDollar(uint256 _dollarAmount) external {
LibFuseRariAmoStrategy.burnDollar(_dollarAmount);
function burnDollarStrategy(uint256 _dollarAmount) external {
LibFuseRariAmoStrategy.burnDollarStrategy(_dollarAmount);
}

/* ========== OWNER / GOVERNANCE FUNCTIONS ONLY ========== */
Expand Down Expand Up @@ -194,10 +194,7 @@ contract FuseRariAmoStrategyFacet is Modifiers {
LibFuseRariAmoStrategy.setAmoMinter(_amoMinterAddress);
}

function recoverERC20(
address _tokenAddress,
uint256 _tokenAmount
) external {
LibFuseRariAmoStrategy.recoverERC20(_tokenAddress, _tokenAmount);
function amoMinterAddress() external view returns (address) {
return LibFuseRariAmoStrategy.amoMinterAddress();
}
}
69 changes: 50 additions & 19 deletions packages/contracts/src/dollar/libraries/LibFuseRariAmoStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ library LibFuseRariAmoStrategy {
_;
}

function init(
/// @notice Initializes the AMO Strategy with the given parameters.
/// @dev Sets up the initial state of the strategy, including pools, AMO minter, and collateral ratio.
/// @param _dollar Address of the Ubiquity Dollar Token contract.
/// @param _initialUnitrollers Array of addresses for the initial unitrollers.
/// @param _initialFusePools Array of addresses for the initial Fuse pools.
/// @param _amoMinterAddress Address of the AMO Minter contract.
/// @param _globalDollarCollateralRatio Initial global dollar collateral ratio.
function initialize(
address _dollar,
address[] memory _initialUnitrollers,
address[] memory _initialFusePools,
Expand Down Expand Up @@ -127,6 +134,9 @@ library LibFuseRariAmoStrategy {
}
}

/// @notice Shows the allocations of the strategy in terms of dollar balance and sum of Fuse pool tallies.
/// @dev Returns an array containing the dollar balance, sum of Fuse pool tallies, and total DOLLAR value.
/// @return allocations An array of three elements: dollar balance, sum of Fuse pool tallies, and total DOLLAR value.
function showAllocations()
internal
view
Expand All @@ -147,10 +157,14 @@ library LibFuseRariAmoStrategy {
}
allocations[1] = sumFusePoolTally;

allocations[2] = allocations[0] + allocations[1]; // Total DOLLAR value
allocations[2] = allocations[0] + allocations[1]; // Total Dollar value
}

function dollarBalances()
/// @notice Provides the dollar and collateral values of the strategy.
/// @dev Returns the total dollar value and its corresponding collateral value.
/// @return dollarVal Total dollar value in the strategy.
/// @return collatVal Corresponding collateral value for the total dollar value.
function dollarBalancesStrategy()
internal
view
returns (uint256 dollarVal, uint256 collatVal)
Expand All @@ -165,20 +179,30 @@ library LibFuseRariAmoStrategy {
UBIQUITY_POOL_PRICE_PRECISION);
}

/// @notice Retrieves all pool addresses involved in the strategy.
/// @dev Returns an array of addresses representing all the Fuse pools.
/// @return An array of addresses for each pool in the strategy.
function allPoolAddresses() internal view returns (address[] memory) {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

return strategyStorage.fusePoolsArray;
}

/// @notice Gets the total number of pools in the strategy.
/// @dev Returns the length of the array containing all Fuse pool addresses.
/// @return The number of pools in the strategy.
function allPoolsLength() internal view returns (uint256) {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

return strategyStorage.fusePoolsArray.length;
}

/// @notice Converts a pool address to its corresponding index in the pool array.
/// @dev Finds the index of the given pool address in the fusePools Array.
/// @param _poolAddress The address of the pool to find.
/// @return The index of the given pool address in the fusePools Array.
function poolAddrToId(
address _poolAddress
) internal view returns (uint256) {
Expand All @@ -193,6 +217,10 @@ library LibFuseRariAmoStrategy {
revert("Pool not found");
}

/// @notice Calculates the dollar value in a specific pool by its ID.
/// @dev Multiplies the cToken balance by the exchange rate to get the dollar value.
/// @param _poolId The ID of the pool to calculate the dollar value for.
/// @return The dollar value in the specified pool.
function dollarInPoolByPoolId(
uint256 _poolId
) internal view returns (uint256) {
Expand All @@ -206,36 +234,41 @@ library LibFuseRariAmoStrategy {
return cTokenBalance * (delegator.exchangeRateStored() / (1e18));
}

/// @notice Calculates the dollar value in a specific pool by its address.
/// @dev Converts the pool address to its ID and then calculates the dollar value.
/// @param _poolAddress The address of the pool to calculate the dollar value for.
/// @return The dollar value in the specified pool.
function dollarInPoolByPoolAddr(
address _poolAddress
) internal view returns (uint256) {
uint256 poolId = poolAddrToId(_poolAddress);
return dollarInPoolByPoolId(poolId);
}

// function mintedBalance() public view returns (int256) {
// return amo_minter.dollar_mint_balances(address(this));
// }

// // Backwards compatibility
// function accumulatedProfit() public view returns (int256) {
// return int256(showAllocations()[2]) - mintedBalance();
// }

/// @notice Retrieves all borrow pool addresses involved in the strategy.
/// @dev Returns an array of addresses representing all the Fuse borrow pools.
/// @return An array of addresses for each borrow pool in the strategy.
function allBorrowPoolAddresses() internal view returns (address[] memory) {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

return strategyStorage.fuseBorrowPoolsArray;
}

/// @notice Gets the total number of borrow pools in the strategy.
/// @dev Returns the length of the array containing all Fuse borrow pool addresses.
/// @return The number of borrow pools in the strategy.
function allBorrowPoolsLength() internal view returns (uint256) {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

return strategyStorage.fuseBorrowPoolsArray.length;
}

/// @notice Converts a borrow pool address to its corresponding index in the borrow pool array.
/// @dev Finds the index of the given borrow pool address in the fuseBorrowPoolsArray.
/// @param _poolAddress The address of the borrow pool to find.
/// @return The index of the given borrow pool address in the fuseBorrowPoolsArray.
function borrowPoolAddrToId(
address _poolAddress
) internal view returns (uint256) {
Expand Down Expand Up @@ -385,7 +418,7 @@ library LibFuseRariAmoStrategy {
/* ========== Burns and givebacks ========== */

// Burn unneeded or excess DOLLAR. Goes through the minter
function burnDollar(uint256 _dollarAmount) internal {
function burnDollarStrategy(uint256 _dollarAmount) internal {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

Expand Down Expand Up @@ -504,12 +537,10 @@ library LibFuseRariAmoStrategy {
);
}

function recoverERC20(
address _tokenAddress,
uint256 _tokenAmount
) internal {
IERC20(_tokenAddress).safeTransfer(msg.sender, _tokenAmount);
function amoMinterAddress() internal view returns (address) {
FuseRariAmoStrategyData
storage strategyStorage = fuseRariAmoStrategyStorage();

emit Recovered(_tokenAddress, _tokenAmount);
return address(strategyStorage.amoMinter);
}
}
2 changes: 1 addition & 1 deletion packages/contracts/test/diamond/DiamondTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract TestDiamond is DiamondTestSetup {
}

function testHasMultipleFacets() public {
assertEq(facetAddressList.length, 21);
assertEq(facetAddressList.length, 22);
}

function testFacetsHaveCorrectSelectors() public {
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/test/diamond/DiamondTestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ abstract contract DiamondTestSetup is DiamondTestHelper, UUPSTestHelper {
selectorsOfDollarAmoMinterFacet = getSelectorsFromAbi(
"/out/DollarAmoMinterFacet.sol/DollarAmoMinterFacet.json"
);
selectorsOfDollarAmoMinterFacet = getSelectorsFromAbi(
"/out/DollarAmoMinterFacet.sol/FuseRariAmoStrategyFacet.json"
selectorsOfFuseRariAmoStrategyFacet = getSelectorsFromAbi(
"/out/FuseRariAmoStrategyFacet.sol/FuseRariAmoStrategyFacet.json"
);

// deploy facet implementation instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,58 @@ contract FuseRariAmoStrategyTest is DiamondTestSetup {
MockERC20 collateralToken;

super.setUp();

vm.startPrank(admin);
// init collateral token
collateralToken = new MockERC20("COLLATERAL", "CLT", 18);

vm.stopPrank();
}

function testAllPoolAddresses_ShouldReturnEmptyArray() public {
address[] memory poolAddresses = fuseRariAmoStrategyFacet
.allPoolAddresses();
assertEq(poolAddresses.length, 0);
}

function testAllPoolAddresses_ShouldReturnCorrectArray() public {
address newPool = address(0x401);
fuseRariAmoStrategyFacet.addFusePool(newPool);

assertTrue(fuseRariAmoStrategyFacet.allPoolAddresses().length > 0);
}

function testAllPoolsLength_ShouldReturnZero() public {
assertEq(fuseRariAmoStrategyFacet.allPoolsLength(), 0);
}

function testAllPoolsLength_ShouldReturnCorrectLength() public {
address alphaPool = address(0x401);
address betaPool = address(0x402);
address gammaPool = address(0x403);

fuseRariAmoStrategyFacet.addFusePool(alphaPool);
fuseRariAmoStrategyFacet.addFusePool(betaPool);
fuseRariAmoStrategyFacet.addFusePool(gammaPool);

assertEq(fuseRariAmoStrategyFacet.allPoolsLength(), 3);
}

function poolAddrToId_ShouldRevertIfPoolDoesNotExist() public {
fuseRariAmoStrategyFacet.poolAddrToId(address(0x401));

vm.expectRevert("Pool not found");
}

function testPoolAddrToId_ShouldReturnCorrectId() public {
address alphaPool = address(0x401);
address betaPool = address(0x402);
address gammaPool = address(0x403);

fuseRariAmoStrategyFacet.addFusePool(alphaPool);
fuseRariAmoStrategyFacet.addFusePool(betaPool);
fuseRariAmoStrategyFacet.addFusePool(gammaPool);

assertEq(fuseRariAmoStrategyFacet.poolAddrToId(alphaPool), 0);
assertEq(fuseRariAmoStrategyFacet.poolAddrToId(betaPool), 1);
assertEq(fuseRariAmoStrategyFacet.poolAddrToId(gammaPool), 2);
}
}

0 comments on commit 65d892d

Please sign in to comment.