code refactor

This commit is contained in:
Richa-iitr 2022-09-01 21:50:18 +05:30
parent 6f61a54cf6
commit 4480f91aed
12 changed files with 87 additions and 77 deletions

View File

@ -1,8 +1,8 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.sol";
import { ComptrollerInterface, CompoundMappingInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {

View File

@ -7,8 +7,8 @@ pragma experimental ABIEncoderV2;
* @dev Lending & Borrowing.
*/
import { TokenInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
import { CETHInterface, CTokenInterface } from "./interface.sol";

View File

@ -2,9 +2,9 @@
pragma solidity ^0.7.0;
pragma abicoder v2;
import { TokenInterface } from "../../common/interfaces.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.sol";
import { CometRewards } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {

View File

@ -7,8 +7,8 @@ pragma experimental ABIEncoderV2;
* @dev Rewards.
*/
import { TokenInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";

View File

@ -2,9 +2,9 @@
pragma solidity ^0.7.0;
pragma abicoder v2;
import { TokenInterface } from "../../common/interfaces.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.sol";
import { CometInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
@ -26,7 +26,7 @@ abstract contract Helpers is DSMath, Basic {
baseToken = CometInterface(market).baseToken();
}
function _withdraw(
function _withdrawHelper(
address market,
address token,
address from,
@ -56,60 +56,79 @@ abstract contract Helpers is DSMath, Basic {
}
}
function _borrowOrWithdraw(
BorrowWithdrawParams memory params,
bool isWithdraw
) internal returns (uint256 amt, uint256 setId) {
function _borrow(BorrowWithdrawParams memory params)
internal
returns (uint256 amt, uint256 setId)
{
uint256 amt_ = getUint(params.getId, params.amt);
require(
params.market != address(0) && params.token != address(0),
"invalid market/token address"
);
bool isEth = params.token == ethAddr || params.token == wethAddr;
bool isEth = params.token == wethAddr;
address token_ = isEth ? wethAddr : params.token;
TokenInterface tokenContract = TokenInterface(token_);
params.from = params.from == address(0) ? address(this) : params.from;
uint256 initialBal = CometInterface(params.market).borrowBalanceOf(
params.from
);
uint256 balance = TokenInterface(params.market).balanceOf(params.from);
require(balance == 0, "borrow-disabled-when-supplied-base");
_withdrawHelper(params.market, token_, params.from, params.to, amt_);
uint256 finalBal = CometInterface(params.market).borrowBalanceOf(params.from);
amt_ = sub(finalBal, initialBal);
convertWethToEth(isEth, tokenContract, amt_);
setUint(params.setId, amt_);
amt = amt_;
setId = params.setId;
}
function _withdraw(BorrowWithdrawParams memory params)
internal
returns (uint256 amt, uint256 setId)
{
uint256 amt_ = getUint(params.getId, params.amt);
require(
params.market != address(0) && params.token != address(0),
"invalid market/token address"
);
bool isEth = params.token == ethAddr || params.token == wethAddr;
address token_ = isEth ? wethAddr : params.token;
TokenInterface tokenContract = TokenInterface(token_);
params.from = params.from == address(0) ? address(this) : params.from;
uint256 initialBal = getAccountSupplyBalanceOfAsset(
address(this),
params.from,
params.market,
token_
);
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
if (isWithdraw) {
if (token_ == getBaseToken(params.market)) {
//from address is address(this) for withdrawOnBehalf
params.from = params.from == address(0)
? address(this)
: params.from;
uint256 balance = TokenInterface(params.market).balanceOf(
params.from
);
if (balance > 0) {
require(
amt_ <= balance,
"withdraw-amt-greater-than-supplies"
);
}
}
} else {
params.from = params.from == address(0)
? address(this)
: params.from;
if (token_ == getBaseToken(params.market)) {
uint256 balance = TokenInterface(params.market).balanceOf(
params.from
);
require(balance == 0, "borrow-disabled-when-supplied-base");
if (balance > 0) {
require(amt_ <= balance, "withdraw-amt-greater-than-supplies");
}
}
_withdraw(params.market, token_, params.from, params.to, amt_);
_withdrawHelper(params.market, token_, params.from, params.to, amt_);
uint256 finalBal = getAccountSupplyBalanceOfAsset(
address(this),
params.from,
params.market,
token_
);

View File

@ -7,11 +7,11 @@ pragma experimental ABIEncoderV2;
* @dev Lending & Borrowing.
*/
import { TokenInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
import { CometInterface } from "./interface.sol";
import "hardhat/console.sol";
abstract contract CompoundV3Resolver is Events, Helpers {
/**
@ -253,7 +253,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdrawOnbehalf(
function withdrawOnBehalf(
address market,
address token,
address to,
@ -265,7 +265,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
(uint256 amt_, uint256 setId_) = _borrowOrWithdraw(
(uint256 amt_, uint256 setId_) = _withdraw(
BorrowWithdrawParams({
market: market,
token: token,
@ -274,8 +274,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
amt: amt,
getId: getId,
setId: setId
}),
true
})
);
eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)";
@ -306,7 +305,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
(uint256 amt_, uint256 setId_) = _borrowOrWithdraw(
(uint256 amt_, uint256 setId_) = _withdraw(
BorrowWithdrawParams({
market: market,
token: token,
@ -315,8 +314,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
amt: amt,
getId: getId,
setId: setId
}),
true
})
);
eventName_ = "LogWithdrawFromUsingManager(address,address,address,address,uint256,uint256,uint256)";
@ -327,7 +325,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
* @dev Borrow base asset.
* @notice Borrow base token from Compound.
* @param market The address of the market.
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param amt The amount of base token to borrow.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
@ -350,28 +348,22 @@ abstract contract CompoundV3Resolver is Events, Helpers {
TokenInterface tokenContract = TokenInterface(token_);
uint256 initialBal = getAccountSupplyBalanceOfAsset(
address(this),
market,
token_
);
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
if (token_ == getBaseToken(market)) {
uint256 balance = CometInterface(market).balanceOf(address(this));
require(balance == 0, "borrow-disabled-when-supplied-base");
}
CometInterface(market).withdraw(token_, amt_);
uint256 finalBal = getAccountSupplyBalanceOfAsset(
address(this),
market,
token_
uint256 initialBal = CometInterface(market).borrowBalanceOf(
address(this)
);
amt_ = sub(initialBal, finalBal);
CometInterface(market).withdraw(token_, amt_);
uint256 finalBal = CometInterface(market).borrowBalanceOf(
address(this)
);
amt_ = sub(finalBal, initialBal);
convertWethToEth(isEth, tokenContract, amt_);
@ -401,7 +393,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
(uint256 amt_, uint256 setId_) = _borrowOrWithdraw(
(uint256 amt_, uint256 setId_) = _borrow(
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
@ -410,8 +402,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
amt: amt,
getId: getId,
setId: setId
}),
false
})
);
eventName_ = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, to, amt_, getId, setId_);
@ -439,7 +430,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
(uint256 amt_, uint256 setId_) = _borrowOrWithdraw(
(uint256 amt_, uint256 setId_) = _borrow(
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
@ -448,8 +439,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
amt: amt,
getId: getId,
setId: setId
}),
false
})
);
eventName_ = "LogBorrowFromUsingManager(address,address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, from, to, amt_, getId, setId_);
@ -481,7 +471,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
TokenInterface tokenContract = TokenInterface(token_);
amt_ = amt_ == uint256(-1)
? TokenInterface(market).balanceOf(address(this))
? CometInterface(market).borrowBalanceOf(address(this))
: amt_;
uint256 borrowBal = CometInterface(market).borrowBalanceOf(
@ -531,7 +521,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
TokenInterface tokenContract = TokenInterface(token_);
amt_ = amt_ == uint256(-1)
? TokenInterface(market).balanceOf(to)
? CometInterface(market).borrowBalanceOf(to)
: amt_;
uint256 borrowBal = CometInterface(market).borrowBalanceOf(to);
@ -580,6 +570,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
TokenInterface tokenContract = TokenInterface(token_);
amt_ = setAmt(market, token_, from, amt_, isEth, true);
console.log(amt_);
uint256 borrowBal = CometInterface(market).borrowBalanceOf(to);
if (borrowBal > 0) {