Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prtk418/23ea620edf730800148656d48171c5fc to your computer and use it in GitHub Desktop.
Save prtk418/23ea620edf730800148656d48171c5fc 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=
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment