Batch flash Loans
A batch flash loan enables the operator to lend itself a very large amount of assets for the duration of a batch. This capacity is fundamental to enabling efficient Automated Market Maker (AMM) capabilities, as described in the dAMM design pattern. This lending functionality is essentially an accounting trick. Because StarkEx only outputs balance differences, the operator can swap with itself 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: 1015 TK_A -→ 0 TK___B
L1L2 #2: 0 TK_B -→ 1015 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 1015 TK_A on their off-chain Vault, and the L1 Vault has -1015 TK_A
Notice, since we don’t provide any TK_B, the operator may reuse the L1LOs as a partial fulfillment.