I am trying to mint ceth from compound and redeem it. But the withdraw function in which I call redeem function of Compound is giving the following response. enter image description here
pragma solidity ^0.8.3;
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 smartBankContract{ event MyLog(string, uint256);
address COMPOUND_CETH_ADDRESS = 0x859e9d8a4edadfEDb5A2fF311243af80F85A91b8;
cETH ceth = cETH(COMPOUND_CETH_ADDRESS);
uint totalContractBalance = 0;
mapping(address => uint) balances;
mapping(address => uint) depositTimestamps;
function getContractBalance() public view returns(uint){
return totalContractBalance;
}
function addBalance() public payable{
uint balanceBefore = ceth.balanceOf(address(this));
ceth.mint{value:msg.value}();
uint balanceAfter = ceth.balanceOf(address(this));
balances[msg.sender] = balanceAfter - balanceBefore;
totalContractBalance = totalContractBalance + msg.value;
depositTimestamps[msg.sender] = block.timestamp;
}
function getBalance(address user) public view returns(uint){
return balances[user] * ceth.exchangeRateStored() / 1e18 ;
}
function getExchangeRate() public view returns(uint){
return ceth.exchangeRateStored() / 1e18 ;
}
function getCethBalance(address user) public view returns(uint){
return balances[user] ;
}
// function withdraw() public payable{
// address payable withdrawTo = payable(msg.sender);
// uint user_balance = getBalance(msg.sender);
// uint redeemResult = ceth.redeem(getCethBalance(msg.sender));
// emit MyLog("If this is not 0, there was an error", redeemResult);
// withdrawTo.transfer((98*user_balance)/100);
// totalContractBalance = totalContractBalance - user_balance;
// balances[msg.sender] = 0;
// }
function withdraw() public payable {
ceth.redeem((90*balances[msg.sender])/100);
balances[msg.sender] = 0;
}
function addMoneyToContract() public payable {
totalContractBalance += msg.value;
}
}