-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8815273
commit 811f234
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//SPDX-License-Identifier:MIT | ||
|
||
//Handler is going to narrow down the way we call functions | ||
|
||
pragma solidity 0.8.24; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
|
||
import {DSCEngine} from "../../src/DSCEngine.sol"; | ||
import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol"; | ||
|
||
import {ERC20Mock} from "../mocks/ERC20Mock.sol"; | ||
|
||
contract Handler is Test { | ||
DSCEngine engine; | ||
DecentralizedStableCoin dsc; | ||
ERC20Mock weth; | ||
ERC20Mock wbtc; | ||
uint256 MAX_COLLATERAL_DEPOSITE_SIZE = type(uint96).max; | ||
|
||
constructor(DSCEngine _engine, DecentralizedStableCoin _dsc) { | ||
engine = _engine; | ||
dsc = _dsc; | ||
address[] memory collateralTokens = engine.getCollateralTokens(); | ||
weth = ERC20Mock(collateralTokens[0]); | ||
wbtc = ERC20Mock(collateralTokens[1]); | ||
} | ||
|
||
//redeem collateral <- | ||
function depositeCollateral(uint256 collateralSeed, uint256 collateralAmount) public { | ||
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed); | ||
collateralAmount = bound(collateralAmount, 1, MAX_COLLATERAL_DEPOSITE_SIZE); | ||
|
||
vm.startPrank(msg.sender); | ||
collateral.mint(msg.sender, collateralAmount); | ||
collateral.approve(address(engine), collateralAmount); | ||
|
||
engine.depositeCollateral(address(collateral), collateralAmount); | ||
|
||
vm.stopPrank(); | ||
} | ||
|
||
function redeemCollateral(uint256 collateralSeed, uint256 collateralAmount) public { | ||
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed); | ||
uint256 maxCollateralToRedeem = engine.getAccountCollateralBalance(msg.sender, address(weth)); | ||
collateralAmount = bound(collateralAmount, 0, maxCollateralToRedeem); | ||
|
||
if(collateralAmount ==0){ | ||
return; // aur use vm.assume | ||
} | ||
|
||
vm.startPrank(msg.sender); | ||
|
||
engine.redeemCollateral(address(collateral), collateralAmount); | ||
|
||
vm.stopPrank(); | ||
} | ||
|
||
// Helper functions | ||
function _getCollateralFromSeed(uint256 collateralSeed) private view returns (ERC20Mock) { | ||
if (collateralSeed % 2 == 0) { | ||
return weth; | ||
} | ||
return wbtc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//SPDX-License-Identifier:MIT | ||
|
||
pragma solidity 0.8.24; | ||
|
||
//have our invariants aka properties that our system should always hold | ||
|
||
//Okay now what are our invariants here ? | ||
|
||
// 1. Total value of DSC minted should be less than the total value of collateral | ||
// 2. Getter view functions should never revert // Everngreen invariant | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {StdInvariant} from "forge-std/StdInvariant.sol"; | ||
import {DeployDSC} from "../../script/DeployDSC.s.sol"; | ||
import {HelperConfig} from "../../script/HelperConfig.s.sol"; | ||
import {DSCEngine} from "../../src/DSCEngine.sol"; | ||
import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol"; | ||
|
||
import {ERC20Mock} from "../mocks/ERC20Mock.sol"; | ||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
import {Handler} from "./Handler.t.sol"; | ||
|
||
contract Invariants is StdInvariant, Test { | ||
// DeployDSC deployer; | ||
DSCEngine engine; | ||
HelperConfig helperConfig; | ||
DecentralizedStableCoin dsc; | ||
|
||
address public ethUsdPriceFeed; | ||
address public btcUsdPriceFeed; | ||
address public weth; | ||
address public wbtc; | ||
uint256 public deployerKey; | ||
Handler handler; | ||
|
||
function setUp() external { | ||
DeployDSC deployer = new DeployDSC(); | ||
console.log("kjkkk", address(deployer)); | ||
(dsc, engine, helperConfig) = deployer.run(); | ||
console.log("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); | ||
|
||
(ethUsdPriceFeed, btcUsdPriceFeed, weth, wbtc, deployerKey) = helperConfig.activeNetworkConfig(); | ||
|
||
console.log(address(engine)); | ||
// targetContract(address(engine)); | ||
// don't call redeemCollateral unless there is collateral to redeem | ||
handler = new Handler(engine, dsc); | ||
targetContract(address(handler)); | ||
} | ||
|
||
function invariant_protocolMustHaveMoreCollateralValueThanTotalDSCSupply() public view { | ||
// get the value of all the collateral in protocol | ||
// Compare it to all the debt ( DSC) | ||
|
||
uint256 totalDSCSupply = dsc.totalSupply(); | ||
uint256 ethCollateralInProtocol = ERC20Mock(weth).balanceOf(address(engine)); | ||
uint256 btcCollateralInProtocol = ERC20Mock(wbtc).balanceOf(address(engine)); | ||
|
||
uint256 ethCollateralUSDValue = engine.getUsdValue(weth, ethCollateralInProtocol); | ||
uint256 btcCollateralUSDValue = engine.getUsdValue(wbtc, btcCollateralInProtocol); | ||
console.log("bbbbbbbbbbb", btcCollateralUSDValue); | ||
uint256 totalUSDValueOfCollaterals = ethCollateralUSDValue + btcCollateralUSDValue; | ||
|
||
assert(totalDSCSupply <= totalUSDValueOfCollaterals); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// //SPDX-License-Identifier:MIT | ||
|
||
// pragma solidity 0.8.24; | ||
|
||
// //have our invariants aka properties that our system should always hold | ||
|
||
// //Okay now what are our invariants here ? | ||
|
||
// // 1. Total value of DSC minted should be less than the total value of collateral | ||
// // 2. Getter view functions should never revert // Everngreen invariant | ||
|
||
// import {Test, console} from "forge-std/Test.sol"; | ||
// import {StdInvariant} from "forge-std/StdInvariant.sol"; | ||
// import {DeployDSC} from "../../script/DeployDSC.s.sol"; | ||
// import {HelperConfig} from "../../script/HelperConfig.s.sol"; | ||
// import {DSCEngine} from "../../src/DSCEngine.sol"; | ||
// import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol"; | ||
|
||
// import {ERC20Mock} from "../mocks/ERC20Mock.sol"; | ||
// import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
// contract OpenInvariantTest is StdInvariant, Test { | ||
// // DeployDSC deployer; | ||
// DSCEngine engine; | ||
// HelperConfig helperConfig; | ||
// DecentralizedStableCoin dsc; | ||
|
||
// address public ethUsdPriceFeed; | ||
// address public btcUsdPriceFeed; | ||
// address public weth; | ||
// address public wbtc; | ||
// uint256 public deployerKey; | ||
|
||
// function setUp() external { | ||
// DeployDSC deployer = new DeployDSC(); | ||
// console.log("kjkkk", address(deployer)); | ||
// (dsc, engine, helperConfig) = deployer.run(); | ||
// console.log("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); | ||
|
||
// (ethUsdPriceFeed, btcUsdPriceFeed, weth, wbtc, deployerKey) = helperConfig.activeNetworkConfig(); | ||
|
||
// console.log(address(engine)); | ||
// targetContract(address(engine)); | ||
// console.log("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); | ||
|
||
// } | ||
|
||
// function invariant_protocolMustHaveMoreCollateralValueThanTotalDSCSupply() public view { | ||
// // get the value of all the collateral in protocol | ||
// // Compare it to all the debt ( DSC) | ||
|
||
// uint256 totalDSCSupply = dsc.totalSupply(); | ||
// uint256 ethCollateralInProtocol = ERC20Mock(weth).balanceOf(address(engine)); | ||
// uint256 btcCollateralInProtocol = ERC20Mock(wbtc).balanceOf(address(engine)); | ||
|
||
// uint256 ethCollateralUSDValue = engine.getUsdValue(weth, ethCollateralInProtocol); | ||
// uint256 btcCollateralUSDValue = engine.getUsdValue(wbtc, btcCollateralInProtocol); | ||
// console.log("bbbbbbbbbbb", btcCollateralUSDValue); | ||
// uint256 totalUSDValueOfCollaterals = ethCollateralUSDValue + btcCollateralUSDValue; | ||
|
||
// assert( totalDSCSupply <= totalUSDValueOfCollaterals); | ||
|
||
// } | ||
|
||
// } |