Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed Apr 8, 2024
1 parent 87ed991 commit d2fe50c
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 126 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/aderyn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
on: [push, pull_request, workflow_dispatch]

name: Run Aderyn

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Install aderyn
run: cargo install aderyn

- name: Run aderyn
run: aderyn . -o ci-report.json

- name: Check report
run: |
jq '.issue_count | .critical, .high, .medium' ci-report.json | while read value; do
if [ "$value" -gt 0 ]; then
echo "Found issues: critical, high, or medium issue count is above zero."
exit 1
fi
done
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ docs/

# Dotenv file
.env
.DS_Store
10 changes: 10 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@ runs = 64
depth = 64
fail_on_revert = true

[fmt]
bracket_spacing = true
int_types = "long"
line_length = 120
multiline_func_header = "all"
number_underscore = "thousands"
quote_style = "double"
tab_width = 4
wrap_comments = true


# See more config options https://github.com/foundry-rs/foundry/tree/master/config
2 changes: 1 addition & 1 deletion lib/foundry-devops
14 changes: 5 additions & 9 deletions script/DeployDSC.s.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {Script} from "forge-std/Script.sol";
import {HelperConfig} from "./HelperConfig.s.sol";
import {DecentralizedStableCoin} from "../src/DecentralizedStableCoin.sol";
import {DSCEngine} from "../src/DSCEngine.sol";
import { Script } from "forge-std/Script.sol";
import { HelperConfig } from "./HelperConfig.s.sol";
import { DecentralizedStableCoin } from "../src/DecentralizedStableCoin.sol";
import { DSCEngine } from "../src/DSCEngine.sol";

