{
    "name": "Loan",
    "desc": "Combines multiple pools together to allows loans to be taken out where some of these pool assets are collateral and some of the pool assets are borrowed.",
    "methods": [
        {
            "name": "create_loan",
            "desc": "Add a loan escrow for a user. The escrow opts in and rekeys itself to the loan application.",
            "args": [
                {
                    "type": "pay",
                    "name": "user_call",
                    "desc": "The transaction from the user to approve the loan escrow."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "add_collateral",
            "desc": "Add support for a collateral in a loan escrow.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool application to add support for."
                },
                {
                    "type": "uint8",
                    "name": "pool_manager_index",
                    "desc": "The index of the pool in the pool manager array."
                },
                {
                    "type": "uint8",
                    "name": "loan_index",
                    "desc": "The index of the pool in the loan array."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "sync_collateral",
            "desc": "Sync the collateral balance of a loan's given collateral. Should be proceeded with transaction sending the collateral to the loan escrow in the same group transaction.",
            "args": [
                {
                    "type": "appl",
                    "name": "refresh_prices",
                    "desc": "The transaction to refresh the pool's asset price."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool application to sync."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the loan."
                },
                {
                    "type": "application",
                    "name": "oracle_adapter",
                    "desc": "The oracle adapter application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "reduce_collateral",
            "desc": "Reduce the collateral of a loan.",
            "args": [
                {
                    "type": "appl",
                    "name": "refresh_prices",
                    "desc": "The transaction to refresh the loan's asset prices."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's escrow."
                },
                {
                    "type": "account",
                    "name": "receiver",
                    "desc": "The account to receive the f asset sent from the loan escrow."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "uint64",
                    "name": "amount",
                    "desc": "The amount to reduce the collateral by."
                },
                {
                    "type": "bool",
                    "name": "is_f_asset_amount",
                    "desc": "Whether the amount of collateral to reduce by is expressed in terms of f asset or asset."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool to reduce the collateral of."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                },
                {
                    "type": "application",
                    "name": "oracle_adapter",
                    "desc": "The oracle adapter application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "swap_collateral_begin",
            "desc": "Withdraw collateral from a loan escrow without checking if under-collateralized. Must be groped together with swap_collateral_end method call.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "account",
                    "name": "receiver",
                    "desc": "The account to receive the f asset sent from the loan escrow."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "uint64",
                    "name": "amount",
                    "desc": "The amount of collateral to swap."
                },
                {
                    "type": "bool",
                    "name": "is_f_asset_amount",
                    "desc": "Whether the amount of collateral to swap is expressed in terms of f asset or asset."
                },
                {
                    "type": "uint64",
                    "name": "txn_index",
                    "desc": "The transaction index in the group transaction for the swap_collateral_end method call."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool to swap the collateral of."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "swap_collateral_end",
            "desc": "Finalise a swap collateral. Must be grouped together with swap_collateral_begin method call.",
            "args": [
                {
                    "type": "appl",
                    "name": "refresh_prices",
                    "desc": "The transaction to refresh the loan's asset prices."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                },
                {
                    "type": "application",
                    "name": "oracle_adapter",
                    "desc": "The oracle adapter application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "remove_collateral",
            "desc": "Remove support for a collateral in a loan escrow.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool application to remove support for."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "borrow",
            "desc": "Borrow an asset using collateral of a loan escrow.",
            "args": [
                {
                    "type": "appl",
                    "name": "refresh_prices",
                    "desc": "The transaction to refresh the loan's asset prices."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "account",
                    "name": "receiver",
                    "desc": "The account to receive the asset sent by the pool."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "uint64",
                    "name": "amount",
                    "desc": "The amount to borrow."
                },
                {
                    "type": "uint64",
                    "name": "max_stable_rate",
                    "desc": "The maximum stable rate of the borrow. If zero then borrow is interpreted as a variable rate borrow."
                },
                {
                    "type": "uint8",
                    "name": "pool_manager_index",
                    "desc": "The index of the pool in the pool manager array."
                },
                {
                    "type": "uint8",
                    "name": "loan_index",
                    "desc": "The index of the pool in the loan array."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool to borrow from."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                },
                {
                    "type": "application",
                    "name": "oracle_adapter",
                    "desc": "The oracle adapter application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "switch_borrow_type",
            "desc": "Switch the borrow type of a borrow from variable to stable or stable to variable.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "uint64",
                    "name": "max_stable_rate",
                    "desc": "The maximum stable rate to switch the borrow to. If zero then interpreted as switching a stable rate borrow to a variable rate borrow."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool application so switch the borrow type of."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "repay_with_txn",
            "desc": "Repay a borrow using an asset transfer transaction.",
            "args": [
                {
                    "type": "txn",
                    "name": "send_asset_txn",
                    "desc": "The transaction to the pool to repay the borrow."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "account",
                    "name": "receiver",
                    "desc": "The account to receive the fr asset rewards if there are any."
                },
                {
                    "type": "account",
                    "name": "reserve",
                    "desc": "The account to receive the protocol revenue from the percentage of the accrued interest."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "asset",
                    "name": "fr_asset",
                    "desc": "The fr asset of the pool."
                },
                {
                    "type": "bool",
                    "name": "is_stable",
                    "desc": "Whether the borrow that is being repaid is a stable or variable rate borrow."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool to repay."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "repay_with_collateral",
            "desc": "Repay a borrow using existing collateral in the loan escrow.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "account",
                    "name": "receiver",
                    "desc": "The account to receive the fr asset rewards if there are any."
                },
                {
                    "type": "account",
                    "name": "reserve",
                    "desc": "The account to receive the protocol revenue from the percentage of the accrued interest."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the pool."
                },
                {
                    "type": "asset",
                    "name": "fr_asset",
                    "desc": "The fr asset of the pool."
                },
                {
                    "type": "uint64",
                    "name": "amount",
                    "desc": "The amount to repay expressed in terms of the asset."
                },
                {
                    "type": "bool",
                    "name": "is_stable",
                    "desc": "Whether the borrow that is being repaid is a stable or variable rate borrow."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool to repay and collateral to use."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "liquidate",
            "desc": "Liquidate a borrow and seize collateral from the loan escrow.",
            "args": [
                {
                    "type": "appl",
                    "name": "refresh_prices",
                    "desc": "The transaction to refresh the loan's asset prices."
                },
                {
                    "type": "txn",
                    "name": "send_asset_txn",
                    "desc": "The transaction to the pool to repay the borrow."
                },
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                },
                {
                    "type": "account",
                    "name": "reserve",
                    "desc": "The account to receive the protocol revenue from the percentage of the accrued interest."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the borrow pool."
                },
                {
                    "type": "asset",
                    "name": "f_asset",
                    "desc": "The f asset of the collateral pool."
                },
                {
                    "type": "uint64",
                    "name": "min_col_amount",
                    "desc": "The minimum collateral amount for the liquidator to receive."
                },
                {
                    "type": "bool",
                    "name": "is_stable",
                    "desc": "Whether the borrow that is being repaid is a stable or variable rate borrow."
                },
                {
                    "type": "application",
                    "name": "col_pool",
                    "desc": "The pool whose collateral is seized."
                },
                {
                    "type": "application",
                    "name": "bor_pool",
                    "desc": "The pool whose borrow is repaid."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                },
                {
                    "type": "application",
                    "name": "oracle_adapter",
                    "desc": "The oracle adapter application of the loan."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "rebalance_up",
            "desc": "Increase the stable interest rate of a borrow through rebalancing",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The loan escrow whose borrow to rebalance up."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool whose borrow to rebalance up."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "rebalance_down",
            "desc": "Decrease the stable interest rate of a borrow through rebalancing",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The loan whose borrow to rebalance down escrow."
                },
                {
                    "type": "asset",
                    "name": "asset",
                    "desc": "The asset of the pool."
                },
                {
                    "type": "application",
                    "name": "pool",
                    "desc": "The pool whose borrow to rebalance down."
                },
                {
                    "type": "application",
                    "name": "pool_manager",
                    "desc": "The pool manager application of the pool."
                }
            ],
            "returns": {
                "type": "void"
            }
        },
        {
            "name": "remove_loan",
            "desc": "Remove a loan escrow for a user and return its minimum balance.",
            "args": [
                {
                    "type": "account",
                    "name": "escrow",
                    "desc": "The user's loan escrow."
                }
            ],
            "returns": {
                "type": "void"
            }
        }
    ],
    "networks": {}
}
