mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge branch 'compound-iii' into compound-v3-1
This commit is contained in:
commit
d4f6b0c332
|
@ -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)
|
||||
|
@ -77,16 +72,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(
|
||||
CometInterface(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,18 +122,28 @@ abstract contract Helpers is DSMath, Basic {
|
|||
params.market,
|
||||
token_
|
||||
);
|
||||
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
|
||||
if (token_ == getBaseToken(params.market)) {
|
||||
|
||||
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 {
|
||||
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,
|
||||
|
@ -162,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(
|
||||
|
@ -202,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);
|
||||
|
@ -239,12 +237,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(
|
||||
|
@ -259,6 +256,7 @@ 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,
|
||||
|
|
|
@ -280,7 +280,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
|||
BorrowWithdrawParams({
|
||||
market: market,
|
||||
token: token,
|
||||
from: address(0),
|
||||
from: address(this),
|
||||
to: to,
|
||||
amt: amt,
|
||||
getId: getId,
|
||||
|
@ -400,12 +400,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)
|
||||
|
@ -457,7 +455,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
|||
BorrowWithdrawParams({
|
||||
market: market,
|
||||
token: token,
|
||||
from: address(0),
|
||||
from: address(this),
|
||||
to: to,
|
||||
amt: amt,
|
||||
getId: getId,
|
||||
|
@ -712,14 +710,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 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to);
|
||||
|
||||
|
@ -811,23 +816,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_);
|
||||
|
||||
|
@ -865,20 +858,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_);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user