# Notional Folding Strategy Bounty ## Intro Notional Finance is a new DeFi lending platform featuring fixed lending rates. Notional has an ongoing liquidity mining program which rewards users for depositing, or providing liquidity (DAI, ETH, USDC, WBTC are all incentivized). Liquidity that's provided can be used as collateral to borrow assets on the platform, and by recursively borrowing and supplying (folding) liquidity of the same assets (e.g. USDC), users can increase their NOTE earning fourfold or thereabouts. Liquidity is deposited on the UI here https://notional.finance/provide. At the time of writing, the yields for USDC deposits are about 58% APR for naked deposits and 233% when the deposits are folded 4x. In the past, Pickle has developed folding strategies on top of Aave and Compound (see the links below), which can be adapted for farming on top of Notional. See the references below for some examples: Medium article explainer: https://picklefinance.medium.com/our-first-single-asset-picklejar-a35182cb0905 Aave DAI folding strategy: https://github.com/pickle-finance/protocol/blob/master/src/strategies/polygon/aave/strategy-aave-dai-v3.sol https://polygonscan.com/address/0x0b198b5EE64aB29c98A094380c867079d5a1682e ## Technical Details All actions on Notional route through their `nProxy` contract at [here](https://etherscan.io/address/0x1344a36a1b56144c3bc62e7757377d288fde0369#code). The implementation contract for the `nProxy` contract is [here](https://etherscan.io/address/0x19152dda25a96d0ca244f0d7f3f13a966f392b23#code), which interfaces with other implementation contracts by way of `delegateCalls`. The destination for deposit/withdraw functions is the `BatchAction` contract [here](https://etherscan.io/address/0x63757f772b8aab66f4de09617e8f257249efcb93#code). ### Step 1: Deposit The first step involves depositing the token we wish to fold into Notional, which serves as the initial liquidity we can borrow against. For the purpose of this document, we will use USDC. An example deposit transaction is seen [here](https://eth.tokenview.com/en/tx/0x6ae9426d20082b648cc1862183b2bb7c4f26f413d2389bcc148725b3dda1033c). Because the transaction data is encoded, it is helpful to decode it using a visual debugger such as [Tenderly](https://dashboard.tenderly.co/tx/mainnet/0x4a368d362cd78d0a3fa982bf5644af53263d5629c6b0c665b3d2ca7fef6a21ec/debugger?trace=0.1.0). The destination of the `delegateCall` is the `BatchAction` contract [here](https://etherscan.io/address/0x63757f772b8aab66f4de09617e8f257249efcb93#code), specifically the `batchBalanceAction` function. ![](https://i.imgur.com/biP4Kb8.png) We observe that the originating call is serialized according to the struct `BalanceAction` (line 42): ```solidity= struct BalanceAction { // Deposit action to take (if any) DepositActionType actionType; uint16 currencyId; // Deposit action amount must correspond to the depositActionType, see documentation above. uint256 depositActionAmount; // Withdraw an amount of asset cash specified in Notional internal 8 decimal precision uint256 withdrawAmountInternalPrecision; // If set to true, will withdraw entire cash balance. Useful if there may be an unknown amount of asset cash // residual left from trading. bool withdrawEntireCashBalance; // If set to true, will redeem asset cash to the underlying token on withdraw. bool redeemToUnderlying; } ``` ### Step 2: Having deposited in Step 1, we now have collateral against which we can borrow more USDC. An example transaction is [here](https://etherscan.io/tx/0x6ae9426d20082b648cc1862183b2bb7c4f26f413d2389bcc148725b3dda1033c), with the stack trace [here](https://dashboard.tenderly.co/tx/mainnet/0x6ae9426d20082b648cc1862183b2bb7c4f26f413d2389bcc148725b3dda1033c/debugger?trace=0.1.0.0). ![](https://i.imgur.com/HVr5Jgv.png) The function signature is `batchBalanceAndTradeAction` and we observe that the input data is similar to that of step 1, but now the input argument `BalanceActionWithTrades` has an additional struct property `bytes32[] trades` through which the requested borrow amount can be encoded. ### Step 3: Repeat steps 1-2 until leveraged to the target ### Step 4: Claim NOTE rewards (steps 4-5 are performed within the strategy `harvest` function) Claim using `nTokenClaimIncentives` in `nTokenAction` [here](https://etherscan.io/address/0x1cd33f81420bfce8a3622dc56bbd617b7fa3c93a#writeContract). Example tx [here](https://etherscan.io/tx/0x3a64be9d1f585bbce04a369814787f53a075e3c3330c055b1b9c6c63c713cdf4). ### Step 5: Sell NOTE rewards for USDC on Balancer and deposit USDC as liquidity. Example tx [here](https://etherscan.io/tx/0x9b7619e713b8a6df5b6c3a57dbb25738fd9a4cfeb71799e77910bae127756b49/advanced). ### Additional Contract Considerations It is necessary to know the internal balances of the strategy contract to properly leverage or deleverage to a suitable target. This can be done by inheriting from `BatchAction`, and particularly the `BalanceHandler.sol` library for the following data: ```solidity= function getBalanceStorage(address account, uint256 currencyId) internal view returns ( int256 cashBalance, int256 nTokenBalance, uint256 lastClaimTime, uint256 lastClaimIntegralSupply ) ``` The amount of available collateral for borrowing can be found in `FreeCollateralExternal.sol` ```solidity= /// @notice Returns the ETH denominated free collateral of an account, represents the amount of /// debt that the account can incur before liquidation. If an account's assets need to be settled this /// will revert, either settle the account or use the off chain SDK to calculate free collateral. /// @dev Called via the Views.sol method to return an account's free collateral. Does not work /// for the nToken, the nToken does not have an account context. /// @param account account to calculate free collateral for /// @return total free collateral in ETH w/ 8 decimal places /// @return array of net local values in asset values ordered by currency id function getFreeCollateralView(address account) external view returns (int256, int256[] memory) { ``` ## Deliverables / Acceptance Criteria - [ ] This Jar (vault) will accept deposits of USDC tokens (see the Jar corresponding to the Aave DAI folding strategy [here](https://polygonscan.com/address/0x0519848e57Ba0469AA5275283ec0712c91e20D8E#code)) - [ ] The Strategy should adapt the existing folding strategies and implement steps 1-5 which are specific to Notional's contracts - [ ] The Strategy should implement all of the same external functions as the Aave DAI folding strategy - [ ] A passing unit test containing all of the functions [here](https://github.com/pickle-finance/protocol/blob/master/src/tests/strategies/compound/strategy-cmpnd-dai-v3.sol). Feel free to use the testing framework of your choice