Skip to content

Instantly share code, notes, and snippets.

@Prometheo
Created September 27, 2021 07:20
Show Gist options
  • Save Prometheo/b7e2ab1749bc259a3667eec2c1d6508f to your computer and use it in GitHub Desktop.
Save Prometheo/b7e2ab1749bc259a3667eec2c1d6508f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=true&runs=200&gist=
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);
}
contract SmartBankAccount {
uint totalContractBalance = 0;
address COMPOUND_CETH_ADDRESS = 0x859e9d8a4edadfEDb5A2fF311243af80F85A91b8;
cETH ceth = cETH(COMPOUND_CETH_ADDRESS);
function getContractBalance() public view returns(uint){
return address(this).balance;
}
mapping(address => uint) ethBalances;
mapping(address => uint) cethBalances;
mapping(address => uint) depositTimestamps;
event myVAr(uint);
function addBalance() public payable {
ethBalances[msg.sender] += msg.value;
}
function deposit(uint amount, bool percent) public {
// allow user to input percentage amount instead.
if (percent) {
amount = (ethBalances[msg.sender]*amount)/100;
}
require(amount <= ethBalances[msg.sender], 'insufficient balance to invest');
uint256 cEthOfContractBeforeMinting = ceth.balanceOf(address(this)); //this refers to the current contract
// send ethers to mint()
ethBalances[msg.sender] -= amount;
ceth.mint{value: amount}();
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
cethBalances[msg.sender] += cEthOfUser;
}
function getBalance() public view returns(uint256) {
return ceth.balanceOf(address(this));
}
function getEthBalance() public view returns(uint256) {
return ethBalances[msg.sender];
}
function getCethBalance(address userAddress) public view returns(uint256) {
return cethBalances[userAddress];
}
function getExchangeRate() public view returns(uint256){
return ceth.exchangeRateStored() / 1e10;
}
function withdraw(uint amount, bool percent) public returns (bool) {
uint rate = getExchangeRate();
uint withdraw_value;
if (percent) {
withdraw_value = (cethBalances[msg.sender]*amount)/100;
}else {
withdraw_value = (amount*1e8)/rate;
}
emit myVAr(withdraw_value);
require(cethBalances[msg.sender] >= withdraw_value, 'insufficient');
cethBalances[msg.sender] -= withdraw_value;
uint totalBefore = getContractBalance();
require(ceth.redeem(withdraw_value) == 0, 'failed');
uint balanceAfter = getContractBalance();
// TODO: keep some percentage profit.
payable(msg.sender).transfer(balanceAfter - totalBefore);
return true;
}
function addMoneyToContract() public payable {
totalContractBalance += msg.value;
}
receive() external payable{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment