From ebc9e4cfdee851ed1d8e1694fe5f6affd5b5075f Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 02:51:30 +0530 Subject: [PATCH] refactor --- .../connectors/compound-iii/events.sol | 34 ---- .../connectors/compound-iii/helpers.sol | 37 +++- .../connectors/compound-iii/interface.sol | 29 +-- .../mainnet/connectors/compound-iii/main.sol | 187 ++---------------- 4 files changed, 51 insertions(+), 236 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index b3075440..f335f4bb 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -97,23 +97,6 @@ contract Events { uint256 setId ); - event LogRewardsClaimed( - address indexed market, - address indexed account, - uint256 indexed totalClaimedInWei, - uint256 getId, - bool accrued - ); - - event LogRewardsClaimedTo( - address indexed market, - address indexed account, - address to, - uint256 indexed totalClaimedInWei, - uint256 getId, - bool accrued - ); - event LogLiquidate( address indexed borrower, address indexed tokenToPay, @@ -133,23 +116,6 @@ contract Events { uint256 setId ); - event LogTransferBase( - address indexed market, - address indexed dest, - uint256 amount, - uint256 getId, - uint256 setId - ); - - event LogTransferBaseFrom( - address indexed market, - address indexed from, - address indexed dest, - uint256 amount, - uint256 getId, - uint256 setId - ); - event LogTransferAsset( address indexed market, address token, diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 63b1a2b8..eeacdaa6 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -5,12 +5,9 @@ pragma abicoder v2; import { TokenInterface } from "../../common/interfaces.sol"; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; -import { CometInterface, CometRewards } from "./interface.sol"; +import { CometInterface } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { - CometRewards internal constant cometRewards = - CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); - struct BorrowWithdrawParams { address market; address token; @@ -116,4 +113,36 @@ abstract contract Helpers is DSMath, Basic { ); } } + + function setAmt( + address market, + address token, + address src, + uint256 amt, + bool isEth + ) internal returns (uint256) { + if (isEth) { + if (amt == uint256(-1)) { + uint256 allowance_ = CometInterface(market).allowance( + src, + market + ); + amt = src.balance < allowance_ ? src.balance : allowance_; + } + convertEthToWeth(isEth, TokenInterface(token), amt); + } else { + if (amt == uint256(-1)) { + uint256 allowance_ = CometInterface(market).allowance( + src, + market + ); + uint256 bal_ = (token == getBaseToken(market)) + ? TokenInterface(market).balanceOf(src) + : CometInterface(market).userCollateral(src, token).balance; + + amt = bal_ < allowance_ ? bal_ : allowance_; + } + } + return amt; + } } diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound-iii/interface.sol index bdbfe7c7..cdf44440 100644 --- a/contracts/mainnet/connectors/compound-iii/interface.sol +++ b/contracts/mainnet/connectors/compound-iii/interface.sol @@ -103,6 +103,11 @@ interface CometInterface { function allow(address manager, bool isAllowed_) external; + function allowance(address owner, address spender) + external + view + returns (uint256); + function allowBySig( address owner, address manager, @@ -114,27 +119,3 @@ interface CometInterface { bytes32 s ) external; } - -interface CometRewards { - function claim( - address comet, - address src, - bool shouldAccrue - ) external; - - function claimTo( - address comet, - address src, - address to, - bool shouldAccrue - ) external; - - function getRewardOwed(address comet, address account) - external - returns (RewardOwed memory); - - function rewardsClaimed(address cometProxy, address account) - external - view - returns (uint256); -} diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 2e716ce9..f00c144b 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -149,15 +149,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - if (isEth) { - amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; - convertEthToWeth(isEth, tokenContract, amt_); - } else { - amt_ = amt_ == uint256(-1) - ? tokenContract.balanceOf(address(this)) - : amt_; - } - approve(tokenContract, market, amt_); + amt_ = setAmt(market, token_, from, amt_, isEth); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -547,15 +539,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - amt_ = amt_ == uint256(-1) - ? TokenInterface(market).balanceOf(to) - : amt_; - - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } - - approve(tokenContract, market, amt_); + amt_ = setAmt(market, token_, from, amt_, isEth); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -621,151 +605,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @dev Claim rewards and interests accrued in supplied/borrowed base asset. - * @notice Claim rewards and interests accrued. - * @param market The address of the market. - * @param account The account of which the rewards are to be claimed. - * @param accrue Should accrue the rewards and interest before claiming. - * @param setId ID stores the amount of rewards claimed. - */ - function claimRewards( - address market, - address account, - bool accrue, - uint256 setId - ) public returns (string memory eventName_, bytes memory eventParam_) { - uint256 rewardsOwed = cometRewards.getRewardOwed(market, account).owed; - cometRewards.claim(market, account, accrue); - - setUint(setId, rewardsOwed); - - eventName_ = "LogRewardsClaimed(address,address,uint256,uint256,bool)"; - eventParam_ = abi.encode(market, account, rewardsOwed, setId, accrue); - } - - /** - * @dev Claim rewards and interests accrued in supplied/borrowed base asset. - * @notice Claim rewards and interests accrued and transfer to dest address. - * @param market The address of the market. - * @param account The account of which the rewards are to be claimed. - * @param dest The account where to transfer the claimed rewards. - * @param accrue Should accrue the rewards and interest before claiming. - * @param setId ID stores the amount of rewards claimed. - */ - function claimRewardsTo( - address market, - address account, - address dest, - bool accrue, - uint256 setId - ) public returns (string memory eventName_, bytes memory eventParam_) { - //in reward token decimals - uint256 rewardsOwed = cometRewards.getRewardOwed(market, account).owed; - cometRewards.claimTo(market, account, dest, accrue); - - setUint(setId, rewardsOwed); - - eventName_ = "LogRewardsClaimedTo(address,address,address,uint256,uint256,bool)"; - eventParam_ = abi.encode( - market, - account, - dest, - rewardsOwed, - setId, - accrue - ); - } - - /** - * @dev Transfer base asset to dest address from this account. - * @notice Transfer base asset to dest address from caller's account. - * @param market The address of the market. - * @param dest The account where to transfer the base assets. - * @param amount The amount of the base token to transfer. (For max: `uint256(-1)`) - * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens transferred. - */ - function transferBase( - address market, - address dest, - uint256 amount, - uint256 getId, - uint256 setId - ) - external - payable - returns (string memory eventName_, bytes memory eventParam_) - { - uint256 amt_ = getUint(getId, amount); - require(market != address(0), "invalid market address"); - - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); - - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } - - amt_ = amt_ == uint256(-1) - ? CometInterface(market).balanceOf(address(this)) - : amt_; - _transfer(market, token_, address(0), dest, amt_); - - setUint(setId, amt_); - - eventName_ = "LogTransferBase(address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, dest, amt_, getId, setId); - } - - /** - * @dev Transfer base asset to dest address from src account. - * @notice Transfer base asset to dest address from src account. - * @param market The address of the market. - * @param src The account to transfer the base assets from. - * @param dest The account to transfer the base assets to. - * @param amount The amount of the base token to transfer. (For max: `uint256(-1)`) - * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens transferred. - */ - function transferBaseFromUsingManager( - address market, - address src, - address dest, - uint256 amount, - uint256 getId, - uint256 setId - ) - external - payable - returns (string memory eventName_, bytes memory eventParam_) - { - uint256 amt_ = getUint(getId, amount); - require(market != address(0), "invalid market address"); - - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); - - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } - - amt_ = amt_ == uint256(-1) - ? CometInterface(market).balanceOf(src) - : amt_; - _transfer(market, token_, src, dest, amt_); - - setUint(setId, amt_); - - eventName_ = "LogTransferBaseFrom(address,address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, src, dest, amt_, getId, setId); - } - - /** - * @dev Transfer collateral asset to dest address from this account. + * @dev Transfer collateral or base asset to dest address from this account. * @notice Transfer collateral asset to dest address from caller's account. * @param market The address of the market. * @param token The collateral asset to transfer to dest address. @@ -801,10 +641,15 @@ abstract contract CompoundV3Resolver is Events, Helpers { } amt_ = amt_ == uint256(-1) - ? CometInterface(market) - .userCollateral(address(this), token_) - .balance + ? ( + (token_ == getBaseToken(market)) + ? TokenInterface(market).balanceOf(address(this)) + : CometInterface(market) + .userCollateral(address(this), token_) + .balance + ) : amt_; + _transfer(market, token_, address(0), dest, amt_); setUint(setId, amt_); @@ -814,7 +659,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @dev Transfer collateral asset to dest address from src account. + * @dev Transfer collateral or base asset to dest address from src account. * @notice Transfer collateral asset to dest address from src's account. * @param market The address of the market. * @param token The collateral asset to transfer to dest address. @@ -840,18 +685,12 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amount); require(market != address(0), "invalid market address"); - address token = getBaseToken(market); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } + amt_ = setAmt(market, token_, src, amt_, isEth); - amt_ = amt_ == uint256(-1) - ? CometInterface(market).userCollateral(src, token_).balance - : amt_; _transfer(market, token_, src, dest, amt_); setUint(setId, amt_);