Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ch07-09 FlashLoanBorrowed 事件与 flashloans 表

返回本章

先看交易边界

读这一节时,把“FlashLoanBorrowed 事件与 flashloans 表”放进一个 PTB 里推演。重点不是能不能借出资产,而是 hot potato 如何强迫调用方在同一交易中归还。

源码入口

  • packages/deepbook/sources/pool.move:DeepBookV3 闪电贷唯一 Spot pool 入口,包含 borrow_flashloan_baseborrow_flashloan_quotereturn_flashloan_basereturn_flashloan_quote
  • packages/deepbook/sources/vault/vault.move:底层 FlashLoan hot potato、FlashLoanBorrowed 事件、pool/asset/amount 校验和 Vault 余额 split/join。
  • crates/indexer/src/handlers/flash_loan_handler.rs:把 FlashLoanBorrowed 事件写入 flashloans 表,注意它只记录借出事件。
  • book/ch07/code/s01-flashloan-move-wrappers02-flashloan-ptbs03-flashloan-event-querys04-flashloan-indexer-row:本节对应的 wrapper、PTB、事件和表查询示例。

在一笔交易里走完

vault.borrow_flashloan_baseborrow_flashloan_quote 都发出:

public struct FlashLoanBorrowed has copy, drop {
    pool_id: ID,
    borrow_quantity: u64,
    type_name: TypeName,
}

Indexer 处理器在 crates/indexer/src/handlers/flash_loan_handler.rs 中把事件写入 flashloans 表,字段包括 event_digestdigestsendercheckpointcheckpoint_timestamp_mspackagepool_idborrow_quantityborrow = truetype_name

因为没有 return 事件,分析闪电贷成功与否应以包含该事件的交易最终成功为前提。失败交易不会产生最终可索引事件。

安全边界:本章的闪电贷只指 DeepBookV3 Spot pool 闪电贷,入口在 pool.move,底层在 vault.move。不要把它和 packages/deepbook_margin 的普通借贷混淆;这里没有跨交易债务账户,也没有隔夜未还状态。

FlashLoanBorrowed 是借出方向事件,indexer 落库后只能证明成功交易里发生过 borrow。归还没有独立表行;如果交易最终失败,事件和表记录也不会成为成功状态的一部分。

组合交易提醒

  • 借 base 必须用 return_flashloan_base 归还 base;借 quote 必须用 return_flashloan_quote 归还 quote。
  • FlashLoan 不能跨交易保存,PTB 中必须让 borrow 返回值最终被 return 消费。
  • 事件查询只把 FlashLoanBorrowed 当作成功交易的借出记录,不能推导出 Margin 债务状态。

动手检查

  • FlashLoanBorrowed 事件与 flashloans 表 的 pool.move 入口是哪一个,底层 vault.move 校验哪几个字段?
  • 如果 PTB abort,是 pool id、资产方向、归还数量、hot potato 未消费,还是中间组合步骤失败?
  • 这段逻辑是否仍明确区别 DeepBookV3 闪电贷和 deepbook_margin 普通借贷?