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 {
|
enum Action {
|
||||||
REPAY,
|
REPAY,
|
||||||
DEPOSIT,
|
DEPOSIT
|
||||||
TRANSFER
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBaseToken(address market)
|
function getBaseToken(address market)
|
||||||
|
@ -52,12 +51,8 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
address to,
|
address to,
|
||||||
uint256 amt
|
uint256 amt
|
||||||
) internal {
|
) 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)
|
function _borrow(BorrowWithdrawParams memory params)
|
||||||
internal
|
internal
|
||||||
|
@ -77,16 +72,17 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
TokenInterface tokenContract = TokenInterface(token_);
|
||||||
|
|
||||||
params.from = params.from == address(0) ? address(this) : params.from;
|
params.from = params.from == address(0) ? address(this) : params.from;
|
||||||
uint256 initialBal = CometInterface(params.market).borrowBalanceOf(
|
|
||||||
params.from
|
|
||||||
);
|
|
||||||
|
|
||||||
require(
|
require(
|
||||||
CometInterface(params.market).balanceOf(params.from) == 0,
|
CometInterface(params.market).balanceOf(params.from) == 0,
|
||||||
"borrow-disabled-when-supplied-base"
|
"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(
|
uint256 finalBal = CometInterface(params.market).borrowBalanceOf(
|
||||||
params.from
|
params.from
|
||||||
|
@ -126,18 +122,28 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
params.market,
|
params.market,
|
||||||
token_
|
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.
|
//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.
|
//if borrow balance > 0, there are no supplies so no withdraw, borrow instead.
|
||||||
require(
|
require(
|
||||||
CometInterface(params.market).borrowBalanceOf(params.from) == 0,
|
CometInterface(market).borrowBalanceOf(params.from) == 0,
|
||||||
"withdraw-disabled-for-zero-supplies"
|
"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(
|
uint256 finalBal = _getAccountSupplyBalanceOfAsset(
|
||||||
params.from,
|
params.from,
|
||||||
|
@ -162,12 +168,8 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
address to,
|
address to,
|
||||||
uint256 amt
|
uint256 amt
|
||||||
) internal {
|
) 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(
|
function _getAccountSupplyBalanceOfAsset(
|
||||||
address account,
|
address account,
|
||||||
|
@ -202,13 +204,9 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
} else if (action == Action.DEPOSIT) {
|
} else if (action == Action.DEPOSIT) {
|
||||||
if (isEth) bal_ = src.balance;
|
if (isEth) bal_ = src.balance;
|
||||||
else bal_ = TokenInterface(token).balanceOf(src);
|
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))
|
if (src == address(this))
|
||||||
convertEthToWeth(isEth, TokenInterface(token), amt);
|
convertEthToWeth(isEth, TokenInterface(token), amt);
|
||||||
|
@ -239,12 +237,11 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
? address(this).balance
|
? address(this).balance
|
||||||
: TokenInterface(params.sellToken).balanceOf(address(this));
|
: TokenInterface(params.sellToken).balanceOf(address(this));
|
||||||
}
|
}
|
||||||
|
convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_);
|
||||||
|
|
||||||
isEth = params.buyAsset == ethAddr;
|
isEth = params.buyAsset == ethAddr;
|
||||||
params.buyAsset = isEth ? wethAddr : params.buyAsset;
|
params.buyAsset = isEth ? wethAddr : params.buyAsset;
|
||||||
|
|
||||||
convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_);
|
|
||||||
|
|
||||||
uint256 slippageAmt_ = convert18ToDec(
|
uint256 slippageAmt_ = convert18ToDec(
|
||||||
TokenInterface(params.buyAsset).decimals(),
|
TokenInterface(params.buyAsset).decimals(),
|
||||||
wmul(
|
wmul(
|
||||||
|
@ -259,6 +256,7 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
uint256 initialCollBal_ = CometInterface(params.market)
|
uint256 initialCollBal_ = CometInterface(params.market)
|
||||||
.userCollateral(address(this), params.buyAsset)
|
.userCollateral(address(this), params.buyAsset)
|
||||||
.balance;
|
.balance;
|
||||||
|
|
||||||
approve(TokenInterface(params.sellToken), params.market, sellAmt_);
|
approve(TokenInterface(params.sellToken), params.market, sellAmt_);
|
||||||
CometInterface(params.market).buyCollateral(
|
CometInterface(params.market).buyCollateral(
|
||||||
params.buyAsset,
|
params.buyAsset,
|
||||||
|
|
|
@ -280,7 +280,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
BorrowWithdrawParams({
|
BorrowWithdrawParams({
|
||||||
market: market,
|
market: market,
|
||||||
token: token,
|
token: token,
|
||||||
from: address(0),
|
from: address(this),
|
||||||
to: to,
|
to: to,
|
||||||
amt: amt,
|
amt: amt,
|
||||||
getId: getId,
|
getId: getId,
|
||||||
|
@ -400,12 +400,10 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
|
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
TokenInterface tokenContract = TokenInterface(token_);
|
||||||
|
|
||||||
if (token_ == getBaseToken(market)) {
|
|
||||||
require(
|
require(
|
||||||
CometInterface(market).balanceOf(address(this)) == 0,
|
CometInterface(market).balanceOf(address(this)) == 0,
|
||||||
"borrow-disabled-when-supplied-base"
|
"borrow-disabled-when-supplied-base"
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
uint256 initialBal = CometInterface(market).borrowBalanceOf(
|
uint256 initialBal = CometInterface(market).borrowBalanceOf(
|
||||||
address(this)
|
address(this)
|
||||||
|
@ -457,7 +455,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
BorrowWithdrawParams({
|
BorrowWithdrawParams({
|
||||||
market: market,
|
market: market,
|
||||||
token: token,
|
token: token,
|
||||||
from: address(0),
|
from: address(this),
|
||||||
to: to,
|
to: to,
|
||||||
amt: amt,
|
amt: amt,
|
||||||
getId: getId,
|
getId: getId,
|
||||||
|
@ -712,6 +710,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
|
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
TokenInterface tokenContract = TokenInterface(token_);
|
||||||
|
|
||||||
|
if (amt_ == uint256(-1)) {
|
||||||
amt_ = _calculateFromAmount(
|
amt_ = _calculateFromAmount(
|
||||||
market,
|
market,
|
||||||
token_,
|
token_,
|
||||||
|
@ -720,6 +719,12 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
isEth,
|
isEth,
|
||||||
Action.REPAY
|
Action.REPAY
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
require(
|
||||||
|
amt_ <= borrowedBalance_,
|
||||||
|
"withdraw-amt-greater-than-supplies"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to);
|
uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to);
|
||||||
|
|
||||||
|
@ -811,23 +816,11 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
"invalid market/token/to address"
|
"invalid market/token/to address"
|
||||||
);
|
);
|
||||||
|
|
||||||
bool isEth = token == ethAddr;
|
address token_ = token == ethAddr ? wethAddr : token;
|
||||||
address token_ = isEth ? wethAddr : token;
|
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
|
||||||
|
|
||||||
convertEthToWeth(isEth, tokenContract, amt_);
|
amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(address(this)) : amt_;
|
||||||
|
|
||||||
amt_ = amt_ == uint256(-1)
|
CometInterface(market).transferAssetFrom(address(this), dest, token_, amt);
|
||||||
? (
|
|
||||||
(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_);
|
setUint(setId, amt_);
|
||||||
|
|
||||||
|
@ -865,20 +858,11 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
"invalid market/token/to address"
|
"invalid market/token/to address"
|
||||||
);
|
);
|
||||||
|
|
||||||
bool isEth = token == ethAddr;
|
address token_ = token == ethAddr ? wethAddr : token;
|
||||||
address token_ = isEth ? wethAddr : token;
|
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
|
||||||
|
|
||||||
amt_ = _calculateFromAmount(
|
amt_ = amt_ == uint256(-1) ? _getAccountSupplyBalanceOfAsset(src) : amt_;
|
||||||
market,
|
|
||||||
token_,
|
|
||||||
src,
|
|
||||||
amt_,
|
|
||||||
isEth,
|
|
||||||
Action.TRANSFER
|
|
||||||
);
|
|
||||||
|
|
||||||
_transfer(market, token_, src, dest, amt_);
|
CometInterface(market).transferAssetFrom(src, dest, token_, amt_);
|
||||||
|
|
||||||
setUint(setId, amt_);
|
setUint(setId, amt_);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user