Skip to content

Commit 12dccc7

Browse files
committed
adding redeemCollateral and burn function in CoinEngine
1 parent 359b615 commit 12dccc7

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/CoinEngine.sol

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ contract CoinEngine is ReentrancyGuard {
118118
///////////////////
119119

120120
/**
121-
*
121+
*
122122
* @param tokenCollateralAddress address of the token to be deposited
123123
* @param amountCollateral amount of the token to be deposited
124124
* @param amountToMint amount of the token to be minted
125125
* @notice This function is used to deposit collateral in the engine and mint SC in one go
126126
*/
127-
function depositeCollateralAndMintSC(address tokenCollateralAddress, uint256 amountCollateral, uint256 amountToMint) external {
127+
function depositeCollateralAndMintSC(address tokenCollateralAddress, uint256 amountCollateral, uint256 amountToMint)
128+
external
129+
{
128130
depositCollateral(tokenCollateralAddress, amountCollateral);
129131
mintSc(amountToMint);
130132
}
@@ -155,9 +157,55 @@ contract CoinEngine is ReentrancyGuard {
155157
_revertIfHealtFactorIsBroken(msg.sender);
156158
}
157159

160+
// Burn you own SC.
161+
function burnSC(uint256 amount) external moreThanZero(amount) {
162+
_burnSc(amount, msg.sender, msg.sender);
163+
_revertIfHealtFactorIsBroken(msg.sender); // technically not needed to do this
164+
}
165+
166+
function redeemCollateral(address tokenCollateralAddress, uint256 amountCollateral)
167+
external
168+
nonReentrant
169+
moreThanZero(amountCollateral)
170+
isAllowedToken(tokenCollateralAddress)
171+
{
172+
s_collateralDeposited[msg.sender][tokenCollateralAddress] -= amountCollateral;
173+
_redeemCollateral(tokenCollateralAddress, amountCollateral, msg.sender, msg.sender);
174+
_revertIfHealtFactorIsBroken(msg.sender);
175+
}
176+
158177
//////////////////////////////
159178
// Private & Internal View & Pure Functions
160179
//////////////////////////////
180+
181+
/**
182+
*
183+
* @param amountScToBurn amount of SC to be burned
184+
* @param onBehalfOf address of the user on behalf of whom the SC is being burned
185+
* @param dscFrom address of user who is burning the SC
186+
* @notice This function is used to burn SC also during liquidation of another user
187+
*
188+
*/
189+
function _burnSc(uint256 amountScToBurn, address onBehalfOf, address dscFrom) private {
190+
// this will auto revert if user balance is less than amount to burn in newer versions
191+
s_scMinted[onBehalfOf] -= amountScToBurn;
192+
bool success = i_sc.transferFrom(dscFrom, address(this), amountScToBurn);
193+
if (!success) {
194+
revert CoinEngine_TransferFailed();
195+
}
196+
i_sc.burn(amountScToBurn);
197+
}
198+
199+
function _redeemCollateral(address tokenCollateralAddress, uint256 amountCollateral, address from, address to)
200+
private
201+
{
202+
s_collateralDeposited[from][tokenCollateralAddress] -= amountCollateral;
203+
(bool success) = IERC20(tokenCollateralAddress).transfer(to, amountCollateral);
204+
if (!success) {
205+
revert CoinEngine_TransferFailed();
206+
}
207+
}
208+
161209
function _getValueUSD(address tokenAddress, uint256 amount) private view returns (uint256) {
162210
AggregatorV3Interface dataFeed = AggregatorV3Interface(s_priceFeeds[tokenAddress]);
163211
(, int256 price,,,) = dataFeed.latestRoundData();
@@ -209,7 +257,7 @@ contract CoinEngine is ReentrancyGuard {
209257
// External & Public View & Pure Functions
210258
/////////////////////////////////////////////
211259

212-
function getValueUSD(address tokenAddress, uint256 amount) external view returns(uint256) {
213-
return _getValueUSD(tokenAddress,amount);
260+
function getValueUSD(address tokenAddress, uint256 amount) external view returns (uint256) {
261+
return _getValueUSD(tokenAddress, amount);
214262
}
215263
}

0 commit comments

Comments
 (0)