Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Gere321123 committed Oct 9, 2024
1 parent 28f5348 commit 0414f88
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/chainlink-brownie-contracts"]
path = lib/chainlink-brownie-contracts
url = https://github.com/smartcontractkit/chainlink-brownie-contracts
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
src = "src"
out = "out"
libs = ["lib"]
remappings = ["@oppenzeppelin/contracts=lib/openzeppelin-contracts/contracts"]
remappings = ["@chainlink/contracts=lib/chainlink-brownie-contracts/contracts","@oppenzeppelin/contracts=lib/openzeppelin-contracts/contracts"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions lib/chainlink-brownie-contracts
101 changes: 99 additions & 2 deletions src/DSCEngine.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.18;

import {DecentralizedStableCoin} from "./DecentralizedStableCoin.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

/**
* @title DecentralizedStableCoin
* @author Gergely Gere
Expand All @@ -17,19 +22,111 @@ pragma solidity ^0.8.18;
* @notice This contract is the core of the DSC System. It handles all the logic for mining and redeeming DSC, as well as depositing and withdrawing collateral.
* @notice This contract is very loosely based on the MakerDAO DSS (DAI) system.
*/
contract DSCEngine is ReentrancyGuard {
error DSCEngine__NeedsMoreThanZero();
error DSCEngine__TokenAddressesNotTheSameLengthToPriceFeedAddresses();
error DSCEngine__NoAllowedToken();
error DSCEngine__TransferFaild();

uint256 private constant ADDITIONAL_FEED_PRECISION = 1e10;
uint256 private constant PRECISION = 1e18;
uint256 private constant LIQUIDATION_THRESHOLD = 50;
uint256 private constant LIQUIDATION_PRECISION = 100;

mapping(address token => address priceFeed) private s_priceFeeds;
mapping(address user => mapping(address token => uint256 amount)) private s_collateralDeposited;
mapping(address user => uint256 amountDscMinted) private s_DSCMinted;
address[] private s_collateralTokens;

DecentralizedStableCoin private immutable i_dsc;

event CollateralDeposited(address indexed user, address indexed token, uint256 indexed amount);

modifier moreThenZero(uint256 amount) {
if (amount == 0) {
revert DSCEngine__NeedsMoreThanZero();
}
_;
}

modifier isAllowedToken(address token) {
if (s_priceFeeds[token] == address(0)) {
revert DSCEngine__NoAllowedToken();
}
_;
}

constructor(address[] memory tokenAddresses, address[] memory priceFeedAddresses, address dscAddress) {
if (tokenAddresses.length != priceFeedAddresses.length) {
revert DSCEngine__TokenAddressesNotTheSameLengthToPriceFeedAddresses();
}
for (uint256 i = 0; i < tokenAddresses.length; i++) {
s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
s_collateralTokens.push(tokenAddresses[i]);
}
i_dsc = DecentralizedStableCoin(dscAddress);
}

contract DSCEngine {
function depositCollateralAndMintDsc() external {}

function depositCollateral(address tokenCollateralAddress, uint256 amountCollateral)
external
moreThenZero(amountCollateral)
isAllowedToken(tokenCollateralAddress)
nonReentrant
{
s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral;
emit CollateralDeposited(msg.sender, tokenCollateralAddress, amountCollateral);
bool success = IERC20(tokenCollateralAddress).transferFrom(msg.sender, address(this), amountCollateral);
if (!success) {
revert DSCEngine__TransferFaild();
}
}

function redeemCollateralForDsc() external {}

function redeemCollateral() external {}

function mintDsc() external {}
function mintDsc(uint256 amountDscToMint) external moreThenZero(amountDscToMint) nonReentrant {
s_DSCMinted[msg.sender] += amountDscToMint;
revertIfHealthFactoreIsBroken(msg.sender);
}

function burnDsc() external {}

function liquidate() external {}

function getHealthFactor() external {}

function _getAccountInformation(address user)
private
view
returns (uint256 totalDscMinted, uint256 collateralValueInUsd)
{
totalDscMinted = s_DSCMinted[user];
collateralvaluInUsd = getAccountCollateralValue(user);
}

function _healthFactor(address user) private view returns (uint256) {
(uint256 totalDscMinted, uint256 collaterlValueInUsd) = _getAccountInformation(user);
uint256 collateralAdjustedForThreshold = (collaterlValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION;
}

function _revertIfHealthFactoreIsBroken(address user) internal view {}

function getAccountCollateralValue(address user) public view returns (uint256 totalCollateralValueInUsd) {
for (uint256 i = 0; i < s_collateralTokens.length; i++) {
address token = s_collateralTokens[i];
uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}
return totalCollateralValueInUsd;
}

function getUsdValue(address token, uint256 amount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
(, int256 price,,,) = priceFeed.latestRoundData();

return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
}
}

0 comments on commit 0414f88

Please sign in to comment.