Batch Trades
A flexible set of methods that enable almost any combination of lending, borrowing and providing liquidity on Notional.
fCash trades in Notional are done by calling eitherbatchBalanceAndTradeAction
or batchLend
in the BatchAction.sol
contract. Both methods require specifying an array of encoded trade objects. The contract address to call to will be the Notional address.
When specifying multiple currencies to batchLend and batchBalanceAndTradeAction they MUST be ordered ascending by currency id. Collateral checks will always be deferred to the end of the transaction.
Batch Lend
batchLend
is a simpler version of batchBalanceTradeAction
that is more gas efficient for lending. Due to the nature of the Notional AMM, it is not possible to exactly calculate deposit amounts for lending until run time (asset cash exchange rates and time to maturity are constantly drifting). batchLend
allows the user to specify a lending trade and then will calculate the required deposit amount at runtime (rather than specify up front like batchBalanceAndTradeAction
) and then just pull the required amount of cash from the account's wallet.
If an account has a matured lending position (or an internal cash balance), this will be applied to the deposit required for lending BEFORE attempting to pull any cash from the account's wallet. Therefore, when rolling matured lending positions forward, a withdraw and deposit is not required.
Trades can be manually encoded using the code on this page below or they can be calculated via the Trade Helpers provided by Notional (specifically getfCashLendFromDeposit and getDepositFromfCashLend).
Batch Balance Trade Action
batchBalanceAndTradeAction
is more powerful method than batchLend
and allows the caller to combine many trading operations on Notional into a single transaction. The lifecycle of a batchBalanceTradeAction
is always as follows:
If the calling account has any matured positions, they will be settled to cash balances at the very beginning of the transaction.
Deposit Actions are performed first and increase the cash available to the account to perform trades.
Trades are performed and generate a net increase or decrease in the cash available to the account.
Withdraws are performed and will return any specified amount of cash back to the user.
Deposit Action
These are the possible deposit actions to specify
None: no deposit will be performed.
DepositAsset: deposits asset cash (i.e. cDAI, cUSDC, aFRAX) into the account's cash balance. This will be used as collateral that earns variable interest based on Compound or Aave supply rates.
depositActionAmount
is denominated in the asset token's native precision.DepositUnderlying: deposits underlying tokens (i.e. DAI, USDC, FRAX) and converts it into asset cash. This will be used as collateral that earns variable interest based on Compound or Aave supply rates.
depositActionAmount
is denominated in the underlying token's native precision.DepositAssetAndMintNToken or DepositUnderlyingAndMintNToken: executes a deposit and then uses all of the cash to mint nTokens. nTokens earn the variable interest rate from asset cash, trading fees from Notional, and incentives in the form of NOTE. nTokens are also automatically considered collateral for borrowing.
RedeemNToken: redeems
depositActionAmount
of nTokens into an asset cash balance. This is considered a deposit action because it increases the cash available to the account.ConvertCashToNToken: converts
depositActionAmount
worth of existing cash balance into nTokens. No transfer from the user's wallet is executed.
Trades
Each trade is a tightly packed bytes32 value. A single batch action may include multiple trades. Lend and borrow are the most common trade types. The easiest way to get encoded trade values is via the Trade Helper calculation views. Lend trades can be generated using getfCashLendFromDeposit and getDepositFromfCashLend, Borrow trades can be generated using getfCashBorrowFromPrincipal and getPrincipalFromfCashBorrow.
There are two other special types of trades which are not covered here and only used in special circumstances:
PurchaseNTokenResidual
SettleCashDebt
AddLiquidity and RemoveLiquidity are currently inactive. Use nTokens to provide liquidity to Notional.
Withdraw
withdrawAmountInternalPrecision: some amount of cash balance (denominated in Notional internal 8 decimal precision) to withdraw. It is not common to need to specify this value unless there is a specific need. The other two parameters are more common.
withdrawEntireCashBalance: withdraws an entire cash balance back to the account's wallet. Set this to true when lending to have any residuals from
depositActionAmount
returned to the wallet (or usebatchLend
instead to avoid this issue altogether) or set this to true to receive all the cash from executing a borrow trade.redeemToUnderlying: set this to true for the account to receive the withdrawn cash as underlying tokens (i.e. DAI, USDC, ETH, etc).
Examples
This is an example of generating an encoded borrow trade:
Deposit ETH and Borrow DAI
An example where the account deposits 1 ETH and borrows 100 DAI in a single transaction using the code above:
Lend ETH and Borrow DAI
Notional also allows you to borrow against fCash assets for greater capital efficiency. In this example, instead of depositing ETH, the account will lend it for a fixed yield and then borrow DAI against it. The account's collateral is earning more interest than in the example above (in that example the ETH is converted to cETH and earn's Compound's lower variable interest). In this example, we use the Trade Helpers to calculate the appropriate values.
Last updated