forked from jklepatch/eattheblocks
-
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
Showing
27 changed files
with
4,186 additions
and
3,931 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
3,912 changes: 0 additions & 3,912 deletions
3,912
defi-development-mastery/2-integrating-defi-protocols/3-compound/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
10 changes: 10 additions & 0 deletions
10
...development-mastery/2-integrating-defi-protocols/4-homework/contracts/CTokenInterface.sol
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,10 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
interface CTokenInterface { | ||
function mint(uint mintAmount) external returns (uint); | ||
function redeem(uint redeemTokens) external returns (uint); | ||
function redeemUnderlying(uint redeemAmount) external returns (uint); | ||
function borrow(uint borrowAmount) external returns (uint); | ||
function repayBorrow(uint repayAmount) external returns (uint); | ||
function underlying() external view returns(address); | ||
} |
10 changes: 10 additions & 0 deletions
10
...opment-mastery/2-integrating-defi-protocols/4-homework/contracts/ComptrollerInterface.sol
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,10 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
interface ComptrollerInterface { | ||
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory); | ||
function getAccountLiquidity(address owner) external view returns(uint, uint, uint); | ||
} | ||
|
||
|
||
|
||
|
File renamed without changes.
84 changes: 84 additions & 0 deletions
84
defi-development-mastery/2-integrating-defi-protocols/4-homework/contracts/MyDeFiProject.sol
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,84 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
import './CTokenInterface.sol'; | ||
import './ComptrollerInterface.sol'; | ||
import './PriceOracleInterface.sol'; | ||
|
||
contract MyDeFiProject { | ||
ComptrollerInterface public comptroller; | ||
PriceOracleInterface public priceOracle; | ||
|
||
constructor( | ||
address _comptroller, | ||
address _priceOracle | ||
) { | ||
comptroller = ComptrollerInterface(_comptroller); | ||
priceOracle = PriceOracleInterface(_priceOracle); | ||
} | ||
|
||
function supply(address cTokenAddress, uint underlyingAmount ) public { | ||
CTokenInterface cToken = CTokenInterface(cTokenAddress); | ||
address underlyingAddress = cToken.underlying(); | ||
IERC20(underlyingAddress).approve(cTokenAddress, underlyingAmount); | ||
uint result = cToken.mint(underlyingAmount); | ||
require( | ||
result == 0, | ||
'cToken#mint() failed. see Compound ErrorReporter.sol for details' | ||
); | ||
} | ||
|
||
function redeem(address cTokenAddress, uint cTokenAmount) external { | ||
CTokenInterface cToken = CTokenInterface(cTokenAddress); | ||
uint result = cToken.redeem(cTokenAmount); | ||
require( | ||
result == 0, | ||
'cToken#redeem() failed. see Compound ErrorReporter.sol for more details' | ||
); | ||
} | ||
|
||
function enterMarket(address cTokenAddress) external { | ||
address[] memory markets = new address[](1); | ||
markets[0] = cTokenAddress; | ||
uint[] memory results = comptroller.enterMarkets(markets); | ||
require( | ||
results[0] == 0, | ||
'comptroller#enterMarket() failed. see Compound ErrorReporter.sol for details' | ||
); | ||
} | ||
|
||
function borrow(address cTokenAddress, uint borrowAmount) external { | ||
CTokenInterface cToken = CTokenInterface(cTokenAddress); | ||
address underlyingAddress = cToken.underlying(); | ||
uint result = cToken.borrow(borrowAmount); | ||
require( | ||
result == 0, | ||
'cToken#borrow() failed. see Compound ErrorReporter.sol for details' | ||
); | ||
} | ||
|
||
function repayBorrow(address cTokenAddress, uint underlyingAmount) external { | ||
CTokenInterface cToken = CTokenInterface(cTokenAddress); | ||
address underlyingAddress = cToken.underlying(); | ||
IERC20(underlyingAddress).approve(cTokenAddress, underlyingAmount); | ||
uint result = cToken.repayBorrow(underlyingAmount); | ||
require( | ||
result == 0, | ||
'cToken#borrow() failed. see Compound ErrorReporter.sol for details' | ||
); | ||
} | ||
|
||
function getMaxBorrow(address cTokenAddress) external view returns(uint) { | ||
(uint result, uint liquidity, uint shortfall) = comptroller | ||
.getAccountLiquidity(address(this)); | ||
require( | ||
result == 0, | ||
'comptroller#getAccountLiquidity() failed. see Compound ErrorReporter.sol for details' | ||
); | ||
require(shortfall == 0, 'account underwater'); | ||
require(liquidity > 0, 'account does not have collateral'); | ||
uint underlyingPrice = priceOracle.getUnderlyingPrice(cTokenAddress); | ||
return liquidity / underlyingPrice; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...opment-mastery/2-integrating-defi-protocols/4-homework/contracts/PriceOracleInterface.sol
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,5 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
interface PriceOracleInterface { | ||
function getUnderlyingPrice(address asset) external view returns(uint); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.