contract DeployDSC is Script {
address[] public tokenAddresses;
Expand All @@ -20,11 +20,7 @@ contract DeployDSC is Script {

vm.startBroadcast(deployerKey);
DecentralizedStableCoin dsc = new DecentralizedStableCoin();
DSCEngine dscEngine = new DSCEngine(
tokenAddresses,
priceFeedAddresses,
address(dsc)
);
DSCEngine dscEngine = new DSCEngine(tokenAddresses, priceFeedAddresses, address(dsc));
dsc.transferOwnership(address(dscEngine));
vm.stopBroadcast();
return (dsc, dscEngine, helperConfig);
Expand Down
18 changes: 6 additions & 12 deletions script/HelperConfig.s.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {MockV3Aggregator} from "../test/mocks/MockV3Aggregator.sol";
import {Script} from "forge-std/Script.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import { MockV3Aggregator } from "../test/mocks/MockV3Aggregator.sol";
import { Script } from "forge-std/Script.sol";
import { ERC20Mock } from "@openzeppelin/contracts/mocks/ERC20Mock.sol";

contract HelperConfig is Script {
NetworkConfig public activeNetworkConfig;
Expand All @@ -23,7 +23,7 @@ contract HelperConfig is Script {
uint256 public DEFAULT_ANVIL_PRIVATE_KEY = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80;

constructor() {
if (block.chainid == 11155111) {
if (block.chainid == 11_155_111) {
activeNetworkConfig = getSepoliaEthConfig();
} else {
activeNetworkConfig = getOrCreateAnvilEthConfig();
Expand All @@ -47,16 +47,10 @@ contract HelperConfig is Script {
}

vm.startBroadcast();
MockV3Aggregator ethUsdPriceFeed = new MockV3Aggregator(
DECIMALS,
ETH_USD_PRICE
);
MockV3Aggregator ethUsdPriceFeed = new MockV3Aggregator(DECIMALS, ETH_USD_PRICE);
ERC20Mock wethMock = new ERC20Mock("WETH", "WETH", msg.sender, 1000e8);

MockV3Aggregator btcUsdPriceFeed = new MockV3Aggregator(
DECIMALS,
BTC_USD_PRICE
);
MockV3Aggregator btcUsdPriceFeed = new MockV3Aggregator(DECIMALS, BTC_USD_PRICE);
ERC20Mock wbtcMock = new ERC20Mock("WBTC", "WBTC", msg.sender, 1000e8);
vm.stopBroadcast();

Expand Down
66 changes: 50 additions & 16 deletions src/DSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

pragma solidity 0.8.19;

import {OracleLib, AggregatorV3Interface} from "./libraries/OracleLib.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {DecentralizedStableCoin} from "./DecentralizedStableCoin.sol";
import { OracleLib, AggregatorV3Interface } from "./libraries/OracleLib.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { DecentralizedStableCoin } from "./DecentralizedStableCoin.sol";

/*
* @title DSCEngine
Expand Down Expand Up @@ -92,7 +92,8 @@ contract DSCEngine is ReentrancyGuard {
// Events
///////////////////
event CollateralDeposited(address indexed user, address indexed token, uint256 indexed amount);
event CollateralRedeemed(address indexed redeemFrom, address indexed redeemTo, address token, uint256 amount); // if redeemFrom != redeemedTo, then it was liquidated
event CollateralRedeemed(address indexed redeemFrom, address indexed redeemTo, address token, uint256 amount); // if
// redeemFrom != redeemedTo, then it was liquidated

///////////////////
// Modifiers
Expand Down Expand Up @@ -140,7 +141,9 @@ contract DSCEngine is ReentrancyGuard {
address tokenCollateralAddress,
uint256 amountCollateral,
uint256 amountDscToMint
) external {
)
external
{
depositCollateral(tokenCollateralAddress, amountCollateral);
mintDsc(amountDscToMint);
}
Expand All @@ -151,7 +154,11 @@ contract DSCEngine is ReentrancyGuard {
* @param amountDscToBurn: The amount of DSC you want to burn
* @notice This function will withdraw your collateral and burn DSC in one transaction
*/
function redeemCollateralForDsc(address tokenCollateralAddress, uint256 amountCollateral, uint256 amountDscToBurn)
function redeemCollateralForDsc(
address tokenCollateralAddress,
uint256 amountCollateral,
uint256 amountDscToBurn
)
external
moreThanZero(amountCollateral)
isAllowedToken(tokenCollateralAddress)
Expand All @@ -167,7 +174,10 @@ contract DSCEngine is ReentrancyGuard {
* @notice This function will redeem your collateral.
* @notice If you have DSC minted, you will not be able to redeem until you burn your DSC
*/
function redeemCollateral(address tokenCollateralAddress, uint256 amountCollateral)
function redeemCollateral(
address tokenCollateralAddress,
uint256 amountCollateral
)
external
moreThanZero(amountCollateral)
nonReentrant
Expand Down Expand Up @@ -196,11 +206,17 @@ contract DSCEngine is ReentrancyGuard {
*
* @notice: You can partially liquidate a user.
* @notice: You will get a 10% LIQUIDATION_BONUS for taking the users funds.
* @notice: This function working assumes that the protocol will be roughly 150% overcollateralized in order for this to work.
* @notice: A known bug would be if the protocol was only 100% collateralized, we wouldn't be able to liquidate anyone.
* @notice: This function working assumes that the protocol will be roughly 150% overcollateralized in order for this
to work.
* @notice: A known bug would be if the protocol was only 100% collateralized, we wouldn't be able to liquidate
anyone.
* For example, if the price of the collateral plummeted before anyone could be liquidated.
*/
function liquidate(address collateral, address user, uint256 debtToCover)
function liquidate(
address collateral,
address user,
uint256 debtToCover
)
external
moreThanZero(debtToCover)
nonReentrant
Expand Down Expand Up @@ -250,7 +266,10 @@ contract DSCEngine is ReentrancyGuard {
* @param tokenCollateralAddress: The ERC20 token address of the collateral you're depositing
* @param amountCollateral: The amount of collateral you're depositing
*/
function depositCollateral(address tokenCollateralAddress, uint256 amountCollateral)
function depositCollateral(
address tokenCollateralAddress,
uint256 amountCollateral
)
public
moreThanZero(amountCollateral)
nonReentrant
Expand All @@ -267,7 +286,12 @@ contract DSCEngine is ReentrancyGuard {
///////////////////
// Private Functions
///////////////////
function _redeemCollateral(address tokenCollateralAddress, uint256 amountCollateral, address from, address to)
function _redeemCollateral(
address tokenCollateralAddress,
uint256 amountCollateral,
address from,
address to
)
private
{
s_collateralDeposited[from][tokenCollateralAddress] -= amountCollateral;
Expand Down Expand Up @@ -317,7 +341,10 @@ contract DSCEngine is ReentrancyGuard {
return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
}

function _calculateHealthFactor(uint256 totalDscMinted, uint256 collateralValueInUsd)
function _calculateHealthFactor(
uint256 totalDscMinted,
uint256 collateralValueInUsd
)
internal
pure
returns (uint256)
Expand All @@ -339,7 +366,10 @@ contract DSCEngine is ReentrancyGuard {
// External & Public View & Pure Functions
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
function calculateHealthFactor(uint256 totalDscMinted, uint256 collateralValueInUsd)
function calculateHealthFactor(
uint256 totalDscMinted,
uint256 collateralValueInUsd
)
external
pure
returns (uint256)
Expand All @@ -358,7 +388,11 @@ contract DSCEngine is ReentrancyGuard {
function getUsdValue(
address token,
uint256 amount // in WEI
) external view returns (uint256) {
)
external
view
returns (uint256)
{
return _getUsdValue(token, amount);
}

Expand Down
12 changes: 7 additions & 5 deletions src/DecentralizedStableCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

pragma solidity 0.8.19;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import { ERC20Burnable, ERC20 } from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";

/*
* @title DecentralizedStableCoin
Expand All @@ -36,21 +36,23 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
* Value (Relative Stability): Anchored (Pegged to USD)
* Collateral Type: Crypto
*
* This is the contract meant to be owned by DSCEngine. It is a ERC20 token that can be minted and burned by the DSCEngine smart contract.
* This is the contract meant to be owned by DSCEngine. It is a ERC20 token that can be minted and burned by the
DSCEngine smart contract.
*/
contract DecentralizedStableCoin is ERC20Burnable, Ownable {
error DecentralizedStableCoin__AmountMustBeMoreThanZero();
error DecentralizedStableCoin__BurnAmountExceedsBalance();
error DecentralizedStableCoin__NotZeroAddress();

/*
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner as a parameter.
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner
as a parameter.
For example:
constructor() ERC20("DecentralizedStableCoin", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {}
Related code changes can be viewed in this commit:
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60
*/
constructor() ERC20("DecentralizedStableCoin", "DSC") {}
constructor() ERC20("DecentralizedStableCoin", "DSC") { }

function burn(uint256 _amount) public override onlyOwner {
uint256 balance = balanceOf(msg.sender);
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/OracleLib.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

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

/*
* @title OracleLib
Expand Down
18 changes: 9 additions & 9 deletions test/fuzz/failOnRevert/StopOnRevertHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

pragma solidity ^0.8.19;

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

import {MockV3Aggregator} from "../../mocks/MockV3Aggregator.sol";
import {DSCEngine, AggregatorV3Interface} from "../../../src/DSCEngine.sol";
import {DecentralizedStableCoin} from "../../../src/DecentralizedStableCoin.sol";
import {MockV3Aggregator} from "../../mocks/MockV3Aggregator.sol";
import {console} from "forge-std/console.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";

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

contract StopOnRevertHandler is Test {
using EnumerableSet for EnumerableSet.AddressSet;
Expand Down
18 changes: 9 additions & 9 deletions test/fuzz/failOnRevert/StopOnRevertInvariants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pragma solidity ^0.8.19;
// TODO: users cant create stablecoins with a bad health factor
// TODO: a user should only be able to be liquidated if they have a bad health factor

import {Test} from "forge-std/Test.sol";
import {StdInvariant} from "forge-std/StdInvariant.sol";
import {DSCEngine} from "../../../src/DSCEngine.sol";
import {DecentralizedStableCoin} from "../../../src/DecentralizedStableCoin.sol";
import {HelperConfig} from "../../../script/HelperConfig.s.sol";
import {DeployDSC} from "../../../script/DeployDSC.s.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import {StopOnRevertHandler} from "./StopOnRevertHandler.t.sol";
import {console} from "forge-std/console.sol";
import { Test } from "forge-std/Test.sol";
import { StdInvariant } from "forge-std/StdInvariant.sol";
import { DSCEngine } from "../../../src/DSCEngine.sol";
import { DecentralizedStableCoin } from "../../../src/DecentralizedStableCoin.sol";
import { HelperConfig } from "../../../script/HelperConfig.s.sol";
import { DeployDSC } from "../../../script/DeployDSC.s.sol";
import { ERC20Mock } from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import { StopOnRevertHandler } from "./StopOnRevertHandler.t.sol";
import { console } from "forge-std/console.sol";

contract StopOnRevertInvariants is StdInvariant, Test {
DSCEngine public dsce;
Expand Down
9 changes: 5 additions & 4 deletions test/mocks/MockFailedMintDSC.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import { ERC20Burnable, ERC20 } from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";

contract MockFailedMintDSC is ERC20Burnable, Ownable {
error DecentralizedStableCoin__AmountMustBeMoreThanZero();
error DecentralizedStableCoin__BurnAmountExceedsBalance();
error DecentralizedStableCoin__NotZeroAddress();

/*
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner as a parameter.
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner
as a parameter.
For example:
constructor() ERC20("DecentralizedStableCoin", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {}
Related code changes can be viewed in this commit:
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60
*/
constructor() ERC20("DecentralizedStableCoin", "DSC") {}
constructor() ERC20("DecentralizedStableCoin", "DSC") { }

function burn(uint256 _amount) public override onlyOwner {
uint256 balance = balanceOf(msg.sender);
Expand Down
Loading

0 comments on commit d2fe50c

Please sign in to comment.