allowing more deposit types in borrow spell

This commit is contained in:
Jeff Wu 2021-12-17 07:57:05 -08:00
parent cfd1eec589
commit 016c8d4370
No known key found for this signature in database
GPG Key ID: 3E6E4F431F4D40C3
2 changed files with 47 additions and 18 deletions

View File

@ -213,7 +213,7 @@ contract Helpers is Basic {
function getDepositCollateralBorrowAndWithdrawActions( function getDepositCollateralBorrowAndWithdrawActions(
uint16 depositCurrencyId, uint16 depositCurrencyId,
bool useUnderlying, DepositActionType depositAction,
uint256 depositAmount, uint256 depositAmount,
uint16 borrowCurrencyId, uint16 borrowCurrencyId,
uint8 marketIndex, uint8 marketIndex,
@ -221,9 +221,29 @@ contract Helpers is Basic {
uint32 maxBorrowRate, uint32 maxBorrowRate,
bool redeemToUnderlying bool redeemToUnderlying
) internal returns (BalanceActionWithTrades[] memory action) { ) internal returns (BalanceActionWithTrades[] memory action) {
// TODO: allow minting nTokens here.... BalanceActionWithTrades[] memory actions;
BalanceActionWithTrades[] bytes32[] memory trades = new bytes32[](1);
memory actions = new BalanceActionWithTrades[](2); trades[0] = encodeBorrowTrade(marketIndex, fCashAmount, maxBorrowRate);
if (depositCurrencyId == borrowCurrencyId) {
// In this case the account is likely borrowing against newly minted nTokens
// in the same currency. Technically the other deposit actions may work but
// there's no good reason to borrow against cToken collateral
actions = new BalanceActionWithTrades[](1);
actions[0].actionType = depositAction;
actions[0].currencyId = depositCurrencyId;
actions[0].depositActionAmount = depositAmount;
// Withdraw borrowed amount to wallet
actions[0].withdrawEntireCashBalance = true;
actions[0].redeemToUnderlying = redeemToUnderlying;
actions[0].trades = trades;
return actions;
}
// This is the more common case that the account is borrowing against
// collateral in a different currency
actions = new BalanceActionWithTrades[](2);
uint256 depositIndex; uint256 depositIndex;
uint256 borrowIndex; uint256 borrowIndex;
@ -236,9 +256,7 @@ contract Helpers is Basic {
borrowIndex = 0; borrowIndex = 0;
} }
actions[depositIndex].actionType = useUnderlying actions[depositIndex].actionType = depositAction;
? DepositActionType.DepositUnderlying
: DepositActionType.DepositAsset;
actions[depositIndex].currencyId = depositCurrencyId; actions[depositIndex].currencyId = depositCurrencyId;
actions[depositIndex].depositActionAmount = depositAmount; actions[depositIndex].depositActionAmount = depositAmount;
@ -247,12 +265,8 @@ contract Helpers is Basic {
// Withdraw borrowed amount to wallet // Withdraw borrowed amount to wallet
actions[borrowIndex].withdrawEntireCashBalance = true; actions[borrowIndex].withdrawEntireCashBalance = true;
actions[borrowIndex].redeemToUnderlying = redeemToUnderlying; actions[borrowIndex].redeemToUnderlying = redeemToUnderlying;
bytes32[] memory trades = new bytes32[](1);
trades[0] = encodeBorrowTrade(marketIndex, fCashAmount, maxBorrowRate);
actions[borrowIndex].trades = trades; actions[borrowIndex].trades = trades;
return actions; return actions;
} }
} }

View File

