mirror of
https://github.com/Instadapp/dsa-connectors-old.git
synced 2024-07-29 22:47:46 +00:00
Modify compound
This commit is contained in:
parent
1f43eaa9e3
commit
f32526fb8e
|
@ -29,8 +29,6 @@ interface CTokenInterface {
|
||||||
|
|
||||||
function balanceOf(address owner) external view returns (uint256 balance);
|
function balanceOf(address owner) external view returns (uint256 balance);
|
||||||
function transferFrom(address, address, uint) external returns (bool);
|
function transferFrom(address, address, uint) external returns (bool);
|
||||||
|
|
||||||
function underlying() external view returns (address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CETHInterface {
|
interface CETHInterface {
|
||||||
|
@ -281,13 +279,6 @@ contract Helpers is DSMath {
|
||||||
// return 0xd0A1E359811322d97991E03f863a0C30C2cF029C; // Kovan WETH Address
|
// return 0xd0A1E359811322d97991E03f863a0C30C2cF029C; // Kovan WETH Address
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Return CEth address
|
|
||||||
*/
|
|
||||||
function getCethAddr() internal pure returns (address) {
|
|
||||||
return 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Return InstaDApp Mapping Address
|
* @dev Return InstaDApp Mapping Address
|
||||||
*/
|
*/
|
||||||
|
@ -530,19 +521,25 @@ contract Helpers is DSMath {
|
||||||
function getCtokenInterfaces(uint length, address[] memory tokens) internal view returns (CTokenInterface[] memory) {
|
function getCtokenInterfaces(uint length, address[] memory tokens) internal view returns (CTokenInterface[] memory) {
|
||||||
CTokenInterface[] memory _ctokens = new CTokenInterface[](length);
|
CTokenInterface[] memory _ctokens = new CTokenInterface[](length);
|
||||||
for (uint i = 0; i < length; i++) {
|
for (uint i = 0; i < length; i++) {
|
||||||
if (tokens[i] == getEthAddr()) {
|
|
||||||
_ctokens[i] = CTokenInterface(getCethAddr());
|
|
||||||
} else {
|
|
||||||
address _cToken = InstaMapping(getMappingAddr()).cTokenMapping(tokens[i]);
|
address _cToken = InstaMapping(getMappingAddr()).cTokenMapping(tokens[i]);
|
||||||
_ctokens[i] = CTokenInterface(_cToken);
|
_ctokens[i] = CTokenInterface(_cToken);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return _ctokens;
|
return _ctokens;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contract CompoundHelpers is Helpers {
|
contract CompoundHelpers is Helpers {
|
||||||
|
|
||||||
|
struct CompoundBorrowData {
|
||||||
|
uint length;
|
||||||
|
uint fee;
|
||||||
|
Protocol target;
|
||||||
|
CTokenInterface[] ctokens;
|
||||||
|
TokenInterface[] tokens;
|
||||||
|
uint[] amts;
|
||||||
|
uint[] rateModes;
|
||||||
|
}
|
||||||
|
|
||||||
function _compEnterMarkets(uint length, CTokenInterface[] memory ctokens) internal {
|
function _compEnterMarkets(uint length, CTokenInterface[] memory ctokens) internal {
|
||||||
ComptrollerInterface troller = ComptrollerInterface(getComptrollerAddress());
|
ComptrollerInterface troller = ComptrollerInterface(getComptrollerAddress());
|
||||||
address[] memory _cTokens = new address[](length);
|
address[] memory _cTokens = new address[](length);
|
||||||
|
@ -553,54 +550,57 @@ contract CompoundHelpers is Helpers {
|
||||||
troller.enterMarkets(_cTokens);
|
troller.enterMarkets(_cTokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _compBorrowOne(uint fee, CTokenInterface ctoken, uint amt, Protocol target, uint rateMode) internal returns (uint) {
|
function _compBorrowOne(
|
||||||
|
uint fee,
|
||||||
|
CTokenInterface ctoken,
|
||||||
|
TokenInterface token,
|
||||||
|
uint amt,
|
||||||
|
Protocol target,
|
||||||
|
uint rateMode
|
||||||
|
) internal returns (uint) {
|
||||||
if (amt > 0) {
|
if (amt > 0) {
|
||||||
|
|
||||||
address token = address(ctoken) == getCethAddr() ? getEthAddr() : ctoken.underlying();
|
|
||||||
|
|
||||||
if (amt == uint(-1)) {
|
if (amt == uint(-1)) {
|
||||||
amt = getMaxBorrow(target, token, rateMode);
|
amt = getMaxBorrow(target, address(token), rateMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint feeAmt = wmul(amt, fee);
|
uint feeAmt = wmul(amt, fee);
|
||||||
uint _amt = add(amt, feeAmt);
|
uint _amt = add(amt, feeAmt);
|
||||||
|
|
||||||
require(ctoken.borrow(_amt) == 0, "borrow-failed-collateral?");
|
require(ctoken.borrow(_amt) == 0, "borrow-failed-collateral?");
|
||||||
transferFees(token, feeAmt);
|
transferFees(address(token), feeAmt);
|
||||||
}
|
}
|
||||||
return amt;
|
return amt;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _compBorrow(
|
function _compBorrow(
|
||||||
uint length,
|
CompoundBorrowData memory data
|
||||||
uint fee,
|
|
||||||
Protocol target,
|
|
||||||
CTokenInterface[] memory ctokens,
|
|
||||||
uint[] memory amts,
|
|
||||||
uint[] memory rateModes
|
|
||||||
) internal returns (uint[] memory) {
|
) internal returns (uint[] memory) {
|
||||||
uint[] memory finalAmts = new uint[](length);
|
uint[] memory finalAmts = new uint[](data.length);
|
||||||
for (uint i = 0; i < length; i++) {
|
for (uint i = 0; i < data.length; i++) {
|
||||||
finalAmts[i] = _compBorrowOne(fee, ctokens[i], amts[i], target, rateModes[i]);
|
finalAmts[i] = _compBorrowOne(
|
||||||
|
data.fee,
|
||||||
|
data.ctokens[i],
|
||||||
|
data.tokens[i],
|
||||||
|
data.amts[i],
|
||||||
|
data.target,
|
||||||
|
data.rateModes[i]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return finalAmts;
|
return finalAmts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _compDepositOne(uint fee, CTokenInterface ctoken, uint amt) internal {
|
function _compDepositOne(uint fee, CTokenInterface ctoken, TokenInterface token, uint amt) internal {
|
||||||
if (amt > 0) {
|
if (amt > 0) {
|
||||||
address token = address(ctoken) == getCethAddr() ? getEthAddr() : ctoken.underlying();
|
|
||||||
|
|
||||||
uint feeAmt = wmul(amt, fee);
|
uint feeAmt = wmul(amt, fee);
|
||||||
uint _amt = sub(amt, feeAmt);
|
uint _amt = sub(amt, feeAmt);
|
||||||
|
|
||||||
if (token != getEthAddr()) {
|
if (address(token) != getEthAddr()) {
|
||||||
TokenInterface tokenContract = TokenInterface(token);
|
token.approve(address(ctoken), _amt);
|
||||||
tokenContract.approve(address(ctoken), _amt);
|
|
||||||
require(ctoken.mint(_amt) == 0, "deposit-failed");
|
require(ctoken.mint(_amt) == 0, "deposit-failed");
|
||||||
} else {
|
} else {
|
||||||
CETHInterface(getCethAddr()).mint.value(_amt)();
|
CETHInterface(address(ctoken)).mint.value(_amt)();
|
||||||
}
|
}
|
||||||
transferFees(token, feeAmt);
|
transferFees(address(token), feeAmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -608,10 +608,11 @@ contract CompoundHelpers is Helpers {
|
||||||
uint length,
|
uint length,
|
||||||
uint fee,
|
uint fee,
|
||||||
CTokenInterface[] memory ctokens,
|
CTokenInterface[] memory ctokens,
|
||||||
|
TokenInterface[] memory tokens,
|
||||||
uint[] memory amts
|
uint[] memory amts
|
||||||
) internal {
|
) internal {
|
||||||
for (uint i = 0; i < length; i++) {
|
for (uint i = 0; i < length; i++) {
|
||||||
_compDepositOne(fee, ctokens[i], amts[i]);
|
_compDepositOne(fee, ctokens[i], tokens[i], amts[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,20 +638,16 @@ contract CompoundHelpers is Helpers {
|
||||||
return finalAmts;
|
return finalAmts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _compPaybackOne(CTokenInterface ctoken, uint amt) internal returns (uint) {
|
function _compPaybackOne(CTokenInterface ctoken, TokenInterface token, uint amt) internal returns (uint) {
|
||||||
if (amt > 0) {
|
if (amt > 0) {
|
||||||
|
|
||||||
address token = address(ctoken) == getCethAddr() ? getEthAddr() : ctoken.underlying();
|
|
||||||
|
|
||||||
if (amt == uint(-1)) {
|
if (amt == uint(-1)) {
|
||||||
amt = ctoken.borrowBalanceCurrent(address(this));
|
amt = ctoken.borrowBalanceCurrent(address(this));
|
||||||
}
|
}
|
||||||
if (token != getEthAddr()) {
|
if (address(token) != getEthAddr()) {
|
||||||
TokenInterface tokenContract = TokenInterface(token);
|
token.approve(address(ctoken), amt);
|
||||||
tokenContract.approve(address(ctoken), amt);
|
|
||||||
require(ctoken.repayBorrow(amt) == 0, "repay-failed.");
|
require(ctoken.repayBorrow(amt) == 0, "repay-failed.");
|
||||||
} else {
|
} else {
|
||||||
CETHInterface(getCethAddr()).repayBorrow.value(amt)();
|
CETHInterface(address(ctoken)).repayBorrow.value(amt)();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return amt;
|
return amt;
|
||||||
|
@ -659,10 +656,11 @@ contract CompoundHelpers is Helpers {
|
||||||
function _compPayback(
|
function _compPayback(
|
||||||
uint length,
|
uint length,
|
||||||
CTokenInterface[] memory ctokens,
|
CTokenInterface[] memory ctokens,
|
||||||
|
TokenInterface[] memory tokens,
|
||||||
uint[] memory amts
|
uint[] memory amts
|
||||||
) internal {
|
) internal {
|
||||||
for (uint i = 0; i < length; i++) {
|
for (uint i = 0; i < length; i++) {
|
||||||
_compPaybackOne(ctokens[i], amts[i]);
|
_compPaybackOne(ctokens[i], tokens[i], amts[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1220,18 +1218,21 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
||||||
_compEnterMarkets(length, _ctokens);
|
_compEnterMarkets(length, _ctokens);
|
||||||
|
|
||||||
paybackAmts = _compBorrow(
|
CompoundBorrowData memory _compoundBorrowData;
|
||||||
length,
|
|
||||||
data.debtFee,
|
_compoundBorrowData.length = length;
|
||||||
data.target,
|
_compoundBorrowData.fee = data.debtFee;
|
||||||
_ctokens,
|
_compoundBorrowData.target = data.target;
|
||||||
data.borrowAmts,
|
_compoundBorrowData.ctokens = _ctokens;
|
||||||
data.borrowRateModes
|
_compoundBorrowData.tokens = tokens;
|
||||||
);
|
_compoundBorrowData.amts = data.borrowAmts;
|
||||||
|
_compoundBorrowData.rateModes = data.borrowRateModes;
|
||||||
|
|
||||||
|
paybackAmts = _compBorrow(_compoundBorrowData);
|
||||||
|
|
||||||
_aaveV1Payback(aaveV1, aaveCore, length, tokens, paybackAmts);
|
_aaveV1Payback(aaveV1, aaveCore, length, tokens, paybackAmts);
|
||||||
depositAmts = _aaveV1Withdraw(aaveV1, aaveCore, length, tokens, data.withdrawAmts);
|
depositAmts = _aaveV1Withdraw(aaveV1, aaveCore, length, tokens, data.withdrawAmts);
|
||||||
_compDeposit(length, data.collateralFee, _ctokens, depositAmts);
|
_compDeposit(length, data.collateralFee, _ctokens, tokens, depositAmts);
|
||||||
} else if (data.source == Protocol.AaveV2 && data.target == Protocol.Aave) {
|
} else if (data.source == Protocol.AaveV2 && data.target == Protocol.Aave) {
|
||||||
|
|
||||||
AaveV1BorrowData memory _aaveV1BorrowData;
|
AaveV1BorrowData memory _aaveV1BorrowData;
|
||||||
|
@ -1253,17 +1254,23 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
||||||
_compEnterMarkets(length, _ctokens);
|
_compEnterMarkets(length, _ctokens);
|
||||||
|
|
||||||
paybackAmts = _compBorrow(
|
{
|
||||||
length,
|
CompoundBorrowData memory _compoundBorrowData;
|
||||||
data.debtFee,
|
|
||||||
data.target,
|
_compoundBorrowData.length = length;
|
||||||
_ctokens,
|
_compoundBorrowData.fee = data.debtFee;
|
||||||
data.borrowAmts,
|
_compoundBorrowData.target = data.target;
|
||||||
data.borrowRateModes
|
_compoundBorrowData.ctokens = _ctokens;
|
||||||
);
|
_compoundBorrowData.tokens = tokens;
|
||||||
|
_compoundBorrowData.amts = data.borrowAmts;
|
||||||
|
_compoundBorrowData.rateModes = data.borrowRateModes;
|
||||||
|
|
||||||
|
paybackAmts = _compBorrow(_compoundBorrowData);
|
||||||
|
}
|
||||||
|
|
||||||
_aaveV2Payback(aaveV2, aaveData, length, tokens, paybackAmts, data.paybackRateModes);
|
_aaveV2Payback(aaveV2, aaveData, length, tokens, paybackAmts, data.paybackRateModes);
|
||||||
depositAmts = _aaveV2Withdraw(aaveV2, aaveData, length, tokens, data.withdrawAmts);
|
depositAmts = _aaveV2Withdraw(aaveV2, aaveData, length, tokens, data.withdrawAmts);
|
||||||
_compDeposit(length, data.collateralFee, _ctokens, depositAmts);
|
_compDeposit(length, data.collateralFee, _ctokens, tokens, depositAmts);
|
||||||
} else if (data.source == Protocol.Compound && data.target == Protocol.Aave) {
|
} else if (data.source == Protocol.Compound && data.target == Protocol.Aave) {
|
||||||
|
|
||||||
AaveV1BorrowData memory _aaveV1BorrowData;
|
AaveV1BorrowData memory _aaveV1BorrowData;
|
||||||
|
@ -1280,7 +1287,7 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
paybackAmts = _aaveV1Borrow(_aaveV1BorrowData);
|
paybackAmts = _aaveV1Borrow(_aaveV1BorrowData);
|
||||||
{
|
{
|
||||||
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
CTokenInterface[] memory _ctokens = getCtokenInterfaces(length, data.tokens);
|
||||||
_compPayback(length, _ctokens, paybackAmts);
|
_compPayback(length, _ctokens, tokens, paybackAmts);
|
||||||
depositAmts = _compWithdraw(length, _ctokens, data.withdrawAmts);
|
depositAmts = _compWithdraw(length, _ctokens, data.withdrawAmts);
|
||||||
}
|
}
|
||||||
_aaveV1Deposit(aaveV1, aaveCore, length, data.collateralFee, tokens, depositAmts);
|
_aaveV1Deposit(aaveV1, aaveCore, length, data.collateralFee, tokens, depositAmts);
|
||||||
|
@ -1298,7 +1305,7 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
_aaveV2BorrowData.rateModes = data.borrowRateModes;
|
_aaveV2BorrowData.rateModes = data.borrowRateModes;
|
||||||
|
|
||||||
paybackAmts = _aaveV2Borrow(_aaveV2BorrowData);
|
paybackAmts = _aaveV2Borrow(_aaveV2BorrowData);
|
||||||
_compPayback(length, _ctokens, paybackAmts);
|
_compPayback(length, _ctokens, tokens, paybackAmts);
|
||||||
depositAmts = _compWithdraw(length, _ctokens, data.withdrawAmts);
|
depositAmts = _compWithdraw(length, _ctokens, data.withdrawAmts);
|
||||||
_aaveV2Deposit(aaveV2, aaveData, length, data.collateralFee, tokens, depositAmts);
|
_aaveV2Deposit(aaveV2, aaveData, length, data.collateralFee, tokens, depositAmts);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1341,8 +1348,8 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
|
|
||||||
_compEnterMarkets(2, _ctokens);
|
_compEnterMarkets(2, _ctokens);
|
||||||
|
|
||||||
_compDepositOne(data.collateralFee, _ctokens[1], depositAmt);
|
_compDepositOne(data.collateralFee, _ctokens[1], dai, depositAmt);
|
||||||
_compBorrowOne(data.debtFee, _ctokens[0], borrowAmt, Protocol.Aave, 2);
|
_compBorrowOne(data.debtFee, _ctokens[0], TokenInterface(data.token), borrowAmt, Protocol.Aave, 2);
|
||||||
} else {
|
} else {
|
||||||
revert("invalid-option");
|
revert("invalid-option");
|
||||||
}
|
}
|
||||||
|
@ -1364,7 +1371,7 @@ contract RefinanceResolver is MakerHelpers {
|
||||||
CTokenInterface cDai = CTokenInterface(_cDai);
|
CTokenInterface cDai = CTokenInterface(_cDai);
|
||||||
CTokenInterface cToken = CTokenInterface(_cToken);
|
CTokenInterface cToken = CTokenInterface(_cToken);
|
||||||
|
|
||||||
borrowAmt = _compPaybackOne(cDai, data.debt);
|
borrowAmt = _compPaybackOne(cDai, dai, data.debt);
|
||||||
depositAmt = _compWithdrawOne(cToken, data.collateral);
|
depositAmt = _compWithdrawOne(cToken, data.collateral);
|
||||||
} else {
|
} else {
|
||||||
revert("invalid-option");
|
revert("invalid-option");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user