From 40d3b6e67a3b76922d0ad42573fb3b07ca5c2509 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 00:25:37 +0530 Subject: [PATCH] rewards, claim functions --- .../connectors/compound-iii/events.sol | 46 +++- .../connectors/compound-iii/helpers.sol | 16 +- .../connectors/compound-iii/interface.sol | 26 +- .../mainnet/connectors/compound-iii/main.sol | 241 ++++++++++++------ 4 files changed, 233 insertions(+), 96 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 9a78dc96..92048049 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -80,18 +80,12 @@ contract Events { uint256 setId ); - event LogPayback( - address indexed market, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogPayback(address indexed market, uint256 tokenAmt, uint256 setId); event LogPaybackOnBehalfOf( address indexed market, address to, uint256 tokenAmt, - uint256 getId, uint256 setId ); @@ -100,17 +94,24 @@ contract Events { address from, address to, uint256 tokenAmt, - uint256 getId, uint256 setId ); event LogRewardsClaimed( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 cTokenAmt, + address indexed market, + address indexed account, + uint256 indexed totalClaimedInWei, uint256 getId, - uint256 setId + bool accrued + ); + + event LogRewardsClaimedTo( + address indexed market, + address indexed account, + address to, + uint256 indexed totalClaimedInWei, + uint256 getId, + bool accrued ); event LogLiquidate( @@ -121,4 +122,23 @@ contract Events { uint256 getId, uint256 setId ); + + event LogBuyCollateral( + address indexed market, + address indexed asset, + uint256 indexed baseAmount, + uint256 minCollateralAmt, + uint256 collateralAmount, + uint256 getId, + uint256 setId + ); + + event LogApproveManager( + address indexed market, + address indexed account, + address indexed asset, + uint256 amount, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 08d4ae3a..f411da59 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -1,11 +1,15 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; +pragma abicoder v2; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; -import { CometInterface } from "./interface.sol"; +import { CometInterface, CometRewards } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { + + CometRewards internal constant cometRewards = CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); + function getBaseToken(address market) internal view @@ -20,7 +24,7 @@ abstract contract Helpers is DSMath, Basic { address from, address to, uint256 amt - ) internal payable returns (bool success) { + ) public payable returns (bool success) { bytes memory data; if (from == address(0) && to == address(0)) { @@ -46,7 +50,7 @@ abstract contract Helpers is DSMath, Basic { ); } - (success, ) = market.delegateCall(data); + (success, ) = market.delegatecall(data); } function _withdraw( @@ -55,7 +59,7 @@ abstract contract Helpers is DSMath, Basic { address from, address to, uint256 amt - ) internal payable returns (bool success) { + ) internal returns (bool success) { bytes memory data; if (from == address(0) && to == address(0)) { @@ -80,14 +84,14 @@ abstract contract Helpers is DSMath, Basic { amt ); } - (success, ) = market.delegateCall(data); + (success, ) = market.delegatecall(data); } function getAccountSupplyBalanceOfAsset( address account, address market, address asset - ) internal view returns (uint256 balance) { + ) internal returns (uint256 balance) { if (asset == getBaseToken(market)) { //balance in base balance = CometInterface(market).balanceOf(account); diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound-iii/interface.sol index a72d2bb6..b1062575 100644 --- a/contracts/mainnet/connectors/compound-iii/interface.sol +++ b/contracts/mainnet/connectors/compound-iii/interface.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; +pragma abicoder v2; struct UserCollateral { uint128 balance; @@ -81,9 +82,8 @@ interface CometInterface { ) external virtual; function quoteCollateral(address asset, uint256 baseAmount) - public + external view - virtual returns (uint256); function userCollateral(address, address) @@ -93,4 +93,26 @@ interface CometInterface { function baseToken() external view returns (address); function balanceOf(address account) external view returns (uint256); + + function borrowBalanceOf(address account) external view returns (uint256); +} + +interface CometRewards { + function claim( + address comet, + address src, + bool shouldAccrue + ) external; + + function claimTo( + address comet, + address src, + address to, + bool shouldAccrue + ) external; + + 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 3e2f0991..aaf66a70 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -51,7 +51,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, 0x00, 0x00, _amt); + bool success = _supply(market, _token, address(0), address(0), _amt); require(success, "supply-failed"); setUint(setId, _amt); @@ -65,7 +65,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Deposit a token to Compound for lending / collaterization on behalf of 'to'. * @param market The address of the market where to supply. * @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE). - * @param to The address on behalf of which the supply is made. + * @param to The address on behalf of which the supply is made. * @param amt The amount of the token to deposit. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens deposited. @@ -99,7 +99,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, 0x00, to, _amt); + bool success = _supply(market, _token, address(0), to, _amt); require(success, "supply-failed"); setUint(setId, _amt); @@ -113,8 +113,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Deposit a token to Compound for lending / collaterization from a address and update the position of 'to'. * @param market The address of the market from where to supply. * @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param from The address from where amount is to be supplied. - * @param to The address on account of which the supply is made or whose positions are updated. + * @param from The address from where amount is to be supplied. + * @param to The address on account of which the supply is made or whose positions are updated. * @param amt The amount of the token to deposit. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens deposited. @@ -123,7 +123,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { address market, address token, address from, - address to, + address to, uint256 amt, uint256 getId, uint256 setId @@ -195,7 +195,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, 0x00, 0x00, _amt); + bool success = _withdraw(market, token, address(0), address(0), _amt); require(success, "withdraw-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -219,7 +219,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Withdraw base token or deposited token from Compound on behalf of an address and transfer to 'to'. * @param market The address of the market from where to withdraw. * @param token The address of the token to be withdrawn. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param to The address to which the borrowed assets are to be transferred. + * @param to The address to which the borrowed assets are to be transferred. * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. @@ -227,7 +227,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { function withdrawOnbehalf( address market, address token, - address to, + address to, uint256 amt, uint256 getId, uint256 setId @@ -253,7 +253,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, 0x00, to, _amt); + bool success = _withdraw(market, token, address(0), to, _amt); require(success, "withdraw-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -277,8 +277,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Withdraw base token or deposited token from Compound from an address and transfer to 'to'. * @param market The address of the market from where to withdraw. * @param token The address of the token to be withdrawn. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param from The address from where asset is to be withdrawed. - * @param to The address to which the borrowed assets are to be transferred. + * @param from The address from where asset is to be withdrawed. + * @param to The address to which the borrowed assets are to be transferred. * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. @@ -286,8 +286,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { function withdrawFrom( address market, address token, - address from, - address to, + address from, + address to, uint256 amt, uint256 getId, uint256 setId @@ -354,7 +354,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { require(market != address(0), "invalid market address"); - bool token = getBaseToken(market); + address token = getBaseToken(market); bool isEth = token == ethAddr; address _token = isEth ? wethAddr : token; @@ -365,7 +365,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, 0x00, 0x00, _amt);; + bool success = _withdraw(market, token, address(0), address(0), _amt); require(success, "borrow-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -388,14 +388,14 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @dev Borrow base asset and transfer to 'to' account. * @notice Withdraw base token from Compound on behalf of an address. * @param market The address of the market from where to withdraw. - * @param to The address to which the borrowed asset is transferred. + * @param to The address to which the borrowed asset is transferred. * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. */ function borrowOnBehalf( address market, - address to, + address to, uint256 amt, uint256 getId, uint256 setId @@ -408,7 +408,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { require(market != address(0), "invalid market address"); - bool token = getBaseToken(market); + address token = getBaseToken(market); bool isEth = token == ethAddr; address _token = isEth ? wethAddr : token; @@ -419,7 +419,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, 0x00, to, _amt); + bool success = _withdraw(market, token, address(0), to, _amt); require(success, "borrow-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -443,15 +443,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Withdraw base token or deposited token from Compound. * @param market The address of the market from where to withdraw. * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) - * @param from The address from where asset is to be withdrawed. - * @param to The address to which the borrowed assets are to be transferred. + * @param from The address from where asset is to be withdrawed. + * @param to The address to which the borrowed assets are to be transferred. * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. */ function borrowFrom( address market, - address from, - address to, + address from, + address to, uint256 amt, uint256 getId, uint256 setId @@ -463,25 +463,23 @@ abstract contract CompoundIIIResolver is Events, Helpers { uint256 _amt = getUint(getId, amt); require(market != address(0), "invalid market address"); - - bool token = getBaseToken(market); - bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; + bool isEth = (getBaseToken(market) == ethAddr); + address _token = isEth ? wethAddr : getBaseToken(market); TokenInterface tokenContract = TokenInterface(_token); uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + getBaseToken(market) ); - bool success = _withdraw(market, token, from, to, _amt); + bool success = _withdraw(market, _token, from, to, _amt); require(success, "borrow-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + getBaseToken(market) ); _amt = sub(finalBal, initialBal); @@ -494,19 +492,13 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, from, to, _amt, getId, setId); } - /** + /** * @dev Repays entire borrow of the base asset. * @notice Repays an entire borrow of the base asset. * @param market The address of the market from where to withdraw. - * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) - * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. */ - function payBack( - address market, - uint256 getId, - uint256 setId - ) + function payBack(address market, uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) @@ -518,34 +510,28 @@ abstract contract CompoundIIIResolver is Events, Helpers { address _token = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(_token); - if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); - } + approve(tokenContract, market, uint256(-1)); + uint256 _amt = CometInterface(market).borrowBalanceOf(address(this)); - approve(tokenContract, market, _amt); - - bool success = _supply(market, _token, 0x00, 0x00, _amt); - require(success, "supply-failed"); + bool success = _supply(market, _token, address(0), address(0), uint256(-1)); + require(success, "payback-failed"); setUint(setId, _amt); - _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, _amt, getId, setId); + _eventName = "LogPayback(address,address,uint256,uint256)"; + _eventParam = abi.encode(market, token, _amt, setId); } - /** + /** * @dev Repays entire borrow of the base asset on behalf of 'to'. * @notice Repays an entire borrow of the base asset on behalf of 'to'. * @param market The address of the market from where to withdraw. - * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) - * @param to The address on behalf of which the borrow is to be repaid. - * @param getId ID to retrieve amt. + * @param to The address on behalf of which the borrow is to be repaid. * @param setId ID stores the amount of tokens withdrawn. */ function payBackOnBehalf( address market, address to, - uint256 getId, uint256 setId ) external @@ -559,36 +545,30 @@ abstract contract CompoundIIIResolver is Events, Helpers { address _token = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(_token); - if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); - } + approve(tokenContract, market, uint256(-1)); + uint256 _amt = CometInterface(market).borrowBalanceOf(to); - approve(tokenContract, market, _amt); - - bool success = _supply(market, _token, 0x00, to, _amt); - require(success, "supply-failed"); + bool success = _supply(market, _token, address(0), to, uint256(-1)); + require(success, "paybackOnBehalf-failed"); setUint(setId, _amt); - _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, to, _amt, getId, setId); + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256)"; + _eventParam = abi.encode(market, token, to, _amt, setId); } - /** + /** * @dev Repays entire borrow of the base asset form 'from' on behalf of 'to'. * @notice Repays an entire borrow of the base asset on behalf of 'to'. * @param market The address of the market from where to withdraw. - * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) - * @param from The address from which the borrow has to be repaid on behalf of 'to'. - * @param to The address on behalf of which the borrow is to be repaid. - * @param getId ID to retrieve amt. + * @param from The address from which the borrow has to be repaid on behalf of 'to'. + * @param to The address on behalf of which the borrow is to be repaid. * @param setId ID stores the amount of tokens withdrawn. */ function payFrom( address market, address from, - address to, - uint256 getId, + address to, uint256 setId ) external @@ -602,23 +582,134 @@ abstract contract CompoundIIIResolver is Events, Helpers { address _token = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(_token); + approve(tokenContract, market, uint256(-1)); + uint256 _amt = CometInterface(market).borrowBalanceOf(to); + + bool success = _supply(market, _token, from, to, uint256(-1)); + require(success, "paybackFrom-failed"); + + setUint(setId, _amt); + + _eventName = "LogPaybackFrom(address,address,address,address,uint256,uint256)"; + _eventParam = abi.encode(market, token, from, to, _amt, setId); + } + + function buyCollateral( + address market, + address asset, + address dest, + uint256 minCollateralAmt, + uint256 baseAmt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, baseAmt); + + bool isEth = asset == ethAddr; + address _token = isEth ? wethAddr : asset; + TokenInterface tokenContract = TokenInterface(_token); + if (isEth) { convertEthToWeth(isEth, tokenContract, _amt); } - approve(tokenContract, market, _amt); + CometInterface(market).buyCollateral( + asset, + minCollateralAmt, + _amt, + dest + ); - bool success = _supply(market, _token, from, to, _amt); - require(success, "supply-failed"); + uint256 collAmt = CometInterface(market).quoteCollateral(asset, _amt); + setUint(setId, _amt); + + _eventName = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + market, + asset, + baseAmt, + minCollateralAmt, + collAmt, + getId, + setId + ); + } + + function approveManager( + address market, + address manager, + address asset, + uint256 amount, + uint256 getId, + uint256 setId + ) public returns (string memory _eventName, bytes memory _eventParam) { + uint256 _amt = getUint(getId, amount); + + CometInterface(market).approveThis(manager, asset, amount); setUint(setId, _amt); - _eventName = "LogPaybackFrom(address,address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + _eventName = "LogApproveManager(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, manager, asset, _amt, getId, setId); } + function claimRewards( + address market, + address account, + bool accrue, + uint256 setId + ) public returns (string memory _eventName, bytes memory _eventParam) { + cometRewards.claim(market, account, accrue); + + //in reward token decimals + uint256 totalRewardsClaimed = cometRewards.rewardsClaimed( + market, + account + ); + setUint(setId, totalRewardsClaimed); + + _eventName = "LogRewardsClaimed(address,address,uint256,uint256,bool)"; + _eventParam = abi.encode( + market, + account, + totalRewardsClaimed, + setId, + accrue + ); + } + + function claimRewardsTo( + address market, + address account, + address dest, + bool accrue, + uint256 setId + ) public returns (string memory _eventName, bytes memory _eventParam) { + cometRewards.claimTo(market, account, dest, accrue); + + //in reward token decimals + uint256 totalRewardsClaimed = cometRewards.rewardsClaimed( + market, + account + ); + setUint(setId, totalRewardsClaimed); + + _eventName = "LogRewardsClaimedTo(address,address,address,uint256,uint256,bool)"; + _eventParam = abi.encode( + market, + account, + dest, + totalRewardsClaimed, + setId, + accrue + ); + } } -contract ConnectV3Compound is CompoundResolver { +contract ConnectV3Compound is CompoundIIIResolver { string public name = "Compound-v1.0"; }