dsa-connectors/contracts/mainnet/connectors/compound/v3/main.sol

806 lines
23 KiB
Solidity
Raw Normal View History

2022-08-30 12:36:00 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
2022-09-01 12:55:16 +00:00
* @title Compound III
2022-08-30 12:36:00 +00:00
* @dev Lending & Borrowing.
*/
2022-09-01 16:20:18 +00:00
import { TokenInterface } from "../../../common/interfaces.sol";
2022-08-30 12:36:00 +00:00
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
import { CometInterface } from "./interface.sol";
2022-08-31 13:43:48 +00:00
abstract contract CompoundV3Resolver is Events, Helpers {
2022-08-30 12:36:00 +00:00
/**
2022-08-30 15:42:32 +00:00
* @dev Deposit base asset or collateral asset supported by the market.
2022-08-30 12:36:00 +00:00
* @notice Deposit a token to Compound for lending / collaterization.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 12:36:00 +00:00
* @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
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 12:36:00 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 12:36:00 +00:00
require(
market != address(0) && token != address(0),
"invalid market/token address"
);
2022-09-01 14:42:44 +00:00
bool isEth = token == ethAddr || token == wethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 12:36:00 +00:00
2022-09-01 12:51:24 +00:00
if (token_ == getBaseToken(market)) {
require(
CometInterface(market).borrowBalanceOf(address(this)) == 0,
"debt-not-repaid"
);
}
2022-08-30 12:36:00 +00:00
if (isEth) {
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1) ? address(this).balance : amt_;
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-31 12:46:12 +00:00
} else {
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1)
2022-08-31 12:46:12 +00:00
? tokenContract.balanceOf(address(this))
2022-08-31 13:43:48 +00:00
: amt_;
2022-08-30 12:36:00 +00:00
}
2022-08-31 14:48:32 +00:00
approve(tokenContract, market, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
CometInterface(market).supply(token_, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogDeposit(address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token, amt_, getId, setId);
2022-08-30 12:36:00 +00:00
}
2022-08-30 15:42:32 +00:00
/**
* @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'.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 15:42:32 +00:00
* @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE).
2022-08-30 18:55:37 +00:00
* @param to The address on behalf of which the supply is made.
2022-08-30 15:42:32 +00:00
* @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
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 15:42:32 +00:00
require(
market != address(0) && token != address(0),
"invalid market/token address"
);
2022-09-01 14:42:44 +00:00
bool isEth = token == ethAddr || token == wethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 15:42:32 +00:00
2022-09-01 12:51:24 +00:00
if (token_ == getBaseToken(market)) {
require(
CometInterface(market).borrowBalanceOf(to) == 0,
"debt-not-repaid"
);
}
2022-08-30 15:42:32 +00:00
if (isEth) {
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1) ? address(this).balance : amt_;
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-31 12:46:12 +00:00
} else {
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1)
2022-08-31 12:46:12 +00:00
? tokenContract.balanceOf(address(this))
2022-08-31 13:43:48 +00:00
: amt_;
2022-08-30 15:42:32 +00:00
}
2022-08-31 14:48:32 +00:00
approve(tokenContract, market, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
CometInterface(market).supplyTo(to, token_, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token, to, amt_, getId, setId);
2022-08-30 15:42:32 +00:00
}
/**
* @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'.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 15:42:32 +00:00
* @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
2022-08-30 18:55:37 +00:00
* @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.
2022-08-30 15:42:32 +00:00
* @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.
*/
2022-08-31 19:49:13 +00:00
function depositFromUsingManager(
2022-08-30 15:42:32 +00:00
address market,
address token,
address from,
2022-08-30 18:55:37 +00:00
address to,
2022-08-30 15:42:32 +00:00
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 15:42:32 +00:00
require(
market != address(0) && token != address(0),
"invalid market/token address"
);
2022-09-01 14:42:44 +00:00
bool isEth = token == ethAddr || token == wethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 15:42:32 +00:00
2022-09-01 12:51:24 +00:00
if (token_ == getBaseToken(market)) {
require(
CometInterface(market).borrowBalanceOf(to) == 0,
"debt-not-repaid"
);
}
2022-09-01 14:35:00 +00:00
amt_ = setAmt(market, token_, from, amt_, isEth, false);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
CometInterface(market).supplyFrom(from, to, token_, amt_);
setUint(setId, amt_);
2022-08-30 15:42:32 +00:00
2022-09-01 10:40:38 +00:00
eventName_ = "LogDepositFromUsingManager(address,address,address,address,uint256,uint256,uint256)";
2022-08-31 13:43:48 +00:00
eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId);
2022-08-30 15:42:32 +00:00
}
2022-08-30 12:36:00 +00:00
/**
2022-08-31 13:43:48 +00:00
* @dev Withdraw base/collateral asset.
2022-08-30 12:36:00 +00:00
* @notice Withdraw base token or deposited token from Compound.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 12:36:00 +00:00
* @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
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 12:36:00 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 12:36:00 +00:00
require(
market != address(0) && token != address(0),
"invalid market/token address"
);
bool isEth = token == ethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 12:36:00 +00:00
uint256 initialBal = getAccountSupplyBalanceOfAsset(
address(this),
market,
2022-09-01 17:58:58 +00:00
token_
2022-08-30 12:36:00 +00:00
);
2022-08-31 08:59:59 +00:00
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
2022-08-31 12:46:12 +00:00
2022-09-01 12:51:24 +00:00
if (token_ == getBaseToken(market)) {
uint256 balance = CometInterface(market).balanceOf(address(this));
if (balance > 0) {
require(amt_ <= balance, "withdraw-amt-greater-than-supplies");
}
}
2022-08-31 13:43:48 +00:00
CometInterface(market).withdraw(token_, amt_);
2022-08-30 12:36:00 +00:00
uint256 finalBal = getAccountSupplyBalanceOfAsset(
address(this),
market,
2022-09-01 17:58:58 +00:00
token_
2022-08-30 12:36:00 +00:00
);
2022-08-31 19:49:13 +00:00
amt_ = sub(initialBal, finalBal);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
convertWethToEth(isEth, tokenContract, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogWithdraw(address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token, amt_, getId, setId);
2022-08-30 12:36:00 +00:00
}
/**
2022-08-31 13:43:48 +00:00
* @dev Withdraw base/collateral asset and transfer to 'to'.
2022-08-30 15:42:32 +00:00
* @notice Withdraw base token or deposited token from Compound on behalf of an address and transfer to 'to'.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 15:42:32 +00:00
* @param token The address of the token to be withdrawn. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
2022-08-30 18:55:37 +00:00
* @param to The address to which the borrowed assets are to be transferred.
2022-08-30 15:42:32 +00:00
* @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.
*/
2022-09-01 16:20:18 +00:00
function withdrawOnBehalf(
2022-08-30 15:42:32 +00:00
address market,
address token,
2022-08-30 18:55:37 +00:00
address to,
2022-08-30 15:42:32 +00:00
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-09-01 16:20:18 +00:00
(uint256 amt_, uint256 setId_) = _withdraw(
2022-08-30 23:24:15 +00:00
BorrowWithdrawParams({
market: market,
token: token,
from: address(0),
to: to,
amt: amt,
getId: getId,
setId: setId
2022-09-01 16:20:18 +00:00
})
2022-08-30 15:42:32 +00:00
);
2022-08-31 13:43:48 +00:00
eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token, to, amt_, getId, setId_);
2022-08-30 15:42:32 +00:00
}
/**
2022-08-31 13:43:48 +00:00
* @dev Withdraw base/collateral asset from an account and transfer to 'to'.
2022-08-30 15:42:32 +00:00
* @notice Withdraw base token or deposited token from Compound from an address and transfer to 'to'.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 15:42:32 +00:00
* @param token The address of the token to be withdrawn. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
2022-08-30 18:55:37 +00:00
* @param from The address from where asset is to be withdrawed.
* @param to The address to which the borrowed assets are to be transferred.
2022-08-30 15:42:32 +00:00
* @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.
*/
2022-08-31 19:49:13 +00:00
function withdrawFromUsingManager(
2022-08-30 15:42:32 +00:00
address market,
address token,
2022-08-30 18:55:37 +00:00
address from,
address to,
2022-08-30 15:42:32 +00:00
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-09-01 16:20:18 +00:00
(uint256 amt_, uint256 setId_) = _withdraw(
2022-08-30 23:24:15 +00:00
BorrowWithdrawParams({
market: market,
token: token,
from: from,
to: to,
amt: amt,
getId: getId,
setId: setId
2022-09-01 16:20:18 +00:00
})
2022-08-30 15:42:32 +00:00
);
2022-09-01 10:40:38 +00:00
eventName_ = "LogWithdrawFromUsingManager(address,address,address,address,uint256,uint256,uint256)";
2022-08-31 13:43:48 +00:00
eventParam_ = abi.encode(market, token, from, to, amt_, getId, setId_);
2022-08-30 15:42:32 +00:00
}
/**
* @dev Borrow base asset.
2022-08-31 13:43:48 +00:00
* @notice Borrow base token from Compound.
* @param market The address of the market.
2022-09-01 16:20:18 +00:00
* @param amt The amount of base token to borrow.
2022-08-30 12:36:00 +00:00
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens borrowed.
2022-08-30 12:36:00 +00:00
*/
function borrow(
address market,
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 12:36:00 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 12:36:00 +00:00
require(market != address(0), "invalid market address");
2022-09-01 14:35:00 +00:00
address token_ = getBaseToken(market);
bool isEth = token_ == wethAddr;
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 12:36:00 +00:00
2022-09-01 12:51:24 +00:00
if (token_ == getBaseToken(market)) {
uint256 balance = CometInterface(market).balanceOf(address(this));
require(balance == 0, "borrow-disabled-when-supplied-base");
}
2022-09-01 16:20:18 +00:00
uint256 initialBal = CometInterface(market).borrowBalanceOf(
address(this)
);
2022-08-31 13:43:48 +00:00
CometInterface(market).withdraw(token_, amt_);
2022-08-30 12:36:00 +00:00
2022-09-01 16:20:18 +00:00
uint256 finalBal = CometInterface(market).borrowBalanceOf(
address(this)
2022-08-30 12:36:00 +00:00
);
2022-09-01 16:20:18 +00:00
amt_ = sub(finalBal, initialBal);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
convertWethToEth(isEth, tokenContract, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogBorrow(address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, amt_, getId, setId);
2022-08-30 12:36:00 +00:00
}
2022-08-30 15:42:32 +00:00
/**
* @dev Borrow base asset and transfer to 'to' account.
2022-08-31 13:43:48 +00:00
* @notice Borrow base token from Compound on behalf of an address.
* @param market The address of the market.
2022-08-30 18:55:37 +00:00
* @param to The address to which the borrowed asset is transferred.
2022-08-30 15:42:32 +00:00
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens borrowed.
2022-08-30 15:42:32 +00:00
*/
function borrowOnBehalf(
address market,
2022-08-30 18:55:37 +00:00
address to,
2022-08-30 15:42:32 +00:00
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-09-01 16:20:18 +00:00
(uint256 amt_, uint256 setId_) = _borrow(
2022-08-30 23:24:15 +00:00
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
from: address(0),
to: to,
amt: amt,
getId: getId,
setId: setId
2022-09-01 16:20:18 +00:00
})
2022-08-30 15:42:32 +00:00
);
2022-08-31 13:43:48 +00:00
eventName_ = "LogBorrowOnBehalf(address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, to, amt_, getId, setId_);
2022-08-30 15:42:32 +00:00
}
/**
* @dev Borrow base asset from 'from' and transfer to 'to'.
2022-08-31 13:43:48 +00:00
* @notice Borrow base token or deposited token from Compound.
* @param market The address of the market.
2022-08-30 15:42:32 +00:00
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
2022-08-30 18:55:37 +00:00
* @param from The address from where asset is to be withdrawed.
* @param to The address to which the borrowed assets are to be transferred.
2022-08-30 15:42:32 +00:00
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens borrowed.
2022-08-30 15:42:32 +00:00
*/
2022-08-31 19:49:13 +00:00
function borrowFromUsingManager(
2022-08-30 15:42:32 +00:00
address market,
2022-08-30 18:55:37 +00:00
address from,
address to,
2022-08-30 15:42:32 +00:00
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-09-01 16:20:18 +00:00
(uint256 amt_, uint256 setId_) = _borrow(
2022-08-30 23:24:15 +00:00
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
from: from,
to: to,
amt: amt,
getId: getId,
setId: setId
2022-09-01 16:20:18 +00:00
})
2022-08-30 15:42:32 +00:00
);
2022-09-01 10:40:38 +00:00
eventName_ = "LogBorrowFromUsingManager(address,address,address,uint256,uint256,uint256)";
2022-08-31 13:43:48 +00:00
eventParam_ = abi.encode(market, from, to, amt_, getId, setId_);
2022-08-30 15:42:32 +00:00
}
2022-08-30 18:55:37 +00:00
/**
2022-08-31 14:48:32 +00:00
* @dev Repays the borrowed base asset.
* @notice Repays the borrow of the base asset.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-31 14:48:32 +00:00
* @param amt The amount to be repaid.
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens repaid.
2022-08-30 15:42:32 +00:00
*/
2022-08-31 14:48:32 +00:00
function payback(
address market,
uint256 amt,
uint256 getId,
uint256 setId
)
2022-08-30 12:36:00 +00:00
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 12:36:00 +00:00
{
2022-08-31 14:48:32 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 12:36:00 +00:00
require(market != address(0), "invalid market address");
2022-09-01 14:35:00 +00:00
address token_ = getBaseToken(market);
2022-09-01 14:48:40 +00:00
bool isEth = token_ == wethAddr;
2022-08-31 13:43:48 +00:00
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 12:36:00 +00:00
2022-08-31 14:48:32 +00:00
amt_ = amt_ == uint256(-1)
2022-09-01 16:20:18 +00:00
? CometInterface(market).borrowBalanceOf(address(this))
2022-08-31 14:48:32 +00:00
: amt_;
2022-09-01 12:51:24 +00:00
uint256 borrowBal = CometInterface(market).borrowBalanceOf(
address(this)
);
if (borrowBal > 0) {
require(amt_ <= borrowBal, "repay-amt-greater-than-debt");
}
2022-09-01 14:42:44 +00:00
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-31 13:43:48 +00:00
approve(tokenContract, market, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 14:48:32 +00:00
CometInterface(market).supply(token_, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 12:36:00 +00:00
2022-08-31 14:48:32 +00:00
eventName_ = "LogPayback(address,address,uint256,uint256,uint256)";
2022-09-01 14:48:40 +00:00
eventParam_ = abi.encode(market, token_, amt_, getId, setId);
2022-08-30 12:36:00 +00:00
}
2022-08-30 15:42:32 +00:00
2022-08-30 18:55:37 +00:00
/**
2022-08-30 15:42:32 +00:00
* @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'.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 18:55:37 +00:00
* @param to The address on behalf of which the borrow is to be repaid.
2022-08-31 14:48:32 +00:00
* @param amt The amount to be repaid.
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens repaid.
2022-08-30 15:42:32 +00:00
*/
2022-08-31 08:59:59 +00:00
function paybackOnBehalf(
2022-08-30 15:42:32 +00:00
address market,
address to,
2022-08-31 14:48:32 +00:00
uint256 amt,
uint256 getId,
2022-08-30 15:42:32 +00:00
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-08-31 14:48:32 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 15:42:32 +00:00
require(market != address(0), "invalid market address");
2022-09-01 14:35:00 +00:00
address token_ = getBaseToken(market);
2022-09-01 14:48:40 +00:00
bool isEth = token_ == wethAddr;
2022-08-31 13:43:48 +00:00
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 15:42:32 +00:00
2022-08-31 14:48:32 +00:00
amt_ = amt_ == uint256(-1)
2022-09-01 16:20:18 +00:00
? CometInterface(market).borrowBalanceOf(to)
2022-08-31 14:48:32 +00:00
: amt_;
2022-09-01 12:51:24 +00:00
uint256 borrowBal = CometInterface(market).borrowBalanceOf(to);
if (borrowBal > 0) {
require(amt_ <= borrowBal, "repay-amt-greater-than-debt");
}
2022-09-01 14:42:44 +00:00
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-31 13:43:48 +00:00
approve(tokenContract, market, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 14:48:32 +00:00
CometInterface(market).supplyTo(to, token_, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 15:42:32 +00:00
2022-08-31 14:48:32 +00:00
eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)";
2022-09-01 14:48:40 +00:00
eventParam_ = abi.encode(market, token_, to, amt_, getId, setId);
2022-08-30 15:42:32 +00:00
}
2022-08-30 18:55:37 +00:00
/**
2022-08-30 15:42:32 +00:00
* @dev Repays entire borrow of the base asset form 'from' on behalf of 'to'.
2022-09-01 12:51:24 +00:00
* @notice Repays an entire borrow of the base asset on behalf of 'to'. Approve the comet markey
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 18:55:37 +00:00
* @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.
2022-08-31 14:48:32 +00:00
* @param amt The amount to be repaid.
* @param getId ID to retrieve amt.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens repaid.
2022-08-30 15:42:32 +00:00
*/
2022-08-31 19:49:13 +00:00
function paybackFromUsingManager(
2022-08-30 15:42:32 +00:00
address market,
address from,
2022-08-30 18:55:37 +00:00
address to,
2022-08-31 14:48:32 +00:00
uint256 amt,
uint256 getId,
2022-08-30 15:42:32 +00:00
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 15:42:32 +00:00
{
2022-08-31 14:48:32 +00:00
uint256 amt_ = getUint(getId, amt);
2022-08-30 15:42:32 +00:00
require(market != address(0), "invalid market address");
2022-09-01 14:35:00 +00:00
address token_ = getBaseToken(market);
2022-09-01 14:48:40 +00:00
bool isEth = token_ == wethAddr;
2022-08-31 13:43:48 +00:00
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 15:42:32 +00:00
2022-09-01 14:35:00 +00:00
amt_ = setAmt(market, token_, from, amt_, isEth, true);
2022-09-01 12:51:24 +00:00
uint256 borrowBal = CometInterface(market).borrowBalanceOf(to);
if (borrowBal > 0) {
require(amt_ <= borrowBal, "repay-amt-greater-than-debt");
}
approve(tokenContract, market, amt_);
2022-08-31 14:48:32 +00:00
CometInterface(market).supplyFrom(from, to, token_, amt_);
2022-08-30 18:55:37 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 18:55:37 +00:00
2022-09-01 10:40:38 +00:00
eventName_ = "LogPaybackFromUsingManager(address,address,address,address,uint256,uint256,uint256)";
2022-09-01 14:48:40 +00:00
eventParam_ = abi.encode(market, token_, from, to, amt_, getId, setId);
2022-08-30 18:55:37 +00:00
}
2022-08-30 22:16:35 +00:00
/**
* @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.
2022-09-01 10:40:38 +00:00
* @param dest The address to transfer the purchased assets.
2022-08-30 22:16:35 +00:00
* @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.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of base tokens sold.
2022-08-30 22:16:35 +00:00
*/
2022-08-30 18:55:37 +00:00
function buyCollateral(
address market,
address asset,
address dest,
uint256 minCollateralAmt,
uint256 baseAmt,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 18:55:37 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, baseAmt);
2022-08-30 18:55:37 +00:00
bool isEth = asset == ethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : asset;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 18:55:37 +00:00
2022-09-01 14:42:44 +00:00
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-30 15:42:32 +00:00
2022-09-01 12:51:24 +00:00
approve(tokenContract, market, amt_);
2022-08-30 18:55:37 +00:00
CometInterface(market).buyCollateral(
asset,
minCollateralAmt,
2022-08-31 13:43:48 +00:00
amt_,
2022-08-30 18:55:37 +00:00
dest
);
2022-08-30 15:42:32 +00:00
2022-08-31 13:43:48 +00:00
uint256 collAmt = CometInterface(market).quoteCollateral(asset, amt_);
setUint(setId, amt_);
2022-08-30 18:55:37 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)";
eventParam_ = abi.encode(
2022-08-30 18:55:37 +00:00
market,
2022-09-01 14:48:40 +00:00
token_,
2022-09-01 10:40:38 +00:00
amt_,
2022-08-30 18:55:37 +00:00
minCollateralAmt,
collAmt,
getId,
setId
);
}
2022-08-30 22:16:35 +00:00
/**
2022-09-01 12:51:24 +00:00
* @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.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 22:16:35 +00:00
* @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.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens transferred.
2022-08-30 22:16:35 +00:00
*/
2022-08-30 21:56:25 +00:00
function transferAsset(
address market,
address token,
address dest,
uint256 amount,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 21:56:25 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amount);
2022-08-30 21:56:25 +00:00
require(
market != address(0) && token != address(0),
"invalid market address"
);
bool isEth = token == ethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 21:56:25 +00:00
2022-09-01 14:42:44 +00:00
convertEthToWeth(isEth, tokenContract, amt_);
2022-08-30 21:56:25 +00:00
2022-08-31 13:43:48 +00:00
amt_ = amt_ == uint256(-1)
2022-08-31 21:21:30 +00:00
? (
(token_ == getBaseToken(market))
? TokenInterface(market).balanceOf(address(this))
: CometInterface(market)
.userCollateral(address(this), token_)
.balance
)
2022-08-31 13:43:48 +00:00
: amt_;
2022-08-31 21:21:30 +00:00
2022-08-31 13:43:48 +00:00
_transfer(market, token_, address(0), dest, amt_);
2022-08-30 21:56:25 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 21:56:25 +00:00
2022-08-31 13:43:48 +00:00
eventName_ = "LogTransferAsset(address,address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token_, dest, amt_, getId, setId);
2022-08-30 21:56:25 +00:00
}
2022-08-30 22:16:35 +00:00
/**
2022-08-31 21:21:30 +00:00
* @dev Transfer collateral or base asset to dest address from src account.
2022-08-30 22:16:35 +00:00
* @notice Transfer collateral asset to dest address from src's account.
2022-08-31 13:43:48 +00:00
* @param market The address of the market.
2022-08-30 22:16:35 +00:00
* @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.
2022-08-31 13:43:48 +00:00
* @param setId ID stores the amount of tokens transferred.
2022-08-30 22:16:35 +00:00
*/
2022-08-31 19:49:13 +00:00
function transferAssetFromUsingManager(
2022-08-30 21:56:25 +00:00
address market,
address token,
address src,
address dest,
uint256 amount,
uint256 getId,
uint256 setId
)
external
payable
2022-08-31 13:43:48 +00:00
returns (string memory eventName_, bytes memory eventParam_)
2022-08-30 21:56:25 +00:00
{
2022-08-31 13:43:48 +00:00
uint256 amt_ = getUint(getId, amount);
2022-08-30 21:56:25 +00:00
require(market != address(0), "invalid market address");
bool isEth = token == ethAddr;
2022-08-31 13:43:48 +00:00
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
2022-08-30 21:56:25 +00:00
2022-09-01 14:35:00 +00:00
amt_ = setAmt(market, token_, src, amt_, isEth, false);
2022-08-30 21:56:25 +00:00
2022-08-31 13:43:48 +00:00
_transfer(market, token_, src, dest, amt_);
2022-08-30 21:56:25 +00:00
2022-08-31 13:43:48 +00:00
setUint(setId, amt_);
2022-08-30 21:56:25 +00:00
2022-09-01 10:40:38 +00:00
eventName_ = "LogTransferAssetFromUsingManager(address,address,address,address,uint256,uint256,uint256)";
2022-08-31 13:43:48 +00:00
eventParam_ = abi.encode(market, token_, src, dest, amt_, getId, setId);
2022-08-30 21:56:25 +00:00
}
2022-08-31 08:59:59 +00:00
/**
* @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.
*/
2022-08-31 18:39:55 +00:00
function toggleAccountManager(
2022-08-31 08:59:59 +00:00
address market,
address manager,
bool isAllowed
2022-08-31 13:43:48 +00:00
) external returns (string memory eventName_, bytes memory eventParam_) {
2022-08-31 08:59:59 +00:00
CometInterface(market).allow(manager, isAllowed);
2022-08-31 13:43:48 +00:00
eventName_ = "LogAllow(address,address,bool)";
eventParam_ = abi.encode(market, manager, isAllowed);
2022-08-31 08:59:59 +00:00
}
/**
* @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.
*/
2022-08-31 18:39:55 +00:00
function toggleAccountManagerWithPermit(
2022-08-31 08:59:59 +00:00
address market,
address owner,
address manager,
bool isAllowed,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
2022-08-31 13:43:48 +00:00
) external returns (string memory eventName_, bytes memory eventParam_) {
2022-08-31 08:59:59 +00:00
CometInterface(market).allowBySig(
owner,
manager,
isAllowed,
nonce,
expiry,
v,
r,
s
);
2022-09-01 16:46:10 +00:00
eventName_ = "LogAllowWithPermit(address,address,address,bool,uint256,uint256,uint8,bytes32,bytes32)";
2022-08-31 13:43:48 +00:00
eventParam_ = abi.encode(
2022-08-31 08:59:59 +00:00
market,
owner,
manager,
isAllowed,
nonce,
expiry,
v,
r,
s
);
}
2022-08-30 12:36:00 +00:00
}
2022-08-31 13:43:48 +00:00
contract ConnectV2CompoundV3 is CompoundV3Resolver {
string public name = "CompoundV3-v1.0";
2022-08-30 12:36:00 +00:00
}