-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter3.sol
More file actions
120 lines (86 loc) · 4.34 KB
/
Copy pathChapter3.sol
File metadata and controls
120 lines (86 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
pragma solidity >=0.7.0 <0.9.0;
interface cETH {
// define functions of COMPOUND we'll be using
function mint() external payable; // to deposit to compound
function redeem(uint redeemTokens) external returns (uint); // to withdraw from compound
//following 2 functions to determine how much you'll be able to withdraw
function exchangeRateStored() external view returns (uint);
function balanceOf(address owner) external view returns (uint256 balance);
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
interface UniswapRouter {
function WETH() external pure returns (address);
function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
}
contract SmartBankAccount {
uint totalContractBalance = 0;
address COMPOUND_CETH_ADDRESS = 0x859e9d8a4edadfEDb5A2fF311243af80F85A91b8;
cETH ceth = cETH(COMPOUND_CETH_ADDRESS);
address UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
UniswapRouter uniswap = UniswapRouter(UNISWAP_ROUTER_ADDRESS);
function getContractBalance() public view returns(uint){
return totalContractBalance;
}
mapping(address => uint) balances;
mapping(address => uint) test;
receive() external payable{}
function addBalance() public payable {
uint256 cEthOfContractBeforeMinting = ceth.balanceOf(address(this)); //this refers to the current contract
// send ethers to mint()
ceth.mint{value: msg.value}();
uint256 cEthOfContractAfterMinting = ceth.balanceOf(address(this)); // updated balance after minting
uint cEthOfUser = cEthOfContractAfterMinting - cEthOfContractBeforeMinting; // the difference is the amount that has been created by the mint() function
balances[msg.sender] = cEthOfUser;
}
function addBalanceERC20(address erc20TokenSmartContractAddress) public {
IERC20 erc20 = IERC20(erc20TokenSmartContractAddress);
// how many erc20tokens has the user (msg.sender) approved this contract to use?
uint approvedAmountOfERC20Tokens = erc20.allowance(msg.sender, address(this));
address token = erc20TokenSmartContractAddress;
uint amountETHMin = 0;
address to = address(this);
uint deadline = block.timestamp + (24 * 60 * 60);
// transfer all those tokens that had been approved by user (msg.sender) to the smart contract (address(this))
erc20.transferFrom(msg.sender, address(this), approvedAmountOfERC20Tokens);
erc20.approve(UNISWAP_ROUTER_ADDRESS, approvedAmountOfERC20Tokens);
address[] memory path = new address[](2);
path[0] = token;
path[1] = uniswap.WETH();
uniswap.swapExactTokensForETH(approvedAmountOfERC20Tokens, amountETHMin, path, to, deadline);
//TODO : rest of the logic
// 3. deposit eth to compound
}
function getAllowanceERC20(address erc20TokenSmartContractAddress) public view returns(uint){
IERC20 erc20 = IERC20(erc20TokenSmartContractAddress);
return erc20.allowance(msg.sender, address(this));
}
function getBalance(address userAddress) public view returns(uint256) {
return balances[userAddress] * ceth.exchangeRateStored() / 1e18;
}
function getCethBalance(address userAddress) public view returns(uint256) {
return balances[userAddress];
}
function getExchangeRate() public view returns(uint256){
return ceth.exchangeRateStored();
}
function withdraw() public payable {
ceth.redeem(balances[msg.sender]);
balances[msg.sender] = 0;
}
function addMoneyToContract() public payable {
totalContractBalance += msg.value;
}
}