mirror of
https://github.com/Instadapp/dsa-connectors-2.0.git
synced 2024-07-29 21:57:39 +00:00
feat: update logics
This commit is contained in:
parent
aa0dedc52c
commit
6d46b8fb48
|
|
@ -45,7 +45,9 @@ abstract contract Helpers is Stores, Basic {
|
||||||
) internal returns (Id _id, MarketParams memory, uint256 _amt) {
|
) internal returns (Id _id, MarketParams memory, uint256 _amt) {
|
||||||
_amt = getUint(_getId, _assets);
|
_amt = getUint(_getId, _assets);
|
||||||
|
|
||||||
bool _isEth = _mode == Mode.Collateral
|
bool _isModeCollateral = _mode == Mode.Collateral;
|
||||||
|
|
||||||
|
bool _isEth = _isModeCollateral
|
||||||
? _marketParams.collateralToken == ethAddr
|
? _marketParams.collateralToken == ethAddr
|
||||||
: _marketParams.loanToken == ethAddr;
|
: _marketParams.loanToken == ethAddr;
|
||||||
|
|
||||||
|
|
@ -55,21 +57,37 @@ abstract contract Helpers is Stores, Basic {
|
||||||
|
|
||||||
// Check for max value
|
// Check for max value
|
||||||
if (_assets == type(uint256).max) {
|
if (_assets == type(uint256).max) {
|
||||||
uint256 _maxAvailable = _isEth
|
uint256 _maxBalance = _isEth
|
||||||
? address(this).balance
|
? address(this).balance
|
||||||
|
: _isModeCollateral
|
||||||
|
? TokenInterface(_marketParams.collateralToken).balanceOf(
|
||||||
|
address(this)
|
||||||
|
)
|
||||||
: TokenInterface(_marketParams.loanToken).balanceOf(
|
: TokenInterface(_marketParams.loanToken).balanceOf(
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_mode == Mode.Repay) {
|
if (_mode == Mode.Repay) {
|
||||||
uint256 _amtDebt = getPaybackBalance(_id, _marketParams, _onBehalf);
|
uint256 _amtDebt = getPaybackBalance(
|
||||||
_amt = UtilsLib.min(_maxAvailable, _amtDebt);
|
_id,
|
||||||
|
_marketParams,
|
||||||
|
_onBehalf
|
||||||
|
);
|
||||||
|
|
||||||
|
_amt = UtilsLib.min(_maxBalance, _amtDebt);
|
||||||
} else {
|
} else {
|
||||||
_amt = _maxAvailable;
|
_amt = _maxBalance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform conversion if necessary
|
// Perform eth to weth conversion if necessary
|
||||||
convertEthToWeth(true, TokenInterface(_marketParams.loanToken), _amt);
|
convertEthToWeth(
|
||||||
|
true,
|
||||||
|
_isModeCollateral
|
||||||
|
? TokenInterface(_marketParams.collateralToken)
|
||||||
|
: TokenInterface(_marketParams.loanToken),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
|
|
||||||
return (_id, _marketParams, _amt);
|
return (_id, _marketParams, _amt);
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +109,7 @@ abstract contract Helpers is Stores, Basic {
|
||||||
|
|
||||||
// Handle the max share case
|
// Handle the max share case
|
||||||
if (_shares == type(uint256).max) {
|
if (_shares == type(uint256).max) {
|
||||||
uint256 _maxAvailable = _isEth
|
uint256 _maxBalance = _isEth
|
||||||
? address(this).balance
|
? address(this).balance
|
||||||
: TokenInterface(_marketParams.loanToken).balanceOf(
|
: TokenInterface(_marketParams.loanToken).balanceOf(
|
||||||
address(this)
|
address(this)
|
||||||
|
|
@ -99,19 +117,32 @@ abstract contract Helpers is Stores, Basic {
|
||||||
|
|
||||||
// If it's repay calculate the min of balance available and debt to repay
|
// If it's repay calculate the min of balance available and debt to repay
|
||||||
if (_isRepay) {
|
if (_isRepay) {
|
||||||
_assets = UtilsLib.min(_maxAvailable, getPaybackBalance(_id, _marketParams, _onBehalf));
|
_assets = UtilsLib.min(
|
||||||
|
_maxBalance,
|
||||||
|
getPaybackBalance(_id, _marketParams, _onBehalf)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
_assets = _maxAvailable;
|
_assets = _maxBalance;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
uint256 totalSupplyAssets,
|
uint256 totalSupplyAssets,
|
||||||
uint256 totalSupplyShares,
|
uint256 totalSupplyShares,
|
||||||
,
|
uint256 totalBorrowAssets,
|
||||||
|
uint256 totalBorrowShares
|
||||||
) = MORPHO_BLUE.expectedMarketBalances(_marketParams);
|
) = MORPHO_BLUE.expectedMarketBalances(_marketParams);
|
||||||
|
|
||||||
_assets = _shareAmt.toAssetsUp(totalSupplyAssets, totalSupplyShares);
|
if (_isRepay) {
|
||||||
|
_assets = _shareAmt.toAssetsUp(
|
||||||
|
totalBorrowAssets,
|
||||||
|
totalBorrowShares
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_assets = _shareAmt.toAssetsUp(
|
||||||
|
totalSupplyAssets,
|
||||||
|
totalSupplyShares
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform ETH to WETH conversion if necessary
|
// Perform ETH to WETH conversion if necessary
|
||||||
|
|
@ -130,12 +161,17 @@ abstract contract Helpers is Stores, Basic {
|
||||||
MarketParams memory _marketParams,
|
MarketParams memory _marketParams,
|
||||||
address _onBehalf
|
address _onBehalf
|
||||||
) internal view returns (uint256 _assets) {
|
) internal view returns (uint256 _assets) {
|
||||||
uint256 _shareAmt = MORPHO_BLUE.position(_id, _onBehalf).supplyShares;
|
uint256 _borrowedShareAmt = MORPHO_BLUE
|
||||||
|
.position(_id, _onBehalf)
|
||||||
|
.borrowShares;
|
||||||
|
|
||||||
(uint256 totalSupplyAssets, uint256 totalSupplyShares, , ) = MORPHO_BLUE
|
(, , uint256 totalBorrowAssets, uint256 totalBorrowShares) = MORPHO_BLUE
|
||||||
.expectedMarketBalances(_marketParams);
|
.expectedMarketBalances(_marketParams);
|
||||||
|
|
||||||
_assets = _shareAmt.toAssetsUp(totalSupplyAssets, totalSupplyShares);
|
_assets = _borrowedShareAmt.toAssetsUp(
|
||||||
|
totalBorrowAssets,
|
||||||
|
totalBorrowShares
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTokenAddresses(
|
function updateTokenAddresses(
|
||||||
|
|
|
||||||
|
|
@ -243,7 +243,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @notice Supplies `assets` of collateral on behalf of `onBehalf`, optionally calling back the caller's `onMorphoSupplyCollateral` function with the given `data`.
|
* @notice Supplies `assets` of collateral on behalf of `onBehalf`.
|
||||||
* @param _marketParams The market to supply assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param _marketParams The market to supply assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param _assets The amount of assets to supply. (For max: `uint256(-1)`)
|
* @param _assets The amount of assets to supply. (For max: `uint256(-1)`)
|
||||||
* @param _onBehalf The address that will get the shares.
|
* @param _onBehalf The address that will get the shares.
|
||||||
|
|
@ -263,7 +263,6 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
{
|
{
|
||||||
uint256 _amt;
|
uint256 _amt;
|
||||||
Id _id;
|
Id _id;
|
||||||
// Final assets amount and token contract
|
|
||||||
(
|
(
|
||||||
_id,
|
_id,
|
||||||
_marketParams, // Updated token contracts in case of Eth
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
|
|
@ -412,7 +411,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @notice Handles the withdrawal of supply.
|
* @notice Handles the withdrawal of supplied assets.
|
||||||
* @dev The market to withdraw assets from. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @dev The market to withdraw assets from. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param _marketParams The market to withdraw assets from.
|
* @param _marketParams The market to withdraw assets from.
|
||||||
* @param _assets The amount of assets to withdraw. (For max: `uint256(-1)`)
|
* @param _assets The amount of assets to withdraw. (For max: `uint256(-1)`)
|
||||||
|
|
@ -499,6 +498,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
Id _id = _marketParams.id();
|
Id _id = _marketParams.id();
|
||||||
|
|
||||||
uint256 _shares = 0;
|
uint256 _shares = 0;
|
||||||
|
|
||||||
// Using shares for max amounts to make sure no dust is left on the contract
|
// Using shares for max amounts to make sure no dust is left on the contract
|
||||||
if (_amt == type(uint256).max) {
|
if (_amt == type(uint256).max) {
|
||||||
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
||||||
|
|
@ -900,7 +900,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
(
|
(
|
||||||
_id,
|
_id,
|
||||||
_marketParams, // Updated token contracts in case of Eth
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
_assetsAmt // Assets final amount to repay
|
_assetsAmt // Shares amount converted to assets
|
||||||
) = _performEthToWethSharesConversion(
|
) = _performEthToWethSharesConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_shares,
|
_shares,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user