@ -455,14 +455,25 @@ abstract contract NotionalResolver is Events, Helpers {
} }
/** /**
* @notice Deposits some amount of tokens as collateral and borrows * @notice Deposits some amount of tokens as collateral and borrows. This can be achieved by combining multiple spells but this
* method is more gas efficient by only making a single call to Notional.
* @dev Setting the fCash amount and maxBorrowRate are best calculated using the Notional SDK off chain. The amount of fCash * @dev Setting the fCash amount and maxBorrowRate are best calculated using the Notional SDK off chain. The amount of fCash
* when borrowing is more forgiving compared to lending since generally accounts will over collateralize and dust amounts are * when borrowing is more forgiving compared to lending since generally accounts will over collateralize and dust amounts are
* less likely to cause reverts. The Notional SDK will also provide calculations to tell the user what their LTV is for a given * less likely to cause reverts. The Notional SDK will also provide calculations to tell the user what their LTV is for a given
* borrowing action. * borrowing action.
* @param depositCurrencyId notional defined currency id of the collateral to deposit * @param depositCurrencyId notional defined currency id of the collateral to deposit
* @param useUnderlying if true, will accept a deposit collateralin the underlying currency (i.e DAI), if false * @param depositAction one of the following values which will define how the collateral is deposited:
* will use the asset currency (i.e. cDAI) * - None: no collateral will be deposited
* - DepositAsset: deposit amount will be specified in asset tokens (i.e. cTokens)
* - DepositUnderlying: deposit amount will be specified in underlying tokens (i.e. DAI)
* - DepositAssetAndMintNToken: deposit amount will be converted to nTokens
* - DepositUnderlyingAndMintNToken: deposit amount will be converted to nTokens
*
* Technically these two deposit types can be used, but there is not a clear reason why they would be used in combination
* with borrowing:
* - RedeemNToken
* - ConvertCashToNToken
*
* @param depositAmount amount of cash to deposit as collateral * @param depositAmount amount of cash to deposit as collateral
* @param borrowCurrencyId id of the currency to borrow * @param borrowCurrencyId id of the currency to borrow
* @param marketIndex the market index to borrow from. This is a number from 1 to 7 which corresponds to the tenor * @param marketIndex the market index to borrow from. This is a number from 1 to 7 which corresponds to the tenor
@ -478,7 +489,7 @@ abstract contract NotionalResolver is Events, Helpers {
*/ */
function depositCollateralBorrowAndWithdraw( function depositCollateralBorrowAndWithdraw(
uint16 depositCurrencyId, uint16 depositCurrencyId,
bool useUnderlying, DepositActionType depositAction,
uint256 depositAmount, uint256 depositAmount,
uint16 borrowCurrencyId, uint16 borrowCurrencyId,
uint8 marketIndex, uint8 marketIndex,
@ -492,7 +503,10 @@ abstract contract NotionalResolver is Events, Helpers {
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
require(depositCurrencyId != borrowCurrencyId); bool useUnderlying = (
depositAction == DepositActionType.DepositUnderlying ||
depositAction == DepositActionType.DepositUnderlyingAndMintNToken
);
depositAmount = getDepositAmountAndSetApproval( depositAmount = getDepositAmountAndSetApproval(
getId, getId,
@ -504,7 +518,7 @@ abstract contract NotionalResolver is Events, Helpers {
BalanceActionWithTrades[] BalanceActionWithTrades[]
memory actions = getDepositCollateralBorrowAndWithdrawActions( memory actions = getDepositCollateralBorrowAndWithdrawActions(
depositCurrencyId, depositCurrencyId,
useUnderlying, depositAction,
depositAmount, depositAmount,
borrowCurrencyId, borrowCurrencyId,
marketIndex, marketIndex,
@ -513,9 +527,10 @@ abstract contract NotionalResolver is Events, Helpers {
redeemToUnderlying redeemToUnderlying
); );
uint256 msgValue = getMsgValue(depositCurrencyId, useUnderlying, depositAmount);
executeTradeActionWithBalanceChange( executeTradeActionWithBalanceChange(
actions, actions,
getMsgValue(depositCurrencyId, useUnderlying, depositAmount), msgValue,
borrowCurrencyId, borrowCurrencyId,
redeemToUnderlying, redeemToUnderlying,
setId setId