From 77f3b6f33d88cf66b9af9e6d4a3b28d097bbd6c5 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Tue, 30 Aug 2022 18:06:00 +0530 Subject: [PATCH 01/75] basic functions added --- .../connectors/compound-iii/events.sol | 54 +++++ .../connectors/compound-iii/helpers.sol | 58 +++++ .../connectors/compound-iii/interface.sol | 96 ++++++++ .../mainnet/connectors/compound-iii/main.sol | 205 ++++++++++++++++++ 4 files changed, 413 insertions(+) create mode 100644 contracts/mainnet/connectors/compound-iii/events.sol create mode 100644 contracts/mainnet/connectors/compound-iii/helpers.sol create mode 100644 contracts/mainnet/connectors/compound-iii/interface.sol create mode 100644 contracts/mainnet/connectors/compound-iii/main.sol diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol new file mode 100644 index 00000000..a83641f4 --- /dev/null +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -0,0 +1,54 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogDeposit( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogWithdraw( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogBorrow( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogPayback( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogRewardsClaimed( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 cTokenAmt, + uint256 getId, + uint256 setId + ); + + event LogLiquidate( + address indexed borrower, + address indexed tokenToPay, + address indexed tokenInReturn, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); +} diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol new file mode 100644 index 00000000..d25925be --- /dev/null +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -0,0 +1,58 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { CometInterface } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + function getBaseToken(address market) + internal + view + returns (address baseToken) + { + baseToken = CometInterface(market).baseToken(); + } + + function _supply( + address market, + address token, + uint256 amt + ) internal payable returns (bool success) { + bytes memory data = abi.encodeWithSignature( + "supply(address, uint256)", + token, + amt + ); + (success, ) = market.delegateCall(data); + } + + function _withdraw( + address market, + address token, + uint256 amt + ) internal payable returns (bool success) { + bytes memory data = abi.encodeWithSignature( + "withdraw(address, uint256)", + token, + amt + ); + (success, ) = market.delegateCall(data); + } + + function getAccountSupplyBalanceOfAsset( + address account, + address market, + address asset + ) internal view returns (uint256 balance) { + if (asset == getBaseToken(market)) { + //balance in base + balance = CometInterface(market).balanceOf(account); + } else { + //balance in asset denomination + balance = uint256( + CometInterface(market).userCollateral(account, asset).balance + ); + } + } +} diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound-iii/interface.sol new file mode 100644 index 00000000..a72d2bb6 --- /dev/null +++ b/contracts/mainnet/connectors/compound-iii/interface.sol @@ -0,0 +1,96 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +struct UserCollateral { + uint128 balance; + uint128 _reserved; +} + +interface CometInterface { + function supply(address asset, uint256 amount) external virtual; + + function supplyTo( + address dst, + address asset, + uint256 amount + ) external virtual; + + function supplyFrom( + address from, + address dst, + address asset, + uint256 amount + ) external virtual; + + function transfer(address dst, uint256 amount) + external + virtual + returns (bool); + + function transferFrom( + address src, + address dst, + uint256 amount + ) external virtual returns (bool); + + function transferAsset( + address dst, + address asset, + uint256 amount + ) external virtual; + + function transferAssetFrom( + address src, + address dst, + address asset, + uint256 amount + ) external virtual; + + function withdraw(address asset, uint256 amount) external virtual; + + function withdrawTo( + address to, + address asset, + uint256 amount + ) external virtual; + + function withdrawFrom( + address src, + address to, + address asset, + uint256 amount + ) external virtual; + + function approveThis( + address manager, + address asset, + uint256 amount + ) external virtual; + + function withdrawReserves(address to, uint256 amount) external virtual; + + function absorb(address absorber, address[] calldata accounts) + external + virtual; + + function buyCollateral( + address asset, + uint256 minAmount, + uint256 baseAmount, + address recipient + ) external virtual; + + function quoteCollateral(address asset, uint256 baseAmount) + public + view + virtual + returns (uint256); + + function userCollateral(address, address) + external + returns (UserCollateral memory); + + function baseToken() external view returns (address); + + function balanceOf(address account) external view returns (uint256); +} diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol new file mode 100644 index 00000000..5bd56a14 --- /dev/null +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -0,0 +1,205 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Compound. + * @dev Lending & Borrowing. + */ + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; +import { CometInterface } from "./interface.sol"; + +abstract contract CompoundIIIResolver is Events, Helpers { + /** + * @dev Deposit base asset or collateral asset supported by the . + * @notice Deposit a token to Compound for lending / collaterization. + * @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 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. + */ + function deposit( + address market, + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(_token); + + if (isEth) { + convertEthToWeth(isEth, tokenContract, _amt); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, _amt, getId, setId); + } + + /** + * @dev Withdraw base/collateral asset or borrow base asset. + * @notice Withdraw base token or deposited token from Compound. + * @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 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 withdraw( + address market, + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, _amt); + require(success, "withdraw-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, _amt, getId, setId); + } + + /** + * @dev Withdraw base/collateral asset or borrow base asset. + * @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 getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ + function borrow( + address market, + uint256 amt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, _amt); + require(success, "borrow-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrow(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, _amt, getId, setId); + } + + function payBack( + address market, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, _amt, getId, setId); + } +} + +contract ConnectV3Compound is CompoundResolver { + string public name = "Compound-v1.0"; +} From a9ed4a2fcb4ec0125fabc3618cdaf796063f31c0 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Tue, 30 Aug 2022 21:12:32 +0530 Subject: [PATCH 02/75] updated onBehalfOf, from methods --- .../connectors/compound-iii/events.sol | 160 +++++-- .../connectors/compound-iii/helpers.sol | 63 ++- .../mainnet/connectors/compound-iii/main.sol | 437 +++++++++++++++++- 3 files changed, 596 insertions(+), 64 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index a83641f4..9a78dc96 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -2,53 +2,123 @@ pragma solidity ^0.7.0; contract Events { - event LogDeposit( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogDeposit( + address indexed market, + address indexed asset, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); - event LogWithdraw( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogDepositOnBehalfOf( + address indexed market, + address indexed asset, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); - event LogBorrow( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogDepositFrom( + address indexed market, + address indexed asset, + address from, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); - event LogPayback( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogWithdraw( + address indexed market, + address indexed asset, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); - event LogRewardsClaimed( - address indexed token, - address cToken, - uint256 tokenAmt, - uint256 cTokenAmt, - uint256 getId, - uint256 setId - ); - - event LogLiquidate( - address indexed borrower, - address indexed tokenToPay, - address indexed tokenInReturn, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); + event LogWithdrawOnBehalfOf( + address indexed market, + address indexed asset, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogWithdrawFrom( + address indexed market, + address indexed asset, + address from, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogBorrow( + address indexed market, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogBorrowOnBehalfOf( + address indexed market, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogBorrowFrom( + address indexed market, + address from, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogPayback( + address indexed market, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogPaybackOnBehalfOf( + address indexed market, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogPaybackFrom( + address indexed market, + address from, + address to, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogRewardsClaimed( + address indexed token, + address cToken, + uint256 tokenAmt, + uint256 cTokenAmt, + uint256 getId, + uint256 setId + ); + + event LogLiquidate( + address indexed borrower, + address indexed tokenToPay, + address indexed tokenInReturn, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index d25925be..08d4ae3a 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -17,26 +17,69 @@ abstract contract Helpers is DSMath, Basic { function _supply( address market, address token, + address from, + address to, uint256 amt ) internal payable returns (bool success) { - bytes memory data = abi.encodeWithSignature( - "supply(address, uint256)", - token, - amt - ); + bytes memory data; + + if (from == address(0) && to == address(0)) { + data = abi.encodeWithSignature( + "supply(address, uint256)", + token, + amt + ); + } else if (from == address(0)) { + data = abi.encodeWithSignature( + "supplyTo(address, address, uint256)", + to, + token, + amt + ); + } else if (from != address(0) && to != address(0)) { + data = abi.encodeWithSignature( + "supplyFrom(address, address, address, uint256)", + from, + to, + token, + amt + ); + } + (success, ) = market.delegateCall(data); } function _withdraw( address market, address token, + address from, + address to, uint256 amt ) internal payable returns (bool success) { - bytes memory data = abi.encodeWithSignature( - "withdraw(address, uint256)", - token, - amt - ); + bytes memory data; + + if (from == address(0) && to == address(0)) { + data = abi.encodeWithSignature( + "withdraw(address, uint256)", + token, + amt + ); + } else if (from == address(0)) { + data = abi.encodeWithSignature( + "withdrawTo(address, address, uint256)", + to, + token, + amt + ); + } else if (from != address(0) && to != address(0)) { + data = abi.encodeWithSignature( + "withdrawFrom(address, address, address, uint256)", + from, + to, + token, + amt + ); + } (success, ) = market.delegateCall(data); } diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 5bd56a14..3e2f0991 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -15,9 +15,9 @@ import { CometInterface } from "./interface.sol"; abstract contract CompoundIIIResolver is Events, Helpers { /** - * @dev Deposit base asset or collateral asset supported by the . + * @dev Deposit base asset or collateral asset supported by the market. * @notice Deposit a token to Compound for lending / collaterization. - * @param market The address of the market from where to supply. + * @param market The address of the market where to supply. * @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param amt The amount of the token to deposit. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. @@ -51,7 +51,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, _amt); + bool success = _supply(market, _token, 0x00, 0x00, _amt); require(success, "supply-failed"); setUint(setId, _amt); @@ -60,6 +60,104 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, token, _amt, getId, setId); } + /** + * @dev Deposit base asset or collateral asset supported by the market on behalf of 'to'. + * @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 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. + */ + function depositOnBehalf( + address market, + address token, + address to, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(_token); + + if (isEth) { + convertEthToWeth(isEth, tokenContract, _amt); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, 0x00, to, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, to, _amt, getId, setId); + } + + /** + * @dev Deposit base asset or collateral asset supported by the market from 'from' address and update the position of 'to'. + * @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 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. + */ + function depositFrom( + address market, + address token, + address from, + address to, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(_token); + + if (isEth) { + convertEthToWeth(isEth, tokenContract, _amt); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, from, to, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogDepositFrom(address,address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + } + /** * @dev Withdraw base/collateral asset or borrow base asset. * @notice Withdraw base token or deposited token from Compound. @@ -97,7 +195,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, _amt); + bool success = _withdraw(market, token, 0x00, 0x00, _amt); require(success, "withdraw-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -117,8 +215,126 @@ abstract contract CompoundIIIResolver is Events, Helpers { } /** - * @dev Withdraw base/collateral asset or borrow base asset. - * @notice Withdraw base token or deposited token from Compound. + * @dev Withdraw base/collateral asset or borrow base asset and transfer to 'to'. + * @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 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 withdrawOnbehalf( + address market, + address token, + address to, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, 0x00, to, _amt); + require(success, "withdraw-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, to, _amt, getId, setId); + } + + /** + * @dev Withdraw base/collateral asset or borrow base asset from an account and transfer to 'to'. + * @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 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 withdrawFrom( + address market, + address token, + address from, + address to, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, from, to, _amt); + require(success, "withdraw-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogWithdrawFrom(address,address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + } + + /** + * @dev Borrow base asset. + * @notice Withdraw base 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 getId ID to retrieve amt. @@ -149,7 +365,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, _amt); + bool success = _withdraw(market, token, 0x00, 0x00, _amt);; require(success, "borrow-failed"); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -168,6 +384,124 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, _amt, getId, setId); } + /** + * @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 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, + uint256 amt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, 0x00, to, _amt); + require(success, "borrow-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, to, _amt, getId, setId); + } + + /** + * @dev Borrow base asset from 'from' and transfer to 'to'. + * @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 getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ + function borrowFrom( + address market, + address from, + address to, + uint256 amt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + bool success = _withdraw(market, token, from, to, _amt); + require(success, "borrow-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + market, + token + ); + + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrowFrom(address,address,address,uint256,uint256,uint256)"; + _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, @@ -190,14 +524,99 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, _amt); + bool success = _supply(market, _token, 0x00, 0x00, _amt); require(success, "supply-failed"); setUint(setId, _amt); - _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode(market, token, _amt, getId, 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 setId ID stores the amount of tokens withdrawn. + */ + function payBackOnBehalf( + address market, + address to, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, 0x00, to, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, to, _amt, getId, 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 setId ID stores the amount of tokens withdrawn. + */ + function payFrom( + address market, + address from, + address to, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + 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); + } + + approve(tokenContract, market, _amt); + + bool success = _supply(market, _token, from, to, _amt); + require(success, "supply-failed"); + + setUint(setId, _amt); + + _eventName = "LogPaybackFrom(address,address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + } + } contract ConnectV3Compound is CompoundResolver { From 40d3b6e67a3b76922d0ad42573fb3b07ca5c2509 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 00:25:37 +0530 Subject: [PATCH 03/75] 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"; } From d5747bd903cb566e24f602b826622bf20e8a9391 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 03:26:25 +0530 Subject: [PATCH 04/75] transfer method --- .../connectors/compound-iii/events.sol | 41 ++++- .../connectors/compound-iii/helpers.sol | 33 +++- .../mainnet/connectors/compound-iii/main.sol | 144 +++++++++++++++++- 3 files changed, 212 insertions(+), 6 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 92048049..4686012c 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -105,10 +105,10 @@ contract Events { bool accrued ); - event LogRewardsClaimedTo( + event LogRewardsClaimedTo( address indexed market, address indexed account, - address to, + address to, uint256 indexed totalClaimedInWei, uint256 getId, bool accrued @@ -141,4 +141,41 @@ contract Events { uint256 getId, 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, + address indexed from, + address indexed dest, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogTransferAssetFrom( + address indexed market, + address token, + address indexed from, + address indexed dest, + 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 f411da59..500329dc 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -7,8 +7,8 @@ import { Basic } from "../../common/basic.sol"; import { CometInterface, CometRewards } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { - - CometRewards internal constant cometRewards = CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); + CometRewards internal constant cometRewards = + CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); function getBaseToken(address market) internal @@ -87,6 +87,35 @@ abstract contract Helpers is DSMath, Basic { (success, ) = market.delegatecall(data); } + function _transfer( + address market, + address token, + address from, + address to, + uint256 amt + ) public payable returns (bool success) { + bytes memory data; + + if (from == address(0)) { + data = abi.encodeWithSignature( + "transferAsset(address, address, uint256)", + dest, + token, + amt + ); + } else { + data = abi.encodeWithSignature( + "transferAssetFrom(address, address, address, uint256)", + from, + dest, + token, + amt + ); + } + + (success, ) = market.delegatecall(data); + } + function getAccountSupplyBalanceOfAsset( address account, address market, diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index aaf66a70..8f690992 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -463,7 +463,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { uint256 _amt = getUint(getId, amt); require(market != address(0), "invalid market address"); - bool isEth = (getBaseToken(market) == ethAddr); + bool isEth = (getBaseToken(market) == ethAddr); address _token = isEth ? wethAddr : getBaseToken(market); TokenInterface tokenContract = TokenInterface(_token); @@ -513,7 +513,13 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, uint256(-1)); uint256 _amt = CometInterface(market).borrowBalanceOf(address(this)); - bool success = _supply(market, _token, address(0), address(0), uint256(-1)); + bool success = _supply( + market, + _token, + address(0), + address(0), + uint256(-1) + ); require(success, "payback-failed"); setUint(setId, _amt); @@ -708,6 +714,140 @@ abstract contract CompoundIIIResolver is Events, Helpers { accrue ); } + + 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); + } + + bool success = _transfer(market, _token, address(0), dest, _amt); + require(success, "transfer-base-failed"); + + setUint(setId, _amt); + + _eventName = "LogTransferBase(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, dest, _amt, getId, setId); + } + + function transferBaseFrom( + 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); + } + + bool success = _transfer(market, _token, src, dest, _amt); + require(success, "transfer-base-from-failed"); + + setUint(setId, _amt); + + _eventName = "LogTransferBaseFrom(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, src, dest, _amt, getId, setId); + } + + function transferAsset( + address market, + address token, + 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) && token != address(0), + "invalid market address" + ); + + bool isEth = token == ethAddr; + address _token = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(_token); + + if (isEth) { + convertEthToWeth(isEth, tokenContract, _amt); + } + + bool success = _transfer(market, _token, address(0), dest, _amt); + require(success, "transfer-asset-failed"); + + setUint(setId, _amt); + + _eventName = "LogTransferAsset(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, _token, dest, _amt, getId, setId); + } + + function transferAssetFrom( + address market, + address token, + 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); + } + + bool success = _transfer(market, _token, src, dest, _amt); + require(success, "transfer-asset-from-failed"); + + setUint(setId, _amt); + + _eventName = "LogTransferAssetFrom(address,address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(market, _token, src, dest, _amt, getId, setId); + } } contract ConnectV3Compound is CompoundIIIResolver { From 6ee0badb2ef6bcbcd06f4a3f72ddef1a44fe7788 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 03:46:35 +0530 Subject: [PATCH 05/75] events + linting --- .../mainnet/connectors/compound-iii/main.sol | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 8f690992..6eca63be 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -600,6 +600,17 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, token, from, to, _amt, setId); } + /** + * @dev Buy collateral asset absorbed, from the market. + * @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached. + * @param market The address of the market from where to withdraw. + * @param asset The collateral asset to purachase. + * @param to The address on to transfer the purchased assets. + * @param minCollateralAmt Minimum amount of collateral expected to be received. + * @param baseAmt Amount of base asset to be sold for collateral. + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ function buyCollateral( address market, address asset, @@ -645,6 +656,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); } + /** + * @dev Authorize manager to perform operations on ERC20 asset for the account. + * @notice Authorize manager to perform operations on ERC20 asset for the account or withdraw Comet's Comet balance. + * @param market The address of the market from where to withdraw. + * @param asset The ERC20 asset to authorize the manager for, use this as Comet's address to withdraw Comet's comet balance. + * @param amount Amount of ERC20 asset to provide allowance for. + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ function approveManager( address market, address manager, @@ -663,6 +683,14 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, manager, asset, _amt, getId, setId); } + /** + * @dev Claim rewards and interests accrued in supplied/borrowed base asset. + * @notice Claim rewards and interests accrued. + * @param market The address of the market from where to withdraw. + * @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 tokens withdrawn. + */ function claimRewards( address market, address account, @@ -688,6 +716,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); } + /** + * @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 from where to withdraw. + * @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 tokens withdrawn. + */ function claimRewardsTo( address market, address account, @@ -715,6 +752,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); } + /** + * @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 where to supply. + * @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 deposited. + */ function transferBase( address market, address dest, @@ -747,6 +793,16 @@ abstract contract CompoundIIIResolver is Events, Helpers { _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 where to supply. + * @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 deposited. + */ function transferBaseFrom( address market, address src, @@ -780,6 +836,16 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, src, dest, _amt, getId, setId); } + /** + * @dev Transfer collateral 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 where to supply. + * @param token The collateral asset to transfer to dest address. + * @param dest The account where to transfer the base assets. + * @param amount The amount of the collateral token to transfer. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens deposited. + */ function transferAsset( address market, address token, @@ -815,6 +881,17 @@ abstract contract CompoundIIIResolver is Events, Helpers { _eventParam = abi.encode(market, _token, dest, _amt, getId, setId); } + /** + * @dev Transfer collateral 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 where to supply. + * @param token The collateral asset to transfer to dest address. + * @param src The account from where to transfer the collaterals. + * @param dest The account where to transfer the collateral assets. + * @param amount The amount of the collateral token to transfer. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens deposited. + */ function transferAssetFrom( address market, address token, From 921af2f626e2d64af439011b3041e3115042338b Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 04:54:15 +0530 Subject: [PATCH 06/75] refactor + resolve stack too deep --- .../connectors/compound-iii/helpers.sol | 59 ++++++- .../mainnet/connectors/compound-iii/main.sol | 166 +++++------------- 2 files changed, 102 insertions(+), 123 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 500329dc..597d9044 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -2,6 +2,7 @@ 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 { CometInterface, CometRewards } from "./interface.sol"; @@ -10,6 +11,16 @@ abstract contract Helpers is DSMath, Basic { CometRewards internal constant cometRewards = CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); + struct BorrowWithdrawParams { + address market; + address token; + address from; + address to; + uint256 amt; + uint256 getId; + uint256 setId; + } + function getBaseToken(address market) internal view @@ -99,7 +110,7 @@ abstract contract Helpers is DSMath, Basic { if (from == address(0)) { data = abi.encodeWithSignature( "transferAsset(address, address, uint256)", - dest, + to, token, amt ); @@ -107,7 +118,7 @@ abstract contract Helpers is DSMath, Basic { data = abi.encodeWithSignature( "transferAssetFrom(address, address, address, uint256)", from, - dest, + to, token, amt ); @@ -116,6 +127,50 @@ abstract contract Helpers is DSMath, Basic { (success, ) = market.delegatecall(data); } + function _borrowOrWithdraw(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; + address _token = isEth ? wethAddr : params.token; + + TokenInterface tokenContract = TokenInterface(_token); + + uint256 initialBal = getAccountSupplyBalanceOfAsset( + address(this), + params.market, + _token + ); + bool success = _withdraw( + params.market, + _token, + params.from, + params.to, + _amt + ); + require(success, "borrow-or-withdraw-failed"); + + uint256 finalBal = getAccountSupplyBalanceOfAsset( + address(this), + params.market, + _token + ); + _amt = sub(finalBal, initialBal); + + convertWethToEth(isEth, tokenContract, _amt); + + setUint(params.setId, _amt); + + amt = _amt; + setId = params.setId; + } + function getAccountSupplyBalanceOfAsset( address account, address market, diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 6eca63be..570f88f3 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -236,40 +236,20 @@ abstract contract CompoundIIIResolver is Events, Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amt); - - require( - market != address(0) && token != address(0), - "invalid market/token address" + (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + BorrowWithdrawParams({ + market: market, + token: token, + from: address(0), + to: to, + amt: amt, + getId: getId, + setId: setId + }) ); - bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - - TokenInterface tokenContract = TokenInterface(_token); - - uint256 initialBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token - ); - bool success = _withdraw(market, token, address(0), to, _amt); - require(success, "withdraw-failed"); - - uint256 finalBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token - ); - - _amt = sub(finalBal, initialBal); - - convertWethToEth(isEth, tokenContract, _amt); - - setUint(setId, _amt); - _eventName = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, to, _amt, getId, setId); + _eventParam = abi.encode(market, token, to, _amt, getId, _setId); } /** @@ -296,40 +276,20 @@ abstract contract CompoundIIIResolver is Events, Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amt); - - require( - market != address(0) && token != address(0), - "invalid market/token address" + (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + BorrowWithdrawParams({ + market: market, + token: token, + from: from, + to: to, + amt: amt, + getId: getId, + setId: setId + }) ); - bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - - TokenInterface tokenContract = TokenInterface(_token); - - uint256 initialBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token - ); - bool success = _withdraw(market, token, from, to, _amt); - require(success, "withdraw-failed"); - - uint256 finalBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token - ); - - _amt = sub(finalBal, initialBal); - - convertWethToEth(isEth, tokenContract, _amt); - - setUint(setId, _amt); - _eventName = "LogWithdrawFrom(address,address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + _eventParam = abi.encode(market, token, from, to, _amt, getId, _setId); } /** @@ -404,38 +364,19 @@ abstract contract CompoundIIIResolver is Events, Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amt); - - require(market != address(0), "invalid market address"); - - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - - TokenInterface tokenContract = TokenInterface(_token); - - uint256 initialBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token + (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + BorrowWithdrawParams({ + market: market, + token: getBaseToken(market), + from: address(0), + to: to, + amt: amt, + getId: getId, + setId: setId + }) ); - bool success = _withdraw(market, token, address(0), to, _amt); - require(success, "borrow-failed"); - - uint256 finalBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - token - ); - - _amt = sub(finalBal, initialBal); - - convertWethToEth(isEth, tokenContract, _amt); - - setUint(setId, _amt); - _eventName = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, to, _amt, getId, setId); + _eventParam = abi.encode(market, to, _amt, getId, _setId); } /** @@ -460,36 +401,19 @@ abstract contract CompoundIIIResolver is Events, Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amt); - - require(market != address(0), "invalid market address"); - bool isEth = (getBaseToken(market) == ethAddr); - address _token = isEth ? wethAddr : getBaseToken(market); - - TokenInterface tokenContract = TokenInterface(_token); - - uint256 initialBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - getBaseToken(market) + (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + BorrowWithdrawParams({ + market: market, + token: getBaseToken(market), + from: from, + to: to, + amt: amt, + getId: getId, + setId: setId + }) ); - bool success = _withdraw(market, _token, from, to, _amt); - require(success, "borrow-failed"); - - uint256 finalBal = getAccountSupplyBalanceOfAsset( - address(this), - market, - getBaseToken(market) - ); - - _amt = sub(finalBal, initialBal); - - convertWethToEth(isEth, tokenContract, _amt); - - setUint(setId, _amt); - _eventName = "LogBorrowFrom(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, from, to, _amt, getId, setId); + _eventParam = abi.encode(market, from, to, _amt, getId, _setId); } /** @@ -605,7 +529,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached. * @param market The address of the market from where to withdraw. * @param asset The collateral asset to purachase. - * @param to The address on to transfer the purchased assets. + * @param dest The address on to transfer the purchased assets. * @param minCollateralAmt Minimum amount of collateral expected to be received. * @param baseAmt Amount of base asset to be sold for collateral. * @param getId ID to retrieve amt. From 80bc284919ff5272180c15219eddbbd2f8669281 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 14:29:59 +0530 Subject: [PATCH 07/75] logic updated --- .../connectors/compound-iii/events.sol | 18 +++ .../connectors/compound-iii/helpers.sol | 87 ++----------- .../connectors/compound-iii/interface.sol | 13 ++ .../mainnet/connectors/compound-iii/main.sol | 119 +++++++++++++----- scripts/tests/mainnet/tokens.ts | 26 +++- 5 files changed, 150 insertions(+), 113 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 4686012c..4fa11cbb 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -178,4 +178,22 @@ contract Events { uint256 getId, uint256 setId ); + + event LogAllow( + address indexed market, + address indexed manager, + bool allow + ); + + event LogAllowWithPermit( + address indexed market, + address indexed owner, + address indexed manager, + uint256 expiry, + uint256 nonce, + uint256 v, + uint256 r, + uint256 s, + bool allow + ); } diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 597d9044..5bffaaff 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -29,73 +29,22 @@ abstract contract Helpers is DSMath, Basic { baseToken = CometInterface(market).baseToken(); } - function _supply( - address market, - address token, - address from, - address to, - uint256 amt - ) public payable returns (bool success) { - bytes memory data; - - if (from == address(0) && to == address(0)) { - data = abi.encodeWithSignature( - "supply(address, uint256)", - token, - amt - ); - } else if (from == address(0)) { - data = abi.encodeWithSignature( - "supplyTo(address, address, uint256)", - to, - token, - amt - ); - } else if (from != address(0) && to != address(0)) { - data = abi.encodeWithSignature( - "supplyFrom(address, address, address, uint256)", - from, - to, - token, - amt - ); - } - - (success, ) = market.delegatecall(data); - } - function _withdraw( address market, address token, address from, address to, uint256 amt - ) internal returns (bool success) { + ) internal { bytes memory data; if (from == address(0) && to == address(0)) { - data = abi.encodeWithSignature( - "withdraw(address, uint256)", - token, - amt - ); + CometInterface(market).withdraw(token, amt); } else if (from == address(0)) { - data = abi.encodeWithSignature( - "withdrawTo(address, address, uint256)", - to, - token, - amt - ); + CometInterface(market).withdrawTo(to, token, amt); } else if (from != address(0) && to != address(0)) { - data = abi.encodeWithSignature( - "withdrawFrom(address, address, address, uint256)", - from, - to, - token, - amt - ); + CometInterface(market).withdrawFrom(from, to, token, amt); } - (success, ) = market.delegatecall(data); } function _transfer( @@ -104,27 +53,14 @@ abstract contract Helpers is DSMath, Basic { address from, address to, uint256 amt - ) public payable returns (bool success) { + ) public payable { bytes memory data; if (from == address(0)) { - data = abi.encodeWithSignature( - "transferAsset(address, address, uint256)", - to, - token, - amt - ); + CometInterface(market).transferAsset(to, token, amt); } else { - data = abi.encodeWithSignature( - "transferAssetFrom(address, address, address, uint256)", - from, - to, - token, - amt - ); + CometInterface(market).transferAssetFrom(from, to, token, amt); } - - (success, ) = market.delegatecall(data); } function _borrowOrWithdraw(BorrowWithdrawParams memory params) @@ -147,14 +83,7 @@ abstract contract Helpers is DSMath, Basic { params.market, _token ); - bool success = _withdraw( - params.market, - _token, - params.from, - params.to, - _amt - ); - require(success, "borrow-or-withdraw-failed"); + _withdraw(params.market, _token, params.from, params.to, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound-iii/interface.sol index b1062575..785ec3b2 100644 --- a/contracts/mainnet/connectors/compound-iii/interface.sol +++ b/contracts/mainnet/connectors/compound-iii/interface.sol @@ -95,6 +95,19 @@ interface CometInterface { function balanceOf(address account) external view returns (uint256); function borrowBalanceOf(address account) external view returns (uint256); + + function allow(address manager, bool isAllowed_) external; + + function allowBySig( + address owner, + address manager, + bool isAllowed_, + uint256 nonce, + uint256 expiry, + uint8 v, + bytes32 r, + bytes32 s + ) external; } interface CometRewards { diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 570f88f3..10c14383 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -12,6 +12,7 @@ import { Stores } from "../../common/stores.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { CometInterface } from "./interface.sol"; +import "hardhat/console.sol"; abstract contract CompoundIIIResolver is Events, Helpers { /** @@ -51,8 +52,11 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, address(0), address(0), _amt); - require(success, "supply-failed"); + CometInterface(market).supply(_token, _amt); + console.log( + CometInterface(market).userCollateral(address(this), _token).balance + ); + console.log(address(this)); setUint(setId, _amt); @@ -99,8 +103,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, address(0), to, _amt); - require(success, "supply-failed"); + CometInterface(market).supplyTo(to, _token, _amt); setUint(setId, _amt); @@ -149,8 +152,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); - bool success = _supply(market, _token, from, to, _amt); - require(success, "supply-failed"); + CometInterface(market).supplyFrom(from, to, _token, _amt); setUint(setId, _amt); @@ -195,8 +197,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, address(0), address(0), _amt); - require(success, "withdraw-failed"); + + CometInterface(market).withdraw(_token, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), @@ -325,8 +327,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, token ); - bool success = _withdraw(market, token, address(0), address(0), _amt); - require(success, "borrow-failed"); + CometInterface(market).withdraw(_token, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), @@ -422,7 +423,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @param market The address of the market from where to withdraw. * @param setId ID stores the amount of tokens withdrawn. */ - function payBack(address market, uint256 setId) + function payback(address market, uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) @@ -437,14 +438,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, uint256(-1)); uint256 _amt = CometInterface(market).borrowBalanceOf(address(this)); - bool success = _supply( - market, - _token, - address(0), - address(0), - uint256(-1) - ); - require(success, "payback-failed"); + CometInterface(market).supply(_token, uint256(-1)); setUint(setId, _amt); @@ -459,7 +453,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @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( + function paybackOnBehalf( address market, address to, uint256 setId @@ -478,8 +472,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, uint256(-1)); uint256 _amt = CometInterface(market).borrowBalanceOf(to); - bool success = _supply(market, _token, address(0), to, uint256(-1)); - require(success, "paybackOnBehalf-failed"); + CometInterface(market).supplyTo(to, _token, uint256(-1)); setUint(setId, _amt); @@ -495,7 +488,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @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( + function paybackFrom( address market, address from, address to, @@ -515,8 +508,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, uint256(-1)); uint256 _amt = CometInterface(market).borrowBalanceOf(to); - bool success = _supply(market, _token, from, to, uint256(-1)); - require(success, "paybackFrom-failed"); + CometInterface(market).supplyFrom(from, to, _token, uint256(-1)); setUint(setId, _amt); @@ -708,8 +700,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } - bool success = _transfer(market, _token, address(0), dest, _amt); - require(success, "transfer-base-failed"); + _transfer(market, _token, address(0), dest, _amt); setUint(setId, _amt); @@ -751,8 +742,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } - bool success = _transfer(market, _token, src, dest, _amt); - require(success, "transfer-base-from-failed"); + _transfer(market, _token, src, dest, _amt); setUint(setId, _amt); @@ -796,8 +786,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } - bool success = _transfer(market, _token, address(0), dest, _amt); - require(success, "transfer-asset-failed"); + _transfer(market, _token, address(0), dest, _amt); setUint(setId, _amt); @@ -841,14 +830,78 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } - bool success = _transfer(market, _token, src, dest, _amt); - require(success, "transfer-asset-from-failed"); + _transfer(market, _token, src, dest, _amt); setUint(setId, _amt); _eventName = "LogTransferAssetFrom(address,address,address,address,uint256,uint256,uint256)"; _eventParam = abi.encode(market, _token, src, dest, _amt, getId, setId); } + + /** + * @dev Allow/Disallow managers to handle position. + * @notice Authorize/Remove managers to perform write operations for the position. + * @param market The address of the market where to supply. + * @param manager The address to be authorized. + * @param isAllowed Whether to allow or disallow the manager. + */ + function allow( + address market, + address manager, + bool isAllowed + ) external returns (string memory _eventName, bytes memory _eventParam) { + CometInterface(market).allow(manager, isAllowed); + _eventName = "LogAllow(address,address,bool)"; + _eventParam = abi.encode(market, manager, isAllowed); + } + + /** + * @dev Allow/Disallow managers to handle owner's position. + * @notice Authorize/Remove managers to perform write operations for owner's position. + * @param market The address of the market where to supply. + * @param owner The authorizind owner account. + * @param manager The address to be authorized. + * @param isAllowed Whether to allow or disallow the manager. + * @param nonce Signer's nonce. + * @param expiry The duration for which to permit the manager. + * @param v Recovery byte of the signature. + * @param r Half of the ECDSA signature pair. + * @param s Half of the ECDSA signature pair. + */ + function allowWithPermit( + address market, + address owner, + address manager, + bool isAllowed, + uint256 nonce, + uint256 expiry, + uint8 v, + bytes32 r, + bytes32 s + ) external returns (string memory _eventName, bytes memory _eventParam) { + CometInterface(market).allowBySig( + owner, + manager, + isAllowed, + nonce, + expiry, + v, + r, + s + ); + _eventName = "LogAllowWithPermit(address,address,address,uint256,uint256,uint256,uint256,uint256,bool)"; + _eventParam = abi.encode( + market, + owner, + manager, + isAllowed, + nonce, + expiry, + v, + r, + s + ); + } } contract ConnectV3Compound is CompoundIIIResolver { diff --git a/scripts/tests/mainnet/tokens.ts b/scripts/tests/mainnet/tokens.ts index 54e1c0bc..a5c4a67e 100644 --- a/scripts/tests/mainnet/tokens.ts +++ b/scripts/tests/mainnet/tokens.ts @@ -41,6 +41,27 @@ export const tokens = { name: "Etherem Name Services", address: "0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72", decimals: 18 + }, + comp: { + type: "token", + symbol: "COMP", + name: "Compound", + address: "0xc00e94Cb662C3520282E6f5717214004A7f26888", + decimals: 18 + }, + link: { + type: "token", + symbol: "LINK", + name: "ChainLink Token", + address: "0x514910771AF9Ca656af840dff83E8264EcF986CA", + decimals: 18 + }, + uni: { + type: "token", + symbol: "UNI", + name: "Uniswap", + address: "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", + decimals: 18 } }; @@ -48,7 +69,10 @@ export const tokenMapping: Record = { usdc: { impersonateSigner: "0xfcb19e6a322b27c06842a71e8c725399f049ae3a", address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", - abi: ["function mint(address _to, uint256 _amount) external returns (bool);"], + abi: [ + "function mint(address _to, uint256 _amount) external returns (bool)", + "function balanceOf(address user) external returns (uint256)" + ], process: async function (owner: Signer | Provider, to: any, amt: any) { const contract = new ethers.Contract(this.address, this.abi, owner); From 44549ca06618878492fe488fa9ce8d99fc0216af Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 14:42:06 +0530 Subject: [PATCH 08/75] minor change --- contracts/mainnet/connectors/compound-iii/main.sol | 5 ----- 1 file changed, 5 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 10c14383..b681d681 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -12,7 +12,6 @@ import { Stores } from "../../common/stores.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { CometInterface } from "./interface.sol"; -import "hardhat/console.sol"; abstract contract CompoundIIIResolver is Events, Helpers { /** @@ -53,10 +52,6 @@ abstract contract CompoundIIIResolver is Events, Helpers { approve(tokenContract, market, _amt); CometInterface(market).supply(_token, _amt); - console.log( - CometInterface(market).userCollateral(address(this), _token).balance - ); - console.log(address(this)); setUint(setId, _amt); From 3013589ec23290af0b39b5268ad09954404f9f0f Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 14:48:56 +0530 Subject: [PATCH 09/75] minor change --- contracts/mainnet/connectors/compound-iii/main.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index b681d681..0436d8a8 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -320,14 +320,14 @@ abstract contract CompoundIIIResolver is Events, Helpers { uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + _token ); CometInterface(market).withdraw(_token, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + _token ); _amt = sub(finalBal, initialBal); From 557d70e5585f756f20e734813c82e733f1e2934e Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 17:00:32 +0530 Subject: [PATCH 10/75] remove approveThis --- .../connectors/compound-iii/events.sol | 9 ------ .../mainnet/connectors/compound-iii/main.sol | 29 ++----------------- 2 files changed, 2 insertions(+), 36 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 4fa11cbb..788b245c 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -133,15 +133,6 @@ contract Events { uint256 setId ); - event LogApproveManager( - address indexed market, - address indexed account, - address indexed asset, - uint256 amount, - uint256 getId, - uint256 setId - ); - event LogTransferBase( address indexed market, address indexed dest, diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 0436d8a8..77678190 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -12,6 +12,7 @@ import { Stores } from "../../common/stores.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { CometInterface } from "./interface.sol"; +import "hardhat/console.sol"; abstract contract CompoundIIIResolver is Events, Helpers { /** @@ -314,6 +315,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { address token = getBaseToken(market); bool isEth = token == ethAddr; address _token = isEth ? wethAddr : token; + console.log(_amt); TokenInterface tokenContract = TokenInterface(_token); @@ -567,33 +569,6 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); } - /** - * @dev Authorize manager to perform operations on ERC20 asset for the account. - * @notice Authorize manager to perform operations on ERC20 asset for the account or withdraw Comet's Comet balance. - * @param market The address of the market from where to withdraw. - * @param asset The ERC20 asset to authorize the manager for, use this as Comet's address to withdraw Comet's comet balance. - * @param amount Amount of ERC20 asset to provide allowance for. - * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens withdrawn. - */ - 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 = "LogApproveManager(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, manager, asset, _amt, getId, setId); - } - /** * @dev Claim rewards and interests accrued in supplied/borrowed base asset. * @notice Claim rewards and interests accrued. From 6e26eb05ea46486be5e97cbc311efa44b53ba404 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 18:16:12 +0530 Subject: [PATCH 11/75] handled maxUint256 --- .../connectors/compound-iii/helpers.sol | 3 ++ .../mainnet/connectors/compound-iii/main.sol | 49 ++++++++++++++----- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 5bffaaff..d71a3cc7 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -83,6 +83,9 @@ abstract contract Helpers is DSMath, Basic { params.market, _token ); + + _amt = _amt == uint256(-1) ? initialBal : _amt; + _withdraw(params.market, _token, params.from, params.to, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 77678190..2b57652e 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -12,7 +12,6 @@ import { Stores } from "../../common/stores.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { CometInterface } from "./interface.sol"; -import "hardhat/console.sol"; abstract contract CompoundIIIResolver is Events, Helpers { /** @@ -47,11 +46,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { 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); } - approve(tokenContract, market, _amt); - CometInterface(market).supply(_token, _amt); setUint(setId, _amt); @@ -94,11 +97,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { 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); } - approve(tokenContract, market, _amt); - CometInterface(market).supplyTo(to, _token, _amt); setUint(setId, _amt); @@ -143,11 +150,15 @@ abstract contract CompoundIIIResolver is Events, Helpers { 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); } - approve(tokenContract, market, _amt); - CometInterface(market).supplyFrom(from, to, _token, _amt); setUint(setId, _amt); @@ -194,6 +205,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { token ); + _amt = _amt == uint256(-1) ? initialBal : _amt; + CometInterface(market).withdraw(_token, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -315,7 +328,6 @@ abstract contract CompoundIIIResolver is Events, Helpers { address token = getBaseToken(market); bool isEth = token == ethAddr; address _token = isEth ? wethAddr : token; - console.log(_amt); TokenInterface tokenContract = TokenInterface(_token); @@ -324,6 +336,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { market, _token ); + + _amt = _amt == uint256(-1) ? initialBal : _amt; + CometInterface(market).withdraw(_token, _amt); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -432,8 +447,8 @@ 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(address(this)); + approve(tokenContract, market, _amt); CometInterface(market).supply(_token, uint256(-1)); @@ -466,8 +481,8 @@ 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); + approve(tokenContract, market, _amt); CometInterface(market).supplyTo(to, _token, uint256(-1)); @@ -502,8 +517,8 @@ 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); + approve(tokenContract, market, _amt); CometInterface(market).supplyFrom(from, to, _token, uint256(-1)); @@ -670,6 +685,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } + _amt = _amt == uint256(-1) + ? CometInterface(market).balanceOf(address(this)) + : _amt; _transfer(market, _token, address(0), dest, _amt); setUint(setId, _amt); @@ -712,6 +730,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } + _amt = _amt == uint256(-1) + ? CometInterface(market).balanceOf(src) + : _amt; _transfer(market, _token, src, dest, _amt); setUint(setId, _amt); @@ -756,6 +777,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } + _amt = _amt == uint256(-1) + ? CometInterface(market).userCollateral(address(this), _token).balance + : _amt; _transfer(market, _token, address(0), dest, _amt); setUint(setId, _amt); @@ -800,6 +824,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, _amt); } + _amt = _amt == uint256(-1) + ? CometInterface(market).userCollateral(src, _token).balance + : _amt; _transfer(market, _token, src, dest, _amt); setUint(setId, _amt); From 1175a5ac620d7139b0909dd80c82214173c31d73 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 19:13:48 +0530 Subject: [PATCH 12/75] addressed reviews --- .../connectors/compound-iii/events.sol | 14 +- .../mainnet/connectors/compound-iii/main.sol | 431 +++++++++--------- 2 files changed, 223 insertions(+), 222 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 788b245c..b3075440 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -4,7 +4,7 @@ pragma solidity ^0.7.0; contract Events { event LogDeposit( address indexed market, - address indexed asset, + address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId @@ -12,7 +12,7 @@ contract Events { event LogDepositOnBehalfOf( address indexed market, - address indexed asset, + address indexed token, address to, uint256 tokenAmt, uint256 getId, @@ -21,7 +21,7 @@ contract Events { event LogDepositFrom( address indexed market, - address indexed asset, + address indexed token, address from, address to, uint256 tokenAmt, @@ -31,7 +31,7 @@ contract Events { event LogWithdraw( address indexed market, - address indexed asset, + address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId @@ -39,7 +39,7 @@ contract Events { event LogWithdrawOnBehalfOf( address indexed market, - address indexed asset, + address indexed token, address to, uint256 tokenAmt, uint256 getId, @@ -48,7 +48,7 @@ contract Events { event LogWithdrawFrom( address indexed market, - address indexed asset, + address indexed token, address from, address to, uint256 tokenAmt, @@ -125,7 +125,7 @@ contract Events { event LogBuyCollateral( address indexed market, - address indexed asset, + address indexed token, uint256 indexed baseAmount, uint256 minCollateralAmt, uint256 collateralAmount, diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 2b57652e..986ddf8f 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -13,11 +13,11 @@ import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { CometInterface } from "./interface.sol"; -abstract contract CompoundIIIResolver is Events, Helpers { +abstract contract CompoundV3Resolver is Events, Helpers { /** * @dev Deposit base asset or collateral asset supported by the market. * @notice Deposit a token to Compound for lending / collaterization. - * @param market The address of the market where to supply. + * @param market The address of the market. * @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param amt The amount of the token to deposit. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. @@ -32,9 +32,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amt); + uint256 amt_ = getUint(getId, amt); require( market != address(0) && token != address(0), @@ -42,31 +42,31 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - _amt = _amt == uint256(-1) ? address(this).balance : _amt; - convertEthToWeth(isEth, tokenContract, _amt); + amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; + convertEthToWeth(isEth, tokenContract, amt_); } else { - _amt = _amt == uint256(-1) + amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) - : _amt; - approve(tokenContract, market, _amt); + : amt_; + approve(tokenContract, market, amt_); } - CometInterface(market).supply(_token, _amt); + CometInterface(market).supply(token_, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, _amt, getId, setId); + eventName_ = "LogDeposit(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, amt_, getId, setId); } /** * @dev Deposit base asset or collateral asset supported by the market on behalf of 'to'. * @notice Deposit a token to Compound for lending / collaterization on behalf of 'to'. - * @param market The address of the market where to supply. + * @param market The address of the market. * @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 amt The amount of the token to deposit. (For max: `uint256(-1)`) @@ -83,9 +83,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amt); + uint256 amt_ = getUint(getId, amt); require( market != address(0) && token != address(0), @@ -93,31 +93,31 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - _amt = _amt == uint256(-1) ? address(this).balance : _amt; - convertEthToWeth(isEth, tokenContract, _amt); + amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; + convertEthToWeth(isEth, tokenContract, amt_); } else { - _amt = _amt == uint256(-1) + amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) - : _amt; - approve(tokenContract, market, _amt); + : amt_; + approve(tokenContract, market, amt_); } - CometInterface(market).supplyTo(to, _token, _amt); + CometInterface(market).supplyTo(to, token_, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, to, _amt, getId, setId); + eventName_ = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, to, amt_, getId, setId); } /** * @dev Deposit base asset or collateral asset supported by the market from 'from' address and update the position of 'to'. * @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 market The address of the market. * @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. @@ -136,9 +136,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amt); + uint256 amt_ = getUint(getId, amt); require( market != address(0) && token != address(0), @@ -146,31 +146,30 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - _amt = _amt == uint256(-1) ? address(this).balance : _amt; - convertEthToWeth(isEth, tokenContract, _amt); + amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; + convertEthToWeth(isEth, tokenContract, amt_); } else { - _amt = _amt == uint256(-1) + amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) - : _amt; - approve(tokenContract, market, _amt); + : amt_; + approve(tokenContract, market, amt_); } - CometInterface(market).supplyFrom(from, to, _token, _amt); + CometInterface(market).supplyFrom(from, to, token_, amt_); + setUint(setId, amt_); - setUint(setId, _amt); - - _eventName = "LogDepositFrom(address,address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, from, to, _amt, getId, setId); + eventName_ = "LogDepositFrom(address,address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId); } /** - * @dev Withdraw base/collateral asset or borrow base asset. + * @dev Withdraw base/collateral asset. * @notice Withdraw base token or deposited token from Compound. - * @param market The address of the market from where to withdraw. + * @param market The address of the market. * @param token The address of the token to be withdrawn. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. @@ -185,9 +184,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amt); + uint256 amt_ = getUint(getId, amt); require( market != address(0) && token != address(0), @@ -195,9 +194,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; + address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + TokenInterface tokenContract = TokenInterface(token_); uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), @@ -205,9 +204,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { token ); - _amt = _amt == uint256(-1) ? initialBal : _amt; + amt_ = amt_ == uint256(-1) ? initialBal : amt_; - CometInterface(market).withdraw(_token, _amt); + CometInterface(market).withdraw(token_, amt_); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), @@ -215,20 +214,20 @@ abstract contract CompoundIIIResolver is Events, Helpers { token ); - _amt = sub(finalBal, initialBal); + amt_ = sub(finalBal, initialBal); - convertWethToEth(isEth, tokenContract, _amt); + convertWethToEth(isEth, tokenContract, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, _amt, getId, setId); + eventName_ = "LogWithdraw(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, amt_, getId, setId); } /** - * @dev Withdraw base/collateral asset or borrow base asset and transfer to 'to'. + * @dev Withdraw base/collateral asset and transfer to 'to'. * @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 market The address of the market. * @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 amt The amount of the token to withdraw. (For max: `uint256(-1)`) @@ -245,9 +244,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + (uint256 amt_, uint256 setId_) = _borrowOrWithdraw( BorrowWithdrawParams({ market: market, token: token, @@ -259,14 +258,14 @@ abstract contract CompoundIIIResolver is Events, Helpers { }) ); - _eventName = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, to, _amt, getId, _setId); + eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, to, amt_, getId, setId_); } /** - * @dev Withdraw base/collateral asset or borrow base asset from an account and transfer to 'to'. + * @dev Withdraw base/collateral asset from an account and transfer to 'to'. * @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 market The address of the market. * @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. @@ -285,9 +284,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) public payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + (uint256 amt_, uint256 setId_) = _borrowOrWithdraw( BorrowWithdrawParams({ market: market, token: token, @@ -299,17 +298,17 @@ abstract contract CompoundIIIResolver is Events, Helpers { }) ); - _eventName = "LogWithdrawFrom(address,address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, token, from, to, _amt, getId, _setId); + eventName_ = "LogWithdrawFrom(address,address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId_); } /** * @dev Borrow base asset. - * @notice Withdraw base token from Compound. - * @param market The address of the market from where to withdraw. + * @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 getId ID to retrieve amt. - * @param setId ID stores the amount of tokens withdrawn. + * @param setId ID stores the amount of tokens borrowed. */ function borrow( address market, @@ -319,52 +318,52 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amt); + uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); address token = getBaseToken(market); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; + address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + TokenInterface tokenContract = TokenInterface(token_); uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), market, - _token + token_ ); - _amt = _amt == uint256(-1) ? initialBal : _amt; + amt_ = amt_ == uint256(-1) ? initialBal : amt_; - CometInterface(market).withdraw(_token, _amt); + CometInterface(market).withdraw(token_, amt_); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), market, - _token + token_ ); - _amt = sub(finalBal, initialBal); + amt_ = sub(finalBal, initialBal); - convertWethToEth(isEth, tokenContract, _amt); + convertWethToEth(isEth, tokenContract, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogBorrow(address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, _amt, getId, setId); + eventName_ = "LogBorrow(address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, amt_, getId, setId); } /** * @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. + * @notice Borrow base token from Compound on behalf of an address. + * @param market The address of the market. * @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. + * @param setId ID stores the amount of tokens borrowed. */ function borrowOnBehalf( address market, @@ -375,9 +374,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + (uint256 amt_, uint256 setId_) = _borrowOrWithdraw( BorrowWithdrawParams({ market: market, token: getBaseToken(market), @@ -388,19 +387,19 @@ abstract contract CompoundIIIResolver is Events, Helpers { setId: setId }) ); - _eventName = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, to, _amt, getId, _setId); + eventName_ = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, to, amt_, getId, setId_); } /** * @dev Borrow base asset from 'from' and transfer to 'to'. - * @notice Withdraw base token or deposited token from Compound. - * @param market The address of the market from where to withdraw. + * @notice Borrow base token or deposited token from Compound. + * @param market The address of the market. * @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 getId ID to retrieve amt. - * @param setId ID stores the amount of tokens withdrawn. + * @param setId ID stores the amount of tokens borrowed. */ function borrowFrom( address market, @@ -412,9 +411,9 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - (uint256 _amt, uint256 _setId) = _borrowOrWithdraw( + (uint256 amt_, uint256 setId_) = _borrowOrWithdraw( BorrowWithdrawParams({ market: market, token: getBaseToken(market), @@ -425,45 +424,45 @@ abstract contract CompoundIIIResolver is Events, Helpers { setId: setId }) ); - _eventName = "LogBorrowFrom(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, from, to, _amt, getId, _setId); + eventName_ = "LogBorrowFrom(address,address,address,uint256,uint256,uint256)"; + 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 setId ID stores the amount of tokens withdrawn. + * @param market The address of the market. + * @param setId ID stores the amount of tokens repaid. */ function payback(address market, uint256 setId) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { require(market != address(0), "invalid market address"); address token = getBaseToken(market); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); - uint256 _amt = CometInterface(market).borrowBalanceOf(address(this)); - approve(tokenContract, market, _amt); + uint256 amt_ = CometInterface(market).borrowBalanceOf(address(this)); + approve(tokenContract, market, amt_); - CometInterface(market).supply(_token, uint256(-1)); + CometInterface(market).supply(token_, uint256(-1)); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogPayback(address,address,uint256,uint256)"; - _eventParam = abi.encode(market, token, _amt, 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 market The address of the market. * @param to The address on behalf of which the borrow is to be repaid. - * @param setId ID stores the amount of tokens withdrawn. + * @param setId ID stores the amount of tokens repaid. */ function paybackOnBehalf( address market, @@ -472,33 +471,33 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { require(market != address(0), "invalid market address"); address token = getBaseToken(market); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); - uint256 _amt = CometInterface(market).borrowBalanceOf(to); - approve(tokenContract, market, _amt); + uint256 amt_ = CometInterface(market).borrowBalanceOf(to); + approve(tokenContract, market, amt_); - CometInterface(market).supplyTo(to, _token, uint256(-1)); + CometInterface(market).supplyTo(to, token_, uint256(-1)); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256)"; - _eventParam = abi.encode(market, token, to, _amt, 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 market The address of the market. * @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. + * @param setId ID stores the amount of tokens repaid. */ function paybackFrom( address market, @@ -508,24 +507,24 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { require(market != address(0), "invalid market address"); address token = getBaseToken(market); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); - uint256 _amt = CometInterface(market).borrowBalanceOf(to); - approve(tokenContract, market, _amt); + uint256 amt_ = CometInterface(market).borrowBalanceOf(to); + approve(tokenContract, market, amt_); - CometInterface(market).supplyFrom(from, to, _token, uint256(-1)); + CometInterface(market).supplyFrom(from, to, token_, uint256(-1)); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogPaybackFrom(address,address,address,address,uint256,uint256)"; - _eventParam = abi.encode(market, token, from, to, _amt, setId); + eventName_ = "LogPaybackFrom(address,address,address,address,uint256,uint256)"; + eventParam_ = abi.encode(market, token, from, to, amt_, setId); } /** @@ -537,7 +536,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { * @param minCollateralAmt Minimum amount of collateral expected to be received. * @param baseAmt Amount of base asset to be sold for collateral. * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens withdrawn. + * @param setId ID stores the amount of base tokens sold. */ function buyCollateral( address market, @@ -550,30 +549,30 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, baseAmt); + uint256 amt_ = getUint(getId, baseAmt); bool isEth = asset == ethAddr; - address _token = isEth ? wethAddr : asset; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : asset; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); + convertEthToWeth(isEth, tokenContract, amt_); } CometInterface(market).buyCollateral( asset, minCollateralAmt, - _amt, + amt_, dest ); - uint256 collAmt = CometInterface(market).quoteCollateral(asset, _amt); - setUint(setId, _amt); + uint256 collAmt = CometInterface(market).quoteCollateral(asset, amt_); + setUint(setId, amt_); - _eventName = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( + eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; + eventParam_ = abi.encode( market, asset, baseAmt, @@ -587,17 +586,17 @@ abstract contract CompoundIIIResolver 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 from where to withdraw. + * @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 tokens withdrawn. + * @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) { + ) public returns (string memory eventName_, bytes memory eventParam_) { cometRewards.claim(market, account, accrue); //in reward token decimals @@ -607,8 +606,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); setUint(setId, totalRewardsClaimed); - _eventName = "LogRewardsClaimed(address,address,uint256,uint256,bool)"; - _eventParam = abi.encode( + eventName_ = "LogRewardsClaimed(address,address,uint256,uint256,bool)"; + eventParam_ = abi.encode( market, account, totalRewardsClaimed, @@ -620,11 +619,11 @@ abstract contract CompoundIIIResolver is Events, Helpers { /** * @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 from where to withdraw. + * @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 tokens withdrawn. + * @param setId ID stores the amount of rewards claimed. */ function claimRewardsTo( address market, @@ -632,7 +631,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { address dest, bool accrue, uint256 setId - ) public returns (string memory _eventName, bytes memory _eventParam) { + ) public returns (string memory eventName_, bytes memory eventParam_) { cometRewards.claimTo(market, account, dest, accrue); //in reward token decimals @@ -642,8 +641,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { ); setUint(setId, totalRewardsClaimed); - _eventName = "LogRewardsClaimedTo(address,address,address,uint256,uint256,bool)"; - _eventParam = abi.encode( + eventName_ = "LogRewardsClaimedTo(address,address,address,uint256,uint256,bool)"; + eventParam_ = abi.encode( market, account, dest, @@ -656,11 +655,11 @@ abstract contract CompoundIIIResolver is Events, Helpers { /** * @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 where to supply. + * @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 deposited. + * @param setId ID stores the amount of tokens transferred. */ function transferBase( address market, @@ -671,40 +670,40 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amount); + 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); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); + convertEthToWeth(isEth, tokenContract, amt_); } - _amt = _amt == uint256(-1) + amt_ = amt_ == uint256(-1) ? CometInterface(market).balanceOf(address(this)) - : _amt; - _transfer(market, _token, address(0), dest, _amt); + : amt_; + _transfer(market, token_, address(0), dest, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogTransferBase(address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, dest, _amt, getId, setId); + 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 where to supply. + * @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 deposited. + * @param setId ID stores the amount of tokens transferred. */ function transferBaseFrom( address market, @@ -716,40 +715,40 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amount); + 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); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); + convertEthToWeth(isEth, tokenContract, amt_); } - _amt = _amt == uint256(-1) + amt_ = amt_ == uint256(-1) ? CometInterface(market).balanceOf(src) - : _amt; - _transfer(market, _token, src, dest, _amt); + : amt_; + _transfer(market, token_, src, dest, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogTransferBaseFrom(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, src, dest, _amt, getId, setId); + 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. * @notice Transfer collateral asset to dest address from caller's account. - * @param market The address of the market where to supply. + * @param market The address of the market. * @param token The collateral asset to transfer to dest address. * @param dest The account where to transfer the base assets. * @param amount The amount of the collateral token to transfer. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens deposited. + * @param setId ID stores the amount of tokens transferred. */ function transferAsset( address market, @@ -761,43 +760,45 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amount); + uint256 amt_ = getUint(getId, amount); require( market != address(0) && token != address(0), "invalid market address" ); bool isEth = token == ethAddr; - address _token = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(_token); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); + convertEthToWeth(isEth, tokenContract, amt_); } - _amt = _amt == uint256(-1) - ? CometInterface(market).userCollateral(address(this), _token).balance - : _amt; - _transfer(market, _token, address(0), dest, _amt); + amt_ = amt_ == uint256(-1) + ? CometInterface(market) + .userCollateral(address(this), token_) + .balance + : amt_; + _transfer(market, token_, address(0), dest, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogTransferAsset(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, _token, dest, _amt, getId, setId); + eventName_ = "LogTransferAsset(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token_, dest, amt_, getId, setId); } /** * @dev Transfer collateral 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 where to supply. + * @param market The address of the market. * @param token The collateral asset to transfer to dest address. * @param src The account from where to transfer the collaterals. * @param dest The account where to transfer the collateral assets. * @param amount The amount of the collateral token to transfer. (For max: `uint256(-1)`) * @param getId ID to retrieve amt. - * @param setId ID stores the amount of tokens deposited. + * @param setId ID stores the amount of tokens transferred. */ function transferAssetFrom( address market, @@ -810,29 +811,29 @@ abstract contract CompoundIIIResolver is Events, Helpers { ) external payable - returns (string memory _eventName, bytes memory _eventParam) + returns (string memory eventName_, bytes memory eventParam_) { - uint256 _amt = getUint(getId, amount); + 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); + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); if (isEth) { - convertEthToWeth(isEth, tokenContract, _amt); + convertEthToWeth(isEth, tokenContract, amt_); } - _amt = _amt == uint256(-1) - ? CometInterface(market).userCollateral(src, _token).balance - : _amt; - _transfer(market, _token, src, dest, _amt); + amt_ = amt_ == uint256(-1) + ? CometInterface(market).userCollateral(src, token_).balance + : amt_; + _transfer(market, token_, src, dest, amt_); - setUint(setId, _amt); + setUint(setId, amt_); - _eventName = "LogTransferAssetFrom(address,address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(market, _token, src, dest, _amt, getId, setId); + eventName_ = "LogTransferAssetFrom(address,address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token_, src, dest, amt_, getId, setId); } /** @@ -846,10 +847,10 @@ abstract contract CompoundIIIResolver is Events, Helpers { address market, address manager, bool isAllowed - ) external returns (string memory _eventName, bytes memory _eventParam) { + ) external returns (string memory eventName_, bytes memory eventParam_) { CometInterface(market).allow(manager, isAllowed); - _eventName = "LogAllow(address,address,bool)"; - _eventParam = abi.encode(market, manager, isAllowed); + eventName_ = "LogAllow(address,address,bool)"; + eventParam_ = abi.encode(market, manager, isAllowed); } /** @@ -875,7 +876,7 @@ abstract contract CompoundIIIResolver is Events, Helpers { uint8 v, bytes32 r, bytes32 s - ) external returns (string memory _eventName, bytes memory _eventParam) { + ) external returns (string memory eventName_, bytes memory eventParam_) { CometInterface(market).allowBySig( owner, manager, @@ -886,8 +887,8 @@ abstract contract CompoundIIIResolver is Events, Helpers { r, s ); - _eventName = "LogAllowWithPermit(address,address,address,uint256,uint256,uint256,uint256,uint256,bool)"; - _eventParam = abi.encode( + eventName_ = "LogAllowWithPermit(address,address,address,uint256,uint256,uint256,uint256,uint256,bool)"; + eventParam_ = abi.encode( market, owner, manager, @@ -901,6 +902,6 @@ abstract contract CompoundIIIResolver is Events, Helpers { } } -contract ConnectV3Compound is CompoundIIIResolver { - string public name = "Compound-v1.0"; +contract ConnectV2CompoundV3 is CompoundV3Resolver { + string public name = "CompoundV3-v1.0"; } From 912f46949b2ab320c134f081df240fdcbd3ae87c Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 31 Aug 2022 20:18:32 +0530 Subject: [PATCH 13/75] updated payback --- .../mainnet/connectors/compound-iii/main.sol | 75 ++++++++++++++----- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 986ddf8f..90b99cdc 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -52,8 +52,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) : amt_; - approve(tokenContract, market, amt_); } + approve(tokenContract, market, amt_); CometInterface(market).supply(token_, amt_); @@ -103,8 +103,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) : amt_; - approve(tokenContract, market, amt_); } + approve(tokenContract, market, amt_); CometInterface(market).supplyTo(to, token_, amt_); @@ -156,8 +156,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt_ = amt_ == uint256(-1) ? tokenContract.balanceOf(address(this)) : amt_; - approve(tokenContract, market, amt_); } + approve(tokenContract, market, amt_); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -429,16 +429,24 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @dev Repays entire borrow of the base asset. - * @notice Repays an entire borrow of the base asset. + * @dev Repays the borrowed base asset. + * @notice Repays the borrow of the base asset. * @param market The address of the market. + * @param amt The amount to be repaid. + * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens repaid. */ - function payback(address market, uint256 setId) + function payback( + address market, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory eventName_, bytes memory eventParam_) { + uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); address token = getBaseToken(market); @@ -446,15 +454,21 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - uint256 amt_ = CometInterface(market).borrowBalanceOf(address(this)); + amt_ = amt_ == uint256(-1) + ? CometInterface(market).borrowBalanceOf(address(this)) + : amt_; + + if (isEth) { + convertEthToWeth(isEth, tokenContract, amt_); + } approve(tokenContract, market, amt_); - CometInterface(market).supply(token_, uint256(-1)); + CometInterface(market).supply(token_, amt_); setUint(setId, amt_); - eventName_ = "LogPayback(address,address,uint256,uint256)"; - eventParam_ = abi.encode(market, token, amt_, setId); + eventName_ = "LogPayback(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, amt_, getId, setId); } /** @@ -462,17 +476,22 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @notice Repays an entire borrow of the base asset on behalf of 'to'. * @param market The address of the market. * @param to The address on behalf of which the borrow is to be repaid. + * @param amt The amount to be repaid. + * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens repaid. */ function paybackOnBehalf( address market, address to, + uint256 amt, + uint256 getId, uint256 setId ) external payable returns (string memory eventName_, bytes memory eventParam_) { + uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); address token = getBaseToken(market); @@ -480,15 +499,22 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - uint256 amt_ = CometInterface(market).borrowBalanceOf(to); + amt_ = amt_ == uint256(-1) + ? CometInterface(market).borrowBalanceOf(to) + : amt_; + + if (isEth) { + convertEthToWeth(isEth, tokenContract, amt_); + } + approve(tokenContract, market, amt_); - CometInterface(market).supplyTo(to, token_, uint256(-1)); + CometInterface(market).supplyTo(to, token_, amt_); setUint(setId, amt_); - eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256)"; - eventParam_ = abi.encode(market, token, to, amt_, setId); + eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, to, amt_, getId, setId); } /** @@ -497,18 +523,23 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param market The address of the market. * @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 amt The amount to be repaid. + * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens repaid. */ function paybackFrom( address market, address from, address to, + uint256 amt, + uint256 getId, uint256 setId ) external payable returns (string memory eventName_, bytes memory eventParam_) { + uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); address token = getBaseToken(market); @@ -516,15 +547,21 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - uint256 amt_ = CometInterface(market).borrowBalanceOf(to); - approve(tokenContract, market, amt_); + amt_ = amt_ == uint256(-1) + ? CometInterface(market).borrowBalanceOf(to) + : amt_; - CometInterface(market).supplyFrom(from, to, token_, uint256(-1)); + if (isEth) { + convertEthToWeth(isEth, tokenContract, amt_); + } + + approve(tokenContract, market, amt_); + CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); - eventName_ = "LogPaybackFrom(address,address,address,address,uint256,uint256)"; - eventParam_ = abi.encode(market, token, from, to, amt_, setId); + eventName_ = "LogPaybackFrom(address,address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId); } /** From 6c457ea60b61badba94be7b8bc97d1c89fda882d Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 00:09:55 +0530 Subject: [PATCH 14/75] reviews addresses --- .../connectors/compound-iii/helpers.sol | 3 +- .../connectors/compound-iii/interface.sol | 9 +++++ .../mainnet/connectors/compound-iii/main.sol | 37 ++++++------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index d71a3cc7..4db95832 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -36,7 +36,6 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - bytes memory data; if (from == address(0) && to == address(0)) { CometInterface(market).withdraw(token, amt); @@ -53,7 +52,7 @@ abstract contract Helpers is DSMath, Basic { address from, address to, uint256 amt - ) public payable { + ) internal { bytes memory data; if (from == address(0)) { diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound-iii/interface.sol index 785ec3b2..bdbfe7c7 100644 --- a/contracts/mainnet/connectors/compound-iii/interface.sol +++ b/contracts/mainnet/connectors/compound-iii/interface.sol @@ -7,6 +7,11 @@ struct UserCollateral { uint128 _reserved; } +struct RewardOwed { + address token; + uint256 owed; +} + interface CometInterface { function supply(address asset, uint256 amount) external virtual; @@ -124,6 +129,10 @@ interface CometRewards { bool shouldAccrue ) external; + function getRewardOwed(address comet, address account) + external + returns (RewardOwed memory); + function rewardsClaimed(address cometProxy, address account) external view diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 90b99cdc..823f1fd0 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -455,7 +455,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) - ? CometInterface(market).borrowBalanceOf(address(this)) + ? TokenInterface(market).balanceOf(address(this)) : amt_; if (isEth) { @@ -500,7 +500,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) - ? CometInterface(market).borrowBalanceOf(to) + ? TokenInterface(market).balanceOf(to) : amt_; if (isEth) { @@ -548,7 +548,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) - ? CometInterface(market).borrowBalanceOf(to) + ? TokenInterface(market).balanceOf(to) : amt_; if (isEth) { @@ -634,23 +634,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { bool accrue, uint256 setId ) public returns (string memory eventName_, bytes memory eventParam_) { + uint256 rewardsOwed = cometRewards.getRewardOwed(market, account).owed; cometRewards.claim(market, account, accrue); - //in reward token decimals - uint256 totalRewardsClaimed = cometRewards.rewardsClaimed( - market, - account - ); - setUint(setId, totalRewardsClaimed); + setUint(setId, rewardsOwed); eventName_ = "LogRewardsClaimed(address,address,uint256,uint256,bool)"; - eventParam_ = abi.encode( - market, - account, - totalRewardsClaimed, - setId, - accrue - ); + eventParam_ = abi.encode(market, account, rewardsOwed, setId, accrue); } /** @@ -669,21 +659,18 @@ abstract contract CompoundV3Resolver is Events, Helpers { 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); - //in reward token decimals - uint256 totalRewardsClaimed = cometRewards.rewardsClaimed( - market, - account - ); - setUint(setId, totalRewardsClaimed); + setUint(setId, rewardsOwed); eventName_ = "LogRewardsClaimedTo(address,address,address,uint256,uint256,bool)"; eventParam_ = abi.encode( market, account, dest, - totalRewardsClaimed, + rewardsOwed, setId, accrue ); @@ -880,7 +867,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param manager The address to be authorized. * @param isAllowed Whether to allow or disallow the manager. */ - function allow( + function toggleAccountManager( address market, address manager, bool isAllowed @@ -903,7 +890,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param r Half of the ECDSA signature pair. * @param s Half of the ECDSA signature pair. */ - function allowWithPermit( + function toggleAccountManagerWithPermit( address market, address owner, address manager, From 751f8ca9e7bd26ef241d119ed9b5f844a3d34af9 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 00:35:49 +0530 Subject: [PATCH 15/75] tests --- test/mainnet/compound/compound.iii.test.ts | 382 +++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 test/mainnet/compound/compound.iii.test.ts diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts new file mode 100644 index 00000000..265c56d4 --- /dev/null +++ b/test/mainnet/compound/compound.iii.test.ts @@ -0,0 +1,382 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { waffle, ethers } = hre; +const { provider, deployContract } = waffle; + +import { Signer, Contract } from "ethers"; +import { BigNumber } from "bignumber.js"; + +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { tokens, tokenMapping } from "../../../scripts/tests/mainnet/tokens"; +import { abis } from "../../../scripts/constant/abis"; +import { constants } from "../../../scripts/constant/constant"; +import { ConnectV2CompoundV3__factory } from "../../../typechain"; +import { MaxUint256 } from "@uniswap/sdk-core"; + +describe("Compound III", function () { + const connectorName = "COMPOUND-V3-TEST-A"; + const market = "0xc3d688B66703497DAA19211EEdff47f25384cdc3"; + const base = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; + const account = "0x10bf1dcb5ab7860bab1c3320163c6dddf8dcc0e4"; + + const ABI = ["function balanceOf(address account) public view returns (uint256)"]; + const wethContract = new ethers.Contract(tokens.weth.address, ABI); + const baseContract = new ethers.Contract(base, ABI); + + const cometABI = [ + { + inputs: [{ internalType: "address", name: "account", type: "address" }], + name: "balanceOf", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "account", type: "address" }], + name: "borrowBalanceOf", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseBorrowMin", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseMinForRewards", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseToken", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "decimals", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "priceFeed", type: "address" }], + name: "getPrice", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "owner", type: "address" }, + { internalType: "address", name: "manager", type: "address" } + ], + name: "hasPermission", + outputs: [{ internalType: "bool", name: "", type: "bool" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "numAssets", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "asset", type: "address" }, + { internalType: "uint256", name: "baseAmount", type: "uint256" } + ], + name: "quoteCollateral", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "", type: "address" }], + name: "userBasic", + outputs: [ + { internalType: "int104", name: "principal", type: "int104" }, + { internalType: "uint64", name: "baseTrackingIndex", type: "uint64" }, + { internalType: "uint64", name: "baseTrackingAccrued", type: "uint64" }, + { internalType: "uint16", name: "assetsIn", type: "uint16" }, + { internalType: "uint8", name: "_reserved", type: "uint8" } + ], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "", type: "address" }, + { internalType: "address", name: "", type: "address" } + ], + name: "userCollateral", + outputs: [ + { internalType: "uint128", name: "balance", type: "uint128" }, + { internalType: "uint128", name: "_reserved", type: "uint128" } + ], + stateMutability: "view", + type: "function" + } + ]; + + let dsaWallet0: any; + let dsaWallet1: any; + let dsaWallet2: any; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: any; + let signer: any; + + const comet = new ethers.Contract(market, cometABI); + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + //@ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 15444500 + } + } + ] + }); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2CompoundV3__factory, + signer: masterSigner, + connectors: instaConnectorsV2 + }); + console.log("Connector address", connector.address); + + await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [account] + }); + + signer = await ethers.getSigner(account); + }); + + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet0.address).to.be.true; + dsaWallet1 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet1.address).to.be.true; + dsaWallet2 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet2.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + await wallet0.sendTransaction({ + to: dsaWallet1.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + }); + }); + + describe("Main", function () { + //deposit asset + it("Should supply ETH collateral in Compound V3", async function () { + const amount = ethers.utils.parseEther("5"); // 1 ETH + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [market, tokens.eth.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("5")); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("5") + ); + }); + + //deposit asset on behalf of + it("Should supply on behalf of dsaWallet0 ETH collateral in Compound V3", async function () { + const amount = ethers.utils.parseEther("1"); // 1 ETH + const spells = [ + { + connector: connectorName, + method: "depositOnBehalf", + args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.lte(ethers.utils.parseEther("9")); + expect((await comet.connect(wallet0).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("6") + ); + }); + + it("Should borrow and payback base token from Compound", async function () { + const amount = ethers.utils.parseUnits("150", 6); + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [market, amount, 0, 0] + }, + { + connector: connectorName, + method: "payback", + args: [market, ethers.utils.parseUnits("50", 6), 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await comet.connect(wallet0).borrowBalanceOf(dsaWallet0.address)).to.be.equal( + ethers.utils.parseUnits("100", 6) + ); + console.log(baseContract); + expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( + ethers.utils.parseUnits("100", 6) + ); + }); + + it("should payback base token from Compound", async function () { + const spells = [ + { + connector: connectorName, + method: "payback", + args: [market, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseUnits("0", 6)); + }); + + it("Should borrow on behalf of from Compound", async function () { + const spells1 = [ + { + connector: connectorName, + method: "deposit", + args: [market, tokens.eth.address, ethers.utils.parseEther("6"), 0, 0] + } + ]; + + const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const amount = ethers.utils.parseUnits("100", 6); + const spells = [ + { + connector: connectorName, + method: "borrowOnBehalf", + args: [market, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( + ethers.utils.parseUnits("100", 6) + ); + }); + + it("Should payback on behalf of from Compound", async function () { + const spells = [ + { + connector: connectorName, + method: "paybackOnBehalf", + args: [market, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal( + ethers.utils.parseUnits("0", 6) + ); + }); + + it("should allow manager for dsaWallet0's collateral", async function () { + const spells = [ + { + connector: connectorName, + method: "allow", + args: [market, dsaWallet2.address, true] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + }); + + it("should withdraw some ETH collateral", async function () { + const amount = ethers.utils.parseEther("2"); + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [market, tokens.eth.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("4") + ); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("2")); + }); + + it("manager should be able to withdraw collateral from the position", async function () { + const amount = ethers.constants.MaxUint256; + const spells = [ + { + connector: connectorName, + method: "withdrawFrom", + args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("0") + ); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("2")); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("5")); + }); + }); +}); From 6dded3fac23c1bd2d21d1ed0fb779efc9a77c950 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 01:19:13 +0530 Subject: [PATCH 16/75] minor chnages --- .../mainnet/connectors/compound-iii/helpers.sol | 3 +-- .../mainnet/connectors/compound-iii/main.sol | 16 ++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index 4db95832..63b1a2b8 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -36,7 +36,6 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - if (from == address(0) && to == address(0)) { CometInterface(market).withdraw(token, amt); } else if (from == address(0)) { @@ -92,7 +91,7 @@ abstract contract Helpers is DSMath, Basic { params.market, _token ); - _amt = sub(finalBal, initialBal); + _amt = sub(initialBal, finalBal); convertWethToEth(isEth, tokenContract, _amt); diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 823f1fd0..2e716ce9 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -125,7 +125,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens deposited. */ - function depositFrom( + function depositFromUsingManager( address market, address token, address from, @@ -214,7 +214,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token ); - amt_ = sub(finalBal, initialBal); + amt_ = sub(initialBal, finalBal); convertWethToEth(isEth, tokenContract, amt_); @@ -273,7 +273,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. */ - function withdrawFrom( + function withdrawFromUsingManager( address market, address token, address from, @@ -346,7 +346,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_ ); - amt_ = sub(finalBal, initialBal); + amt_ = sub(initialBal, finalBal); convertWethToEth(isEth, tokenContract, amt_); @@ -401,7 +401,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens borrowed. */ - function borrowFrom( + function borrowFromUsingManager( address market, address from, address to, @@ -527,7 +527,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens repaid. */ - function paybackFrom( + function paybackFromUsingManager( address market, address from, address to, @@ -729,7 +729,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens transferred. */ - function transferBaseFrom( + function transferBaseFromUsingManager( address market, address src, address dest, @@ -824,7 +824,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens transferred. */ - function transferAssetFrom( + function transferAssetFromUsingManager( address market, address token, address src, From ebc9e4cfdee851ed1d8e1694fe5f6affd5b5075f Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 02:51:30 +0530 Subject: [PATCH 17/75] 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_); From c2ae141388e2fc21ba1903c40e92bec0b2337445 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 02:51:50 +0530 Subject: [PATCH 18/75] rewards --- .../connectors/compound-rewards/events.sol | 22 ++++++ .../connectors/compound-rewards/helpers.sol | 13 ++++ .../connectors/compound-rewards/interface.sol | 37 +++++++++ .../connectors/compound-rewards/main.sol | 75 +++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 contracts/mainnet/connectors/compound-rewards/events.sol create mode 100644 contracts/mainnet/connectors/compound-rewards/helpers.sol create mode 100644 contracts/mainnet/connectors/compound-rewards/interface.sol create mode 100644 contracts/mainnet/connectors/compound-rewards/main.sol diff --git a/contracts/mainnet/connectors/compound-rewards/events.sol b/contracts/mainnet/connectors/compound-rewards/events.sol new file mode 100644 index 00000000..08d936d9 --- /dev/null +++ b/contracts/mainnet/connectors/compound-rewards/events.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.7.6; +pragma experimental ABIEncoderV2; + +contract Events { + 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 + ); +} diff --git a/contracts/mainnet/connectors/compound-rewards/helpers.sol b/contracts/mainnet/connectors/compound-rewards/helpers.sol new file mode 100644 index 00000000..748d5bcf --- /dev/null +++ b/contracts/mainnet/connectors/compound-rewards/helpers.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +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 { CometRewards } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + CometRewards internal constant cometRewards = + CometRewards(0x1B0e765F6224C21223AeA2af16c1C46E38885a40); +} diff --git a/contracts/mainnet/connectors/compound-rewards/interface.sol b/contracts/mainnet/connectors/compound-rewards/interface.sol new file mode 100644 index 00000000..b2157255 --- /dev/null +++ b/contracts/mainnet/connectors/compound-rewards/interface.sol @@ -0,0 +1,37 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +struct UserCollateral { + uint128 balance; + uint128 _reserved; +} + +struct RewardOwed { + address token; + uint256 owed; +} + +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-rewards/main.sol b/contracts/mainnet/connectors/compound-rewards/main.sol new file mode 100644 index 00000000..527c631d --- /dev/null +++ b/contracts/mainnet/connectors/compound-rewards/main.sol @@ -0,0 +1,75 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Compound. + * @dev Rewards. + */ + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract CompoundV3RewardsResolver 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 + ); + } +} + +contract ConnectV2CompoundV3Rewards is CompoundV3RewardsResolver { + string public name = "CompoundV3Rewards-v1.0"; +} From a801cedff72e8dfd604a2a992de6803885ba498f Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 16:10:38 +0530 Subject: [PATCH 19/75] reviews addressed --- .../connectors/compound-iii/events.sol | 23 ++++-------------- .../connectors/compound-iii/helpers.sol | 24 +++++++++---------- .../mainnet/connectors/compound-iii/main.sol | 16 ++++++------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index f335f4bb..095d189c 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -19,7 +19,7 @@ contract Events { uint256 setId ); - event LogDepositFrom( + event LogDepositFromUsingManager( address indexed market, address indexed token, address from, @@ -46,7 +46,7 @@ contract Events { uint256 setId ); - event LogWithdrawFrom( + event LogWithdrawFromUsingManager( address indexed market, address indexed token, address from, @@ -89,7 +89,7 @@ contract Events { uint256 setId ); - event LogPaybackFrom( + event LogPaybackFromUsingManager( address indexed market, address from, address to, @@ -97,15 +97,6 @@ contract Events { uint256 setId ); - event LogLiquidate( - address indexed borrower, - address indexed tokenToPay, - address indexed tokenInReturn, - uint256 tokenAmt, - uint256 getId, - uint256 setId - ); - event LogBuyCollateral( address indexed market, address indexed token, @@ -126,7 +117,7 @@ contract Events { uint256 setId ); - event LogTransferAssetFrom( + event LogTransferAssetFromUsingManager( address indexed market, address token, address indexed from, @@ -136,11 +127,7 @@ contract Events { uint256 setId ); - event LogAllow( - address indexed market, - address indexed manager, - bool allow - ); + event LogAllow(address indexed market, address indexed manager, bool allow); event LogAllowWithPermit( address indexed market, diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index eeacdaa6..c8211216 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -49,8 +49,6 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - bytes memory data; - if (from == address(0)) { CometInterface(market).transferAsset(to, token, amt); } else { @@ -62,39 +60,39 @@ abstract contract Helpers is DSMath, Basic { internal returns (uint256 amt, uint256 setId) { - uint256 _amt = getUint(params.getId, params.amt); + 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; - address _token = isEth ? wethAddr : params.token; + address token_ = isEth ? wethAddr : params.token; - TokenInterface tokenContract = TokenInterface(_token); + TokenInterface tokenContract = TokenInterface(token_); uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), params.market, - _token + token_ ); - _amt = _amt == uint256(-1) ? initialBal : _amt; + amt_ = amt_ == uint256(-1) ? initialBal : amt_; - _withdraw(params.market, _token, params.from, params.to, _amt); + _withdraw(params.market, token_, params.from, params.to, amt_); uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), params.market, - _token + token_ ); - _amt = sub(initialBal, finalBal); + amt_ = sub(initialBal, finalBal); - convertWethToEth(isEth, tokenContract, _amt); + convertWethToEth(isEth, tokenContract, amt_); - setUint(params.setId, _amt); + setUint(params.setId, amt_); - amt = _amt; + amt = amt_; setId = params.setId; } diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index f00c144b..f1b388db 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -154,7 +154,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); - eventName_ = "LogDepositFrom(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogDepositFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId); } @@ -290,7 +290,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { }) ); - eventName_ = "LogWithdrawFrom(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogWithdrawFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId_); } @@ -416,7 +416,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setId: setId }) ); - eventName_ = "LogBorrowFrom(address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogBorrowFromUsingManager(address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, from, to, amt_, getId, setId_); } @@ -511,7 +511,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { /** * @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'. + * @notice Repays an entire borrow of the base asset on behalf of 'to'. Approve the comet markey * @param market The address of the market. * @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. @@ -544,7 +544,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); - eventName_ = "LogPaybackFrom(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogPaybackFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId); } @@ -553,7 +553,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached. * @param market The address of the market from where to withdraw. * @param asset The collateral asset to purachase. - * @param dest The address on to transfer the purchased assets. + * @param dest The address to transfer the purchased assets. * @param minCollateralAmt Minimum amount of collateral expected to be received. * @param baseAmt Amount of base asset to be sold for collateral. * @param getId ID to retrieve amt. @@ -596,7 +596,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { eventParam_ = abi.encode( market, asset, - baseAmt, + amt_, minCollateralAmt, collAmt, getId, @@ -695,7 +695,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); - eventName_ = "LogTransferAssetFrom(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogTransferAssetFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token_, src, dest, amt_, getId, setId); } From ec12f884544146deabf9e20886a6b21fb1ee081c Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 16:12:08 +0530 Subject: [PATCH 20/75] minor change --- .../connectors/compound-iii/events.sol | 2 +- test/mainnet/compound/compound.iii.test.ts | 76 ++++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 095d189c..83cd44c7 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -71,7 +71,7 @@ contract Events { uint256 setId ); - event LogBorrowFrom( + event LogBorrowFromUsingManager( address indexed market, address from, address to, diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 265c56d4..342c7232 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -21,11 +21,12 @@ describe("Compound III", function () { const connectorName = "COMPOUND-V3-TEST-A"; const market = "0xc3d688B66703497DAA19211EEdff47f25384cdc3"; const base = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; - const account = "0x10bf1dcb5ab7860bab1c3320163c6dddf8dcc0e4"; + const account = "0x72a53cdbbcc1b9efa39c834a540550e23463aacb"; const ABI = ["function balanceOf(address account) public view returns (uint256)"]; const wethContract = new ethers.Contract(tokens.weth.address, ABI); const baseContract = new ethers.Contract(base, ABI); + const linkContract = new ethers.Contract(tokens.link.address, ABI); const cometABI = [ { @@ -226,6 +227,7 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("5") ); + //dsawallet0 --> collateral 5eth, balance 5eth }); //deposit asset on behalf of @@ -333,7 +335,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "allow", + method: "toggleAccountManager", args: [market, dsaWallet2.address, true] } ]; @@ -378,5 +380,75 @@ describe("Compound III", function () { expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("2")); expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("5")); }); + + it("Should withdraw on behalf of from Compound", async function () { + const spells1 = [ + { + connector: connectorName, + method: "deposit", + args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] + } + ]; + + const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const amount = ethers.utils.parseEther("2"); + const spells = [ + { + connector: connectorName, + method: "withdrawOnBehalf", + args: [market, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.eth.address).balance).toFixed()).to.be.equal( + ethers.utils.parseEther("5") + ); + }); + + it("should buy collateral", async function () { + //deposit 10 usdc(base token) to dsa + await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 0)); + const amount = ethers.utils.parseEther("1"); + const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); + const spells = [ + { + connector: connectorName, + method: "buyCollateral", + args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( + ethers.utils.parseEther("1") + ); + }); + + it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () { + const spells = [ + { + connector: connectorName, + method: "transferAsset", + args: [market, tokens.eth.address, dsaWallet0.address, ethers.utils.parseEther("3"), 0, 0] + } + ]; + + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("0") + ); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("0") + ); + }) + + //depositFromUsing --> approve first + //paybackUsingManager --> approve first + //borrow using manager + //transferAssetUsingManager }); }); From 6666fc58d4d47853998d694beb1b732a1387b4f4 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 18:21:24 +0530 Subject: [PATCH 21/75] supply-withdraw logic --- .../connectors/compound-iii/events.sol | 8 + .../connectors/compound-iii/helpers.sol | 31 ++- .../mainnet/connectors/compound-iii/main.sol | 110 ++++++++++- test/mainnet/compound/compound.iii.test.ts | 178 ++++++++++++++---- 4 files changed, 283 insertions(+), 44 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound-iii/events.sol index 83cd44c7..ef317e17 100644 --- a/contracts/mainnet/connectors/compound-iii/events.sol +++ b/contracts/mainnet/connectors/compound-iii/events.sol @@ -140,4 +140,12 @@ contract Events { uint256 s, bool allow ); + + event LogApproveMarket( + address indexed market, + address indexed token, + uint256 indexed amount, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index c8211216..b416e40d 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -56,10 +56,10 @@ abstract contract Helpers is DSMath, Basic { } } - function _borrowOrWithdraw(BorrowWithdrawParams memory params) - internal - returns (uint256 amt, uint256 setId) - { + function _borrowOrWithdraw( + BorrowWithdrawParams memory params, + bool isWithdraw + ) internal returns (uint256 amt, uint256 setId) { uint256 amt_ = getUint(params.getId, params.amt); require( @@ -79,6 +79,29 @@ abstract contract Helpers is DSMath, Basic { 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 = CometInterface(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; + uint256 balance = CometInterface(params.market).balanceOf(params.from); + + require(balance == 0, "borrow-disabled-when-supplied-base"); + } + _withdraw(params.market, token_, params.from, params.to, amt_); uint256 finalBal = getAccountSupplyBalanceOfAsset( diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index f1b388db..ede519ef 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -45,6 +45,12 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); + if (token_ == getBaseToken(market)) { + require( + CometInterface(market).borrowBalanceOf(address(this)) == 0, + "debt-not-repaid" + ); + } if (isEth) { amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; convertEthToWeth(isEth, tokenContract, amt_); @@ -96,6 +102,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); + if (token_ == getBaseToken(market)) { + require( + CometInterface(market).borrowBalanceOf(to) == 0, + "debt-not-repaid" + ); + } + if (isEth) { amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; convertEthToWeth(isEth, tokenContract, amt_); @@ -149,6 +162,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); + if (token_ == getBaseToken(market)) { + require( + CometInterface(market).borrowBalanceOf(to) == 0, + "debt-not-repaid" + ); + } + amt_ = setAmt(market, token_, from, amt_, isEth); CometInterface(market).supplyFrom(from, to, token_, amt_); @@ -198,6 +218,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt_ = amt_ == uint256(-1) ? initialBal : amt_; + if (token_ == getBaseToken(market)) { + uint256 balance = CometInterface(market).balanceOf(address(this)); + if (balance > 0) { + require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); + } + } + CometInterface(market).withdraw(token_, amt_); uint256 finalBal = getAccountSupplyBalanceOfAsset( @@ -247,7 +274,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt: amt, getId: getId, setId: setId - }) + }), + true ); eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; @@ -287,7 +315,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt: amt, getId: getId, setId: setId - }) + }), + true ); eventName_ = "LogWithdrawFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; @@ -330,6 +359,11 @@ abstract contract CompoundV3Resolver is Events, Helpers { 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( @@ -377,7 +411,8 @@ 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_); @@ -414,7 +449,8 @@ 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_); @@ -450,6 +486,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { ? TokenInterface(market).balanceOf(address(this)) : amt_; + uint256 borrowBal = CometInterface(market).borrowBalanceOf( + address(this) + ); + if (borrowBal > 0) { + require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); + } + if (isEth) { convertEthToWeth(isEth, tokenContract, amt_); } @@ -495,6 +538,11 @@ abstract contract CompoundV3Resolver is Events, Helpers { ? TokenInterface(market).balanceOf(to) : amt_; + uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); + if (borrowBal > 0) { + require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); + } + if (isEth) { convertEthToWeth(isEth, tokenContract, amt_); } @@ -511,7 +559,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { /** * @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'. Approve the comet markey + * @notice Repays an entire borrow of the base asset on behalf of 'to'. Approve the comet markey * @param market The address of the market. * @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. @@ -540,6 +588,18 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); amt_ = setAmt(market, token_, from, amt_, isEth); + + uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); + if (borrowBal > 0) { + require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); + } + + if (isEth) { + convertEthToWeth(isEth, tokenContract, amt_); + } + + approve(tokenContract, market, amt_); + CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -582,6 +642,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { convertEthToWeth(isEth, tokenContract, amt_); } + approve(tokenContract, market, amt_); + CometInterface(market).buyCollateral( asset, minCollateralAmt, @@ -605,8 +667,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @dev Transfer collateral or base asset to dest address from this account. - * @notice Transfer collateral asset to dest address from caller's account. + * @dev Transfer base/collateral or base asset to dest address from this account. + * @notice Transfer base/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. * @param dest The account where to transfer the base assets. @@ -763,6 +825,40 @@ abstract contract CompoundV3Resolver is Events, Helpers { s ); } + + function approveMarket( + address market, + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory eventName_, bytes memory eventParam_) + { + uint256 amt_ = getUint(getId, amt); + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); + require(amt > 0, "amount-cannot-be-zero"); + + bool isEth = token == ethAddr; + address token_ = isEth ? wethAddr : token; + TokenInterface tokenContract = TokenInterface(token_); + + amt_ = amt_ == uint256(-1) + ? TokenInterface(market).balanceOf(address(this)) + : amt_; + + approve(tokenContract, market, amt_); + + setUint(setId, amt_); + + eventName_ = "LogApproveMarket(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, amt_, getId, setId); + } } contract ConnectV2CompoundV3 is CompoundV3Resolver { diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 342c7232..7a21a0f3 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -136,6 +136,8 @@ describe("Compound III", function () { let dsaWallet0: any; let dsaWallet1: any; let dsaWallet2: any; + let wallet: any; + let dsa0Signer: any; let masterSigner: Signer; let instaConnectorsV2: Contract; let connector: any; @@ -193,9 +195,17 @@ describe("Compound III", function () { expect(!!dsaWallet1.address).to.be.true; dsaWallet2 = await buildDSAv2(wallet0.address); expect(!!dsaWallet2.address).to.be.true; + wallet = await ethers.getSigner(dsaWallet0.address); + expect(!!dsaWallet1.address).to.be.true; }); it("Deposit ETH into DSA wallet", async function () { + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wallet.address] + }); + + dsa0Signer = await ethers.getSigner(wallet.address); await wallet0.sendTransaction({ to: dsaWallet0.address, value: ethers.utils.parseEther("10") @@ -247,6 +257,8 @@ describe("Compound III", function () { expect((await comet.connect(wallet0).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("6") ); + //dsawallet0 --> collateral 6eth, balance 5eth + //dsaWallet1 --> balance 9eth coll: 0eth }); it("Should borrow and payback base token from Compound", async function () { @@ -269,18 +281,34 @@ describe("Compound III", function () { expect(await comet.connect(wallet0).borrowBalanceOf(dsaWallet0.address)).to.be.equal( ethers.utils.parseUnits("100", 6) ); - console.log(baseContract); expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( ethers.utils.parseUnits("100", 6) ); }); - it("should payback base token from Compound", async function () { + it("should allow manager for dsaWallet0's collateral", async function () { const spells = [ { connector: connectorName, - method: "payback", - args: [market, ethers.constants.MaxUint256, 0, 0] + method: "toggleAccountManager", + args: [market, dsaWallet2.address, true] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + }); + + it("should payback base token from Compound", async function () { + const amount = ethers.utils.parseUnits("100", 6); + //approve market to access dsaWallet0 + await baseContract.connect(dsa0Signer).approve(market, amount); + + const spells = [ + { + connector: connectorName, + method: "paybackFromUsingManager", + args: [market, dsaWallet0.address, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] } ]; @@ -313,6 +341,8 @@ describe("Compound III", function () { expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( ethers.utils.parseUnits("100", 6) ); + //dsawallet0 --> collateral 6eth, balance 5eth, 100usdc + //dsaWallet1 --> balance 3eth coll: 0eth }); it("Should payback on behalf of from Compound", async function () { @@ -329,19 +359,9 @@ describe("Compound III", function () { expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal( ethers.utils.parseUnits("0", 6) ); - }); - it("should allow manager for dsaWallet0's collateral", async function () { - const spells = [ - { - connector: connectorName, - method: "toggleAccountManager", - args: [market, dsaWallet2.address, true] - } - ]; - - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - const receipt = await tx.wait(); + //dsawallet0 --> collateral 6eth, balance 5eth + //dsaWallet1 --> balance 3eth coll: 0eth }); it("should withdraw some ETH collateral", async function () { @@ -359,7 +379,7 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("4") ); - expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("2")); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("7")); }); it("manager should be able to withdraw collateral from the position", async function () { @@ -367,7 +387,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawFrom", + method: "withdrawFromUsingManager", args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; @@ -377,8 +397,10 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("0") ); - expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("2")); - expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("5")); + + //dsawallet0 --> collateral 0eth, balance 7eth + //dsaWallet1 --> balance 7eth coll: 0eth + expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("7")); }); it("Should withdraw on behalf of from Compound", async function () { @@ -389,27 +411,39 @@ describe("Compound III", function () { args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] } ]; + //dsawallet0 --> collateral 0eth, balance 7eth + //dsaWallet1 --> balance 2eth coll: 5eth const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const amount = ethers.utils.parseEther("2"); const spells = [ { connector: connectorName, method: "withdrawOnBehalf", - args: [market, dsaWallet1.address, amount, 0, 0] + args: [market, dsaWallet0.address, amount, 0, 0] } ]; + //dsawallet0 --> collateral 0eth, balance 9eth + //dsaWallet1 --> balance 2eth coll: 3eth - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(new BigNumber(await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.eth.address).balance).toFixed()).to.be.equal( - ethers.utils.parseEther("5") - ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("9")); + + expect( + new BigNumber( + await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.eth.address).balance + ).toFixed() + ).to.be.equal(ethers.utils.parseEther("3")); }); it("should buy collateral", async function () { //deposit 10 usdc(base token) to dsa - await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 0)); + await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); + + //dsawallet0 --> collateral 0eth, balance 9eth 10usdc + //dsaWallet1 --> balance 2eth coll: 3eth const amount = ethers.utils.parseEther("1"); const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); const spells = [ @@ -425,6 +459,9 @@ describe("Compound III", function () { expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( ethers.utils.parseEther("1") ); + + //dsawallet0 --> collateral 0eth, balance 9eth >1link + //dsaWallet1 --> balance 2eth coll: 3eth }); it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () { @@ -438,17 +475,92 @@ describe("Compound III", function () { const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("0") ); expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseEther("0") + ethers.utils.parseEther("3") ); - }) - //depositFromUsing --> approve first - //paybackUsingManager --> approve first - //borrow using manager - //transferAssetUsingManager + //dsawallet0 --> collateral 3eth, balance 9eth >1link + //dsaWallet1 --> balance 2eth coll: 0eth + }); + + it("should deposit link from using manager", async function () { + const amount = ethers.utils.parseEther("1"); + //approve market to access dsaWallet0 + await baseContract.connect(dsa0Signer).approve(market, amount); + + const spells = [ + { + connector: connectorName, + method: "depositFromUsingManager", + args: [market, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.link.address)).balance).to.be.gte( + ethers.utils.parseEther("1") + ); + + //dsawallet0 --> collateral 3eth, balance 9eth previous-1link + //dsaWallet1 --> balance 2eth coll: 0eth,1link + }); + + it("should borrow using manager", async function () { + const spells1 = [ + { + connector: connectorName, + method: "deposit", + args: [market, tokens.eth.address, ethers.utils.parseEther("6"), 0, 0] + } + ]; + + const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const amount = ethers.utils.parseUnits("50", 6); + const spells = [ + { + connector: connectorName, + method: "borrowFromUsingManager", + args: [market, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( + ethers.utils.parseUnits("50", 6) + ); + expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( + ethers.utils.parseUnits("50", 6) + ); + + //dsawallet0 --> collateral 9eth, balance 3eth previous-1link borrowed 50usdc(transferred to dsa1) + //dsaWallet1 --> balance 2eth coll: 0eth,1link, 50usdc + }); + + it("should transferAsset using manager", async function () { + const spells = [ + { + connector: connectorName, + method: "transferAssetFromUsingManager", + args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, ethers.utils.parseEther("1"), 0, 0] + } + ]; + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("3") + ); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("8") + ); + + //dsawallet0 --> collateral 8eth, balance 3eth previous-1link borrowed 50usdc(transferred to dsa1) + //dsaWallet1 --> balance 3eth coll: 0eth,1link, 50usdc + }); }); }); From c0e37856089cd66ef5c532a86e3d689ec618591b Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 18:24:56 +0530 Subject: [PATCH 22/75] title changed --- contracts/mainnet/connectors/compound-iii/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index ede519ef..8954e952 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; /** - * @title Compound. + * @title Compound III. * @dev Lending & Borrowing. */ From 38884eb491ce6a044709ce7695c388211c1fe20a Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 18:25:16 +0530 Subject: [PATCH 23/75] title changed --- contracts/mainnet/connectors/compound-iii/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 8954e952..5eb3c9f4 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; /** - * @title Compound III. + * @title Compound III * @dev Lending & Borrowing. */ From 4235f994436b5ee7735853b466f7e782be302670 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 20:05:00 +0530 Subject: [PATCH 24/75] changes --- contracts/mainnet/common/interfaces.sol | 1 + .../connectors/compound-iii/helpers.sol | 44 ++++++++++++++----- .../mainnet/connectors/compound-iii/main.sol | 38 +++++----------- test/mainnet/compound/compound.iii.test.ts | 20 +++------ 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/contracts/mainnet/common/interfaces.sol b/contracts/mainnet/common/interfaces.sol index 9b872418..4fa3bec9 100644 --- a/contracts/mainnet/common/interfaces.sol +++ b/contracts/mainnet/common/interfaces.sol @@ -11,6 +11,7 @@ interface TokenInterface { function balanceOf(address) external view returns (uint); function decimals() external view returns (uint); function totalSupply() external view returns (uint); + function allowance(address owner, address spender) external view returns (uint256); } interface MemoryInterface { diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound-iii/helpers.sol index b416e40d..3ee3ac73 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound-iii/helpers.sol @@ -66,7 +66,7 @@ abstract contract Helpers is DSMath, Basic { params.market != address(0) && params.token != address(0), "invalid market/token address" ); - bool isEth = params.token == ethAddr; + bool isEth = params.token == ethAddr || params.token == wethAddr; address token_ = isEth ? wethAddr : params.token; TokenInterface tokenContract = TokenInterface(token_); @@ -85,7 +85,9 @@ abstract contract Helpers is DSMath, Basic { params.from = params.from == address(0) ? address(this) : params.from; - uint256 balance = CometInterface(params.market).balanceOf(params.from); + uint256 balance = TokenInterface(params.market).balanceOf( + params.from + ); if (balance > 0) { require( amt_ <= balance, @@ -97,7 +99,9 @@ abstract contract Helpers is DSMath, Basic { params.from = params.from == address(0) ? address(this) : params.from; - uint256 balance = CometInterface(params.market).balanceOf(params.from); + uint256 balance = TokenInterface(params.market).balanceOf( + params.from + ); require(balance == 0, "borrow-disabled-when-supplied-base"); } @@ -126,7 +130,7 @@ abstract contract Helpers is DSMath, Basic { ) internal returns (uint256 balance) { if (asset == getBaseToken(market)) { //balance in base - balance = CometInterface(market).balanceOf(account); + balance = TokenInterface(market).balanceOf(account); } else { //balance in asset denomination balance = uint256( @@ -140,26 +144,44 @@ abstract contract Helpers is DSMath, Basic { address token, address src, uint256 amt, - bool isEth + bool isEth, + bool isRepay ) internal returns (uint256) { if (isEth) { if (amt == uint256(-1)) { - uint256 allowance_ = CometInterface(market).allowance( + uint256 allowance_ = TokenInterface(token).allowance( src, market ); - amt = src.balance < allowance_ ? src.balance : allowance_; + uint256 bal_; + if (isRepay) { + bal_ = CometInterface(market).borrowBalanceOf(src); + } else { + bal_ = src.balance; + } + amt = bal_ < allowance_ ? bal_ : allowance_; } convertEthToWeth(isEth, TokenInterface(token), amt); } else { if (amt == uint256(-1)) { - uint256 allowance_ = CometInterface(market).allowance( + uint256 allowance_ = TokenInterface(token).allowance( src, market ); - uint256 bal_ = (token == getBaseToken(market)) - ? TokenInterface(market).balanceOf(src) - : CometInterface(market).userCollateral(src, token).balance; + uint256 bal_; + if (isRepay) { + bal_ = (token == getBaseToken(market)) + ? CometInterface(market).borrowBalanceOf(src) + : CometInterface(market) + .userCollateral(src, token) + .balance; + } else { + bal_ = (token == getBaseToken(market)) + ? TokenInterface(market).balanceOf(src) + : CometInterface(market) + .userCollateral(src, token) + .balance; + } amt = bal_ < allowance_ ? bal_ : allowance_; } diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 5eb3c9f4..07265559 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -169,7 +169,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); } - amt_ = setAmt(market, token_, from, amt_, isEth); + amt_ = setAmt(market, token_, from, amt_, isEth, false); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -345,9 +345,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; + address token_ = getBaseToken(market); + bool isEth = token_ == wethAddr; TokenInterface tokenContract = TokenInterface(token_); @@ -477,9 +476,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; + address token_ = getBaseToken(market); + bool isEth = token == wethAddr; TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -493,9 +491,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } approve(tokenContract, market, amt_); CometInterface(market).supply(token_, amt_); @@ -529,9 +524,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; + address token_ = getBaseToken(market); + bool isEth = token == wethAddr; TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -542,11 +536,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (borrowBal > 0) { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } - - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } - approve(tokenContract, market, amt_); CometInterface(market).supplyTo(to, token_, amt_); @@ -582,22 +571,17 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); - address token = getBaseToken(market); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; + address token_ = getBaseToken(market); + bool isEth = token == wethAddr; TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, from, amt_, isEth); + amt_ = setAmt(market, token_, from, amt_, isEth, true); uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); if (borrowBal > 0) { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } - approve(tokenContract, market, amt_); CometInterface(market).supplyFrom(from, to, token_, amt_); @@ -751,7 +735,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, src, amt_, isEth); + amt_ = setAmt(market, token_, src, amt_, isEth, false); _transfer(market, token_, src, dest, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 7a21a0f3..40d05ef5 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -23,7 +23,10 @@ describe("Compound III", function () { const base = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; const account = "0x72a53cdbbcc1b9efa39c834a540550e23463aacb"; - const ABI = ["function balanceOf(address account) public view returns (uint256)"]; + const ABI = [ + "function balanceOf(address account) public view returns (uint256)", + "function approve(address spender, uint256 amount) external returns(bool)" + ]; const wethContract = new ethers.Contract(tokens.weth.address, ABI); const baseContract = new ethers.Contract(base, ABI); const linkContract = new ethers.Contract(tokens.link.address, ABI); @@ -312,21 +315,12 @@ describe("Compound III", function () { } ]; - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseUnits("0", 6)); }); it("Should borrow on behalf of from Compound", async function () { - const spells1 = [ - { - connector: connectorName, - method: "deposit", - args: [market, tokens.eth.address, ethers.utils.parseEther("6"), 0, 0] - } - ]; - - const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); const amount = ethers.utils.parseUnits("100", 6); const spells = [ { @@ -342,7 +336,7 @@ describe("Compound III", function () { ethers.utils.parseUnits("100", 6) ); //dsawallet0 --> collateral 6eth, balance 5eth, 100usdc - //dsaWallet1 --> balance 3eth coll: 0eth + //dsaWallet1 --> balance 9eth coll: 0eth }); it("Should payback on behalf of from Compound", async function () { From 2e226603506b1904726bc6084139c51477d6e809 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 20:12:44 +0530 Subject: [PATCH 25/75] minor changes --- .../mainnet/connectors/compound-iii/main.sol | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 07265559..03d7bb66 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -41,7 +41,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token address" ); - bool isEth = token == ethAddr; + bool isEth = token == ethAddr || token == wethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -98,7 +98,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token address" ); - bool isEth = token == ethAddr; + bool isEth = token == ethAddr || token == wethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -158,7 +158,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token address" ); - bool isEth = token == ethAddr; + bool isEth = token == ethAddr || token == wethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -491,6 +491,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } + convertEthToWeth(isEth, tokenContract, amt_); + approve(tokenContract, market, amt_); CometInterface(market).supply(token_, amt_); @@ -536,6 +538,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (borrowBal > 0) { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } + + convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); CometInterface(market).supplyTo(to, token_, amt_); @@ -582,6 +586,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } + convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); CometInterface(market).supplyFrom(from, to, token_, amt_); @@ -622,9 +627,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : asset; TokenInterface tokenContract = TokenInterface(token_); - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } + convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); @@ -682,9 +685,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - if (isEth) { - convertEthToWeth(isEth, tokenContract, amt_); - } + convertEthToWeth(isEth, tokenContract, amt_); amt_ = amt_ == uint256(-1) ? ( From d88efa962b253385cd779c206c65344295746d16 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 20:18:40 +0530 Subject: [PATCH 26/75] minor changes --- .../mainnet/connectors/compound-iii/main.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound-iii/main.sol index 03d7bb66..3e55c6ff 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound-iii/main.sol @@ -477,7 +477,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); address token_ = getBaseToken(market); - bool isEth = token == wethAddr; + bool isEth = token_ == wethAddr; TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -500,7 +500,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); eventName_ = "LogPayback(address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token, amt_, getId, setId); + eventParam_ = abi.encode(market, token_, amt_, getId, setId); } /** @@ -527,7 +527,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); address token_ = getBaseToken(market); - bool isEth = token == wethAddr; + bool isEth = token_ == wethAddr; TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -547,7 +547,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token, to, amt_, getId, setId); + eventParam_ = abi.encode(market, token_, to, amt_, getId, setId); } /** @@ -576,7 +576,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); address token_ = getBaseToken(market); - bool isEth = token == wethAddr; + bool isEth = token_ == wethAddr; TokenInterface tokenContract = TokenInterface(token_); amt_ = setAmt(market, token_, from, amt_, isEth, true); @@ -594,7 +594,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); eventName_ = "LogPaybackFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId); + eventParam_ = abi.encode(market, token_, from, to, amt_, getId, setId); } /** @@ -644,7 +644,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; eventParam_ = abi.encode( market, - asset, + token_, amt_, minCollateralAmt, collAmt, @@ -842,7 +842,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); eventName_ = "LogApproveMarket(address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token, amt_, getId, setId); + eventParam_ = abi.encode(market, token_, amt_, getId, setId); } } From 6f61a54cf697d365df34ea0b4335f4d91854e686 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 21:49:52 +0530 Subject: [PATCH 27/75] updated test --- test/mainnet/compound/compound.iii.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 40d05ef5..312491c1 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -25,7 +25,8 @@ describe("Compound III", function () { const ABI = [ "function balanceOf(address account) public view returns (uint256)", - "function approve(address spender, uint256 amount) external returns(bool)" + "function approve(address spender, uint256 amount) external returns(bool)", + "function transfer(address recipient, uint256 amount) external returns (bool)" ]; const wethContract = new ethers.Contract(tokens.weth.address, ABI); const baseContract = new ethers.Contract(base, ABI); @@ -303,7 +304,7 @@ describe("Compound III", function () { }); it("should payback base token from Compound", async function () { - const amount = ethers.utils.parseUnits("100", 6); + const amount = ethers.utils.parseUnits("102", 6); //approve market to access dsaWallet0 await baseContract.connect(dsa0Signer).approve(market, amount); @@ -317,7 +318,9 @@ describe("Compound III", function () { const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal( + ethers.utils.parseUnits("0", 6) + ); }); it("Should borrow on behalf of from Compound", async function () { @@ -335,8 +338,9 @@ describe("Compound III", function () { expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( ethers.utils.parseUnits("100", 6) ); - //dsawallet0 --> collateral 6eth, balance 5eth, 100usdc - //dsaWallet1 --> balance 9eth coll: 0eth + console.log(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)); + //dsawallet0 --> collateral 6eth, balance 5eth, borrowed 100usdc + //dsaWallet1 --> balance 9eth coll: 0eth, 100usdc }); it("Should payback on behalf of from Compound", async function () { From 4480f91aedfa6fb56af7dab95937c20323bb8738 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 21:50:18 +0530 Subject: [PATCH 28/75] code refactor --- .../connectors/compound/{ => v2}/events.sol | 0 .../connectors/compound/{ => v2}/helpers.sol | 4 +- .../compound/{ => v2}/interface.sol | 0 .../connectors/compound/{ => v2}/main.sol | 4 +- .../v3-rewards}/events.sol | 0 .../v3-rewards}/helpers.sol | 6 +- .../v3-rewards}/interface.sol | 0 .../v3-rewards}/main.sol | 4 +- .../{compound-iii => compound/v3}/events.sol | 0 .../{compound-iii => compound/v3}/helpers.sol | 89 +++++++++++-------- .../v3}/interface.sol | 0 .../{compound-iii => compound/v3}/main.sol | 57 +++++------- 12 files changed, 87 insertions(+), 77 deletions(-) rename contracts/mainnet/connectors/compound/{ => v2}/events.sol (100%) rename contracts/mainnet/connectors/compound/{ => v2}/helpers.sol (91%) rename contracts/mainnet/connectors/compound/{ => v2}/interface.sol (100%) rename contracts/mainnet/connectors/compound/{ => v2}/main.sol (99%) rename contracts/mainnet/connectors/{compound-rewards => compound/v3-rewards}/events.sol (100%) rename contracts/mainnet/connectors/{compound-rewards => compound/v3-rewards}/helpers.sol (62%) rename contracts/mainnet/connectors/{compound-rewards => compound/v3-rewards}/interface.sol (100%) rename contracts/mainnet/connectors/{compound-rewards => compound/v3-rewards}/main.sol (95%) rename contracts/mainnet/connectors/{compound-iii => compound/v3}/events.sol (100%) rename contracts/mainnet/connectors/{compound-iii => compound/v3}/helpers.sol (67%) rename contracts/mainnet/connectors/{compound-iii => compound/v3}/interface.sol (100%) rename contracts/mainnet/connectors/{compound-iii => compound/v3}/main.sol (96%) diff --git a/contracts/mainnet/connectors/compound/events.sol b/contracts/mainnet/connectors/compound/v2/events.sol similarity index 100% rename from contracts/mainnet/connectors/compound/events.sol rename to contracts/mainnet/connectors/compound/v2/events.sol diff --git a/contracts/mainnet/connectors/compound/helpers.sol b/contracts/mainnet/connectors/compound/v2/helpers.sol similarity index 91% rename from contracts/mainnet/connectors/compound/helpers.sol rename to contracts/mainnet/connectors/compound/v2/helpers.sol index 1150a72a..d7b82dfa 100644 --- a/contracts/mainnet/connectors/compound/helpers.sol +++ b/contracts/mainnet/connectors/compound/v2/helpers.sol @@ -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 { diff --git a/contracts/mainnet/connectors/compound/interface.sol b/contracts/mainnet/connectors/compound/v2/interface.sol similarity index 100% rename from contracts/mainnet/connectors/compound/interface.sol rename to contracts/mainnet/connectors/compound/v2/interface.sol diff --git a/contracts/mainnet/connectors/compound/main.sol b/contracts/mainnet/connectors/compound/v2/main.sol similarity index 99% rename from contracts/mainnet/connectors/compound/main.sol rename to contracts/mainnet/connectors/compound/v2/main.sol index 216ef5cc..aea631ea 100644 --- a/contracts/mainnet/connectors/compound/main.sol +++ b/contracts/mainnet/connectors/compound/v2/main.sol @@ -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"; diff --git a/contracts/mainnet/connectors/compound-rewards/events.sol b/contracts/mainnet/connectors/compound/v3-rewards/events.sol similarity index 100% rename from contracts/mainnet/connectors/compound-rewards/events.sol rename to contracts/mainnet/connectors/compound/v3-rewards/events.sol diff --git a/contracts/mainnet/connectors/compound-rewards/helpers.sol b/contracts/mainnet/connectors/compound/v3-rewards/helpers.sol similarity index 62% rename from contracts/mainnet/connectors/compound-rewards/helpers.sol rename to contracts/mainnet/connectors/compound/v3-rewards/helpers.sol index 748d5bcf..769eb8aa 100644 --- a/contracts/mainnet/connectors/compound-rewards/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3-rewards/helpers.sol @@ -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 { diff --git a/contracts/mainnet/connectors/compound-rewards/interface.sol b/contracts/mainnet/connectors/compound/v3-rewards/interface.sol similarity index 100% rename from contracts/mainnet/connectors/compound-rewards/interface.sol rename to contracts/mainnet/connectors/compound/v3-rewards/interface.sol diff --git a/contracts/mainnet/connectors/compound-rewards/main.sol b/contracts/mainnet/connectors/compound/v3-rewards/main.sol similarity index 95% rename from contracts/mainnet/connectors/compound-rewards/main.sol rename to contracts/mainnet/connectors/compound/v3-rewards/main.sol index 527c631d..d0b8f354 100644 --- a/contracts/mainnet/connectors/compound-rewards/main.sol +++ b/contracts/mainnet/connectors/compound/v3-rewards/main.sol @@ -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"; diff --git a/contracts/mainnet/connectors/compound-iii/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol similarity index 100% rename from contracts/mainnet/connectors/compound-iii/events.sol rename to contracts/mainnet/connectors/compound/v3/events.sol diff --git a/contracts/mainnet/connectors/compound-iii/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol similarity index 67% rename from contracts/mainnet/connectors/compound-iii/helpers.sol rename to contracts/mainnet/connectors/compound/v3/helpers.sol index 3ee3ac73..dd20c01d 100644 --- a/contracts/mainnet/connectors/compound-iii/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -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_ ); diff --git a/contracts/mainnet/connectors/compound-iii/interface.sol b/contracts/mainnet/connectors/compound/v3/interface.sol similarity index 100% rename from contracts/mainnet/connectors/compound-iii/interface.sol rename to contracts/mainnet/connectors/compound/v3/interface.sol diff --git a/contracts/mainnet/connectors/compound-iii/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol similarity index 96% rename from contracts/mainnet/connectors/compound-iii/main.sol rename to contracts/mainnet/connectors/compound/v3/main.sol index 3e55c6ff..76557d4c 100644 --- a/contracts/mainnet/connectors/compound-iii/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -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) { From 34fd05864ccc90357dbc06087352f77640076d24 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 21:56:32 +0530 Subject: [PATCH 29/75] minor change --- contracts/mainnet/connectors/compound/v3/main.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 76557d4c..10d16c68 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -11,7 +11,6 @@ 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 { /** @@ -570,7 +569,6 @@ 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) { From 278019753da7a6feec833e99d053e7de0e16e9f5 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 22:05:47 +0530 Subject: [PATCH 30/75] minor change --- test/mainnet/compound/compound.iii.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 312491c1..be33d408 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -304,6 +304,9 @@ describe("Compound III", function () { }); it("should payback base token from Compound", async function () { + //deposit 10 usdc(base token) to dsa + await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("5", 6)); + const amount = ethers.utils.parseUnits("102", 6); //approve market to access dsaWallet0 await baseContract.connect(dsa0Signer).approve(market, amount); @@ -359,7 +362,7 @@ describe("Compound III", function () { ); //dsawallet0 --> collateral 6eth, balance 5eth - //dsaWallet1 --> balance 3eth coll: 0eth + //dsaWallet1 --> balance 9eth coll: 0eth }); it("should withdraw some ETH collateral", async function () { @@ -419,7 +422,7 @@ describe("Compound III", function () { { connector: connectorName, method: "withdrawOnBehalf", - args: [market, dsaWallet0.address, amount, 0, 0] + args: [market, dsaWallet0.address, dsaWallet0.address, amount, 0, 0] } ]; //dsawallet0 --> collateral 0eth, balance 9eth From 2030617e74bec8b37bb6e2338c6a223c95a2260e Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 22:16:10 +0530 Subject: [PATCH 31/75] refactor code --- .../mainnet/connectors/compound/v3/events.sol | 16 +++------ .../connectors/compound/v3/helpers.sol | 10 +++++- .../mainnet/connectors/compound/v3/main.sol | 36 +------------------ 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index ef317e17..2961c99b 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -133,19 +133,11 @@ contract Events { address indexed market, address indexed owner, address indexed manager, + bool allow, uint256 expiry, uint256 nonce, - uint256 v, - uint256 r, - uint256 s, - bool allow - ); - - event LogApproveMarket( - address indexed market, - address indexed token, - uint256 indexed amount, - uint256 getId, - uint256 setId + uint8 v, + bytes32 r, + bytes32 s, ); } diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index dd20c01d..5a8bb9e0 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -26,6 +26,12 @@ abstract contract Helpers is DSMath, Basic { baseToken = CometInterface(market).baseToken(); } + /** + *@dev helper function for three withdraw or borrow cases: + *withdraw - for `withdraw` withdraws the collateral or base from DSA's position to account. + *withdrawFrom - for `withdrawFromUsingManager` withdraws from src to dest using DSA as manager + *withdrawTo - for `withdrawTo` withdraws from DSA to dest address. + */ function _withdrawHelper( address market, address token, @@ -81,7 +87,9 @@ abstract contract Helpers is DSMath, Basic { _withdrawHelper(params.market, token_, params.from, params.to, amt_); - uint256 finalBal = CometInterface(params.market).borrowBalanceOf(params.from); + uint256 finalBal = CometInterface(params.market).borrowBalanceOf( + params.from + ); amt_ = sub(finalBal, initialBal); convertWethToEth(isEth, tokenContract, amt_); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 10d16c68..f9d89288 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -786,7 +786,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { r, s ); - eventName_ = "LogAllowWithPermit(address,address,address,uint256,uint256,uint256,uint256,uint256,bool)"; + eventName_ = "LogAllowWithPermit(address,address,address,bool,uint256,uint256,uint8,bytes32,bytes32)"; eventParam_ = abi.encode( market, owner, @@ -799,40 +799,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { s ); } - - function approveMarket( - address market, - address token, - uint256 amt, - uint256 getId, - uint256 setId - ) - external - payable - returns (string memory eventName_, bytes memory eventParam_) - { - uint256 amt_ = getUint(getId, amt); - require( - market != address(0) && token != address(0), - "invalid market/token address" - ); - require(amt > 0, "amount-cannot-be-zero"); - - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); - - amt_ = amt_ == uint256(-1) - ? TokenInterface(market).balanceOf(address(this)) - : amt_; - - approve(tokenContract, market, amt_); - - setUint(setId, amt_); - - eventName_ = "LogApproveMarket(address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token_, amt_, getId, setId); - } } contract ConnectV2CompoundV3 is CompoundV3Resolver { From f8b2e31e241adb23b92d5a5cd8b5e3d7c4d39383 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 22:22:09 +0530 Subject: [PATCH 32/75] typo --- contracts/mainnet/connectors/compound/v3/events.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index 2961c99b..0e6ea07c 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -138,6 +138,6 @@ contract Events { uint256 nonce, uint8 v, bytes32 r, - bytes32 s, + bytes32 s ); } From 6289a0ae812ba495d7aa14f16e4bab579d59c40c Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 1 Sep 2022 23:28:58 +0530 Subject: [PATCH 33/75] test --- contracts/mainnet/connectors/compound/v3/main.sol | 9 +++++++-- test/mainnet/compound/compound.iii.test.ts | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index f9d89288..31e40a21 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -11,6 +11,7 @@ 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 { /** @@ -212,7 +213,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 initialBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + token_ ); amt_ = amt_ == uint256(-1) ? initialBal : amt_; @@ -229,11 +230,15 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 finalBal = getAccountSupplyBalanceOfAsset( address(this), market, - token + token_ ); amt_ = sub(initialBal, finalBal); + console.log(amt_); + console.log(initialBal); + console.log(finalBal); + convertWethToEth(isEth, tokenContract, amt_); setUint(setId, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index be33d408..f1dc4eb5 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -366,6 +366,8 @@ describe("Compound III", function () { }); it("should withdraw some ETH collateral", async function () { + let initialBal = await ethers.provider.getBalance(dsaWallet0.address); + console.log(initialBal) const amount = ethers.utils.parseEther("2"); const spells = [ { @@ -380,7 +382,9 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("4") ); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("7")); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther(initialBal.add(2).toString()) + ); }); it("manager should be able to withdraw collateral from the position", async function () { From c0ea8a55239d4f6cddf0063bf61bd88b05c8b5f4 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 00:59:35 +0530 Subject: [PATCH 34/75] errors fix --- .../connectors/compound/v3/helpers.sol | 39 ++++++++++--------- .../mainnet/connectors/compound/v3/main.sol | 6 --- test/mainnet/compound/compound.iii.test.ts | 10 +++-- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 5a8bb9e0..540838a2 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -39,29 +39,13 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - if (from == address(0) && to == address(0)) { - CometInterface(market).withdraw(token, amt); - } else if (from == address(0)) { + if (from == address(0)) { CometInterface(market).withdrawTo(to, token, amt); } else if (from != address(0) && to != address(0)) { CometInterface(market).withdrawFrom(from, to, token, amt); } } - function _transfer( - address market, - address token, - address from, - address to, - uint256 amt - ) internal { - if (from == address(0)) { - CometInterface(market).transferAsset(to, token, amt); - } else { - CometInterface(market).transferAssetFrom(from, to, token, amt); - } - } - function _borrow(BorrowWithdrawParams memory params) internal returns (uint256 amt, uint256 setId) @@ -93,6 +77,8 @@ abstract contract Helpers is DSMath, Basic { amt_ = sub(finalBal, initialBal); convertWethToEth(isEth, tokenContract, amt_); + if (params.from == address(0) || params.to == address(this)) + convertWethToEth(isEth, tokenContract, amt_); setUint(params.setId, amt_); @@ -111,7 +97,7 @@ abstract contract Helpers is DSMath, Basic { "invalid market/token address" ); - bool isEth = params.token == ethAddr || params.token == wethAddr; + bool isEth = params.token == ethAddr; address token_ = isEth ? wethAddr : params.token; TokenInterface tokenContract = TokenInterface(token_); @@ -142,7 +128,8 @@ abstract contract Helpers is DSMath, Basic { ); amt_ = sub(initialBal, finalBal); - convertWethToEth(isEth, tokenContract, amt_); + if (params.from == address(0) || params.to == address(this)) + convertWethToEth(isEth, tokenContract, amt_); setUint(params.setId, amt_); @@ -150,6 +137,20 @@ abstract contract Helpers is DSMath, Basic { setId = params.setId; } + function _transfer( + address market, + address token, + address from, + address to, + uint256 amt + ) internal { + if (from == address(0)) { + CometInterface(market).transferAsset(to, token, amt); + } else { + CometInterface(market).transferAssetFrom(from, to, token, amt); + } + } + function getAccountSupplyBalanceOfAsset( address account, address market, diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 31e40a21..bb0e42ff 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -11,7 +11,6 @@ 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 { /** @@ -235,10 +234,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { amt_ = sub(initialBal, finalBal); - console.log(amt_); - console.log(initialBal); - console.log(finalBal); - convertWethToEth(isEth, tokenContract, amt_); setUint(setId, amt_); @@ -580,7 +575,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); } - convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); CometInterface(market).supplyFrom(from, to, token_, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index f1dc4eb5..9ca69609 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -367,7 +367,7 @@ describe("Compound III", function () { it("should withdraw some ETH collateral", async function () { let initialBal = await ethers.provider.getBalance(dsaWallet0.address); - console.log(initialBal) + console.log(initialBal); const amount = ethers.utils.parseEther("2"); const spells = [ { @@ -382,12 +382,14 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("4") ); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( - ethers.utils.parseEther(initialBal.add(2).toString()) - ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(2).toString()); }); it("manager should be able to withdraw collateral from the position", async function () { + await wallet1.sendTransaction({ + to: tokens.weth.address, + value: ethers.utils.parseEther("10") + }); const amount = ethers.constants.MaxUint256; const spells = [ { From 866e3456e73e0d3b012e50379861d4a6b5037a47 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 01:13:59 +0530 Subject: [PATCH 35/75] errors fixes --- contracts/mainnet/connectors/compound/v3/helpers.sol | 3 ++- contracts/mainnet/connectors/compound/v3/main.sol | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 540838a2..7887236d 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -189,7 +189,8 @@ abstract contract Helpers is DSMath, Basic { } amt = bal_ < allowance_ ? bal_ : allowance_; } - convertEthToWeth(isEth, TokenInterface(token), amt); + if (src == address(this)) + convertEthToWeth(isEth, TokenInterface(token), amt); } else { if (amt == uint256(-1)) { uint256 allowance_ = TokenInterface(token).allowance( diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index bb0e42ff..56906522 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -481,9 +481,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { } convertEthToWeth(isEth, tokenContract, amt_); - approve(tokenContract, market, amt_); - CometInterface(market).supply(token_, amt_); setUint(setId, amt_); From b5e05f353ce66fc67705b8ac07e8f7b561411458 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 03:08:14 +0530 Subject: [PATCH 36/75] test update --- .../connectors/compound/v3/helpers.sol | 1 - .../mainnet/connectors/compound/v3/main.sol | 3 ++- test/mainnet/compound/compound.iii.test.ts | 27 ++++++++++--------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 7887236d..99ec284d 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -76,7 +76,6 @@ abstract contract Helpers is DSMath, Basic { ); amt_ = sub(finalBal, initialBal); - convertWethToEth(isEth, tokenContract, amt_); if (params.from == address(0) || params.to == address(this)) convertWethToEth(isEth, tokenContract, amt_); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 56906522..451003b2 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -96,6 +96,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { market != address(0) && token != address(0), "invalid market/token address" ); + require(to != address(0), "invalid to address"); bool isEth = token == ethAddr || token == wethAddr; address token_ = isEth ? wethAddr : token; @@ -156,6 +157,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { market != address(0) && token != address(0), "invalid market/token address" ); + require(to != address(0), "invalid to address"); bool isEth = token == ethAddr || token == wethAddr; address token_ = isEth ? wethAddr : token; @@ -614,7 +616,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); convertEthToWeth(isEth, tokenContract, amt_); - approve(tokenContract, market, amt_); CometInterface(market).buyCollateral( diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 9ca69609..3a1ac73d 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -16,6 +16,7 @@ import { abis } from "../../../scripts/constant/abis"; import { constants } from "../../../scripts/constant/constant"; import { ConnectV2CompoundV3__factory } from "../../../typechain"; import { MaxUint256 } from "@uniswap/sdk-core"; +import { USDC_OPTIMISTIC_KOVAN } from "@uniswap/smart-order-router"; describe("Compound III", function () { const connectorName = "COMPOUND-V3-TEST-A"; @@ -367,13 +368,12 @@ describe("Compound III", function () { it("should withdraw some ETH collateral", async function () { let initialBal = await ethers.provider.getBalance(dsaWallet0.address); - console.log(initialBal); - const amount = ethers.utils.parseEther("2"); + const amount_ = ethers.utils.parseEther("2"); const spells = [ { connector: connectorName, method: "withdraw", - args: [market, tokens.eth.address, amount, 0, 0] + args: [market, tokens.eth.address, amount_, 0, 0] } ]; @@ -382,7 +382,7 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("4") ); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(2).toString()); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(amount_).toString()); }); it("manager should be able to withdraw collateral from the position", async function () { @@ -407,7 +407,7 @@ describe("Compound III", function () { //dsawallet0 --> collateral 0eth, balance 7eth //dsaWallet1 --> balance 7eth coll: 0eth - expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("7")); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("4")); }); it("Should withdraw on behalf of from Compound", async function () { @@ -422,13 +422,14 @@ describe("Compound III", function () { //dsaWallet1 --> balance 2eth coll: 5eth const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + let initialBal = await ethers.provider.getBalance(dsaWallet0.address); const amount = ethers.utils.parseEther("2"); const spells = [ { connector: connectorName, method: "withdrawOnBehalf", - args: [market, dsaWallet0.address, dsaWallet0.address, amount, 0, 0] + args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; //dsawallet0 --> collateral 0eth, balance 9eth @@ -436,23 +437,23 @@ describe("Compound III", function () { const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("9")); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(amount); - expect( - new BigNumber( - await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.eth.address).balance - ).toFixed() - ).to.be.equal(ethers.utils.parseEther("3")); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("3") + ); }); it("should buy collateral", async function () { //deposit 10 usdc(base token) to dsa await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); + console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); //dsawallet0 --> collateral 0eth, balance 9eth 10usdc //dsaWallet1 --> balance 2eth coll: 3eth const amount = ethers.utils.parseEther("1"); const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); + await baseContract.connect(wallet0).approve(market, bal); const spells = [ { connector: connectorName, @@ -502,7 +503,7 @@ describe("Compound III", function () { { connector: connectorName, method: "depositFromUsingManager", - args: [market, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + args: [market, tokens.link.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; From eaf3a4cbce12135a1ffd6d97b0467c2913d83835 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 03:17:27 +0530 Subject: [PATCH 37/75] minor change --- contracts/mainnet/connectors/compound/v3/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 451003b2..5259b006 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -616,7 +616,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); convertEthToWeth(isEth, tokenContract, amt_); - approve(tokenContract, market, amt_); + approve(TokenInterface(getBaseToken(market)), market, amt_); CometInterface(market).buyCollateral( asset, From 204c9f924ecfccc6af574f9a84c0841f1698b787 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 11:18:43 +0530 Subject: [PATCH 38/75] code refactor --- .../connectors/compound/v3/helpers.sol | 2 +- .../mainnet/connectors/compound/v3/main.sol | 39 ++++++-- test/mainnet/compound/compound.iii.test.ts | 98 +++++++++++++------ 3 files changed, 98 insertions(+), 41 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 99ec284d..36fdff43 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -56,7 +56,7 @@ abstract contract Helpers is DSMath, Basic { params.market != address(0) && params.token != address(0), "invalid market/token address" ); - bool isEth = params.token == wethAddr; + bool isEth = params.token == ethAddr; address token_ = isEth ? wethAddr : params.token; TokenInterface tokenContract = TokenInterface(token_); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 5259b006..8588d8a4 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -40,7 +40,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token address" ); - bool isEth = token == ethAddr || token == wethAddr; + bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -98,7 +98,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); require(to != address(0), "invalid to address"); - bool isEth = token == ethAddr || token == wethAddr; + bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -159,7 +159,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); require(to != address(0), "invalid to address"); - bool isEth = token == ethAddr || token == wethAddr; + bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); @@ -332,6 +332,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function borrow( address market, + address token, uint256 amt, uint256 getId, uint256 setId @@ -344,8 +345,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); + bool isEth = token == ethAddr; address token_ = getBaseToken(market); - bool isEth = token_ == wethAddr; + require(token == token_ || token == ethAddr, "invalid-token"); TokenInterface tokenContract = TokenInterface(token_); @@ -385,6 +387,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function borrowOnBehalf( address market, + address token, address to, uint256 amt, uint256 getId, @@ -394,10 +397,14 @@ abstract contract CompoundV3Resolver is Events, Helpers { payable returns (string memory eventName_, bytes memory eventParam_) { + require( + token == ethAddr || token == getBaseToken(market), + "invalid-token" + ); (uint256 amt_, uint256 setId_) = _borrow( BorrowWithdrawParams({ market: market, - token: getBaseToken(market), + token: token, from: address(0), to: to, amt: amt, @@ -423,6 +430,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address market, address from, address to, + address token, uint256 amt, uint256 getId, uint256 setId @@ -431,10 +439,14 @@ abstract contract CompoundV3Resolver is Events, Helpers { payable returns (string memory eventName_, bytes memory eventParam_) { + require( + token == ethAddr || token == getBaseToken(market), + "invalid-token" + ); (uint256 amt_, uint256 setId_) = _borrow( BorrowWithdrawParams({ market: market, - token: getBaseToken(market), + token: token, from: from, to: to, amt: amt, @@ -456,6 +468,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function payback( address market, + address token, uint256 amt, uint256 getId, uint256 setId @@ -467,8 +480,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require(market != address(0), "invalid market address"); + bool isEth = token == ethAddr; address token_ = getBaseToken(market); - bool isEth = token_ == wethAddr; + require(token == token_ || token == ethAddr, "invalid-token"); + TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -503,6 +518,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function paybackOnBehalf( address market, + address token, address to, uint256 amt, uint256 getId, @@ -516,7 +532,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); address token_ = getBaseToken(market); - bool isEth = token_ == wethAddr; + bool isEth = token == ethAddr; + require(token == token_ || token == ethAddr, "invalid-token"); + TokenInterface tokenContract = TokenInterface(token_); amt_ = amt_ == uint256(-1) @@ -551,6 +569,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function paybackFromUsingManager( address market, + address token, address from, address to, uint256 amt, @@ -565,7 +584,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(market != address(0), "invalid market address"); address token_ = getBaseToken(market); - bool isEth = token_ == wethAddr; + bool isEth = token == ethAddr; + require(token == token_ || token == ethAddr, "invalid-token"); + TokenInterface tokenContract = TokenInterface(token_); amt_ = setAmt(market, token_, from, amt_, isEth, true); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 3a1ac73d..3853fd6d 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -272,12 +272,12 @@ describe("Compound III", function () { { connector: connectorName, method: "borrow", - args: [market, amount, 0, 0] + args: [market, base, amount, 0, 0] }, { connector: connectorName, method: "payback", - args: [market, ethers.utils.parseUnits("50", 6), 0, 0] + args: [market, base, ethers.utils.parseUnits("50", 6), 0, 0] } ]; @@ -316,7 +316,7 @@ describe("Compound III", function () { { connector: connectorName, method: "paybackFromUsingManager", - args: [market, dsaWallet0.address, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + args: [market, base, dsaWallet0.address, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] } ]; @@ -333,7 +333,7 @@ describe("Compound III", function () { { connector: connectorName, method: "borrowOnBehalf", - args: [market, dsaWallet1.address, amount, 0, 0] + args: [market, base, dsaWallet1.address, amount, 0, 0] } ]; @@ -352,7 +352,7 @@ describe("Compound III", function () { { connector: connectorName, method: "paybackOnBehalf", - args: [market, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] } ]; @@ -444,34 +444,69 @@ describe("Compound III", function () { ); }); - it("should buy collateral", async function () { - //deposit 10 usdc(base token) to dsa - await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); - console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + it("Should withdraw asset and transfer", async function () { + const spells1 = [ + { + connector: connectorName, + method: "deposit", + args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0] + } + ]; + //dsawallet0 --> collateral 0eth, balance 7eth + //dsaWallet1 --> balance 2eth coll: 5eth - //dsawallet0 --> collateral 0eth, balance 9eth 10usdc - //dsaWallet1 --> balance 2eth coll: 3eth - const amount = ethers.utils.parseEther("1"); - const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); - await baseContract.connect(wallet0).approve(market, bal); + const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + let initialBal = await ethers.provider.getBalance(dsaWallet0.address); + + const amount = ethers.utils.parseEther("2"); const spells = [ { connector: connectorName, - method: "buyCollateral", - args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] + method: "withdrawOnBehalf", + args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; + //dsawallet0 --> collateral 0eth, balance 9eth + //dsaWallet1 --> balance 2eth coll: 3eth const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(amount)); + + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("1") ); - - //dsawallet0 --> collateral 0eth, balance 9eth >1link - //dsaWallet1 --> balance 2eth coll: 3eth }); + //can buy only when target reserves not reached. + + // it("should buy collateral", async function () { + // //deposit 10 usdc(base token) to dsa + // await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); + // console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + + // //dsawallet0 --> collateral 0eth, balance 9eth 10usdc + // //dsaWallet1 --> balance 2eth coll: 3eth + // const amount = ethers.utils.parseUnits("1",6); + // const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); + // const spells = [ + // { + // connector: connectorName, + // method: "buyCollateral", + // args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] + // } + // ]; + + // const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + // const receipt = await tx.wait(); + // expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( + // ethers.utils.parseEther("1") + // ); + + // //dsawallet0 --> collateral 0eth, balance 9eth >1link + // //dsaWallet1 --> balance 2eth coll: 3eth + // }); + it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () { const spells = [ { @@ -494,27 +529,27 @@ describe("Compound III", function () { //dsaWallet1 --> balance 2eth coll: 0eth }); - it("should deposit link from using manager", async function () { + it("should deposit eth from using manager", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("5") + }); const amount = ethers.utils.parseEther("1"); - //approve market to access dsaWallet0 await baseContract.connect(dsa0Signer).approve(market, amount); const spells = [ { connector: connectorName, method: "depositFromUsingManager", - args: [market, tokens.link.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.link.address)).balance).to.be.gte( + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("1") ); - - //dsawallet0 --> collateral 3eth, balance 9eth previous-1link - //dsaWallet1 --> balance 2eth coll: 0eth,1link }); it("should borrow using manager", async function () { @@ -522,17 +557,16 @@ describe("Compound III", function () { { connector: connectorName, method: "deposit", - args: [market, tokens.eth.address, ethers.utils.parseEther("6"), 0, 0] + args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] } ]; - const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); const amount = ethers.utils.parseUnits("50", 6); const spells = [ { connector: connectorName, method: "borrowFromUsingManager", - args: [market, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + args: [market, base, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; @@ -550,6 +584,8 @@ describe("Compound III", function () { }); it("should transferAsset using manager", async function () { + let bal1 = (await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance; + let bal0 = (await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance const spells = [ { connector: connectorName, From 56e3fcbdb13408ca0b20e53b7b3701fcd80ea642 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 14:10:25 +0530 Subject: [PATCH 39/75] minor changes --- .../connectors/compound/v3/helpers.sol | 30 ++-- .../mainnet/connectors/compound/v3/main.sol | 8 +- test/mainnet/compound/compound.iii.test.ts | 142 ++++++++++-------- 3 files changed, 103 insertions(+), 77 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 36fdff43..5ec28f8c 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -18,6 +18,12 @@ abstract contract Helpers is DSMath, Basic { uint256 setId; } + enum ACTION { + repay, + deposit, + transfer + } + function getBaseToken(address market) internal view @@ -172,7 +178,7 @@ abstract contract Helpers is DSMath, Basic { address src, uint256 amt, bool isEth, - bool isRepay + ACTION action ) internal returns (uint256) { if (isEth) { if (amt == uint256(-1)) { @@ -181,10 +187,16 @@ abstract contract Helpers is DSMath, Basic { market ); uint256 bal_; - if (isRepay) { + if (action == ACTION.repay) { bal_ = CometInterface(market).borrowBalanceOf(src); - } else { + } else if (action == ACTION.deposit) { bal_ = src.balance; + } else if (action == ACTION.transfer) { + bal_ = (token == getBaseToken(market)) + ? TokenInterface(market).balanceOf(src) + : CometInterface(market) + .userCollateral(src, token) + .balance; } amt = bal_ < allowance_ ? bal_ : allowance_; } @@ -197,13 +209,11 @@ abstract contract Helpers is DSMath, Basic { market ); uint256 bal_; - if (isRepay) { - bal_ = (token == getBaseToken(market)) - ? CometInterface(market).borrowBalanceOf(src) - : CometInterface(market) - .userCollateral(src, token) - .balance; - } else { + if (action == ACTION.repay) { + bal_ = CometInterface(market).borrowBalanceOf(src); + } else if (action == ACTION.deposit) { + bal_ = TokenInterface(token).balanceOf(src); + } else if (action == ACTION.transfer) { bal_ = (token == getBaseToken(market)) ? TokenInterface(market).balanceOf(src) : CometInterface(market) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 8588d8a4..00485b53 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -170,7 +170,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); } - amt_ = setAmt(market, token_, from, amt_, isEth, false); + amt_ = setAmt(market, token_, from, amt_, isEth, ACTION.deposit); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -428,9 +428,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { */ function borrowFromUsingManager( address market, + address token, address from, address to, - address token, uint256 amt, uint256 getId, uint256 setId @@ -589,7 +589,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, from, amt_, isEth, true); + amt_ = setAmt(market, token_, from, amt_, isEth, ACTION.repay); uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); if (borrowBal > 0) { @@ -744,7 +744,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, src, amt_, isEth, false); + amt_ = setAmt(market, token_, src, amt_, isEth, ACTION.transfer); _transfer(market, token_, src, dest, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 3853fd6d..5ea71421 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -242,7 +242,6 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("5") ); - //dsawallet0 --> collateral 5eth, balance 5eth }); //deposit asset on behalf of @@ -262,8 +261,6 @@ describe("Compound III", function () { expect((await comet.connect(wallet0).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("6") ); - //dsawallet0 --> collateral 6eth, balance 5eth - //dsaWallet1 --> balance 9eth coll: 0eth }); it("Should borrow and payback base token from Compound", async function () { @@ -305,11 +302,9 @@ describe("Compound III", function () { }); it("should payback base token from Compound", async function () { - //deposit 10 usdc(base token) to dsa await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("5", 6)); const amount = ethers.utils.parseUnits("102", 6); - //approve market to access dsaWallet0 await baseContract.connect(dsa0Signer).approve(market, amount); const spells = [ @@ -343,8 +338,6 @@ describe("Compound III", function () { ethers.utils.parseUnits("100", 6) ); console.log(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)); - //dsawallet0 --> collateral 6eth, balance 5eth, borrowed 100usdc - //dsaWallet1 --> balance 9eth coll: 0eth, 100usdc }); it("Should payback on behalf of from Compound", async function () { @@ -361,9 +354,6 @@ describe("Compound III", function () { expect(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).to.be.equal( ethers.utils.parseUnits("0", 6) ); - - //dsawallet0 --> collateral 6eth, balance 5eth - //dsaWallet1 --> balance 9eth coll: 0eth }); it("should withdraw some ETH collateral", async function () { @@ -404,9 +394,6 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("0") ); - - //dsawallet0 --> collateral 0eth, balance 7eth - //dsaWallet1 --> balance 7eth coll: 0eth expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("4")); }); @@ -418,8 +405,6 @@ describe("Compound III", function () { args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] } ]; - //dsawallet0 --> collateral 0eth, balance 7eth - //dsaWallet1 --> balance 2eth coll: 5eth const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); let initialBal = await ethers.provider.getBalance(dsaWallet0.address); @@ -432,8 +417,6 @@ describe("Compound III", function () { args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; - //dsawallet0 --> collateral 0eth, balance 9eth - //dsaWallet1 --> balance 2eth coll: 3eth const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); @@ -452,8 +435,6 @@ describe("Compound III", function () { args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0] } ]; - //dsawallet0 --> collateral 0eth, balance 7eth - //dsaWallet1 --> balance 2eth coll: 5eth const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); let initialBal = await ethers.provider.getBalance(dsaWallet0.address); @@ -466,8 +447,6 @@ describe("Compound III", function () { args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; - //dsawallet0 --> collateral 0eth, balance 9eth - //dsaWallet1 --> balance 2eth coll: 3eth const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); @@ -478,35 +457,6 @@ describe("Compound III", function () { ); }); - //can buy only when target reserves not reached. - - // it("should buy collateral", async function () { - // //deposit 10 usdc(base token) to dsa - // await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); - // console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); - - // //dsawallet0 --> collateral 0eth, balance 9eth 10usdc - // //dsaWallet1 --> balance 2eth coll: 3eth - // const amount = ethers.utils.parseUnits("1",6); - // const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); - // const spells = [ - // { - // connector: connectorName, - // method: "buyCollateral", - // args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] - // } - // ]; - - // const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - // const receipt = await tx.wait(); - // expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( - // ethers.utils.parseEther("1") - // ); - - // //dsawallet0 --> collateral 0eth, balance 9eth >1link - // //dsaWallet1 --> balance 2eth coll: 3eth - // }); - it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () { const spells = [ { @@ -524,9 +474,6 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("3") ); - - //dsawallet0 --> collateral 3eth, balance 9eth >1link - //dsaWallet1 --> balance 2eth coll: 0eth }); it("should deposit eth from using manager", async function () { @@ -534,6 +481,7 @@ describe("Compound III", function () { to: dsaWallet0.address, value: ethers.utils.parseEther("5") }); + console.log("balance wallet 0", await ethers.provider.getBalance(dsaWallet0.address)); const amount = ethers.utils.parseEther("1"); await baseContract.connect(dsa0Signer).approve(market, amount); @@ -578,14 +526,56 @@ describe("Compound III", function () { expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( ethers.utils.parseUnits("50", 6) ); - - //dsawallet0 --> collateral 9eth, balance 3eth previous-1link borrowed 50usdc(transferred to dsa1) - //dsaWallet1 --> balance 2eth coll: 0eth,1link, 50usdc }); - it("should transferAsset using manager", async function () { + it("should transfer base token from dsaWallet1 to dsaWallet0 position", async function () { + await baseContract.connect(signer).transfer(dsaWallet1.address, ethers.utils.parseUnits("10", 6)); + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [market, base, ethers.constants.MaxUint256, 0, 0] + }, + { + connector: connectorName, + method: "transferAsset", + args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.lte( + ethers.utils.parseUnits("0", 6) + ); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseUnits("10", 6) + ); + }); + + it("should transfer base token using manager from dsaWallet0 to dsaWallet1 position", async function () { + const spells = [ + { + connector: connectorName, + method: "transferAssetFromUsingManager", + args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.lte( + ethers.utils.parseUnits("10", 6) + ); + expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseUnits("0", 6) + ); + }); + + it("should transferAsset collateral using manager", async function () { let bal1 = (await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance; - let bal0 = (await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance + let bal0 = (await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance; const spells = [ { connector: connectorName, @@ -597,14 +587,40 @@ describe("Compound III", function () { const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseEther("3") + bal1.add(ethers.utils.parseEther("1")).toString() ); expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseEther("8") + bal0.sub(ethers.utils.parseEther("1")).toString() ); - - //dsawallet0 --> collateral 8eth, balance 3eth previous-1link borrowed 50usdc(transferred to dsa1) - //dsaWallet1 --> balance 3eth coll: 0eth,1link, 50usdc }); + + //can buy only when target reserves not reached. + + // it("should buy collateral", async function () { + // //deposit 10 usdc(base token) to dsa + // await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); + // console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + + // //dsawallet0 --> collateral 0eth, balance 9eth 10usdc + // //dsaWallet1 --> balance 2eth coll: 3eth + // const amount = ethers.utils.parseUnits("1",6); + // const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); + // const spells = [ + // { + // connector: connectorName, + // method: "buyCollateral", + // args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] + // } + // ]; + + // const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + // const receipt = await tx.wait(); + // expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte( + // ethers.utils.parseEther("1") + // ); + + // //dsawallet0 --> collateral 0eth, balance 9eth >1link + // //dsaWallet1 --> balance 2eth coll: 3eth + // }); }); }); From d6d3fc317b15fa62b0af7003d0b58a5b1b999f92 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 14:14:52 +0530 Subject: [PATCH 40/75] withdraw -> withdrawTo --- contracts/mainnet/connectors/compound/v3/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 00485b53..92ef9992 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -254,7 +254,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 withdrawTo( address market, address token, address to, From 5470f19b79a99a6c4819b02b2ff460efd636d228 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 16:44:31 +0530 Subject: [PATCH 41/75] test modified --- .../mainnet/connectors/compound/v3/events.sol | 2 +- .../connectors/compound/v3/helpers.sol | 7 +- .../mainnet/connectors/compound/v3/main.sol | 3 +- test/mainnet/compound/compound.iii.test.ts | 146 +++++++++++------- 4 files changed, 96 insertions(+), 62 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index 0e6ea07c..a58462be 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -37,7 +37,7 @@ contract Events { uint256 setId ); - event LogWithdrawOnBehalfOf( + event LogWithdrawTo( address indexed market, address indexed token, address to, diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 5ec28f8c..2a6770ae 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -198,7 +198,8 @@ abstract contract Helpers is DSMath, Basic { .userCollateral(src, token) .balance; } - amt = bal_ < allowance_ ? bal_ : allowance_; + if (action == ACTION.transfer) amt = bal_; + else amt = bal_ < allowance_ ? bal_ : allowance_; } if (src == address(this)) convertEthToWeth(isEth, TokenInterface(token), amt); @@ -220,8 +221,8 @@ abstract contract Helpers is DSMath, Basic { .userCollateral(src, token) .balance; } - - amt = bal_ < allowance_ ? bal_ : allowance_; + if (action == ACTION.transfer) amt = bal_; + else amt = bal_ < allowance_ ? bal_ : allowance_; } } return amt; diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 92ef9992..653b9153 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -278,7 +278,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { }) ); - eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogWithdrawTo(address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, to, amt_, getId, setId_); } @@ -739,6 +739,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { { uint256 amt_ = getUint(getId, amount); require(market != address(0), "invalid market address"); + require(dest != address(0), "invalid destination address"); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 5ea71421..264f538d 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -23,6 +23,7 @@ describe("Compound III", function () { const market = "0xc3d688B66703497DAA19211EEdff47f25384cdc3"; const base = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; const account = "0x72a53cdbbcc1b9efa39c834a540550e23463aacb"; + const wethWhale = "0x1c11ba15939e1c16ec7ca1678df6160ea2063bc5"; const ABI = [ "function balanceOf(address account) public view returns (uint256)", @@ -147,6 +148,7 @@ describe("Compound III", function () { let instaConnectorsV2: Contract; let connector: any; let signer: any; + let wethSigner: any; const comet = new ethers.Contract(market, cometABI); @@ -184,6 +186,12 @@ describe("Compound III", function () { }); signer = await ethers.getSigner(account); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wethWhale] + }); + wethSigner = await ethers.getSigner(wethWhale); }); it("Should have contracts deployed.", async function () { @@ -413,7 +421,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawOnBehalf", + method: "withdrawTo", args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; @@ -443,7 +451,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawOnBehalf", + method: "withdrawTo", args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; @@ -476,14 +484,57 @@ describe("Compound III", function () { ); }); - it("should deposit eth from using manager", async function () { - await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("5") - }); - console.log("balance wallet 0", await ethers.provider.getBalance(dsaWallet0.address)); + it("should transfer base token from dsaWallet1 to dsaWallet0 position", async function () { + await baseContract.connect(signer).transfer(dsaWallet1.address, ethers.utils.parseUnits("10", 6)); + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [market, base, ethers.constants.MaxUint256, 0, 0] + } + ]; + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet1.address)); + console.log(initialBal.toString()); + let spells1 = [ + { + connector: connectorName, + method: "transferAsset", + args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const receipt1 = await tx.wait(); + expect(await comet.connect(signer).balanceOf(dsaWallet1.address)).to.be.lte(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.gte(initialBal); + }); + + it("should transfer base token using manager from dsaWallet0 to dsaWallet1 position", async function () { + const spells = [ + { + connector: connectorName, + method: "transferAssetFromUsingManager", + args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + console.log(initialBal.toString()); + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.lte(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).balanceOf(dsaWallet1.address)).to.be.gte(initialBal); + }); + + it("should deposit weth from using manager", async function () { + await wethContract.connect(wethSigner).transfer(dsaWallet0.address, ethers.utils.parseEther("10")); + let initialBal = await wethContract.connect(wallet0).balanceOf(dsaWallet0.address); + const amount = ethers.utils.parseEther("1"); - await baseContract.connect(dsa0Signer).approve(market, amount); + await wethContract.connect(dsa0Signer).approve(market, amount); const spells = [ { @@ -498,18 +549,44 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("1") ); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); + }); + + it("should deposit eth from using manager same as 'from'", async function () { + const amount = ethers.utils.parseEther("1"); + await wethContract.connect(dsa0Signer).approve(market, amount); + let initialBal = await ethers.provider.getBalance(dsaWallet0.address); + + const spells = [ + { + connector: connectorName, + method: "depositFromUsingManager", + args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("2") + ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); }); it("should borrow using manager", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("5") + }); const spells1 = [ { connector: connectorName, method: "deposit", - args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] + args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0] } ]; const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); - const amount = ethers.utils.parseUnits("50", 6); + const amount = ethers.utils.parseUnits("10", 6); const spells = [ { connector: connectorName, @@ -521,56 +598,11 @@ describe("Compound III", function () { const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( - ethers.utils.parseUnits("50", 6) - ); - expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( - ethers.utils.parseUnits("50", 6) - ); - }); - - it("should transfer base token from dsaWallet1 to dsaWallet0 position", async function () { - await baseContract.connect(signer).transfer(dsaWallet1.address, ethers.utils.parseUnits("10", 6)); - - const spells = [ - { - connector: connectorName, - method: "deposit", - args: [market, base, ethers.constants.MaxUint256, 0, 0] - }, - { - connector: connectorName, - method: "transferAsset", - args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] - } - ]; - - const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.lte( - ethers.utils.parseUnits("0", 6) - ); - expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseUnits("10", 6) ); - }); - - it("should transfer base token using manager from dsaWallet0 to dsaWallet1 position", async function () { - const spells = [ - { - connector: connectorName, - method: "transferAssetFromUsingManager", - args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] - } - ]; - - const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.lte( + expect(await baseContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.equal( ethers.utils.parseUnits("10", 6) ); - expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseUnits("0", 6) - ); }); it("should transferAsset collateral using manager", async function () { From d48da206da4bd24af9b0facec916ca5544952b6e Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 17:00:56 +0530 Subject: [PATCH 42/75] test modified --- contracts/mainnet/connectors/compound/v3/main.sol | 10 ++++++++-- test/mainnet/compound/compound.iii.test.ts | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 653b9153..f3b5727f 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -326,6 +326,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 token The address of the token to be borrowed. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param amt The amount of base token to borrow. * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens borrowed. @@ -380,12 +381,13 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @dev Borrow base asset and transfer to 'to' account. * @notice Borrow base token from Compound on behalf of an address. * @param market The address of the market. + * @param token The address of the token to be borrowed. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @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 borrowed. */ - function borrowOnBehalf( + function borrowTo( address market, address token, address to, @@ -420,13 +422,14 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @dev Borrow base asset from 'from' and transfer to 'to'. * @notice Borrow base token or deposited token from Compound. * @param market The address of the market. + * @param token The address of the token to be borrowed. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @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 getId ID to retrieve amt. * @param setId ID stores the amount of tokens borrowed. */ - function borrowFromUsingManager( + function borrowOnBehalf( address market, address token, address from, @@ -462,6 +465,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @dev Repays the borrowed base asset. * @notice Repays the borrow of the base asset. * @param market The address of the market. + * @param token The address of the token to be repaid. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param amt The amount to be repaid. * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens repaid. @@ -511,6 +515,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @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. + * @param token The address of the token to be repaid. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param to The address on behalf of which the borrow is to be repaid. * @param amt The amount to be repaid. * @param getId ID to retrieve amt. @@ -561,6 +566,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @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'. Approve the comet markey * @param market The address of the market. + * @param token The address of the token to be repaid. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @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 amt The amount to be repaid. diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 264f538d..53a569c4 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -335,7 +335,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "borrowOnBehalf", + method: "borrowTo", args: [market, base, dsaWallet1.address, amount, 0, 0] } ]; @@ -590,7 +590,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "borrowFromUsingManager", + method: "borrowOnBehalf", args: [market, base, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; From be922547a90eeb6bd573e12bb21a6b1801f45a6f Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 18:52:45 +0530 Subject: [PATCH 43/75] fixed tests --- .../connectors/compound/v3/helpers.sol | 17 +++---- test/mainnet/compound/compound.iii.test.ts | 44 +++++++++++++------ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 2a6770ae..bd4d7cfa 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -33,8 +33,7 @@ abstract contract Helpers is DSMath, Basic { } /** - *@dev helper function for three withdraw or borrow cases: - *withdraw - for `withdraw` withdraws the collateral or base from DSA's position to account. + *@dev helper function for following withdraw or borrow cases: *withdrawFrom - for `withdrawFromUsingManager` withdraws from src to dest using DSA as manager *withdrawTo - for `withdrawTo` withdraws from DSA to dest address. */ @@ -45,9 +44,9 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - if (from == address(0)) { + if (from == address(this)) { CometInterface(market).withdrawTo(to, token, amt); - } else if (from != address(0) && to != address(0)) { + } else { CometInterface(market).withdrawFrom(from, to, token, amt); } } @@ -72,8 +71,10 @@ abstract contract Helpers is DSMath, Basic { params.from ); - uint256 balance = TokenInterface(params.market).balanceOf(params.from); - require(balance == 0, "borrow-disabled-when-supplied-base"); + require( + TokenInterface(params.market).balanceOf(params.from) == 0, + "borrow-disabled-when-supplied-base" + ); _withdrawHelper(params.market, token_, params.from, params.to, amt_); @@ -82,7 +83,7 @@ abstract contract Helpers is DSMath, Basic { ); amt_ = sub(finalBal, initialBal); - if (params.from == address(0) || params.to == address(this)) + if (params.to == address(this)) convertWethToEth(isEth, tokenContract, amt_); setUint(params.setId, amt_); @@ -133,7 +134,7 @@ abstract contract Helpers is DSMath, Basic { ); amt_ = sub(initialBal, finalBal); - if (params.from == address(0) || params.to == address(this)) + if (params.to == address(this)) convertWethToEth(isEth, tokenContract, amt_); setUint(params.setId, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 53a569c4..5fa3aead 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -142,6 +142,7 @@ describe("Compound III", function () { let dsaWallet0: any; let dsaWallet1: any; let dsaWallet2: any; + let dsaWallet3: any; let wallet: any; let dsa0Signer: any; let masterSigner: Signer; @@ -208,6 +209,8 @@ describe("Compound III", function () { expect(!!dsaWallet1.address).to.be.true; dsaWallet2 = await buildDSAv2(wallet0.address); expect(!!dsaWallet2.address).to.be.true; + dsaWallet3 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet3.address).to.be.true; wallet = await ethers.getSigner(dsaWallet0.address); expect(!!dsaWallet1.address).to.be.true; }); @@ -229,6 +232,11 @@ describe("Compound III", function () { value: ethers.utils.parseEther("10") }); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + await wallet0.sendTransaction({ + to: dsaWallet3.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); }); }); @@ -496,7 +504,7 @@ describe("Compound III", function () { ]; const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet1.address)); + let initialBal = await baseContract.connect(signer).balanceOf(dsaWallet1.address); console.log(initialBal.toString()); let spells1 = [ { @@ -520,7 +528,7 @@ describe("Compound III", function () { args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] } ]; - let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + let initialBal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); console.log(initialBal.toString()); const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); @@ -572,37 +580,47 @@ describe("Compound III", function () { ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); }); + it("should allow manager for dsaWallet0's collateral", async function () { + const spells = [ + { + connector: connectorName, + method: "toggleAccountManager", + args: [market, dsaWallet2.address, true] + } + ]; + const tx = await dsaWallet3.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + }); it("should borrow using manager", async function () { + let initialBal = await baseContract.connect(wallet0).balanceOf(dsaWallet0.address); await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("5") + to: dsaWallet3.address, + value: ethers.utils.parseEther("15") }); const spells1 = [ { connector: connectorName, method: "deposit", - args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0] + args: [market, tokens.eth.address, ethers.utils.parseEther("15"), 0, 0] } ]; - const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); - const amount = ethers.utils.parseUnits("10", 6); + const tx1 = await dsaWallet3.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const amount = ethers.utils.parseUnits("500", 6); const spells = [ { connector: connectorName, method: "borrowOnBehalf", - args: [market, base, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + args: [market, base, dsaWallet3.address, dsaWallet0.address, amount, 0, 0] } ]; const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( - ethers.utils.parseUnits("10", 6) - ); - expect(await baseContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.equal( - ethers.utils.parseUnits("10", 6) + expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet3.address)).toFixed()).to.be.equal( + ethers.utils.parseUnits("500", 6) ); + expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal(initialBal.add(amount)); }); it("should transferAsset collateral using manager", async function () { From cfbb031772c471253979ac019b45c293522ed902 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 18:54:22 +0530 Subject: [PATCH 44/75] minor change --- test/mainnet/compound/compound.iii.test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 5fa3aead..4e788afb 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -353,7 +353,6 @@ describe("Compound III", function () { expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( ethers.utils.parseUnits("100", 6) ); - console.log(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)); }); it("Should payback on behalf of from Compound", async function () { @@ -505,7 +504,6 @@ describe("Compound III", function () { const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); let initialBal = await baseContract.connect(signer).balanceOf(dsaWallet1.address); - console.log(initialBal.toString()); let spells1 = [ { connector: connectorName, @@ -529,7 +527,6 @@ describe("Compound III", function () { } ]; let initialBal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); - console.log(initialBal.toString()); const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); From 249530f8f14ca7a86e9b40524f59fde6c30b1ca4 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 19:01:09 +0530 Subject: [PATCH 45/75] test statements --- .../{compound => compound-ii}/compound.test.ts | 0 test/mainnet/compound/compound.iii.test.ts | 18 +++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) rename test/mainnet/{compound => compound-ii}/compound.test.ts (100%) diff --git a/test/mainnet/compound/compound.test.ts b/test/mainnet/compound-ii/compound.test.ts similarity index 100% rename from test/mainnet/compound/compound.test.ts rename to test/mainnet/compound-ii/compound.test.ts diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 4e788afb..6cec12c0 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -261,7 +261,7 @@ describe("Compound III", function () { }); //deposit asset on behalf of - it("Should supply on behalf of dsaWallet0 ETH collateral in Compound V3", async function () { + it("Should supply ETH collateral on behalf of dsaWallet0 in Compound V3", async function () { const amount = ethers.utils.parseEther("1"); // 1 ETH const spells = [ { @@ -304,7 +304,7 @@ describe("Compound III", function () { ); }); - it("should allow manager for dsaWallet0's collateral", async function () { + it("should allow manager for dsaWallet0's collateral and base", async function () { const spells = [ { connector: connectorName, @@ -317,7 +317,7 @@ describe("Compound III", function () { const receipt = await tx.wait(); }); - it("should payback base token from Compound", async function () { + it("should payback base token on Compound using manager", async function () { await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("5", 6)); const amount = ethers.utils.parseUnits("102", 6); @@ -338,7 +338,7 @@ describe("Compound III", function () { ); }); - it("Should borrow on behalf of from Compound", async function () { + it("Should borrow to another dsa from Compound", async function () { const amount = ethers.utils.parseUnits("100", 6); const spells = [ { @@ -412,7 +412,7 @@ describe("Compound III", function () { expect(await wethContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("4")); }); - it("Should withdraw on behalf of from Compound", async function () { + it("Should withdraw collateral to another DSA", async function () { const spells1 = [ { connector: connectorName, @@ -442,7 +442,7 @@ describe("Compound III", function () { ); }); - it("Should withdraw asset and transfer", async function () { + it("Should withdraw collateral to another DSA", async function () { const spells1 = [ { connector: connectorName, @@ -534,7 +534,7 @@ describe("Compound III", function () { expect(await comet.connect(signer).balanceOf(dsaWallet1.address)).to.be.gte(initialBal); }); - it("should deposit weth from using manager", async function () { + it("should deposit weth using manager", async function () { await wethContract.connect(wethSigner).transfer(dsaWallet0.address, ethers.utils.parseEther("10")); let initialBal = await wethContract.connect(wallet0).balanceOf(dsaWallet0.address); @@ -557,7 +557,7 @@ describe("Compound III", function () { expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); }); - it("should deposit eth from using manager same as 'from'", async function () { + it("should deposit eth using manager same as 'from'", async function () { const amount = ethers.utils.parseEther("1"); await wethContract.connect(dsa0Signer).approve(market, amount); let initialBal = await ethers.provider.getBalance(dsaWallet0.address); @@ -589,7 +589,7 @@ describe("Compound III", function () { const tx = await dsaWallet3.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); }); - it("should borrow using manager", async function () { + it("should borrow on behalf using manager", async function () { let initialBal = await baseContract.connect(wallet0).balanceOf(dsaWallet0.address); await wallet0.sendTransaction({ to: dsaWallet3.address, From 826051ac502edfe91592ce8a716447bf159c2c51 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 19:11:36 +0530 Subject: [PATCH 46/75] directory --- test/mainnet/{compound-ii => compound}/compound.test.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/mainnet/{compound-ii => compound}/compound.test.ts (100%) diff --git a/test/mainnet/compound-ii/compound.test.ts b/test/mainnet/compound/compound.test.ts similarity index 100% rename from test/mainnet/compound-ii/compound.test.ts rename to test/mainnet/compound/compound.test.ts From 28ef1ae2bb6e35ad28b420437175c1caccedab69 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 19:43:42 +0530 Subject: [PATCH 47/75] setAmt --- .../connectors/compound/v3/helpers.sol | 61 ++++++------------- 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index bd4d7cfa..ad83fdf8 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -181,51 +181,26 @@ abstract contract Helpers is DSMath, Basic { bool isEth, ACTION action ) internal returns (uint256) { - if (isEth) { - if (amt == uint256(-1)) { - uint256 allowance_ = TokenInterface(token).allowance( - src, - market - ); - uint256 bal_; - if (action == ACTION.repay) { - bal_ = CometInterface(market).borrowBalanceOf(src); - } else if (action == ACTION.deposit) { - bal_ = src.balance; - } else if (action == ACTION.transfer) { - bal_ = (token == getBaseToken(market)) - ? TokenInterface(market).balanceOf(src) - : CometInterface(market) - .userCollateral(src, token) - .balance; - } - if (action == ACTION.transfer) amt = bal_; - else amt = bal_ < allowance_ ? bal_ : allowance_; - } - if (src == address(this)) - convertEthToWeth(isEth, TokenInterface(token), amt); - } else { - if (amt == uint256(-1)) { - uint256 allowance_ = TokenInterface(token).allowance( - src, - market - ); - uint256 bal_; - if (action == ACTION.repay) { - bal_ = CometInterface(market).borrowBalanceOf(src); - } else if (action == ACTION.deposit) { - bal_ = TokenInterface(token).balanceOf(src); - } else if (action == ACTION.transfer) { - bal_ = (token == getBaseToken(market)) - ? TokenInterface(market).balanceOf(src) - : CometInterface(market) - .userCollateral(src, token) - .balance; - } - if (action == ACTION.transfer) amt = bal_; - else amt = bal_ < allowance_ ? bal_ : allowance_; + if (amt == uint256(-1)) { + uint256 allowance_ = TokenInterface(token).allowance(src, market); + uint256 bal_; + + if (action == ACTION.repay) { + bal_ = CometInterface(market).borrowBalanceOf(src); + } else if (action == ACTION.deposit) { + if (isEth) bal_ = src.balance; + else bal_ = TokenInterface(token).balanceOf(src); + } else if (action == ACTION.transfer) { + bal_ = (token == getBaseToken(market)) + ? TokenInterface(market).balanceOf(src) + : CometInterface(market).userCollateral(src, token).balance; } + if (action == ACTION.transfer) amt = bal_; + else amt = bal_ < allowance_ ? bal_ : allowance_; } + if (src == address(this)) + convertEthToWeth(isEth, TokenInterface(token), amt); + return amt; } } From 5571cf187e5577773efc21f567c8b0849194fe72 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 19:51:56 +0530 Subject: [PATCH 48/75] require statements format --- .../connectors/compound/v3/helpers.sol | 12 ++++-- .../mainnet/connectors/compound/v3/main.sol | 39 ++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index ad83fdf8..62a9251a 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -58,8 +58,10 @@ abstract contract Helpers is DSMath, Basic { uint256 amt_ = getUint(params.getId, params.amt); require( - params.market != address(0) && params.token != address(0), - "invalid market/token address" + params.market != address(0) && + params.token != address(0) && + params.to != address(0), + "invalid market/token/to address" ); bool isEth = params.token == ethAddr; address token_ = isEth ? wethAddr : params.token; @@ -99,8 +101,10 @@ abstract contract Helpers is DSMath, Basic { uint256 amt_ = getUint(params.getId, params.amt); require( - params.market != address(0) && params.token != address(0), - "invalid market/token address" + params.market != address(0) && + params.token != address(0) && + params.to != address(0), + "invalid market/token/to address" ); bool isEth = params.token == ethAddr; diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index f3b5727f..5ea5e4de 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -93,10 +93,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require( - market != address(0) && token != address(0), - "invalid market/token address" + market != address(0) && token != address(0) && to != address(0), + "invalid market/token/to address" ); - require(to != address(0), "invalid to address"); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; @@ -154,10 +153,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 amt_ = getUint(getId, amt); require( - market != address(0) && token != address(0), - "invalid market/token address" + market != address(0) && token != address(0) && to != address(0), + "invalid market/token/to address" ); - require(to != address(0), "invalid to address"); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; @@ -482,7 +480,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { returns (string memory eventName_, bytes memory eventParam_) { uint256 amt_ = getUint(getId, amt); - require(market != address(0), "invalid market address"); + require( + market != address(0) && token != address(0), + "invalid market/token address" + ); bool isEth = token == ethAddr; address token_ = getBaseToken(market); @@ -534,7 +535,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { returns (string memory eventName_, bytes memory eventParam_) { uint256 amt_ = getUint(getId, amt); - require(market != address(0), "invalid market address"); + require( + market != address(0) && token != address(0) && to != address(0), + "invalid market/token/to address" + ); address token_ = getBaseToken(market); bool isEth = token == ethAddr; @@ -587,7 +591,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { returns (string memory eventName_, bytes memory eventParam_) { uint256 amt_ = getUint(getId, amt); - require(market != address(0), "invalid market address"); + require( + market != address(0) && token != address(0) && to != address(0), + "invalid market/token/to address" + ); address token_ = getBaseToken(market); bool isEth = token == ethAddr; @@ -637,6 +644,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { returns (string memory eventName_, bytes memory eventParam_) { uint256 amt_ = getUint(getId, baseAmt); + require( + market != address(0) && asset != address(0) && dest != address(0), + "invalid market/token/to address" + ); bool isEth = asset == ethAddr; address token_ = isEth ? wethAddr : asset; @@ -691,8 +702,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { { uint256 amt_ = getUint(getId, amount); require( - market != address(0) && token != address(0), - "invalid market address" + market != address(0) && token != address(0) && dest != address(0), + "invalid market/token/to address" ); bool isEth = token == ethAddr; @@ -744,8 +755,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { returns (string memory eventName_, bytes memory eventParam_) { uint256 amt_ = getUint(getId, amount); - require(market != address(0), "invalid market address"); - require(dest != address(0), "invalid destination address"); + require( + market != address(0) && token != address(0) && dest != address(0), + "invalid market/token/to address" + ); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; From cf3c71986dd37b2b25f08738e3a21194ebd47b66 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 20:07:38 +0530 Subject: [PATCH 49/75] function names --- .../connectors/compound/v3/helpers.sol | 26 +++++++++---------- .../mainnet/connectors/compound/v3/main.sol | 10 +++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 62a9251a..3087f7a3 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -18,10 +18,10 @@ abstract contract Helpers is DSMath, Basic { uint256 setId; } - enum ACTION { - repay, - deposit, - transfer + enum Action { + REPAY, + DEPOSIT, + TRANSFER } function getBaseToken(address market) @@ -113,7 +113,7 @@ abstract contract Helpers is DSMath, Basic { TokenInterface tokenContract = TokenInterface(token_); params.from = params.from == address(0) ? address(this) : params.from; - uint256 initialBal = getAccountSupplyBalanceOfAsset( + uint256 initialBal = _getAccountSupplyBalanceOfAsset( params.from, params.market, token_ @@ -131,7 +131,7 @@ abstract contract Helpers is DSMath, Basic { _withdrawHelper(params.market, token_, params.from, params.to, amt_); - uint256 finalBal = getAccountSupplyBalanceOfAsset( + uint256 finalBal = _getAccountSupplyBalanceOfAsset( params.from, params.market, token_ @@ -161,7 +161,7 @@ abstract contract Helpers is DSMath, Basic { } } - function getAccountSupplyBalanceOfAsset( + function _getAccountSupplyBalanceOfAsset( address account, address market, address asset @@ -177,29 +177,29 @@ abstract contract Helpers is DSMath, Basic { } } - function setAmt( + function _setAmt( address market, address token, address src, uint256 amt, bool isEth, - ACTION action + Action action ) internal returns (uint256) { if (amt == uint256(-1)) { uint256 allowance_ = TokenInterface(token).allowance(src, market); uint256 bal_; - if (action == ACTION.repay) { + if (action == Action.REPAY) { bal_ = CometInterface(market).borrowBalanceOf(src); - } else if (action == ACTION.deposit) { + } else if (action == Action.DEPOSIT) { if (isEth) bal_ = src.balance; else bal_ = TokenInterface(token).balanceOf(src); - } else if (action == ACTION.transfer) { + } else if (action == Action.TRANSFER) { bal_ = (token == getBaseToken(market)) ? TokenInterface(market).balanceOf(src) : CometInterface(market).userCollateral(src, token).balance; } - if (action == ACTION.transfer) amt = bal_; + if (action == Action.TRANSFER) amt = bal_; else amt = bal_ < allowance_ ? bal_ : allowance_; } if (src == address(this)) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 5ea5e4de..0f09d1aa 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -168,7 +168,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); } - amt_ = setAmt(market, token_, from, amt_, isEth, ACTION.deposit); + amt_ = _setAmt(market, token_, from, amt_, isEth, Action.DEPOSIT); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -209,7 +209,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - uint256 initialBal = getAccountSupplyBalanceOfAsset( + uint256 initialBal = _getAccountSupplyBalanceOfAsset( address(this), market, token_ @@ -226,7 +226,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { CometInterface(market).withdraw(token_, amt_); - uint256 finalBal = getAccountSupplyBalanceOfAsset( + uint256 finalBal = _getAccountSupplyBalanceOfAsset( address(this), market, token_ @@ -602,7 +602,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, from, amt_, isEth, ACTION.repay); + amt_ = _setAmt(market, token_, from, amt_, isEth, Action.REPAY); uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); if (borrowBal > 0) { @@ -764,7 +764,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - amt_ = setAmt(market, token_, src, amt_, isEth, ACTION.transfer); + amt_ = _setAmt(market, token_, src, amt_, isEth, Action.TRANSFER); _transfer(market, token_, src, dest, amt_); From e3946201a8d870359a2b590431a44f9f6a3410c3 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 20:50:12 +0530 Subject: [PATCH 50/75] function name changes --- .../mainnet/connectors/compound/v3/events.sol | 14 ++--- .../connectors/compound/v3/helpers.sol | 2 +- .../mainnet/connectors/compound/v3/main.sol | 51 +++++++++++++------ test/mainnet/compound/compound.iii.test.ts | 2 +- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index a58462be..b20c51f9 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -10,7 +10,7 @@ contract Events { uint256 setId ); - event LogDepositOnBehalfOf( + event LogDepositOnBehalf( address indexed market, address indexed token, address to, @@ -46,7 +46,7 @@ contract Events { uint256 setId ); - event LogWithdrawFromUsingManager( + event LogWithdrawOnBehalf( address indexed market, address indexed token, address from, @@ -63,7 +63,7 @@ contract Events { uint256 setId ); - event LogBorrowOnBehalfOf( + event LogBorrowTo( address indexed market, address to, uint256 tokenAmt, @@ -71,7 +71,7 @@ contract Events { uint256 setId ); - event LogBorrowFromUsingManager( + event LogBorrowOnBehalf( address indexed market, address from, address to, @@ -82,7 +82,7 @@ contract Events { event LogPayback(address indexed market, uint256 tokenAmt, uint256 setId); - event LogPaybackOnBehalfOf( + event LogPaybackOnBehalf( address indexed market, address to, uint256 tokenAmt, @@ -127,9 +127,9 @@ contract Events { uint256 setId ); - event LogAllow(address indexed market, address indexed manager, bool allow); + event LogToggleAccountManager(address indexed market, address indexed manager, bool allow); - event LogAllowWithPermit( + event LogToggleAccountManagerWithPermit( address indexed market, address indexed owner, address indexed manager, diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 3087f7a3..3def6184 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -177,7 +177,7 @@ abstract contract Helpers is DSMath, Basic { } } - function _setAmt( + function _calculateFromAmount( address market, address token, address src, diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 0f09d1aa..21573be1 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -104,7 +104,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (token_ == getBaseToken(market)) { require( CometInterface(market).borrowBalanceOf(to) == 0, - "debt-not-repaid" + "to-address-position-debt-not-repaid" ); } @@ -164,11 +164,18 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (token_ == getBaseToken(market)) { require( CometInterface(market).borrowBalanceOf(to) == 0, - "debt-not-repaid" + "to-address-position-debt-not-repaid" ); } - amt_ = _setAmt(market, token_, from, amt_, isEth, Action.DEPOSIT); + amt_ = _calculateFromAmount( + market, + token_, + from, + amt_, + isEth, + Action.DEPOSIT + ); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); @@ -291,7 +298,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens withdrawn. */ - function withdrawFromUsingManager( + function withdrawOnBehalf( address market, address token, address from, @@ -316,7 +323,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { }) ); - eventName_ = "LogWithdrawFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogWithdrawOnBehalf(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId_); } @@ -346,7 +353,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { bool isEth = token == ethAddr; address token_ = getBaseToken(market); - require(token == token_ || token == ethAddr, "invalid-token"); + require(token == token_ || isEth, "invalid-token"); TokenInterface tokenContract = TokenInterface(token_); @@ -412,7 +419,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setId: setId }) ); - eventName_ = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; + eventName_ = "LogBorrowTo(address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, to, amt_, getId, setId_); } @@ -455,7 +462,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setId: setId }) ); - eventName_ = "LogBorrowFromUsingManager(address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogBorrowOnBehalf(address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, from, to, amt_, getId, setId_); } @@ -487,7 +494,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { bool isEth = token == ethAddr; address token_ = getBaseToken(market); - require(token == token_ || token == ethAddr, "invalid-token"); + require(token == token_ || isEth, "invalid-token"); TokenInterface tokenContract = TokenInterface(token_); @@ -542,7 +549,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = getBaseToken(market); bool isEth = token == ethAddr; - require(token == token_ || token == ethAddr, "invalid-token"); + require(token == token_ || isEth, "invalid-token"); TokenInterface tokenContract = TokenInterface(token_); @@ -598,11 +605,18 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = getBaseToken(market); bool isEth = token == ethAddr; - require(token == token_ || token == ethAddr, "invalid-token"); + require(token == token_ || isEth, "invalid-token"); TokenInterface tokenContract = TokenInterface(token_); - amt_ = _setAmt(market, token_, from, amt_, isEth, Action.REPAY); + amt_ = _calculateFromAmount( + market, + token_, + from, + amt_, + isEth, + Action.REPAY + ); uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); if (borrowBal > 0) { @@ -764,7 +778,14 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = isEth ? wethAddr : token; TokenInterface tokenContract = TokenInterface(token_); - amt_ = _setAmt(market, token_, src, amt_, isEth, Action.TRANSFER); + amt_ = _calculateFromAmount( + market, + token_, + src, + amt_, + isEth, + Action.TRANSFER + ); _transfer(market, token_, src, dest, amt_); @@ -787,7 +808,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { bool isAllowed ) external returns (string memory eventName_, bytes memory eventParam_) { CometInterface(market).allow(manager, isAllowed); - eventName_ = "LogAllow(address,address,bool)"; + eventName_ = "LogToggleAccountManager(address,address,bool)"; eventParam_ = abi.encode(market, manager, isAllowed); } @@ -825,7 +846,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { r, s ); - eventName_ = "LogAllowWithPermit(address,address,address,bool,uint256,uint256,uint8,bytes32,bytes32)"; + eventName_ = "LogToggleAccountManagerWithPermit(address,address,address,bool,uint256,uint256,uint8,bytes32,bytes32)"; eventParam_ = abi.encode( market, owner, diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 6cec12c0..34077859 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -399,7 +399,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawFromUsingManager", + method: "withdrawOnBehalf", args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; From fa345da4fc8f84982bb5d8fbce2bb4ceab03432c Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 21:08:24 +0530 Subject: [PATCH 51/75] modifications --- .../mainnet/connectors/compound/v3/main.sol | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 21573be1..5be82956 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -502,12 +502,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { ? CometInterface(market).borrowBalanceOf(address(this)) : amt_; - uint256 borrowBal = CometInterface(market).borrowBalanceOf( + uint256 supplyBalance_ = CometInterface(market).balanceOf( address(this) ); - if (borrowBal > 0) { - require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); - } + require(supplyBalance_ == 0, "cannot-repay-when-supplied"); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); @@ -557,10 +555,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { ? CometInterface(market).borrowBalanceOf(to) : amt_; - uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); - if (borrowBal > 0) { - require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); - } + uint256 supplyBalance_ = CometInterface(market).balanceOf(to); + require(supplyBalance_ == 0, "cannot-repay-when-supplied"); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); @@ -618,12 +614,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { Action.REPAY ); - uint256 borrowBal = CometInterface(market).borrowBalanceOf(to); - if (borrowBal > 0) { - require(amt_ <= borrowBal, "repay-amt-greater-than-debt"); - } - - approve(tokenContract, market, amt_); + uint256 supplyBalance_ = CometInterface(market).balanceOf(to); + require(supplyBalance_ == 0, "cannot-repay-when-supplied"); CometInterface(market).supplyFrom(from, to, token_, amt_); From 91bb5ea57c0c5db9bb2605c51f840761bff523c0 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 22:56:08 +0530 Subject: [PATCH 52/75] error --- contracts/mainnet/connectors/compound/v3/main.sol | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 5be82956..335bdd4c 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -226,9 +226,15 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (token_ == getBaseToken(market)) { uint256 balance = CometInterface(market).balanceOf(address(this)); + //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. if (balance > 0) { require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); } + //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. + require( + CometInterface(market).borrowBalanceOf(address(this)) == 0, + "withdraw-disabled-for-zero-supplies" + ); } CometInterface(market).withdraw(token_, amt_); From b88d9d887a6d687d1a79c59ab5f713f3d7794fa6 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 23:13:39 +0530 Subject: [PATCH 53/75] withdraw and borrow update --- .../mainnet/connectors/compound/v3/events.sol | 23 ++++- .../mainnet/connectors/compound/v3/main.sol | 85 ++++++++++++++++++- test/mainnet/compound/compound.iii.test.ts | 6 +- 3 files changed, 107 insertions(+), 7 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index b20c51f9..8edb0d8c 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -47,6 +47,15 @@ contract Events { ); event LogWithdrawOnBehalf( + address indexed market, + address indexed token, + address from, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogWithdrawOnBehalfAndTransfer( address indexed market, address indexed token, address from, @@ -72,6 +81,14 @@ contract Events { ); event LogBorrowOnBehalf( + address indexed market, + address from, + uint256 tokenAmt, + uint256 getId, + uint256 setId + ); + + event LogBorrowOnBehalfAndTransfer( address indexed market, address from, address to, @@ -127,7 +144,11 @@ contract Events { uint256 setId ); - event LogToggleAccountManager(address indexed market, address indexed manager, bool allow); + event LogToggleAccountManager( + address indexed market, + address indexed manager, + bool allow + ); event LogToggleAccountManagerWithPermit( address indexed market, diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 335bdd4c..ec6e015d 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -293,6 +293,44 @@ abstract contract CompoundV3Resolver is Events, Helpers { eventParam_ = abi.encode(market, token, to, amt_, getId, setId_); } + /** + * @dev Withdraw base/collateral asset from an account and transfer to DSA. + * @notice Withdraw base token or deposited token from Compound from an address and transfer to DSA. + * @param market The address of the market. + * @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 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 withdrawOnBehalf( + address market, + address token, + address from, + uint256 amt, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory eventName_, bytes memory eventParam_) + { + (uint256 amt_, uint256 setId_) = _withdraw( + BorrowWithdrawParams({ + market: market, + token: token, + from: from, + to: address(this), + amt: amt, + getId: getId, + setId: setId + }) + ); + + eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, token, from, amt_, getId, setId_); + } + /** * @dev Withdraw base/collateral asset from an account and transfer to 'to'. * @notice Withdraw base token or deposited token from Compound from an address and transfer to 'to'. @@ -304,7 +342,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 withdrawOnBehalfAndTransfer( address market, address token, address from, @@ -436,11 +474,52 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param token The address of the token to be borrowed. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @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 getId ID to retrieve amt. * @param setId ID stores the amount of tokens borrowed. */ function borrowOnBehalf( + address market, + address token, + address from, + uint256 amt, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory eventName_, bytes memory eventParam_) + { + require( + token == ethAddr || token == getBaseToken(market), + "invalid-token" + ); + (uint256 amt_, uint256 setId_) = _borrow( + BorrowWithdrawParams({ + market: market, + token: token, + from: from, + to: address(this), + amt: amt, + getId: getId, + setId: setId + }) + ); + eventName_ = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, from, amt_, getId, setId_); + } + + /** + * @dev Borrow base asset from 'from' and transfer to 'to'. + * @notice Borrow base token or deposited token from Compound. + * @param market The address of the market. + * @param token The address of the token to be borrowed. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @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 getId ID to retrieve amt. + * @param setId ID stores the amount of tokens borrowed. + */ + function borrowOnBehalfAndTransfer( address market, address token, address from, @@ -468,7 +547,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setId: setId }) ); - eventName_ = "LogBorrowOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogBorrowOnBehalfAndTransfer(address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, from, to, amt_, getId, setId_); } diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 34077859..02973a6d 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -390,7 +390,7 @@ describe("Compound III", function () { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(amount_).toString()); }); - it("manager should be able to withdraw collateral from the position", async function () { + it("manager should be able to withdraw collateral from the position and transfer", async function () { await wallet1.sendTransaction({ to: tokens.weth.address, value: ethers.utils.parseEther("10") @@ -399,7 +399,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawOnBehalf", + method: "withdrawOnBehalfAndTransfer", args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] } ]; @@ -607,7 +607,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "borrowOnBehalf", + method: "borrowOnBehalfAndTransfer", args: [market, base, dsaWallet3.address, dsaWallet0.address, amount, 0, 0] } ]; From 75c092c266b78a146ab7e65ee16b075c1ddfbeb1 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 2 Sep 2022 23:44:51 +0530 Subject: [PATCH 54/75] refactor --- contracts/mainnet/connectors/compound/v3/helpers.sol | 10 +++++++--- contracts/mainnet/connectors/compound/v3/main.sol | 12 +++++------- test/mainnet/compound/compound.iii.test.ts | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 3def6184..05ef8f2a 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -121,12 +121,16 @@ abstract contract Helpers is DSMath, Basic { amt_ = amt_ == uint256(-1) ? initialBal : amt_; if (token_ == getBaseToken(params.market)) { - uint256 balance = TokenInterface(params.market).balanceOf( - params.from - ); + uint256 balance = CometInterface(params.market).balanceOf(params.from); + //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. if (balance > 0) { require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); } + //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. + require( + CometInterface(params.market).borrowBalanceOf(params.from) == 0, + "withdraw-disabled-for-zero-supplies" + ); } _withdrawHelper(params.market, token_, params.from, params.to, amt_); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index ec6e015d..af95ffa2 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -715,7 +715,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached. * @param market The address of the market from where to withdraw. * @param asset The collateral asset to purachase. - * @param dest The address to transfer the purchased assets. * @param minCollateralAmt Minimum amount of collateral expected to be received. * @param baseAmt Amount of base asset to be sold for collateral. * @param getId ID to retrieve amt. @@ -724,7 +723,6 @@ abstract contract CompoundV3Resolver is Events, Helpers { function buyCollateral( address market, address asset, - address dest, uint256 minCollateralAmt, uint256 baseAmt, uint256 getId, @@ -736,8 +734,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { { uint256 amt_ = getUint(getId, baseAmt); require( - market != address(0) && asset != address(0) && dest != address(0), - "invalid market/token/to address" + market != address(0) && asset != address(0), + "invalid market/token address" ); bool isEth = asset == ethAddr; @@ -751,7 +749,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { asset, minCollateralAmt, amt_, - dest + address(this) ); uint256 collAmt = CometInterface(market).quoteCollateral(asset, amt_); @@ -832,7 +830,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens transferred. */ - function transferAssetFromUsingManager( + function transferAssetOnBehalf( address market, address token, address src, @@ -868,7 +866,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); - eventName_ = "LogTransferAssetFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogTransferAssetOnBehalf(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token_, src, dest, amt_, getId, setId); } diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 02973a6d..f36b66cf 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -522,7 +522,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "transferAssetFromUsingManager", + method: "transferAssetOnBehalf", args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] } ]; @@ -626,7 +626,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "transferAssetFromUsingManager", + method: "transferAssetOnBehalf", args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, ethers.utils.parseEther("1"), 0, 0] } ]; From 8250831342b3ffe880488eb1bb5d73fc2b71e060 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 00:28:11 +0530 Subject: [PATCH 55/75] buyCollateral updated --- .../mainnet/connectors/compound/v3/events.sol | 8 +- .../connectors/compound/v3/helpers.sol | 80 ++++++++++++++++++- .../mainnet/connectors/compound/v3/main.sol | 52 ++++-------- 3 files changed, 99 insertions(+), 41 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index 8edb0d8c..3ec68d11 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -116,10 +116,10 @@ contract Events { event LogBuyCollateral( address indexed market, - address indexed token, - uint256 indexed baseAmount, - uint256 minCollateralAmt, - uint256 collateralAmount, + address indexed buyToken, + uint256 indexed baseSellAmt, + uint256 unitAmt, + uint256 buyAmount, uint256 getId, uint256 setId ); diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 05ef8f2a..985685db 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -18,6 +18,14 @@ abstract contract Helpers is DSMath, Basic { uint256 setId; } + struct BuyCollateralData { + address market; + address sellToken; + address buyAsset; + uint256 unitAmt; + uint256 baseSellAmt; + } + enum Action { REPAY, DEPOSIT, @@ -121,7 +129,9 @@ abstract contract Helpers is DSMath, Basic { amt_ = amt_ == uint256(-1) ? initialBal : amt_; if (token_ == getBaseToken(params.market)) { - uint256 balance = CometInterface(params.market).balanceOf(params.from); + uint256 balance = CometInterface(params.market).balanceOf( + params.from + ); //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. if (balance > 0) { require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); @@ -211,4 +221,72 @@ abstract contract Helpers is DSMath, Basic { return amt; } + + function _buyCollateral( + BuyCollateralData memory params, + uint256 getId, + uint256 setId + ) internal returns (string memory eventName_, bytes memory eventParam_) { + uint256 sellAmt_ = getUint(getId, params.baseSellAmt); + require( + params.market != address(0) && params.buyAsset != address(0), + "invalid market/token address" + ); + require( + params.sellToken == getBaseToken(params.market), + "invalid-sell-token" + ); + + bool isEth = params.sellToken == ethAddr; + params.sellToken = isEth ? wethAddr : params.sellToken; + + if (sellAmt_ == uint256(-1)) { + sellAmt_ = isEth + ? address(this).balance + : TokenInterface(params.sellToken).balanceOf(address(this)); + } + + isEth = params.buyAsset == ethAddr; + params.buyAsset = isEth ? wethAddr : params.buyAsset; + + convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_); + + uint256 slippageAmt_ = convert18ToDec( + TokenInterface(params.buyAsset).decimals(), + wmul( + params.unitAmt, + convertTo18( + TokenInterface(params.sellToken).decimals(), + sellAmt_ + ) + ) + ); + approve(TokenInterface(params.sellToken), params.market, sellAmt_); + CometInterface(params.market).buyCollateral( + params.buyAsset, + slippageAmt_, + sellAmt_, + address(this) + ); + + uint256 buyAmt_ = CometInterface(params.market).quoteCollateral( + params.buyAsset, + sellAmt_ + ); + require(slippageAmt_ <= buyAmt_, "too-much-slippage"); + + convertWethToEth(isEth, TokenInterface(params.buyAsset), buyAmt_); + setUint(setId, sellAmt_); + + eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; + eventParam_ = abi.encode( + params.market, + params.buyAsset, + sellAmt_, + params.unitAmt, + buyAmt_, + getId, + setId + ); + } } diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index af95ffa2..6120c8fd 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -714,17 +714,19 @@ abstract contract CompoundV3Resolver is Events, Helpers { * @dev Buy collateral asset absorbed, from the market. * @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached. * @param market The address of the market from where to withdraw. - * @param asset The collateral asset to purachase. - * @param minCollateralAmt Minimum amount of collateral expected to be received. - * @param baseAmt Amount of base asset to be sold for collateral. + * @param sellToken base token. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param buyAsset The collateral asset to purachase. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param unitAmt Minimum amount of collateral expected to be received. + * @param baseSellAmt Amount of base asset to be sold for collateral. * @param getId ID to retrieve amt. * @param setId ID stores the amount of base tokens sold. */ function buyCollateral( address market, - address asset, - uint256 minCollateralAmt, - uint256 baseAmt, + address sellToken, + address buyAsset, + uint256 unitAmt, + uint256 baseSellAmt, uint256 getId, uint256 setId ) @@ -732,36 +734,14 @@ abstract contract CompoundV3Resolver is Events, Helpers { payable returns (string memory eventName_, bytes memory eventParam_) { - uint256 amt_ = getUint(getId, baseAmt); - require( - market != address(0) && asset != address(0), - "invalid market/token address" - ); - - bool isEth = asset == ethAddr; - address token_ = isEth ? wethAddr : asset; - TokenInterface tokenContract = TokenInterface(token_); - - convertEthToWeth(isEth, tokenContract, amt_); - approve(TokenInterface(getBaseToken(market)), market, amt_); - - CometInterface(market).buyCollateral( - asset, - minCollateralAmt, - amt_, - address(this) - ); - - uint256 collAmt = CometInterface(market).quoteCollateral(asset, amt_); - setUint(setId, amt_); - - eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)"; - eventParam_ = abi.encode( - market, - token_, - amt_, - minCollateralAmt, - collAmt, + (eventName_, eventParam_) = _buyCollateral( + BuyCollateralData({ + market: market, + sellToken: sellToken, + buyAsset: buyAsset, + unitAmt: unitAmt, + baseSellAmt: baseSellAmt + }), getId, setId ); From 52c07d27e781f8e11ef517b1c30fcd8684393fa6 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 00:30:04 +0530 Subject: [PATCH 56/75] code refactor --- .../mainnet/connectors/compound/v3/main.sol | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 6120c8fd..9dfd197a 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -50,6 +50,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { "debt-not-repaid" ); } + if (isEth) { amt_ = amt_ == uint256(-1) ? address(this).balance : amt_; convertEthToWeth(isEth, tokenContract, amt_); @@ -222,19 +223,21 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_ ); - amt_ = amt_ == uint256(-1) ? initialBal : amt_; - if (token_ == getBaseToken(market)) { - uint256 balance = CometInterface(market).balanceOf(address(this)); //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. - if (balance > 0) { - require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); + if (amt_ == uint256(-1)) { + amt_ = initialBal; + } else { + require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); } + //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( CometInterface(market).borrowBalanceOf(address(this)) == 0, "withdraw-disabled-for-zero-supplies" ); + } else { + amt_ = amt_ == uint256(-1) ? initialBal : amt_; } CometInterface(market).withdraw(token_, amt_); @@ -402,8 +405,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); if (token_ == getBaseToken(market)) { - uint256 balance = CometInterface(market).balanceOf(address(this)); - require(balance == 0, "borrow-disabled-when-supplied-base"); + require(CometInterface(market).balanceOf(address(this)) == 0, "borrow-disabled-when-supplied-base"); } uint256 initialBal = CometInterface(market).borrowBalanceOf( @@ -583,17 +585,20 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - amt_ = amt_ == uint256(-1) - ? CometInterface(market).borrowBalanceOf(address(this)) - : amt_; + uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(address(this)); - uint256 supplyBalance_ = CometInterface(market).balanceOf( - address(this) - ); - require(supplyBalance_ == 0, "cannot-repay-when-supplied"); + if (amt_ == uint256(-1)) { + amt_ = initialBal; + } else { + require(amt_ <= borrowedBalance_, "withdraw-amt-greater-than-supplies"); + } + + //if supply balance > 0, there are no borrowing so no repay, withdraw instead. + require(CometInterface(market).balanceOf(address(this)) == 0, "cannot-repay-when-supplied"); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); + CometInterface(market).supply(token_, amt_); setUint(setId, amt_); @@ -636,12 +641,16 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - amt_ = amt_ == uint256(-1) - ? CometInterface(market).borrowBalanceOf(to) - : amt_; + uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); - uint256 supplyBalance_ = CometInterface(market).balanceOf(to); - require(supplyBalance_ == 0, "cannot-repay-when-supplied"); + if (amt_ == uint256(-1)) { + amt_ = initialBal; + } else { + require(amt_ <= borrowedBalance_, "withdraw-amt-greater-than-supplies"); + } + + //if supply balance > 0, there are no borrowing so no repay, withdraw instead. + require(CometInterface(market).balanceOf(to) == 0, "cannot-repay-when-supplied"); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); From 69eb33d64ae577a07b39778c5c454238fb77fb03 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 00:38:00 +0530 Subject: [PATCH 57/75] comments --- .../mainnet/connectors/compound/v3/main.sol | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 9dfd197a..532d1a5d 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -228,7 +228,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (amt_ == uint256(-1)) { amt_ = initialBal; } else { - require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); + require( + amt_ <= initialBal, + "withdraw-amt-greater-than-supplies" + ); } //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. @@ -405,7 +408,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); if (token_ == getBaseToken(market)) { - require(CometInterface(market).balanceOf(address(this)) == 0, "borrow-disabled-when-supplied-base"); + require( + CometInterface(market).balanceOf(address(this)) == 0, + "borrow-disabled-when-supplied-base" + ); } uint256 initialBal = CometInterface(market).borrowBalanceOf( @@ -585,16 +591,24 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(address(this)); + uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf( + address(this) + ); if (amt_ == uint256(-1)) { amt_ = initialBal; } else { - require(amt_ <= borrowedBalance_, "withdraw-amt-greater-than-supplies"); + require( + amt_ <= borrowedBalance_, + "withdraw-amt-greater-than-supplies" + ); } //if supply balance > 0, there are no borrowing so no repay, withdraw instead. - require(CometInterface(market).balanceOf(address(this)) == 0, "cannot-repay-when-supplied"); + require( + CometInterface(market).balanceOf(address(this)) == 0, + "cannot-repay-when-supplied" + ); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); @@ -608,8 +622,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @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'. + * @dev Repays borrow of the base asset on behalf of 'to'. + * @notice Repays borrow of the base asset on behalf of 'to'. * @param market The address of the market. * @param token The address of the token to be repaid. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param to The address on behalf of which the borrow is to be repaid. @@ -646,11 +660,17 @@ abstract contract CompoundV3Resolver is Events, Helpers { if (amt_ == uint256(-1)) { amt_ = initialBal; } else { - require(amt_ <= borrowedBalance_, "withdraw-amt-greater-than-supplies"); + require( + amt_ <= borrowedBalance_, + "withdraw-amt-greater-than-supplies" + ); } //if supply balance > 0, there are no borrowing so no repay, withdraw instead. - require(CometInterface(market).balanceOf(to) == 0, "cannot-repay-when-supplied"); + require( + CometInterface(market).balanceOf(to) == 0, + "cannot-repay-when-supplied" + ); convertEthToWeth(isEth, tokenContract, amt_); approve(tokenContract, market, amt_); @@ -664,8 +684,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { } /** - * @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'. Approve the comet markey + * @dev Repays borrow of the base asset form 'from' on behalf of 'to'. + * @notice Repays borrow of the base asset on behalf of 'to'. 'From' address must approve the comet market. * @param market The address of the market. * @param token The address of the token to be repaid. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param from The address from which the borrow has to be repaid on behalf of 'to'. From 791d04091addce28d40d4a23fcfbfec75c6d2133 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:04:50 +0530 Subject: [PATCH 58/75] code refactor --- .../connectors/compound/v3/helpers.sol | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 985685db..79a15cbd 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -77,16 +77,17 @@ abstract contract Helpers is DSMath, Basic { TokenInterface tokenContract = TokenInterface(token_); params.from = params.from == address(0) ? address(this) : params.from; - uint256 initialBal = CometInterface(params.market).borrowBalanceOf( - params.from - ); require( TokenInterface(params.market).balanceOf(params.from) == 0, "borrow-disabled-when-supplied-base" ); - _withdrawHelper(params.market, token_, params.from, params.to, amt_); + uint256 initialBal = CometInterface(params.market).borrowBalanceOf( + params.from + ); + + CometInterface(params.market).withdrawFrom(params.from, params.to, token_, amt_); uint256 finalBal = CometInterface(params.market).borrowBalanceOf( params.from @@ -126,24 +127,28 @@ abstract contract Helpers is DSMath, Basic { params.market, token_ ); - amt_ = amt_ == uint256(-1) ? initialBal : amt_; - if (token_ == getBaseToken(params.market)) { - uint256 balance = CometInterface(params.market).balanceOf( - params.from - ); + if (token_ == getBaseToken(market)) { //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. - if (balance > 0) { - require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); + if (amt_ == uint256(-1)) { + amt_ = initialBal; + } else { + require( + amt_ <= initialBal, + "withdraw-amt-greater-than-supplies" + ); } + //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( - CometInterface(params.market).borrowBalanceOf(params.from) == 0, + CometInterface(market).borrowBalanceOf(params.from) == 0, "withdraw-disabled-for-zero-supplies" ); + } else { + amt_ = amt_ == uint256(-1) ? initialBal : amt_; } - _withdrawHelper(params.market, token_, params.from, params.to, amt_); + CometInterface(params.market).withdrawFrom(params.from, params.to, token_, amt_); uint256 finalBal = _getAccountSupplyBalanceOfAsset( params.from, @@ -245,12 +250,11 @@ abstract contract Helpers is DSMath, Basic { ? address(this).balance : TokenInterface(params.sellToken).balanceOf(address(this)); } + convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_); isEth = params.buyAsset == ethAddr; params.buyAsset = isEth ? wethAddr : params.buyAsset; - convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_); - uint256 slippageAmt_ = convert18ToDec( TokenInterface(params.buyAsset).decimals(), wmul( @@ -261,6 +265,7 @@ abstract contract Helpers is DSMath, Basic { ) ) ); + approve(TokenInterface(params.sellToken), params.market, sellAmt_); CometInterface(params.market).buyCollateral( params.buyAsset, From 611ff8a3e7b4e275d2eb8cce4d61e787f2cadb95 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:20:02 +0530 Subject: [PATCH 59/75] fixes --- .../connectors/compound/v3/helpers.sol | 25 +++------- .../mainnet/connectors/compound/v3/main.sol | 47 +++++-------------- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 79a15cbd..52472e48 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -28,8 +28,7 @@ abstract contract Helpers is DSMath, Basic { enum Action { REPAY, - DEPOSIT, - TRANSFER + DEPOSIT } function getBaseToken(address market) @@ -52,11 +51,7 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - if (from == address(this)) { - CometInterface(market).withdrawTo(to, token, amt); - } else { - CometInterface(market).withdrawFrom(from, to, token, amt); - } + CometInterface(market).withdrawFrom(from, to, token, amt); } function _borrow(BorrowWithdrawParams memory params) @@ -173,11 +168,7 @@ abstract contract Helpers is DSMath, Basic { address to, uint256 amt ) internal { - if (from == address(0)) { - CometInterface(market).transferAsset(to, token, amt); - } else { - CometInterface(market).transferAssetFrom(from, to, token, amt); - } + CometInterface(market).transferAssetFrom(from, to, token, amt); } function _getAccountSupplyBalanceOfAsset( @@ -213,13 +204,9 @@ abstract contract Helpers is DSMath, Basic { } else if (action == Action.DEPOSIT) { if (isEth) bal_ = src.balance; else bal_ = TokenInterface(token).balanceOf(src); - } else if (action == Action.TRANSFER) { - bal_ = (token == getBaseToken(market)) - ? TokenInterface(market).balanceOf(src) - : CometInterface(market).userCollateral(src, token).balance; - } - if (action == Action.TRANSFER) amt = bal_; - else amt = bal_ < allowance_ ? bal_ : allowance_; + } + + amt = bal_ < allowance_ ? bal_ : allowance_; } if (src == address(this)) convertEthToWeth(isEth, TokenInterface(token), amt); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 532d1a5d..688080d9 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -287,7 +287,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { BorrowWithdrawParams({ market: market, token: token, - from: address(0), + from: address(this), to: to, amt: amt, getId: getId, @@ -407,12 +407,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - if (token_ == getBaseToken(market)) { - require( - CometInterface(market).balanceOf(address(this)) == 0, - "borrow-disabled-when-supplied-base" - ); - } + require( + CometInterface(market).balanceOf(address(this)) == 0, + "borrow-disabled-when-supplied-base" + ); uint256 initialBal = CometInterface(market).borrowBalanceOf( address(this) @@ -464,7 +462,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { BorrowWithdrawParams({ market: market, token: token, - from: address(0), + from: address(this), to: to, amt: amt, getId: getId, @@ -804,23 +802,11 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token/to address" ); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); + address token_ = token == ethAddr ? wethAddr : token; - convertEthToWeth(isEth, tokenContract, amt_); + amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(address(this)) : amt_; - amt_ = amt_ == uint256(-1) - ? ( - (token_ == getBaseToken(market)) - ? TokenInterface(market).balanceOf(address(this)) - : CometInterface(market) - .userCollateral(address(this), token_) - .balance - ) - : amt_; - - _transfer(market, token_, address(0), dest, amt_); + CometInterface(market).transferAssetFrom(address(this), dest, token_, amt); setUint(setId, amt_); @@ -858,20 +844,11 @@ abstract contract CompoundV3Resolver is Events, Helpers { "invalid market/token/to address" ); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); + address token_ = token == ethAddr ? wethAddr : token; - amt_ = _calculateFromAmount( - market, - token_, - src, - amt_, - isEth, - Action.TRANSFER - ); + amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(src) : amt_; - _transfer(market, token_, src, dest, amt_); + CometInterface(market).transferAssetFrom(src, dest, token_, amt_); setUint(setId, amt_); From 745f7c6a61376a38c0ea079268eea190394743dc Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:22:14 +0530 Subject: [PATCH 60/75] fixes --- .../mainnet/connectors/compound/v3/main.sol | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 688080d9..4b2c1ecf 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -717,14 +717,21 @@ abstract contract CompoundV3Resolver is Events, Helpers { TokenInterface tokenContract = TokenInterface(token_); - amt_ = _calculateFromAmount( - market, - token_, - from, - amt_, - isEth, - Action.REPAY - ); + if (amt_ == uint256(-1)) { + amt_ = _calculateFromAmount( + market, + token_, + from, + amt_, + isEth, + Action.REPAY + ); + } else { + require( + amt_ <= borrowedBalance_, + "withdraw-amt-greater-than-supplies" + ); + } uint256 supplyBalance_ = CometInterface(market).balanceOf(to); require(supplyBalance_ == 0, "cannot-repay-when-supplied"); From c40aa080e085bce314dea79193e671d7082951c3 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:24:52 +0530 Subject: [PATCH 61/75] minor fix --- contracts/mainnet/connectors/compound/v3/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 4b2c1ecf..bbf0c667 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -594,7 +594,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); if (amt_ == uint256(-1)) { - amt_ = initialBal; + amt_ = borrowedBalance_; } else { require( amt_ <= borrowedBalance_, From 9b3e5b6c0bbce466b1f1cb4e81fb13db78186479 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 01:29:21 +0530 Subject: [PATCH 62/75] minor changes --- .../mainnet/connectors/compound/v3/events.sol | 7 ++- .../connectors/compound/v3/helpers.sol | 24 ++++---- .../mainnet/connectors/compound/v3/main.sol | 57 +++++++++++-------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index 3ec68d11..6de419f6 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -97,12 +97,13 @@ contract Events { uint256 setId ); - event LogPayback(address indexed market, uint256 tokenAmt, uint256 setId); + event LogPayback(address indexed market, uint256 tokenAmt, uint256 getId, uint256 setId); event LogPaybackOnBehalf( address indexed market, address to, uint256 tokenAmt, + uint256 getId, uint256 setId ); @@ -111,6 +112,7 @@ contract Events { address from, address to, uint256 tokenAmt, + uint256 getId, uint256 setId ); @@ -127,14 +129,13 @@ contract Events { event LogTransferAsset( address indexed market, address token, - address indexed from, address indexed dest, uint256 amount, uint256 getId, uint256 setId ); - event LogTransferAssetFromUsingManager( + event LogTransferAssetOnBehalf( address indexed market, address token, address indexed from, diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 985685db..bb162c2c 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -82,7 +82,7 @@ abstract contract Helpers is DSMath, Basic { ); require( - TokenInterface(params.market).balanceOf(params.from) == 0, + CometInterface(params.market).balanceOf(params.from) == 0, "borrow-disabled-when-supplied-base" ); @@ -127,22 +127,16 @@ abstract contract Helpers is DSMath, Basic { token_ ); amt_ = amt_ == uint256(-1) ? initialBal : amt_; - if (token_ == getBaseToken(params.market)) { - uint256 balance = CometInterface(params.market).balanceOf( - params.from - ); //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. - if (balance > 0) { - require(amt_ <= balance, "withdraw-amt-greater-than-supplies"); - } + require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); + //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( CometInterface(params.market).borrowBalanceOf(params.from) == 0, "withdraw-disabled-for-zero-supplies" ); } - _withdrawHelper(params.market, token_, params.from, params.to, amt_); uint256 finalBal = _getAccountSupplyBalanceOfAsset( @@ -261,6 +255,10 @@ abstract contract Helpers is DSMath, Basic { ) ) ); + + uint256 initialCollBal_ = CometInterface(params.market) + .userCollateral(address(this), params.buyAsset) + .balance; approve(TokenInterface(params.sellToken), params.market, sellAmt_); CometInterface(params.market).buyCollateral( params.buyAsset, @@ -268,11 +266,11 @@ abstract contract Helpers is DSMath, Basic { sellAmt_, address(this) ); + uint256 finalCollBal_ = CometInterface(params.market) + .userCollateral(address(this), params.buyAsset) + .balance; - uint256 buyAmt_ = CometInterface(params.market).quoteCollateral( - params.buyAsset, - sellAmt_ - ); + uint256 buyAmt_ = sub(finalCollBal_, initialCollBal_); require(slippageAmt_ <= buyAmt_, "too-much-slippage"); convertWethToEth(isEth, TokenInterface(params.buyAsset), buyAmt_); diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 532d1a5d..56eb11e3 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -223,24 +223,17 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_ ); + amt_ = amt_ == uint256(-1) ? initialBal : amt_; if (token_ == getBaseToken(market)) { //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. - if (amt_ == uint256(-1)) { - amt_ = initialBal; - } else { - require( - amt_ <= initialBal, - "withdraw-amt-greater-than-supplies" - ); - } + + require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( CometInterface(market).borrowBalanceOf(address(this)) == 0, "withdraw-disabled-for-zero-supplies" ); - } else { - amt_ = amt_ == uint256(-1) ? initialBal : amt_; } CometInterface(market).withdraw(token_, amt_); @@ -596,15 +589,15 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); if (amt_ == uint256(-1)) { - amt_ = initialBal; + amt_ = borrowedBalance_; } else { require( amt_ <= borrowedBalance_, - "withdraw-amt-greater-than-supplies" + "payback-amt-greater-than-borrows" ); } - //if supply balance > 0, there are no borrowing so no repay, withdraw instead. + //if supply balance > 0, there are no borrowing so no repay, supply instead. require( CometInterface(market).balanceOf(address(this)) == 0, "cannot-repay-when-supplied" @@ -617,8 +610,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); - eventName_ = "LogPayback(address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token_, amt_, getId, setId); + eventName_ = "LogPayback(address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, amt_, getId, setId); } /** @@ -658,15 +651,15 @@ abstract contract CompoundV3Resolver is Events, Helpers { uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); if (amt_ == uint256(-1)) { - amt_ = initialBal; + amt_ = borrowedBalance_; } else { require( amt_ <= borrowedBalance_, - "withdraw-amt-greater-than-supplies" + "payback-amt-greater-than-borrows" ); } - //if supply balance > 0, there are no borrowing so no repay, withdraw instead. + //if supply balance > 0, there are no borrowing so no repay, supply instead. require( CometInterface(market).balanceOf(to) == 0, "cannot-repay-when-supplied" @@ -679,8 +672,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { setUint(setId, amt_); - eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token_, to, amt_, getId, setId); + eventName_ = "LogPaybackOnBehalf(address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, to, amt_, getId, setId); } /** @@ -728,15 +721,29 @@ abstract contract CompoundV3Resolver is Events, Helpers { Action.REPAY ); - uint256 supplyBalance_ = CometInterface(market).balanceOf(to); - require(supplyBalance_ == 0, "cannot-repay-when-supplied"); + uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); + + if (amt_ == uint256(-1)) { + amt_ = borrowedBalance_; + } else { + require( + amt_ <= borrowedBalance_, + "payback-amt-greater-than-borrows" + ); + } + + //if supply balance > 0, there are no borrowing so no repay, withdraw instead. + require( + CometInterface(market).balanceOf(to) == 0, + "cannot-repay-when-supplied" + ); CometInterface(market).supplyFrom(from, to, token_, amt_); setUint(setId, amt_); - eventName_ = "LogPaybackFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; - eventParam_ = abi.encode(market, token_, from, to, amt_, getId, setId); + eventName_ = "LogPaybackFromUsingManager(address,address,address,uint256,uint256,uint256)"; + eventParam_ = abi.encode(market, from, to, amt_, getId, setId); } /** @@ -936,8 +943,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { owner, manager, isAllowed, - nonce, expiry, + nonce, v, r, s From d8c17a81b52f9ef4923bff164d47047e809354c7 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:41:16 +0530 Subject: [PATCH 63/75] minor fix --- contracts/mainnet/connectors/compound/v3/main.sol | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 73671494..80d67842 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -223,17 +223,21 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_ ); - amt_ = amt_ == uint256(-1) ? initialBal : amt_; if (token_ == getBaseToken(market)) { - //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. - - require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); + if (amt_ == uint256(-1)) { + amt_ = initialBal; + } else { + //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. + require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies"); + } //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( CometInterface(market).borrowBalanceOf(address(this)) == 0, "withdraw-disabled-for-zero-supplies" ); + } else { + amt_ = amt_ == uint256(-1) ? initialBal : amt_; } CometInterface(market).withdraw(token_, amt_); From a4bf92bdbb44be69437664a129498cbceddf28d5 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:51:05 +0530 Subject: [PATCH 64/75] minor fixes --- .../mainnet/connectors/compound/v3/main.sol | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 80d67842..9cdfe2b6 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -157,10 +157,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { market != address(0) && token != address(0) && to != address(0), "invalid market/token/to address" ); + require(from != address(this), "from-cannot-be-address(this)-use-paybackOnBehalf"); - bool isEth = token == ethAddr; - address token_ = isEth ? wethAddr : token; - TokenInterface tokenContract = TokenInterface(token_); + address token_ = token == ethAddr ? wethAddr : token; if (token_ == getBaseToken(market)) { require( @@ -707,12 +706,10 @@ abstract contract CompoundV3Resolver is Events, Helpers { market != address(0) && token != address(0) && to != address(0), "invalid market/token/to address" ); + require(from != address(this), "from-cannot-be-address(this)-use-paybackOnBehalf"); address token_ = getBaseToken(market); - bool isEth = token == ethAddr; - require(token == token_ || isEth, "invalid-token"); - - TokenInterface tokenContract = TokenInterface(token_); + require(token == token_ || token == ethAddr, "invalid-token"); if (amt_ == uint256(-1)) { amt_ = _calculateFromAmount( @@ -724,17 +721,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { Action.REPAY ); } else { - require( - amt_ <= borrowedBalance_, - "withdraw-amt-greater-than-supplies" - ); - } - - uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); - - if (amt_ == uint256(-1)) { - amt_ = borrowedBalance_; - } else { + uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); require( amt_ <= borrowedBalance_, "payback-amt-greater-than-borrows" From 15b978a577e4ff3e95d35e09af5b0834b4dddd49 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:56:13 +0530 Subject: [PATCH 65/75] fixed buyCollateral function --- contracts/mainnet/connectors/compound/v3/helpers.sol | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 6ab86013..466a2688 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -253,9 +253,7 @@ abstract contract Helpers is DSMath, Basic { ) ); - uint256 initialCollBal_ = CometInterface(params.market) - .userCollateral(address(this), params.buyAsset) - .balance; + uint256 initialCollBal_ = TokenInterface(params.buyAsset).balanceOf(address(this)); approve(TokenInterface(params.sellToken), params.market, sellAmt_); CometInterface(params.market).buyCollateral( @@ -264,9 +262,8 @@ abstract contract Helpers is DSMath, Basic { sellAmt_, address(this) ); - uint256 finalCollBal_ = CometInterface(params.market) - .userCollateral(address(this), params.buyAsset) - .balance; + + uint256 finalCollBal_ = TokenInterface(params.buyAsset).balanceOf(address(this)); uint256 buyAmt_ = sub(finalCollBal_, initialCollBal_); require(slippageAmt_ <= buyAmt_, "too-much-slippage"); From c4c44a9b2be733fbd649ee4718ef8e577ad06c6b Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:58:07 +0530 Subject: [PATCH 66/75] minor fix --- contracts/mainnet/connectors/compound/v3/helpers.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 466a2688..b43c9974 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -123,7 +123,7 @@ abstract contract Helpers is DSMath, Basic { token_ ); - if (token_ == getBaseToken(market)) { + if (token_ == getBaseToken(params.market)) { //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. if (amt_ == uint256(-1)) { amt_ = initialBal; @@ -262,7 +262,7 @@ abstract contract Helpers is DSMath, Basic { sellAmt_, address(this) ); - + uint256 finalCollBal_ = TokenInterface(params.buyAsset).balanceOf(address(this)); uint256 buyAmt_ = sub(finalCollBal_, initialCollBal_); From 8bf26ed1cd5e5bdc1f2fb794c3b08a5676ee120d Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:58:42 +0530 Subject: [PATCH 67/75] fix --- contracts/mainnet/connectors/compound/v3/helpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index b43c9974..9b4ac387 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -136,7 +136,7 @@ abstract contract Helpers is DSMath, Basic { //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( - CometInterface(market).borrowBalanceOf(params.from) == 0, + CometInterface(params.market).borrowBalanceOf(params.from) == 0, "withdraw-disabled-for-zero-supplies" ); } else { From 3b8aa3f0a1d4a6c81ddf08bece4d157a5a7f5e7d Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 01:59:30 +0530 Subject: [PATCH 68/75] fix --- contracts/mainnet/connectors/compound/v3/helpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 9b4ac387..80f57e65 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -178,7 +178,7 @@ abstract contract Helpers is DSMath, Basic { ) internal returns (uint256 balance) { if (asset == getBaseToken(market)) { //balance in base - balance = TokenInterface(market).balanceOf(account); + balance = CometInterface(market).balanceOf(account); } else { //balance in asset denomination balance = uint256( From bfe984d5eca13e1e03a5165592545396a00cc98b Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 02:00:57 +0530 Subject: [PATCH 69/75] removed convert eth condition --- contracts/mainnet/connectors/compound/v3/helpers.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 80f57e65..a3c36a8f 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -208,8 +208,6 @@ abstract contract Helpers is DSMath, Basic { amt = bal_ < allowance_ ? bal_ : allowance_; } - if (src == address(this)) - convertEthToWeth(isEth, TokenInterface(token), amt); return amt; } From 6cff35529b32cd6c84c35cad4fa54adc38d8dfc3 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 02:01:29 +0530 Subject: [PATCH 70/75] convert _calculateFromAmount to view function --- contracts/mainnet/connectors/compound/v3/helpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index a3c36a8f..82ecf7c0 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -194,7 +194,7 @@ abstract contract Helpers is DSMath, Basic { uint256 amt, bool isEth, Action action - ) internal returns (uint256) { + ) internal view returns (uint256) { if (amt == uint256(-1)) { uint256 allowance_ = TokenInterface(token).allowance(src, market); uint256 bal_; From bd1dcf5ddc091c497aa7115e030587f2d09c6f13 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 02:06:46 +0530 Subject: [PATCH 71/75] fixes --- .../connectors/compound/v3/helpers.sol | 47 +++++++------------ .../mainnet/connectors/compound/v3/main.sol | 12 ++--- test/mainnet/compound/compound.iii.test.ts | 20 -------- 3 files changed, 22 insertions(+), 57 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 6ab86013..3c1a3268 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -39,21 +39,6 @@ abstract contract Helpers is DSMath, Basic { baseToken = CometInterface(market).baseToken(); } - /** - *@dev helper function for following withdraw or borrow cases: - *withdrawFrom - for `withdrawFromUsingManager` withdraws from src to dest using DSA as manager - *withdrawTo - for `withdrawTo` withdraws from DSA to dest address. - */ - function _withdrawHelper( - address market, - address token, - address from, - address to, - uint256 amt - ) internal { - CometInterface(market).withdrawFrom(from, to, token, amt); - } - function _borrow(BorrowWithdrawParams memory params) internal returns (uint256 amt, uint256 setId) @@ -82,7 +67,12 @@ abstract contract Helpers is DSMath, Basic { params.from ); - CometInterface(params.market).withdrawFrom(params.from, params.to, token_, amt_); + CometInterface(params.market).withdrawFrom( + params.from, + params.to, + token_, + amt_ + ); uint256 finalBal = CometInterface(params.market).borrowBalanceOf( params.from @@ -123,7 +113,7 @@ abstract contract Helpers is DSMath, Basic { token_ ); - if (token_ == getBaseToken(market)) { + if (token_ == getBaseToken(params.market)) { //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw. if (amt_ == uint256(-1)) { amt_ = initialBal; @@ -136,14 +126,19 @@ abstract contract Helpers is DSMath, Basic { //if borrow balance > 0, there are no supplies so no withdraw, borrow instead. require( - CometInterface(market).borrowBalanceOf(params.from) == 0, + CometInterface(params.market).borrowBalanceOf(params.from) == 0, "withdraw-disabled-for-zero-supplies" ); } else { amt_ = amt_ == uint256(-1) ? initialBal : amt_; } - CometInterface(params.market).withdrawFrom(params.from, params.to, token_, amt_); + CometInterface(params.market).withdrawFrom( + params.from, + params.to, + token_, + amt_ + ); uint256 finalBal = _getAccountSupplyBalanceOfAsset( params.from, @@ -161,16 +156,6 @@ abstract contract Helpers is DSMath, Basic { setId = params.setId; } - function _transfer( - address market, - address token, - address from, - address to, - uint256 amt - ) internal { - CometInterface(market).transferAssetFrom(from, to, token, amt); - } - function _getAccountSupplyBalanceOfAsset( address account, address market, @@ -204,8 +189,8 @@ abstract contract Helpers is DSMath, Basic { } else if (action == Action.DEPOSIT) { if (isEth) bal_ = src.balance; else bal_ = TokenInterface(token).balanceOf(src); - } - + } + amt = bal_ < allowance_ ? bal_ : allowance_; } if (src == address(this)) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 9cdfe2b6..0de0d544 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -157,7 +157,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { market != address(0) && token != address(0) && to != address(0), "invalid market/token/to address" ); - require(from != address(this), "from-cannot-be-address(this)-use-paybackOnBehalf"); + require(from != address(this), "from-cannot-be-address(this)-use-depositOnBehalf"); address token_ = token == ethAddr ? wethAddr : token; @@ -173,7 +173,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_, from, amt_, - isEth, + token == ethAddr, Action.DEPOSIT ); @@ -717,7 +717,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_, from, amt_, - isEth, + token == ethAddr, Action.REPAY ); } else { @@ -809,9 +809,9 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = token == ethAddr ? wethAddr : token; - amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(address(this)) : amt_; + amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(address(this), market, token) : amt_; - CometInterface(market).transferAssetFrom(address(this), dest, token_, amt); + CometInterface(market).transferAssetFrom(address(this), dest, token_, amt_); setUint(setId, amt_); @@ -851,7 +851,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { address token_ = token == ethAddr ? wethAddr : token; - amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(src) : amt_; + amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(src, market, token) : amt_; CometInterface(market).transferAssetFrom(src, dest, token_, amt_); diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index f36b66cf..3d60c914 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -557,26 +557,6 @@ describe("Compound III", function () { expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); }); - it("should deposit eth using manager same as 'from'", async function () { - const amount = ethers.utils.parseEther("1"); - await wethContract.connect(dsa0Signer).approve(market, amount); - let initialBal = await ethers.provider.getBalance(dsaWallet0.address); - - const spells = [ - { - connector: connectorName, - method: "depositFromUsingManager", - args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] - } - ]; - - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseEther("2") - ); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); - }); it("should allow manager for dsaWallet0's collateral", async function () { const spells = [ { From 5e1cc0a7a5836246d19489f5fbf5568e2fedb1a5 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 3 Sep 2022 02:09:51 +0530 Subject: [PATCH 72/75] fix --- contracts/mainnet/connectors/compound/v3/main.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 9cdfe2b6..e26cae35 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -159,7 +159,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { ); require(from != address(this), "from-cannot-be-address(this)-use-paybackOnBehalf"); - address token_ = token == ethAddr ? wethAddr : token; + bool isEth = token == ethAddr; + address token_ = isEth? wethAddr : token; if (token_ == getBaseToken(market)) { require( @@ -709,7 +710,8 @@ abstract contract CompoundV3Resolver is Events, Helpers { require(from != address(this), "from-cannot-be-address(this)-use-paybackOnBehalf"); address token_ = getBaseToken(market); - require(token == token_ || token == ethAddr, "invalid-token"); + bool isEth = token == ethAddr; + require(token == token_ || isEth, "invalid-token"); if (amt_ == uint256(-1)) { amt_ = _calculateFromAmount( From 30e0562707337a0fe5f25be6b0396a47872356c1 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 02:20:31 +0530 Subject: [PATCH 73/75] event name update --- contracts/mainnet/connectors/compound/v3/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 2edb5b48..f4522667 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -370,7 +370,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { }) ); - eventName_ = "LogWithdrawOnBehalf(address,address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogWithdrawOnBehalfAndTransfer(address,address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId_); } From f8f57d6b18cf8a2bace6b4d7a7c06a4072272e64 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 02:30:38 +0530 Subject: [PATCH 74/75] fix --- contracts/mainnet/connectors/compound/v3/main.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index f4522667..82f2d8d6 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -174,7 +174,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_, from, amt_, - token == ethAddr, + isEth, Action.DEPOSIT ); @@ -719,7 +719,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { token_, from, amt_, - token == ethAddr, + isEth, Action.REPAY ); } else { From c6c3244c1ff69720b9eb20b4e429d666acaf3360 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 3 Sep 2022 03:00:13 +0530 Subject: [PATCH 75/75] isEth fix --- .../mainnet/connectors/compound/v3/helpers.sol | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 5942d72d..9043605d 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -207,14 +207,15 @@ abstract contract Helpers is DSMath, Basic { params.market != address(0) && params.buyAsset != address(0), "invalid market/token address" ); + + bool isEth = params.sellToken == ethAddr; + params.sellToken = isEth ? wethAddr : params.sellToken; + require( params.sellToken == getBaseToken(params.market), "invalid-sell-token" ); - bool isEth = params.sellToken == ethAddr; - params.sellToken = isEth ? wethAddr : params.sellToken; - if (sellAmt_ == uint256(-1)) { sellAmt_ = isEth ? address(this).balance @@ -236,7 +237,9 @@ abstract contract Helpers is DSMath, Basic { ) ); - uint256 initialCollBal_ = TokenInterface(params.buyAsset).balanceOf(address(this)); + uint256 initialCollBal_ = TokenInterface(params.buyAsset).balanceOf( + address(this) + ); approve(TokenInterface(params.sellToken), params.market, sellAmt_); CometInterface(params.market).buyCollateral( @@ -246,7 +249,9 @@ abstract contract Helpers is DSMath, Basic { address(this) ); - uint256 finalCollBal_ = TokenInterface(params.buyAsset).balanceOf(address(this)); + uint256 finalCollBal_ = TokenInterface(params.buyAsset).balanceOf( + address(this) + ); uint256 buyAmt_ = sub(finalCollBal_, initialCollBal_); require(slippageAmt_ <= buyAmt_, "too-much-slippage");