Merge pull request #258 from Instadapp/compound-v3-1

Compound V3 | 2
This commit is contained in:
Thrilok kumar 2022-09-03 01:42:06 +05:30 committed by GitHub
commit 27454a75d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 27 deletions

View File

@ -97,12 +97,13 @@ contract Events {
uint256 setId uint256 setId
); );
event LogPayback(address indexed market, uint256 tokenAmt, uint256 setId); event LogPayback(address indexed market, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogPaybackOnBehalf( event LogPaybackOnBehalf(
address indexed market, address indexed market,
address to, address to,
uint256 tokenAmt, uint256 tokenAmt,
uint256 getId,
uint256 setId uint256 setId
); );
@ -111,6 +112,7 @@ contract Events {
address from, address from,
address to, address to,
uint256 tokenAmt, uint256 tokenAmt,
uint256 getId,
uint256 setId uint256 setId
); );
@ -127,14 +129,13 @@ contract Events {
event LogTransferAsset( event LogTransferAsset(
address indexed market, address indexed market,
address token, address token,
address indexed from,
address indexed dest, address indexed dest,
uint256 amount, uint256 amount,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
); );
event LogTransferAssetFromUsingManager( event LogTransferAssetOnBehalf(
address indexed market, address indexed market,
address token, address token,
address indexed from, address indexed from,

View File

@ -74,7 +74,7 @@ abstract contract Helpers is DSMath, Basic {
params.from = params.from == address(0) ? address(this) : params.from; params.from = params.from == address(0) ? address(this) : params.from;
require( require(
TokenInterface(params.market).balanceOf(params.from) == 0, CometInterface(params.market).balanceOf(params.from) == 0,
"borrow-disabled-when-supplied-base" "borrow-disabled-when-supplied-base"
); );
@ -253,6 +253,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_); approve(TokenInterface(params.sellToken), params.market, sellAmt_);
CometInterface(params.market).buyCollateral( CometInterface(params.market).buyCollateral(
params.buyAsset, params.buyAsset,
@ -260,11 +264,11 @@ abstract contract Helpers is DSMath, Basic {
sellAmt_, sellAmt_,
address(this) address(this)
); );
uint256 finalCollBal_ = CometInterface(params.market)
.userCollateral(address(this), params.buyAsset)
.balance;
uint256 buyAmt_ = CometInterface(params.market).quoteCollateral( uint256 buyAmt_ = sub(finalCollBal_, initialCollBal_);
params.buyAsset,
sellAmt_
);
require(slippageAmt_ <= buyAmt_, "too-much-slippage"); require(slippageAmt_ <= buyAmt_, "too-much-slippage");
convertWethToEth(isEth, TokenInterface(params.buyAsset), buyAmt_); convertWethToEth(isEth, TokenInterface(params.buyAsset), buyAmt_);

View File

@ -224,14 +224,11 @@ abstract contract CompoundV3Resolver is Events, Helpers {
); );
if (token_ == getBaseToken(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 (amt_ == uint256(-1)) { if (amt_ == uint256(-1)) {
amt_ = initialBal; amt_ = initialBal;
} else { } else {
require( //if there are supplies, ensure withdrawn amount is not greater than supplied i.e can't borrow using withdraw.
amt_ <= initialBal, require(amt_ <= initialBal, "withdraw-amt-greater-than-supplies");
"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.
@ -598,11 +595,11 @@ abstract contract CompoundV3Resolver is Events, Helpers {
} else { } else {
require( require(
amt_ <= borrowedBalance_, 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( require(
CometInterface(market).balanceOf(address(this)) == 0, CometInterface(market).balanceOf(address(this)) == 0,
"cannot-repay-when-supplied" "cannot-repay-when-supplied"
@ -615,8 +612,8 @@ abstract contract CompoundV3Resolver is Events, Helpers {
setUint(setId, amt_); setUint(setId, amt_);
eventName_ = "LogPayback(address,address,uint256,uint256,uint256)"; eventName_ = "LogPayback(address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token_, amt_, getId, setId); eventParam_ = abi.encode(market, amt_, getId, setId);
} }
/** /**
@ -656,15 +653,15 @@ abstract contract CompoundV3Resolver is Events, Helpers {
uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to); uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to);
if (amt_ == uint256(-1)) { if (amt_ == uint256(-1)) {
amt_ = initialBal; amt_ = borrowedBalance_;
} else { } else {
require( require(
amt_ <= borrowedBalance_, 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( require(
CometInterface(market).balanceOf(to) == 0, CometInterface(market).balanceOf(to) == 0,
"cannot-repay-when-supplied" "cannot-repay-when-supplied"
@ -677,8 +674,8 @@ abstract contract CompoundV3Resolver is Events, Helpers {
setUint(setId, amt_); setUint(setId, amt_);
eventName_ = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; eventName_ = "LogPaybackOnBehalf(address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token_, to, amt_, getId, setId); eventParam_ = abi.encode(market, to, amt_, getId, setId);
} }
/** /**
@ -733,15 +730,29 @@ abstract contract CompoundV3Resolver is Events, Helpers {
); );
} }
uint256 supplyBalance_ = CometInterface(market).balanceOf(to); uint256 borrowedBalance_ = CometInterface(market).borrowBalanceOf(to);
require(supplyBalance_ == 0, "cannot-repay-when-supplied");
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_); CometInterface(market).supplyFrom(from, to, token_, amt_);
setUint(setId, amt_); setUint(setId, amt_);
eventName_ = "LogPaybackFromUsingManager(address,address,address,address,uint256,uint256,uint256)"; eventName_ = "LogPaybackFromUsingManager(address,address,address,uint256,uint256,uint256)";
eventParam_ = abi.encode(market, token_, from, to, amt_, getId, setId); eventParam_ = abi.encode(market, from, to, amt_, getId, setId);
} }
/** /**
@ -920,8 +931,8 @@ abstract contract CompoundV3Resolver is Events, Helpers {
owner, owner,
manager, manager,
isAllowed, isAllowed,
nonce,
expiry, expiry,
nonce,
v, v,
r, r,
s s