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

Adding in-line config to invariant tests #70

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ clean :; forge clean
# Remove modules
remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules"

install :; forge install cyfrin/foundry-devops@0.0.11 --no-commit && forge install smartcontractkit/[email protected] --no-commit && forge install foundry-rs/[email protected] --no-commit && forge install openzeppelin/[email protected] --no-commit
install :; forge install cyfrin/foundry-devops@0.1.0 --no-commit && forge install smartcontractkit/[email protected] --no-commit && forge install foundry-rs/[email protected] --no-commit && forge install openzeppelin/[email protected] --no-commit

# Update Dependencies
update:; forge update
Expand Down
2 changes: 1 addition & 1 deletion script/DeployDSC.s.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
pragma solidity ^0.8.25;

import { Script } from "forge-std/Script.sol";
import { HelperConfig } from "./HelperConfig.s.sol";
Expand Down
2 changes: 1 addition & 1 deletion script/HelperConfig.s.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.25;

import { MockV3Aggregator } from "../test/mocks/MockV3Aggregator.sol";
import { Script } from "forge-std/Script.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/DSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;
pragma solidity 0.8.25;

import { OracleLib, AggregatorV3Interface } from "./libraries/OracleLib.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/DecentralizedStableCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// private
// view & pure functions

pragma solidity 0.8.19;
pragma solidity 0.8.25;

import { ERC20Burnable, ERC20 } from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/OracleLib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
pragma solidity 0.8.25;

import { AggregatorV3Interface } from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

Expand Down
240 changes: 108 additions & 132 deletions test/fuzz/continueOnRevert/ContinueOnRevertHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,136 +4,112 @@

pragma solidity ^0.8.19;

// import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
// import {Test} from "forge-std/Test.sol";
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { Test } from "forge-std/Test.sol";
// import { ERC20Mock } from "@openzeppelin/contracts/mocks/ERC20Mock.sol"; Updated mock location
// import { ERC20Mock } from "../../mocks/ERC20Mock.sol";

// import {MockV3Aggregator} from "../../mocks/MockV3Aggregator.sol";
// import {DSCEngine, AggregatorV3Interface} from "../../../src/DSCEngine.sol";
// import {DecentralizedStableCoin} from "../../../src/DecentralizedStableCoin.sol";
// import {Randomish, EnumerableSet} from "../Randomish.sol";
// import {MockV3Aggregator} from "../../mocks/MockV3Aggregator.sol";
// import {console} from "forge-std/console.sol";

// contract ContinueOnRevertHandler is Test {
// using EnumerableSet for EnumerableSet.AddressSet;
// using Randomish for EnumerableSet.AddressSet;

// // Deployed contracts to interact with
// DSCEngine public dscEngine;
// DecentralizedStableCoin public dsc;
// MockV3Aggregator public ethUsdPriceFeed;
// MockV3Aggregator public btcUsdPriceFeed;
// ERC20Mock public weth;
// ERC20Mock public wbtc;

// // Ghost Variables
// uint96 public constant MAX_DEPOSIT_SIZE = type(uint96).max;

// constructor(DSCEngine _dscEngine, DecentralizedStableCoin _dsc) {
// dscEngine = _dscEngine;
// dsc = _dsc;

// address[] memory collateralTokens = dscEngine.getCollateralTokens();
// weth = ERC20Mock(collateralTokens[0]);
// wbtc = ERC20Mock(collateralTokens[1]);

// ethUsdPriceFeed = MockV3Aggregator(
// dscEngine.getCollateralTokenPriceFeed(address(weth))
// );
// btcUsdPriceFeed = MockV3Aggregator(
// dscEngine.getCollateralTokenPriceFeed(address(wbtc))
// );
// }

// // FUNCTOINS TO INTERACT WITH

// ///////////////
// // DSCEngine //
// ///////////////
// function mintAndDepositCollateral(
// uint256 collateralSeed,
// uint256 amountCollateral
// ) public {
// amountCollateral = bound(amountCollateral, 0, MAX_DEPOSIT_SIZE);
// ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
// collateral.mint(msg.sender, amountCollateral);
// dscEngine.depositCollateral(address(collateral), amountCollateral);
// }

// function redeemCollateral(
// uint256 collateralSeed,
// uint256 amountCollateral
// ) public {
// amountCollateral = bound(amountCollateral, 0, MAX_DEPOSIT_SIZE);
// ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
// dscEngine.redeemCollateral(address(collateral), amountCollateral);
// }

// function burnDsc(uint256 amountDsc) public {
// amountDsc = bound(amountDsc, 0, dsc.balanceOf(msg.sender));
// dsc.burn(amountDsc);
// }

// function mintDsc(uint256 amountDsc) public {
// amountDsc = bound(amountDsc, 0, MAX_DEPOSIT_SIZE);
// dsc.mint(msg.sender, amountDsc);
// }

// function liquidate(
// uint256 collateralSeed,
// address userToBeLiquidated,
// uint256 debtToCover
// ) public {
// ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
// dscEngine.liquidate(
// address(collateral),
// userToBeLiquidated,
// debtToCover
// );
// }

// /////////////////////////////
// // DecentralizedStableCoin //
// /////////////////////////////
// function transferDsc(uint256 amountDsc, address to) public {
// amountDsc = bound(amountDsc, 0, dsc.balanceOf(msg.sender));
// vm.prank(msg.sender);
// dsc.transfer(to, amountDsc);
// }

