mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
buyCollateral updated
This commit is contained in:
parent
75c092c266
commit
8250831342
|
@ -116,10 +116,10 @@ contract Events {
|
||||||
|
|
||||||
event LogBuyCollateral(
|
event LogBuyCollateral(
|
||||||
address indexed market,
|
address indexed market,
|
||||||
address indexed token,
|
address indexed buyToken,
|
||||||
uint256 indexed baseAmount,
|
uint256 indexed baseSellAmt,
|
||||||
uint256 minCollateralAmt,
|
uint256 unitAmt,
|
||||||
uint256 collateralAmount,
|
uint256 buyAmount,
|
||||||
uint256 getId,
|
uint256 getId,
|
||||||
uint256 setId
|
uint256 setId
|
||||||
);
|
);
|
||||||
|
|
|
@ -18,6 +18,14 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
uint256 setId;
|
uint256 setId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct BuyCollateralData {
|
||||||
|
address market;
|
||||||
|
address sellToken;
|
||||||
|
address buyAsset;
|
||||||
|
uint256 unitAmt;
|
||||||
|
uint256 baseSellAmt;
|
||||||
|
}
|
||||||
|
|
||||||
enum Action {
|
enum Action {
|
||||||
REPAY,
|
REPAY,
|
||||||
DEPOSIT,
|
DEPOSIT,
|
||||||
|
@ -121,7 +129,9 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
|
amt_ = amt_ == uint256(-1) ? initialBal : amt_;
|
||||||
|
|
||||||
if (token_ == getBaseToken(params.market)) {
|
if (token_ == getBaseToken(params.market)) {
|
||||||
uint256 balance = CometInterface(params.market).balanceOf(params.from);
|
uint256 balance = CometInterface(params.market).balanceOf(
|
||||||
|
params.from
|
||||||
|
);
|
||||||
//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.
|
||||||
if (balance > 0) {
|
if (balance > 0) {
|
||||||
require(amt_ <= balance, "withdraw-amt-greater-than-supplies");
|
require(amt_ <= balance, "withdraw-amt-greater-than-supplies");
|
||||||
|
@ -211,4 +221,72 @@ abstract contract Helpers is DSMath, Basic {
|
||||||
|
|
||||||
return amt;
|
return amt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _buyCollateral(
|
||||||
|
BuyCollateralData memory params,
|
||||||
|
uint256 getId,
|
||||||
|
uint256 setId
|
||||||
|
) internal returns (string memory eventName_, bytes memory eventParam_) {
|
||||||
|
uint256 sellAmt_ = getUint(getId, params.baseSellAmt);
|
||||||
|
require(
|
||||||
|
params.market != address(0) && params.buyAsset != address(0),
|
||||||
|
"invalid market/token address"
|
||||||
|
);
|
||||||
|
require(
|
||||||
|
params.sellToken == getBaseToken(params.market),
|
||||||
|
"invalid-sell-token"
|
||||||
|
);
|
||||||
|
|
||||||
|
bool isEth = params.sellToken == ethAddr;
|
||||||
|
params.sellToken = isEth ? wethAddr : params.sellToken;
|
||||||
|
|
||||||
|
if (sellAmt_ == uint256(-1)) {
|
||||||
|
sellAmt_ = isEth
|
||||||
|
? address(this).balance
|
||||||
|
: TokenInterface(params.sellToken).balanceOf(address(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
isEth = params.buyAsset == ethAddr;
|
||||||
|
params.buyAsset = isEth ? wethAddr : params.buyAsset;
|
||||||
|
|
||||||
|
convertEthToWeth(isEth, TokenInterface(params.sellToken), sellAmt_);
|
||||||
|
|
||||||
|
uint256 slippageAmt_ = convert18ToDec(
|
||||||
|
TokenInterface(params.buyAsset).decimals(),
|
||||||
|
wmul(
|
||||||
|
params.unitAmt,
|
||||||
|
convertTo18(
|
||||||
|
TokenInterface(params.sellToken).decimals(),
|
||||||
|
sellAmt_
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
approve(TokenInterface(params.sellToken), params.market, sellAmt_);
|
||||||
|
CometInterface(params.market).buyCollateral(
|
||||||
|
params.buyAsset,
|
||||||
|
slippageAmt_,
|
||||||
|
sellAmt_,
|
||||||
|
address(this)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 buyAmt_ = CometInterface(params.market).quoteCollateral(
|
||||||
|
params.buyAsset,
|
||||||
|
sellAmt_
|
||||||
|
);
|
||||||
|
require(slippageAmt_ <= buyAmt_, "too-much-slippage");
|
||||||
|
|
||||||
|
convertWethToEth(isEth, TokenInterface(params.buyAsset), buyAmt_);
|
||||||
|
setUint(setId, sellAmt_);
|
||||||
|
|
||||||
|
eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)";
|
||||||
|
eventParam_ = abi.encode(
|
||||||
|
params.market,
|
||||||
|
params.buyAsset,
|
||||||
|
sellAmt_,
|
||||||
|
params.unitAmt,
|
||||||
|
buyAmt_,
|
||||||
|
getId,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -714,17 +714,19 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
* @dev Buy collateral asset absorbed, from the market.
|
* @dev Buy collateral asset absorbed, from the market.
|
||||||
* @notice Buy collateral asset to increase protocol base reserves until targetReserves is reached.
|
* @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 market The address of the market from where to withdraw.
|
||||||
* @param asset The collateral asset to purachase.
|
* @param sellToken base token. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param minCollateralAmt Minimum amount of collateral expected to be received.
|
* @param buyAsset The collateral asset to purachase. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param baseAmt Amount of base asset to be sold for collateral.
|
* @param unitAmt Minimum amount of collateral expected to be received.
|
||||||
|
* @param baseSellAmt Amount of base asset to be sold for collateral.
|
||||||
* @param getId ID to retrieve amt.
|
* @param getId ID to retrieve amt.
|
||||||
* @param setId ID stores the amount of base tokens sold.
|
* @param setId ID stores the amount of base tokens sold.
|
||||||
*/
|
*/
|
||||||
function buyCollateral(
|
function buyCollateral(
|
||||||
address market,
|
address market,
|
||||||
address asset,
|
address sellToken,
|
||||||
uint256 minCollateralAmt,
|
address buyAsset,
|
||||||
uint256 baseAmt,
|
uint256 unitAmt,
|
||||||
|
uint256 baseSellAmt,
|
||||||
uint256 getId,
|
uint256 getId,
|
||||||
uint256 setId
|
uint256 setId
|
||||||
)
|
)
|
||||||
|
@ -732,36 +734,14 @@ abstract contract CompoundV3Resolver is Events, Helpers {
|
||||||
payable
|
payable
|
||||||
returns (string memory eventName_, bytes memory eventParam_)
|
returns (string memory eventName_, bytes memory eventParam_)
|
||||||
{
|
{
|
||||||
uint256 amt_ = getUint(getId, baseAmt);
|
(eventName_, eventParam_) = _buyCollateral(
|
||||||
require(
|
BuyCollateralData({
|
||||||
market != address(0) && asset != address(0),
|
market: market,
|
||||||
"invalid market/token address"
|
sellToken: sellToken,
|
||||||
);
|
buyAsset: buyAsset,
|
||||||
|
unitAmt: unitAmt,
|
||||||
bool isEth = asset == ethAddr;
|
baseSellAmt: baseSellAmt
|
||||||
address token_ = isEth ? wethAddr : asset;
|
}),
|
||||||
TokenInterface tokenContract = TokenInterface(token_);
|
|
||||||
|
|
||||||
convertEthToWeth(isEth, tokenContract, amt_);
|
|
||||||
approve(TokenInterface(getBaseToken(market)), market, amt_);
|
|
||||||
|
|
||||||
CometInterface(market).buyCollateral(
|
|
||||||
asset,
|
|
||||||
minCollateralAmt,
|
|
||||||
amt_,
|
|
||||||
address(this)
|
|
||||||
);
|
|
||||||
|
|
||||||
uint256 collAmt = CometInterface(market).quoteCollateral(asset, amt_);
|
|
||||||
setUint(setId, amt_);
|
|
||||||
|
|
||||||
eventName_ = "LogBuyCollateral(address,address,uint256,uint256,uint256,uint256,uint256)";
|
|
||||||
eventParam_ = abi.encode(
|
|
||||||
market,
|
|
||||||
token_,
|
|
||||||
amt_,
|
|
||||||
minCollateralAmt,
|
|
||||||
collAmt,
|
|
||||||
getId,
|
getId,
|
||||||
setId
|
setId
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user