Skip to content

Instantly share code, notes, and snippets.

@TobiCrackIT
Created April 24, 2022 19:37
Show Gist options
  • Save TobiCrackIT/48ee7ff676474f7d813b1932f77bb8ab to your computer and use it in GitHub Desktop.
Save TobiCrackIT/48ee7ff676474f7d813b1932f77bb8ab to your computer and use it in GitHub Desktop.
Savings Smart Contract using Compound
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
interface cETH {
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 SolidBank {
uint totalContractBalance = 0;
//Compound ETH (cETH) contract address
address COMPOUND_CETH_ADDRESS = 0x859e9d8a4edadfEDb5A2fF311243af80F85A91b8;
cETH ceth = cETH(COMPOUND_CETH_ADDRESS);
function getContractBalance() public view returns(uint){
return totalContractBalance;
}
mapping(address => uint) balances;
function addBalance() public payable {
// send ethers to Compound
ceth.mint{value: msg.value}();
// get cETH received in return for ETH sent
balances[msg.sender] = balances[msg.sender] + calculateBalances();
}
function calculateBalances() public returns(uint256) {
uint oldBalance = getContractBalance();
totalContractBalance = ceth.balanceOf(address(this));
return totalContractBalance - oldBalance;
}
function getBalance(address userAddress) public view returns(uint256) {
return balances[userAddress] * ceth.exchangeRateStored() / 1e18;
}
function withdraw() public payable {
//Address to withdraw ETH into
address payable withdrawTo = payable(msg.sender);
uint amount = getBalance(msg.sender);
ceth.redeem(amount);
withdrawTo.transfer(amount);
balances[msg.sender] = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment