Skip to content

Commit

Permalink
split compound in 2 lectures
Browse files Browse the repository at this point in the history
  • Loading branch information
jklepatch committed Dec 20, 2020
1 parent e661bb2 commit e529444
Show file tree
Hide file tree
Showing 27 changed files with 4,186 additions and 3,931 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ contract MyDeFiProject {

function redeem(address cTokenAddress, uint cTokenAmount) external {
CTokenInterface cToken = CTokenInterface(cTokenAddress);
cToken.redeem(cTokenAmount);
uint result = cToken.redeem(cTokenAmount);
require(
result == 0,
'cToken#redeem() failed. see Compound ErrorReporter.sol for more details'
);
}

function enterMarket(address cTokenAddress) external {
Expand All @@ -56,7 +60,6 @@ contract MyDeFiProject {
function repayBorrow(address cTokenAddress, uint underlyingAmount) external {
CTokenInterface cToken = CTokenInterface(cTokenAddress);
address underlyingAddress = cToken.underlying();
//bat.approve(address(cBat), underlyingAmount);
IERC20(underlyingAddress).approve(cTokenAddress, underlyingAmount);
uint result = cToken.repayBorrow(underlyingAmount);
require(
Expand All @@ -76,7 +79,6 @@ contract MyDeFiProject {
require(liquidity > 0, 'account does not have collateral');
uint underlyingPrice = priceOracle.getUnderlyingPrice(cTokenAddress);
return liquidity / underlyingPrice;
//require(borrowAmount <= maxBorrowUnderlying, 'not enough collateral');
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^3.2.1-solc-0.7",
"@truffle/hdwallet-provider": "^1.2.0"
"@openzeppelin/contracts": "^3.2.1-solc-0.7"
},
"devDependencies": {},
"description": ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const HDWalletProvider = require('@truffle/hdwallet-provider');

const privateKey = '';
const infuraUrl = '';
/**
* Use this file to configure your truffle project. It's seeded with some
* common settings for different networks and features like migrations,
Expand Down Expand Up @@ -62,16 +58,6 @@ module.exports = {
// },
// Useful for deploying to a public network.
// NB: It's important to wrap the provider as a function.
rinkeby: {
timeoutBlocks: 200000,
networkCheckTimeout: 10000,
provider: () => new HDWalletProvider(
privateKey,
infuraUrl
),
network_id: 4,
skipDryRun: true
},
// Useful for private networks
// private: {
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
Expand Down
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);
}
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);
}




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;
}

}
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);
}
Loading

0 comments on commit e529444

Please sign in to comment.