From 66899068ea4604354f9f0becbd7e10855d974cbc Mon Sep 17 00:00:00 2001 From: green Date: Tue, 27 Aug 2024 23:25:07 +0200 Subject: [PATCH] test(ubiquity-pool-facet): add collateral, ceiling, redeem, and dollar price manipulation tests --- .../diamond/facets/PoolFacetHandler.sol | 113 ++++++++++++++---- .../facets/UbiquityPoolFacet.invariant.t.sol | 5 +- 2 files changed, 92 insertions(+), 26 deletions(-) diff --git a/packages/contracts/test/invariant/diamond/facets/PoolFacetHandler.sol b/packages/contracts/test/invariant/diamond/facets/PoolFacetHandler.sol index b746d31e0..a9776214e 100644 --- a/packages/contracts/test/invariant/diamond/facets/PoolFacetHandler.sol +++ b/packages/contracts/test/invariant/diamond/facets/PoolFacetHandler.sol @@ -2,53 +2,116 @@ pragma solidity 0.8.19; import "forge-std/Test.sol"; +import "forge-std/console.sol"; import {UbiquityPoolFacet} from "../../../../src/dollar/facets/UbiquityPoolFacet.sol"; import {MockChainLinkFeed} from "../../../../src/dollar/mocks/MockChainLinkFeed.sol"; +import {MockCurveStableSwapNG} from "../../../../src/dollar/mocks/MockCurveStableSwapNG.sol"; contract PoolFacetHandler is Test { MockChainLinkFeed collateralTokenPriceFeed; UbiquityPoolFacet ubiquityPoolFacet; + address admin; + address user; + MockCurveStableSwapNG curveDollarPlainPool; + + event MintSuccess(uint256 dollarAmount); + event MintFailed(bytes reason); constructor( MockChainLinkFeed _collateralTokenPriceFeed, - UbiquityPoolFacet _ubiquityPoolFacet + UbiquityPoolFacet _ubiquityPoolFacet, + address _admin, + address _user, + MockCurveStableSwapNG _curveDollarPlainPool ) { collateralTokenPriceFeed = _collateralTokenPriceFeed; ubiquityPoolFacet = _ubiquityPoolFacet; + admin = _admin; + user = _user; + curveDollarPlainPool = _curveDollarPlainPool; } - function setCollateralRatio(uint256 newRatio) public { - ubiquityPoolFacet.setCollateralRatio(newRatio); + // Dollar price manipulations + //======================== + function setDollarPriceAboveThreshold() public { + vm.prank(admin); + curveDollarPlainPool.updateMockParams(1.02e18); } - function updateCollateralPrice(uint256 newPrice) public { - uint256 timestamp = block.timestamp; + function setDollarPriceBelowThreshold() public { + vm.prank(admin); + curveDollarPlainPool.updateMockParams(0.98e18); + } - collateralTokenPriceFeed.updateMockParams( - 1, - int256(newPrice), - timestamp, - timestamp, - 1 - ); + // Redeem manipulations + //======================== + function setMintAndRedeemFees(uint256 mintFee, uint256 redeemFee) public { + vm.prank(admin); + ubiquityPoolFacet.setFees(0, mintFee, redeemFee); + } - ubiquityPoolFacet.updateChainLinkCollateralPrice(0); + function setRedemptionDelay(uint256 delay) public { + vm.prank(admin); + ubiquityPoolFacet.setRedemptionDelayBlocks(delay); } - function mintUbiquityDollars( - uint256 dollarAmount, - uint256 dollarOutMin, - uint256 maxCollateralIn, - uint256 maxGovernanceIn, - bool isOneToOne + function collectRedemption() public { + ubiquityPoolFacet.collectRedemption(0); + } + + function redeemDollar( + uint256 _dollarAmount, + uint256 _governanceOutMin, + uint256 _collateralOutMin ) public { - ubiquityPoolFacet.mintDollar( + vm.prank(user); + ubiquityPoolFacet.redeemDollar( 0, - dollarAmount, - dollarOutMin, - maxCollateralIn, - maxGovernanceIn, - isOneToOne + _dollarAmount, + _governanceOutMin, + _collateralOutMin ); } + + // Ceiling manipulations + //======================== + function setPoolCeiling(uint256 newCeiling) public { + vm.prank(admin); + ubiquityPoolFacet.setPoolCeiling(0, newCeiling); + } + + // Collateral manipulations + //======================== + function updateCollateralRatio(uint256 newRatio) public { + vm.prank(admin); + ubiquityPoolFacet.setCollateralRatio(newRatio); + } + + function mintUbiquityDollars( + uint256 _dollarAmount, + uint256 _dollarOutMin, + uint256 _maxCollateralIn, + uint256 _maxGovernanceIn, + bool _isOneToOne + ) public { + vm.prank(user); + + uint256 dollarPrice = ubiquityPoolFacet.getDollarPriceUsd(); + console.log("::::::: DOLLAR PRICE:", dollarPrice); + + try + ubiquityPoolFacet.mintDollar( + 0, + _dollarAmount, + _dollarOutMin, + _maxCollateralIn, + _maxGovernanceIn, + _isOneToOne + ) + { + emit MintSuccess(_dollarAmount); + } catch (bytes memory reason) { + emit MintFailed(reason); + } + } } diff --git a/packages/contracts/test/invariant/diamond/facets/UbiquityPoolFacet.invariant.t.sol b/packages/contracts/test/invariant/diamond/facets/UbiquityPoolFacet.invariant.t.sol index f9471919f..6ebc324f9 100644 --- a/packages/contracts/test/invariant/diamond/facets/UbiquityPoolFacet.invariant.t.sol +++ b/packages/contracts/test/invariant/diamond/facets/UbiquityPoolFacet.invariant.t.sol @@ -148,7 +148,10 @@ contract UbiquityPoolFacetInvariantTest is DiamondTestSetup { handler = new PoolFacetHandler( collateralTokenPriceFeed, - ubiquityPoolFacet + ubiquityPoolFacet, + admin, + user, + curveDollarPlainPool ); targetContract(address(handler)); }