Batch flash loans offer the capacity for the Operator to lend themselves a very large amount of assets for the duration of a batch. This capacity is fundamental to enable efficient Automated Market Maker (AMM) capabilities as described in the dAMM design pattern. Such "loan" functionality is essentially an accounting trick. Since StarkEx only outputs balance differences, the Operator can swap with themselves from L1 to L2 and back.
Beginning of batch - K TK_A --> 0 TK_B (K being a large amount)
End of batch - 0 TK_B --> K TK_A
The batch flash loan feature can be activated or deactivated by setting parameters at contract deployment. When deactivated, the contract checks the maximum negative balance the L1 vault held during the batch and rejects the transaction if the difference is greater than the amount in the L1 vault.
To flash loan TK_A, the Operator creates 2 L1 Limit Orders (known as L1LOs)
L1LO #1: 10**15 TK_A --> 0 TK___B
L1L2 #2: 0 TK_B --> 10**15 TK___A
To create a loan, the Operator must follow these steps. For brevity, we call an "off-chain Limit Order", L2LO.
Operator signs a L2LO#3: 0 TK_B --> 0 TK___A
Operator matches L1LO#1 with L2LO#3
Operator now owns 10**15 TK_A on their off-chain vault, and the L1 vault has -10**15 TK_A
Notice, since we don't provide any TK_B, the Operator may reuse the L1LOs as a partial fulfillment.
To reimburse the loan, the Operator follows these steps.
Operator signs a L2LO#4: 10**15 TK_A --> 0 TK___B
Operator matches L1LO#2 with L2LO#4
L1 vault net balance is now 0 for both TK_A and TK_B