// /////////////////////////////
// // Aggregator //
// /////////////////////////////
// function updateCollateralPrice(
// uint128 newPrice,
// uint256 collateralSeed
// ) public {
// // int256 intNewPrice = int256(uint256(newPrice));
// int256 intNewPrice = 0;
// ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
// MockV3Aggregator priceFeed = MockV3Aggregator(
// dscEngine.getCollateralTokenPriceFeed(address(collateral))
// );

// priceFeed.updateAnswer(intNewPrice);
// }

// /// Helper Functions
// function _getCollateralFromSeed(
// uint256 collateralSeed
// ) private view returns (ERC20Mock) {
// if (collateralSeed % 2 == 0) {
// return weth;
// } else {
// return wbtc;
// }
// }

// function callSummary() external view {
// console.log("Weth total deposited", weth.balanceOf(address(dscEngine)));
// console.log("Wbtc total deposited", wbtc.balanceOf(address(dscEngine)));
// console.log("Total supply of DSC", dsc.totalSupply());
// }
// }
import { ERC20Mock } from "../../mocks/ERC20Mock.sol";

import { MockV3Aggregator } from "../../mocks/MockV3Aggregator.sol";
import { DSCEngine, AggregatorV3Interface } from "../../../src/DSCEngine.sol";
import { DecentralizedStableCoin } from "../../../src/DecentralizedStableCoin.sol";
// import {Randomish, EnumerableSet} from "../Randomish.sol"; // Randomish is not found in the codebase, EnumerableSet
// is imported from openzeppelin
import { MockV3Aggregator } from "../../mocks/MockV3Aggregator.sol";
import { console } from "forge-std/console.sol";

contract ContinueOnRevertHandler is Test {
// using EnumerableSet for EnumerableSet.AddressSet;
// using Randomish for EnumerableSet.AddressSet;

// Deployed contracts to interact with
DSCEngine public dscEngine;
DecentralizedStableCoin public dsc;
MockV3Aggregator public ethUsdPriceFeed;
MockV3Aggregator public btcUsdPriceFeed;
ERC20Mock public weth;
ERC20Mock public wbtc;

// Ghost Variables
uint96 public constant MAX_DEPOSIT_SIZE = type(uint96).max;

constructor(DSCEngine _dscEngine, DecentralizedStableCoin _dsc) {
dscEngine = _dscEngine;
dsc = _dsc;

address[] memory collateralTokens = dscEngine.getCollateralTokens();
weth = ERC20Mock(collateralTokens[0]);
wbtc = ERC20Mock(collateralTokens[1]);

ethUsdPriceFeed = MockV3Aggregator(dscEngine.getCollateralTokenPriceFeed(address(weth)));
btcUsdPriceFeed = MockV3Aggregator(dscEngine.getCollateralTokenPriceFeed(address(wbtc)));
}

// FUNCTOINS TO INTERACT WITH

///////////////
// DSCEngine //
///////////////
function mintAndDepositCollateral(uint256 collateralSeed, uint256 amountCollateral) public {
amountCollateral = bound(amountCollateral, 0, MAX_DEPOSIT_SIZE);
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
collateral.mint(msg.sender, amountCollateral);
dscEngine.depositCollateral(address(collateral), amountCollateral);
}

function redeemCollateral(uint256 collateralSeed, uint256 amountCollateral) public {
amountCollateral = bound(amountCollateral, 0, MAX_DEPOSIT_SIZE);
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
dscEngine.redeemCollateral(address(collateral), amountCollateral);
}

function burnDsc(uint256 amountDsc) public {
amountDsc = bound(amountDsc, 0, dsc.balanceOf(msg.sender));
dsc.burn(amountDsc);
}

function mintDsc(uint256 amountDsc) public {
amountDsc = bound(amountDsc, 0, MAX_DEPOSIT_SIZE);
dsc.mint(msg.sender, amountDsc);
}

function liquidate(uint256 collateralSeed, address userToBeLiquidated, uint256 debtToCover) public {
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
dscEngine.liquidate(address(collateral), userToBeLiquidated, debtToCover);
}

/////////////////////////////
// DecentralizedStableCoin //
/////////////////////////////
function transferDsc(uint256 amountDsc, address to) public {
amountDsc = bound(amountDsc, 0, dsc.balanceOf(msg.sender));
vm.prank(msg.sender);
dsc.transfer(to, amountDsc);
}

/////////////////////////////
// Aggregator //
/////////////////////////////
function updateCollateralPrice(uint128 newPrice, uint256 collateralSeed) public {
// int256 intNewPrice = int256(uint256(newPrice));
int256 intNewPrice = 0;
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
MockV3Aggregator priceFeed = MockV3Aggregator(dscEngine.getCollateralTokenPriceFeed(address(collateral)));

priceFeed.updateAnswer(intNewPrice);
}

/// Helper Functions
function _getCollateralFromSeed(uint256 collateralSeed) private view returns (ERC20Mock) {
if (collateralSeed % 2 == 0) {
return weth;
} else {
return wbtc;
}
}

function callSummary() external view {
console.log("Weth total deposited", weth.balanceOf(address(dscEngine)));
console.log("Wbtc total deposited", wbtc.balanceOf(address(dscEngine)));
console.log("Total supply of DSC", dsc.totalSupply());
}
}
Loading