Flash Loans

What are Flash Loans?

Flash loans are a type of uncollateralized loan that must be borrowed and repaid within a single transaction. They are a powerful DeFi tool, enabling users to access large amounts of liquidity for a very short period of time, typically for purposes such as arbitrage, collateral swaps, or refinancing.

Key Features:

  • Supports both BCH and token flash loans.
  • Enforces a 0.09% fee on the borrowed amount.
  • Repayment within the same transaction.
  • Withdrawal mechanism for the contract owner.

For more details, see the FlashLoan.cash contract in the repository.

How do flash loans work?

  • Atomicity: The key property of flash loans is atomicity. The entire loan process i.e borrowing, using, and repaying the funds must occur within one transaction.
  • No Collateral Needed: Unlike traditional loans, flash loans do not require the borrower to provide collateral, since the risk to the lender is mitigated by the atomic nature of the transaction.
  • Use Cases: Flash loans are commonly used for arbitrage opportunities, self-liquidation, collateral swaps, and other advanced financial operations that benefit from instant, temporary liquidity.

Bitauth IDE link for mocknet contract from https://playground.cashscript.org/

4 Likes

For straight Bitcoin Cash liquidity, using NFT authentication would allow all liquidity providers to use the same contract. Each thread could have it’s own flat fee, specified in the nft commitment.

Liquidity providers could stake money on an immutable NFT output, then collect it off with the minting NFT of the same category.

There would be no need to broadcast or index specific contracts by pkh.

With each thread having a different fee, a whole market is created on a single contract location.

There’s interesting market dynamics over time with a flat fee. The rates tend downward. That is, since each time a thread is used, the fees accrue toward the principal, but the rate stays fixed, the effective loan rate drops slightly for the next user. This would create a market dynamic where, over time, threads that are not re-balanced will accumulate a lot of principal, on the same initial flat rate, resulting in a very low effective loan rate.

pragma cashscript 0.11.0;

/**
 * @title FlashLiquidityMarket
 * @notice This contract enables flash loans with a flat fee, only supporting straight BCH loans.
 * @dev The contract ensures that the loan is repaid with the required fee in the same transaction.
 *      It also provides a secure withdrawal function for the contract owner's minting NFT.
 *      Liquidity providers send funds with an immutable NFT to a common address. 
 *      Liquidity providers withdraw threads using a minting NFT from their token series.
 */
contract FlashLiquidityMarket() {

    //
    function loan() {

        // Ensure transaction version is 2 for compatibility and future-proofing
        // OP_TXVERSION OP_2 OP_NUMEQUALVERIFY 
        require(tx.version == 2);

        // If a minitng NFT is given as the first input for a transaction, allow
        //  withdrawing (or rebalancing) all threads with a matching token category.
        // OP_0 OP_UTXOTOKENCATEGORY 
        // OP_INPUTINDEX OP_UTXOTOKENCATEGORY 20 OP_SPLIT OP_DROP OP_2 OP_CAT OP_EQUAL OP_IF
        if(tx.inputs[0].tokenCategory == 
           tx.inputs[this.activeInputIndex].tokenCategory.split(32)[0] + 0x02){
              // threads of matching category are unencumbered by contract rules
        }
        // OP_ELSE
        else{
            // Verify that funds are set back to this contract.
            // OP_INPUTINDEX OP_UTXOBYTECODE 
            // OP_INPUTINDEX OP_OUTPUTBYTECODE OP_EQUALVERIFY 
            require(tx.inputs[this.activeInputIndex].lockingBytecode ==  
                    tx.outputs[this.activeInputIndex].lockingBytecode);

            // Ensure the token category remains unchanged
            // OP_INPUTINDEX OP_UTXOTOKENCATEGORY 
            // OP_INPUTINDEX OP_OUTPUTTOKENCATEGORY OP_EQUALVERIFY 
            require(tx.inputs[this.activeInputIndex].tokenCategory == 
                    tx.outputs[this.activeInputIndex].tokenCategory);

            // The owner NFT carrying thread sats MUST be immutable.
            // The immutable value is the flat fee.

            // Require the output have an additional flat fee
            // OP_INPUTINDEX OP_OUTPUTVALUE 
            // OP_INPUTINDEX OP_UTXOVALUE 
            // OP_UTXOTOKENCOMMITMENT OP_BIN2NUM OP_ADD OP_NUMEQUALVERIFY
            require(tx.outputs[this.activeInputIndex].value == 
                    tx.inputs[this.activeInputIndex].value + 
                    int(tx.inputs[this.activeInputIndex].nftCommitment));
        }
    } // OP_ENDIF OP_1 
    
}
1 Like