Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akash-joshi/d2f47d3005208aa143ec220f36067e46 to your computer and use it in GitHub Desktop.
Save akash-joshi/d2f47d3005208aa143ec220f36067e46 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=false&runs=200&gist=
// SPDX-License-Identifier: UNLICENSED
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 totalContractBalance;
}
mapping(address => uint) balances;
mapping(address => uint) cethbalances;
mapping(address => uint) depositTimestamps;
function addBalance() public payable {
balances[msg.sender] += msg.value;
cethbalances[msg.sender] += (msg.value * ceth.exchangeRateStored() / 1e18);
totalContractBalance = totalContractBalance + msg.value;
depositTimestamps[msg.sender] = block.timestamp;
// send ethers to mint()
ceth.mint{value: msg.value}();
}
function getBalance(address userAddress) public view returns(uint256) {
return balances[userAddress] * ceth.exchangeRateStored() / 1e18;
}
function getCethBalance(address userAddress) public view returns(uint256) {
return cethbalances[userAddress];
}
function getEthBalance(address userAddress) public view returns(uint256) {
return balances[userAddress];
}
function withdraw() public payable {
uint amountToTransfer = getBalance(msg.sender);
totalContractBalance = totalContractBalance - amountToTransfer;
balances[msg.sender] = 0;
ceth.redeem(getBalance(msg.sender));
(bool sent, ) = msg.sender.call{value: amountToTransfer}("");
require(sent, "Failed to withdraw");
}
function addMoneyToContract() public payable {
totalContractBalance += msg.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment