mirror of
https://github.com/Instadapp/dsa-connectors-2.0.git
synced 2024-07-29 21:57:39 +00:00
feat: update contract
This commit is contained in:
parent
3cab2e4527
commit
9f22da24d3
|
|
@ -2,13 +2,12 @@
|
||||||
pragma solidity ^0.8.2;
|
pragma solidity ^0.8.2;
|
||||||
|
|
||||||
contract Events {
|
contract Events {
|
||||||
event LogOperate (
|
event LogOperate(
|
||||||
address vaultAddress,
|
address vaultAddress,
|
||||||
uint256 nftId,
|
uint256 nftId,
|
||||||
int256 newCol,
|
int256 newCol,
|
||||||
int256 newDebt,
|
int256 newDebt,
|
||||||
address to,
|
uint256[] getIds,
|
||||||
uint256 getId,
|
uint256[] setIds
|
||||||
uint256 setId
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
pragma solidity ^0.8.2;
|
pragma solidity ^0.8.2;
|
||||||
|
|
||||||
interface IVault {
|
interface IVault {
|
||||||
|
|
||||||
/// @dev Single function which handles supply, withdraw, borrow & payback
|
/// @dev Single function which handles supply, withdraw, borrow & payback
|
||||||
/// @param nftId_ NFT ID for interaction. If 0 then create new NFT/position.
|
/// @param nftId_ NFT ID for interaction. If 0 then create new NFT/position.
|
||||||
/// @param newCol_ new collateral. If positive then deposit, if negative then withdraw, if 0 then do nohing
|
/// @param newCol_ new collateral. If positive then deposit, if negative then withdraw, if 0 then do nohing
|
||||||
|
|
@ -43,5 +42,8 @@ interface IVault {
|
||||||
bytes32 liquidityUserBorrowSlot;
|
bytes32 liquidityUserBorrowSlot;
|
||||||
}
|
}
|
||||||
|
|
||||||
function constantsView() external view returns (ConstantViews memory constantsView_);
|
function constantsView()
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (ConstantViews memory constantsView_);
|
||||||
}
|
}
|
||||||
|
|
@ -28,7 +28,7 @@ abstract contract FluidConnector is Events, Stores {
|
||||||
* For max deposit use type(uint25).max, for max withdraw use type(uint25).min.
|
* For max deposit use type(uint25).max, for max withdraw use type(uint25).min.
|
||||||
* @param newDebt_ New debt. If positive then borrow, if negative then payback, if 0 then do nothing
|
* @param newDebt_ New debt. If positive then borrow, if negative then payback, if 0 then do nothing
|
||||||
* For max payback use type(uint25).min.
|
* For max payback use type(uint25).min.
|
||||||
* @param to_ Address where withdraw or borrow should go. If address(0) then msg.sender
|
* @param repayApproveAmt_ In case of max amount for payback, this amount will be approved for spending.
|
||||||
* @param getIds_ Array of 5 elements to retrieve IDs:
|
* @param getIds_ Array of 5 elements to retrieve IDs:
|
||||||
* Nft Id, Supply amount, Withdraw amount, Borrow Amount, Payback Amount
|
* Nft Id, Supply amount, Withdraw amount, Borrow Amount, Payback Amount
|
||||||
* @param setIds_ Array of 5 elements to store IDs generated:
|
* @param setIds_ Array of 5 elements to store IDs generated:
|
||||||
|
|
@ -39,85 +39,115 @@ abstract contract FluidConnector is Events, Stores {
|
||||||
uint256 nftId_,
|
uint256 nftId_,
|
||||||
int256 newCol_,
|
int256 newCol_,
|
||||||
int256 newDebt_,
|
int256 newDebt_,
|
||||||
address to_,
|
uint256 repayApproveAmt_,
|
||||||
uint256[] memory getIds_, // nft id, supply amount, withdraw amount, Borrow Amount, Payback Amount
|
uint256[] memory getIds_,
|
||||||
uint256[] memory setIds_
|
uint256[] memory setIds_
|
||||||
)
|
)
|
||||||
external
|
external
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (getIds_[1] > 0 && getIds_[2] > 0) {
|
if (getIds_[1] > 0 && getIds_[2] > 0) {
|
||||||
revert ("Supply and withdraw amount get IDs cannot be > 0 at once.");
|
revert("Supply and withdraw get IDs cannot both be > 0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getIds_[3] > 0 && getIds_[4] > 0) {
|
if (getIds_[3] > 0 && getIds_[4] > 0) {
|
||||||
revert ("Borrow and payback amount get IDs cannot be > 0 at once.");
|
revert("Borrow and payback get IDs cannot both be > 0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setIds_[1] > 0 && setIds_[2] > 0) {
|
if (setIds_[1] > 0 && setIds_[2] > 0) {
|
||||||
revert ("Supply and withdraw amount get IDs cannot be > 0 at once.");
|
revert("Supply and withdraw set IDs cannot both be > 0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setIds_[3] > 0 && setIds_[4] > 0) {
|
if (setIds_[3] > 0 && setIds_[4] > 0) {
|
||||||
revert ("Borrow and payback amount get IDs cannot be > 0 at once.");
|
revert("Borrow and payback set IDs cannot both be > 0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
nftId_ = getUint(getIds_[0], nftId_);
|
nftId_ = getUint(getIds_[0], nftId_);
|
||||||
|
|
||||||
// newCol_ = getIds_[1] > 0 ? getUint(getIds_[1], newCol_) : getUint(getIds_[2], newCol_);
|
newCol_ = getIds_[1] > 0
|
||||||
|
? int256(getUint(getIds_[1], uint256(newCol_)))
|
||||||
|
: getIds_[2] > 0
|
||||||
|
? -int256(getUint(getIds_[2], uint256(newCol_)))
|
||||||
|
: newCol_;
|
||||||
|
|
||||||
// newDebt_ = getIds_[3] > 0 ? getUint(getIds_[3], newDebt_) : getUint(getIds_[4], newDebt_);
|
newDebt_ = getIds_[3] > 0
|
||||||
|
? int256(getUint(getIds_[3], uint256(newDebt_)))
|
||||||
|
: getIds_[4] > 0
|
||||||
|
? -int256(getUint(getIds_[4], uint256(newDebt_)))
|
||||||
|
: newDebt_;
|
||||||
|
|
||||||
IVault vault_ = IVault(vaultAddress_);
|
IVault vault_ = IVault(vaultAddress_);
|
||||||
|
|
||||||
IVault.ConstantViews memory vaultDetails_ = vault_.constantsView();
|
IVault.ConstantViews memory vaultDetails_ = vault_.constantsView();
|
||||||
|
|
||||||
uint256 colEthAmount_;
|
uint256 ethAmount_;
|
||||||
uint256 debtEthAmount_;
|
|
||||||
|
bool isColMax_ = newCol_ == type(int256).max;
|
||||||
|
|
||||||
if (newCol_ > 0) {
|
if (newCol_ > 0) {
|
||||||
if (vaultDetails_.supplyToken == getMaticAddr()) {
|
if (vaultDetails_.supplyToken == getMaticAddr()) {
|
||||||
colEthAmount_ = uint256(newCol_);
|
ethAmount_ = isColMax_
|
||||||
|
? address(this).balance
|
||||||
|
: uint256(newCol_);
|
||||||
} else {
|
} else {
|
||||||
|
if (isColMax_) {
|
||||||
|
newCol_ = int256(
|
||||||
|
TokenInterface(vaultDetails_.supplyToken).balanceOf(
|
||||||
|
address(this)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
TokenInterface(vaultDetails_.supplyToken).approve(
|
TokenInterface(vaultDetails_.supplyToken).approve(
|
||||||
vaultAddress_,
|
vaultAddress_,
|
||||||
uint256(newCol_)
|
uint256(newCol_)
|
||||||
);
|
);
|
||||||
|
|
||||||
colEthAmount_ = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isPaybackMax_ = newDebt_ == type(int256).min;
|
||||||
|
|
||||||
if (newDebt_ < 0) {
|
if (newDebt_ < 0) {
|
||||||
if (vaultDetails_.borrowToken == getMaticAddr()) {
|
if (vaultDetails_.borrowToken == getMaticAddr()) {
|
||||||
debtEthAmount_ = uint256(-1 * newDebt_);
|
ethAmount_ = isPaybackMax_
|
||||||
|
? repayApproveAmt_
|
||||||
|
: uint256(-1 * newDebt_);
|
||||||
} else {
|
} else {
|
||||||
TokenInterface(vaultDetails_.borrowToken).approve(
|
isPaybackMax_
|
||||||
vaultAddress_,
|
? TokenInterface(vaultDetails_.borrowToken).approve(
|
||||||
uint256(-1 * newDebt_)
|
vaultAddress_,
|
||||||
);
|
repayApproveAmt_
|
||||||
|
)
|
||||||
debtEthAmount_ = 0;
|
: TokenInterface(vaultDetails_.borrowToken).approve(
|
||||||
|
vaultAddress_,
|
||||||
|
uint256(-1 * newDebt_)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(nftId_, newCol_, newDebt_) = vault_.operate{
|
(nftId_, newCol_, newDebt_) = vault_.operate{value: ethAmount_}(
|
||||||
value: colEthAmount_ + debtEthAmount_
|
nftId_,
|
||||||
}(nftId_, newCol_, newDebt_, to_);
|
newCol_,
|
||||||
|
newDebt_,
|
||||||
|
address(this)
|
||||||
|
);
|
||||||
|
|
||||||
setUint(setIds_[0], nftId_);
|
setUint(setIds_[0], nftId_);
|
||||||
// setIds_[1] > 0 ? setUint(setIds_[1], newCol_) : setUint(setIds_[2], newCol_);
|
|
||||||
// setIds_[3] > 0 ? setUint(setIds_[3], newDebt_) : setUint(setIds_[4], newDebt_);
|
|
||||||
|
|
||||||
_eventName = "LogOperate(address,uint256,int256,int256,address,uint256[],uint256[])";
|
setIds_[1] > 0
|
||||||
|
? setUint(setIds_[1], uint256(newCol_))
|
||||||
|
: setUint(setIds_[2], uint256(newCol_)); // If setIds_[2] != 0, it will set the ID.
|
||||||
|
setIds_[3] > 0
|
||||||
|
? setUint(setIds_[3], uint256(newDebt_))
|
||||||
|
: setUint(setIds_[4], uint256(newDebt_)); // If setIds_[4] != 0, it will set the ID.
|
||||||
|
|
||||||
|
_eventName = "LogOperate(address,uint256,int256,int256,uint256[],uint256[])";
|
||||||
_eventParam = abi.encode(
|
_eventParam = abi.encode(
|
||||||
vaultAddress_,
|
vaultAddress_,
|
||||||
nftId_,
|
nftId_,
|
||||||
newCol_,
|
newCol_,
|
||||||
newDebt_,
|
newDebt_,
|
||||||
to_,
|
|
||||||
getIds_,
|
getIds_,
|
||||||
setIds_
|
setIds_
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user