@gvnrdao/dh-lit-actions 0.0.60 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/package.json +3 -3
  2. package/pkg-dist/pkg-src/constants/chunks/contract-deployments.d.ts.map +1 -0
  3. package/pkg-dist/pkg-src/constants/chunks/contract-deployments.js.map +1 -0
  4. package/pkg-dist/pkg-src/constants/chunks/lit-actions-registry.d.ts.map +1 -0
  5. package/pkg-dist/pkg-src/constants/chunks/lit-actions-registry.js.map +1 -0
  6. package/pkg-dist/pkg-src/constants/chunks/package-registry.d.ts.map +1 -0
  7. package/pkg-dist/pkg-src/constants/chunks/package-registry.js.map +1 -0
  8. package/pkg-dist/pkg-src/constants/index.d.ts.map +1 -0
  9. package/pkg-dist/pkg-src/constants/index.js.map +1 -0
  10. package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.d.ts +40 -0
  11. package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.d.ts.map +1 -0
  12. package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.js +102 -0
  13. package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.js.map +1 -0
  14. package/pkg-dist/pkg-src/executors/index.d.ts +43 -0
  15. package/pkg-dist/pkg-src/executors/index.d.ts.map +1 -0
  16. package/pkg-dist/pkg-src/executors/index.js +48 -0
  17. package/pkg-dist/pkg-src/executors/index.js.map +1 -0
  18. package/pkg-dist/pkg-src/index.d.ts.map +1 -0
  19. package/pkg-dist/pkg-src/index.js.map +1 -0
  20. package/pkg-dist/pkg-src/interfaces/chunks/diamond-hands-lit-actions.i.d.ts.map +1 -0
  21. package/pkg-dist/pkg-src/interfaces/chunks/diamond-hands-lit-actions.i.js.map +1 -0
  22. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-config.i.d.ts.map +1 -0
  23. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-config.i.js.map +1 -0
  24. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-name.i.d.ts.map +1 -0
  25. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-name.i.js.map +1 -0
  26. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-registry.i.d.ts.map +1 -0
  27. package/pkg-dist/pkg-src/interfaces/chunks/lit-action-registry.i.js.map +1 -0
  28. package/pkg-dist/pkg-src/interfaces/chunks/pkp-info.i.d.ts.map +1 -0
  29. package/pkg-dist/pkg-src/interfaces/chunks/pkp-info.i.js.map +1 -0
  30. package/pkg-dist/pkg-src/interfaces/deployments.d.ts.map +1 -0
  31. package/pkg-dist/pkg-src/interfaces/deployments.js.map +1 -0
  32. package/pkg-dist/pkg-src/interfaces/index.d.ts.map +1 -0
  33. package/pkg-dist/pkg-src/interfaces/index.js.map +1 -0
  34. package/pkg-dist/pkg-src/utils/chunks/cid-utils.d.ts.map +1 -0
  35. package/pkg-dist/pkg-src/utils/chunks/cid-utils.js.map +1 -0
  36. package/pkg-dist/pkg-src/utils/chunks/connection-helpers.d.ts.map +1 -0
  37. package/pkg-dist/pkg-src/utils/chunks/connection-helpers.js.map +1 -0
  38. package/pkg-dist/pkg-src/utils/chunks/debug-logger.d.ts.map +1 -0
  39. package/pkg-dist/pkg-src/utils/chunks/debug-logger.js.map +1 -0
  40. package/pkg-dist/pkg-src/utils/chunks/error-classification.d.ts.map +1 -0
  41. package/pkg-dist/pkg-src/utils/chunks/error-classification.js.map +1 -0
  42. package/pkg-dist/pkg-src/utils/chunks/lit-action-helpers.d.ts.map +1 -0
  43. package/pkg-dist/pkg-src/utils/chunks/lit-action-helpers.js.map +1 -0
  44. package/pkg-dist/pkg-src/utils/chunks/pkp-setup.d.ts.map +1 -0
  45. package/pkg-dist/pkg-src/utils/chunks/pkp-setup.js.map +1 -0
  46. package/pkg-dist/pkg-src/utils/chunks/session-signature-cache.d.ts.map +1 -0
  47. package/pkg-dist/pkg-src/utils/chunks/session-signature-cache.js.map +1 -0
  48. package/pkg-dist/pkg-src/utils/index.d.ts.map +1 -0
  49. package/pkg-dist/pkg-src/utils/index.js.map +1 -0
  50. package/pkg-dist/src/constants/chunks/bitcoin-network-config.d.ts +130 -0
  51. package/pkg-dist/src/constants/chunks/bitcoin-network-config.d.ts.map +1 -0
  52. package/pkg-dist/src/constants/chunks/bitcoin-network-config.js +244 -0
  53. package/pkg-dist/src/constants/chunks/bitcoin-network-config.js.map +1 -0
  54. package/pkg-dist/src/constants/chunks/bitcoin.d.ts +42 -0
  55. package/pkg-dist/src/constants/chunks/bitcoin.d.ts.map +1 -0
  56. package/pkg-dist/src/constants/chunks/bitcoin.js +45 -0
  57. package/pkg-dist/src/constants/chunks/bitcoin.js.map +1 -0
  58. package/pkg-dist/src/constants/chunks/decimals.d.ts +44 -0
  59. package/pkg-dist/src/constants/chunks/decimals.d.ts.map +1 -0
  60. package/pkg-dist/src/constants/chunks/decimals.js +47 -0
  61. package/pkg-dist/src/constants/chunks/decimals.js.map +1 -0
  62. package/pkg-dist/src/constants/chunks/liquidation.d.ts +51 -0
  63. package/pkg-dist/src/constants/chunks/liquidation.d.ts.map +1 -0
  64. package/pkg-dist/src/constants/chunks/liquidation.js +54 -0
  65. package/pkg-dist/src/constants/chunks/liquidation.js.map +1 -0
  66. package/pkg-dist/src/constants/chunks/loan-status.d.ts +57 -0
  67. package/pkg-dist/src/constants/chunks/loan-status.d.ts.map +1 -0
  68. package/pkg-dist/src/constants/chunks/loan-status.js +95 -0
  69. package/pkg-dist/src/constants/chunks/loan-status.js.map +1 -0
  70. package/pkg-dist/src/constants/chunks/price-oracle.d.ts +46 -0
  71. package/pkg-dist/src/constants/chunks/price-oracle.d.ts.map +1 -0
  72. package/pkg-dist/src/constants/chunks/price-oracle.js +49 -0
  73. package/pkg-dist/src/constants/chunks/price-oracle.js.map +1 -0
  74. package/pkg-dist/src/constants/chunks/quantum-time.d.ts +63 -0
  75. package/pkg-dist/src/constants/chunks/quantum-time.d.ts.map +1 -0
  76. package/pkg-dist/src/constants/chunks/quantum-time.js +66 -0
  77. package/pkg-dist/src/constants/chunks/quantum-time.js.map +1 -0
  78. package/pkg-dist/src/constants/index.d.ts +16 -0
  79. package/pkg-dist/src/constants/index.d.ts.map +1 -0
  80. package/pkg-dist/src/constants/index.js +57 -0
  81. package/pkg-dist/src/constants/index.js.map +1 -0
  82. package/pkg-dist/src/interfaces/chunks/authorization.i.d.ts +19 -0
  83. package/pkg-dist/src/interfaces/chunks/authorization.i.d.ts.map +1 -0
  84. package/pkg-dist/src/interfaces/chunks/authorization.i.js +3 -0
  85. package/pkg-dist/src/interfaces/chunks/authorization.i.js.map +1 -0
  86. package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.d.ts +60 -0
  87. package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.d.ts.map +1 -0
  88. package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.js +3 -0
  89. package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.js.map +1 -0
  90. package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.d.ts +18 -0
  91. package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.d.ts.map +1 -0
  92. package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.js +8 -0
  93. package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.js.map +1 -0
  94. package/pkg-dist/src/interfaces/chunks/mint-authorization.i.d.ts +24 -0
  95. package/pkg-dist/src/interfaces/chunks/mint-authorization.i.d.ts.map +1 -0
  96. package/pkg-dist/src/interfaces/chunks/mint-authorization.i.js +5 -0
  97. package/pkg-dist/src/interfaces/chunks/mint-authorization.i.js.map +1 -0
  98. package/pkg-dist/src/interfaces/chunks/owner-authorization.d.ts +60 -0
  99. package/pkg-dist/src/interfaces/chunks/owner-authorization.d.ts.map +1 -0
  100. package/pkg-dist/src/interfaces/chunks/owner-authorization.js +8 -0
  101. package/pkg-dist/src/interfaces/chunks/owner-authorization.js.map +1 -0
  102. package/pkg-dist/src/interfaces/chunks/price-oracle.i.d.ts +26 -0
  103. package/pkg-dist/src/interfaces/chunks/price-oracle.i.d.ts.map +1 -0
  104. package/pkg-dist/src/interfaces/chunks/price-oracle.i.js +3 -0
  105. package/pkg-dist/src/interfaces/chunks/price-oracle.i.js.map +1 -0
  106. package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.d.ts +23 -0
  107. package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.d.ts.map +1 -0
  108. package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.js +3 -0
  109. package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.js.map +1 -0
  110. package/pkg-dist/src/interfaces/chunks/vault-balance.i.d.ts +63 -0
  111. package/pkg-dist/src/interfaces/chunks/vault-balance.i.d.ts.map +1 -0
  112. package/pkg-dist/src/interfaces/chunks/vault-balance.i.js +3 -0
  113. package/pkg-dist/src/interfaces/chunks/vault-balance.i.js.map +1 -0
  114. package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.d.ts +51 -0
  115. package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.d.ts.map +1 -0
  116. package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.js +3 -0
  117. package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.js.map +1 -0
  118. package/pkg-dist/src/interfaces/index.d.ts +15 -0
  119. package/pkg-dist/src/interfaces/index.d.ts.map +1 -0
  120. package/pkg-dist/src/interfaces/index.js +30 -0
  121. package/pkg-dist/src/interfaces/index.js.map +1 -0
  122. package/pkg-dist/src/modules/authorization.module.d.ts +160 -0
  123. package/pkg-dist/src/modules/authorization.module.d.ts.map +1 -0
  124. package/pkg-dist/src/modules/authorization.module.js +317 -0
  125. package/pkg-dist/src/modules/authorization.module.js.map +1 -0
  126. package/pkg-dist/src/modules/bitcoin-data-provider.module.d.ts +91 -0
  127. package/pkg-dist/src/modules/bitcoin-data-provider.module.d.ts.map +1 -0
  128. package/pkg-dist/src/modules/bitcoin-data-provider.module.js +261 -0
  129. package/pkg-dist/src/modules/bitcoin-data-provider.module.js.map +1 -0
  130. package/pkg-dist/src/modules/business-rules-math.module.d.ts +155 -0
  131. package/pkg-dist/src/modules/business-rules-math.module.d.ts.map +1 -0
  132. package/pkg-dist/src/modules/business-rules-math.module.js +309 -0
  133. package/pkg-dist/src/modules/business-rules-math.module.js.map +1 -0
  134. package/pkg-dist/src/modules/price-oracle.module.d.ts +29 -0
  135. package/pkg-dist/src/modules/price-oracle.module.d.ts.map +1 -0
  136. package/pkg-dist/src/modules/price-oracle.module.js +223 -0
  137. package/pkg-dist/src/modules/price-oracle.module.js.map +1 -0
  138. package/pkg-dist/src/modules/protocol-parameters.module.d.ts +58 -0
  139. package/pkg-dist/src/modules/protocol-parameters.module.d.ts.map +1 -0
  140. package/pkg-dist/src/modules/protocol-parameters.module.js +125 -0
  141. package/pkg-dist/src/modules/protocol-parameters.module.js.map +1 -0
  142. package/pkg-dist/src/modules/quantum-time.module.d.ts +149 -0
  143. package/pkg-dist/src/modules/quantum-time.module.d.ts.map +1 -0
  144. package/pkg-dist/src/modules/quantum-time.module.js +240 -0
  145. package/pkg-dist/src/modules/quantum-time.module.js.map +1 -0
  146. package/pkg-dist/src/modules/vault-balance.module.d.ts +172 -0
  147. package/pkg-dist/src/modules/vault-balance.module.d.ts.map +1 -0
  148. package/pkg-dist/src/modules/vault-balance.module.js +318 -0
  149. package/pkg-dist/src/modules/vault-balance.module.js.map +1 -0
  150. package/pkg-dist/src/modules/vault-snapshot.d.ts +168 -0
  151. package/pkg-dist/src/modules/vault-snapshot.d.ts.map +1 -0
  152. package/pkg-dist/src/modules/vault-snapshot.js +370 -0
  153. package/pkg-dist/src/modules/vault-snapshot.js.map +1 -0
  154. package/pkg-dist/constants/chunks/contract-deployments.d.ts.map +0 -1
  155. package/pkg-dist/constants/chunks/contract-deployments.js.map +0 -1
  156. package/pkg-dist/constants/chunks/lit-actions-registry.d.ts.map +0 -1
  157. package/pkg-dist/constants/chunks/lit-actions-registry.js.map +0 -1
  158. package/pkg-dist/constants/chunks/package-registry.d.ts.map +0 -1
  159. package/pkg-dist/constants/chunks/package-registry.js.map +0 -1
  160. package/pkg-dist/constants/index.d.ts.map +0 -1
  161. package/pkg-dist/constants/index.js.map +0 -1
  162. package/pkg-dist/index.d.ts.map +0 -1
  163. package/pkg-dist/index.js.map +0 -1
  164. package/pkg-dist/interfaces/chunks/diamond-hands-lit-actions.i.d.ts.map +0 -1
  165. package/pkg-dist/interfaces/chunks/diamond-hands-lit-actions.i.js.map +0 -1
  166. package/pkg-dist/interfaces/chunks/lit-action-config.i.d.ts.map +0 -1
  167. package/pkg-dist/interfaces/chunks/lit-action-config.i.js.map +0 -1
  168. package/pkg-dist/interfaces/chunks/lit-action-name.i.d.ts.map +0 -1
  169. package/pkg-dist/interfaces/chunks/lit-action-name.i.js.map +0 -1
  170. package/pkg-dist/interfaces/chunks/lit-action-registry.i.d.ts.map +0 -1
  171. package/pkg-dist/interfaces/chunks/lit-action-registry.i.js.map +0 -1
  172. package/pkg-dist/interfaces/chunks/pkp-info.i.d.ts.map +0 -1
  173. package/pkg-dist/interfaces/chunks/pkp-info.i.js.map +0 -1
  174. package/pkg-dist/interfaces/deployments.d.ts.map +0 -1
  175. package/pkg-dist/interfaces/deployments.js.map +0 -1
  176. package/pkg-dist/interfaces/index.d.ts.map +0 -1
  177. package/pkg-dist/interfaces/index.js.map +0 -1
  178. package/pkg-dist/utils/chunks/cid-utils.d.ts.map +0 -1
  179. package/pkg-dist/utils/chunks/cid-utils.js.map +0 -1
  180. package/pkg-dist/utils/chunks/connection-helpers.d.ts.map +0 -1
  181. package/pkg-dist/utils/chunks/connection-helpers.js.map +0 -1
  182. package/pkg-dist/utils/chunks/debug-logger.d.ts.map +0 -1
  183. package/pkg-dist/utils/chunks/debug-logger.js.map +0 -1
  184. package/pkg-dist/utils/chunks/error-classification.d.ts.map +0 -1
  185. package/pkg-dist/utils/chunks/error-classification.js.map +0 -1
  186. package/pkg-dist/utils/chunks/lit-action-helpers.d.ts.map +0 -1
  187. package/pkg-dist/utils/chunks/lit-action-helpers.js.map +0 -1
  188. package/pkg-dist/utils/chunks/pkp-setup.d.ts.map +0 -1
  189. package/pkg-dist/utils/chunks/pkp-setup.js.map +0 -1
  190. package/pkg-dist/utils/chunks/session-signature-cache.d.ts.map +0 -1
  191. package/pkg-dist/utils/chunks/session-signature-cache.js.map +0 -1
  192. package/pkg-dist/utils/index.d.ts.map +0 -1
  193. package/pkg-dist/utils/index.js.map +0 -1
  194. /package/pkg-dist/{constants → pkg-src/constants}/chunks/contract-deployments.d.ts +0 -0
  195. /package/pkg-dist/{constants → pkg-src/constants}/chunks/contract-deployments.js +0 -0
  196. /package/pkg-dist/{constants → pkg-src/constants}/chunks/lit-actions-registry.d.ts +0 -0
  197. /package/pkg-dist/{constants → pkg-src/constants}/chunks/lit-actions-registry.js +0 -0
  198. /package/pkg-dist/{constants → pkg-src/constants}/chunks/package-registry.d.ts +0 -0
  199. /package/pkg-dist/{constants → pkg-src/constants}/chunks/package-registry.js +0 -0
  200. /package/pkg-dist/{constants → pkg-src/constants}/index.d.ts +0 -0
  201. /package/pkg-dist/{constants → pkg-src/constants}/index.js +0 -0
  202. /package/pkg-dist/{index.d.ts → pkg-src/index.d.ts} +0 -0
  203. /package/pkg-dist/{index.js → pkg-src/index.js} +0 -0
  204. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/diamond-hands-lit-actions.i.d.ts +0 -0
  205. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/diamond-hands-lit-actions.i.js +0 -0
  206. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-config.i.d.ts +0 -0
  207. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-config.i.js +0 -0
  208. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-name.i.d.ts +0 -0
  209. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-name.i.js +0 -0
  210. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-registry.i.d.ts +0 -0
  211. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-registry.i.js +0 -0
  212. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/pkp-info.i.d.ts +0 -0
  213. /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/pkp-info.i.js +0 -0
  214. /package/pkg-dist/{interfaces → pkg-src/interfaces}/deployments.d.ts +0 -0
  215. /package/pkg-dist/{interfaces → pkg-src/interfaces}/deployments.js +0 -0
  216. /package/pkg-dist/{interfaces → pkg-src/interfaces}/index.d.ts +0 -0
  217. /package/pkg-dist/{interfaces → pkg-src/interfaces}/index.js +0 -0
  218. /package/pkg-dist/{utils → pkg-src/utils}/chunks/cid-utils.d.ts +0 -0
  219. /package/pkg-dist/{utils → pkg-src/utils}/chunks/cid-utils.js +0 -0
  220. /package/pkg-dist/{utils → pkg-src/utils}/chunks/connection-helpers.d.ts +0 -0
  221. /package/pkg-dist/{utils → pkg-src/utils}/chunks/connection-helpers.js +0 -0
  222. /package/pkg-dist/{utils → pkg-src/utils}/chunks/debug-logger.d.ts +0 -0
  223. /package/pkg-dist/{utils → pkg-src/utils}/chunks/debug-logger.js +0 -0
  224. /package/pkg-dist/{utils → pkg-src/utils}/chunks/error-classification.d.ts +0 -0
  225. /package/pkg-dist/{utils → pkg-src/utils}/chunks/error-classification.js +0 -0
  226. /package/pkg-dist/{utils → pkg-src/utils}/chunks/lit-action-helpers.d.ts +0 -0
  227. /package/pkg-dist/{utils → pkg-src/utils}/chunks/lit-action-helpers.js +0 -0
  228. /package/pkg-dist/{utils → pkg-src/utils}/chunks/pkp-setup.d.ts +0 -0
  229. /package/pkg-dist/{utils → pkg-src/utils}/chunks/pkp-setup.js +0 -0
  230. /package/pkg-dist/{utils → pkg-src/utils}/chunks/session-signature-cache.d.ts +0 -0
  231. /package/pkg-dist/{utils → pkg-src/utils}/chunks/session-signature-cache.js +0 -0
  232. /package/pkg-dist/{utils → pkg-src/utils}/index.d.ts +0 -0
  233. /package/pkg-dist/{utils → pkg-src/utils}/index.js +0 -0
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Vault Balance Module - Super Module for Trusted Balance Calculation
3
+ *
4
+ * Combines Bitcoin network data with smart contract state to calculate
5
+ * the "trusted balance" - the amount available for new authorizations.
6
+ *
7
+ * Formula: Available Balance = Total UTXOs - Authorized Spends
8
+ *
9
+ * This is the primary source of truth for determining if a new spending
10
+ * authorization can be created for a given position.
11
+ */
12
+ import { AuthorizedSpend, UTXO, VaultBalanceConfig, VaultBalanceResult } from "../interfaces/chunks/vault-balance.i";
13
+ import { IVaultBalance } from "../interfaces/chunks/vault-balance-module.i";
14
+ /**
15
+ * Vault Balance Module
16
+ *
17
+ * This module is the "super module" that combines:
18
+ * 1. Bitcoin network data (UTXOs from bitcoin-data-provider)
19
+ * 2. Smart contract state (authorized spends from contract)
20
+ *
21
+ * To produce: Trusted Balance = Total - Authorized
22
+ *
23
+ * DESIGN NOTES:
24
+ *
25
+ * 1. Change Output Handling:
26
+ * When a UTXO is spent, the change output will have a DIFFERENT txid.
27
+ * This means we don't need special logic for change - it appears as a new UTXO.
28
+ * The old UTXO (original deposit) will be gone from Bitcoin queries.
29
+ *
30
+ * 2. Authorization is Commitment:
31
+ * Any UTXO in the AuthorizedSpend array is considered committed/consumed.
32
+ * Once authorized, it's the user's responsibility to sign and broadcast.
33
+ * If they don't, that's their problem - not the ecosystem's concern.
34
+ *
35
+ * 3. Security Model - Dual Source of Truth:
36
+ * - Bitcoin Network: What UTXOs actually exist
37
+ * - Smart Contract: What UTXOs are authorized to be spent
38
+ * - Available Balance = Total UTXOs - Authorized UTXOs
39
+ *
40
+ * This prevents double-spending by ensuring the same UTXO cannot be
41
+ * authorized twice. The contract's isAuthorizedSpend mapping enforces this.
42
+ *
43
+ * 4. Race Condition Protection:
44
+ * The FAFO (First Actor Fails Others) model protects against concurrent
45
+ * authorization attempts via state hash commitment in quantum time windows.
46
+ * This module provides the trusted balance INPUT to that authorization process.
47
+ *
48
+ * 5. UTXO Matching:
49
+ * We match authorized spends to UTXOs by (txid, vout) pair.
50
+ * A UTXO is available if it exists in Bitcoin but NOT in authorized spends.
51
+ *
52
+ * 6. Edge Cases:
53
+ * - UTXO authorized but not yet spent: Appears in Bitcoin, appears in contract
54
+ * - UTXO spent: Doesn't appear in Bitcoin, may still appear in contract
55
+ * - New deposit: Appears in Bitcoin, doesn't appear in contract
56
+ */
57
+ export declare class VaultBalanceModule implements IVaultBalance {
58
+ private config;
59
+ private bitcoinProvider;
60
+ constructor(config: VaultBalanceConfig);
61
+ /**
62
+ * Calculate trusted balance for a position
63
+ *
64
+ * This is the main entry point for determining available balance.
65
+ *
66
+ * CRITICAL: Validates that all authorized spends have been properly broadcasted
67
+ * to Bitcoin network with sufficient confirmations. Throws errors if:
68
+ * 1. Authorized transaction not found on network (not broadcasted)
69
+ * 2. Authorized transaction has insufficient confirmations (network updating)
70
+ *
71
+ * @param positionId - The position ID to check
72
+ * @param vaultAddress - Bitcoin address of the vault
73
+ * @returns Complete breakdown of vault balance
74
+ * @throws Error if authorized spends are in invalid state
75
+ */
76
+ calculateTrustedBalance(positionId: string, vaultAddress: string): Promise<VaultBalanceResult>;
77
+ /**
78
+ * Get just the trusted balance amount
79
+ *
80
+ * Convenience method when you only need the number.
81
+ *
82
+ * @param positionId - The position ID to check
83
+ * @param vaultAddress - Bitcoin address of the vault
84
+ * @returns Available balance in satoshis
85
+ */
86
+ getTrustedBalance(positionId: string, vaultAddress: string): Promise<bigint>;
87
+ /**
88
+ * Get available UTXOs that can be used for new authorizations
89
+ *
90
+ * This is useful when you need to select specific UTXOs for authorization.
91
+ *
92
+ * @param positionId - The position ID to check
93
+ * @param vaultAddress - Bitcoin address of the vault
94
+ * @returns Array of available UTXOs
95
+ */
96
+ getAvailableUTXOs(positionId: string, vaultAddress: string): Promise<UTXO[]>;
97
+ /**
98
+ * Check if a specific UTXO is available for authorization
99
+ *
100
+ * @param positionId - The position ID to check
101
+ * @param vaultAddress - Bitcoin address of the vault
102
+ * @param txid - Transaction ID of the UTXO
103
+ * @param vout - Output index of the UTXO
104
+ * @returns True if UTXO is available, false otherwise
105
+ */
106
+ isUTXOAvailable(positionId: string, vaultAddress: string, txid: string, vout: number): Promise<boolean>;
107
+ /**
108
+ * Check if a UTXO is authorized (appears in authorized spends)
109
+ *
110
+ * @param utxo - The UTXO to check
111
+ * @param authorizedSpends - List of authorized spends from contract
112
+ * @returns True if UTXO is authorized, false otherwise
113
+ */
114
+ private isUTXOAuthorized;
115
+ /**
116
+ * Query smart contract for authorized spends
117
+ *
118
+ * Calls the contract's getAuthorizedSpends(positionId) view function
119
+ * to retrieve all authorized Bitcoin spends for this position.
120
+ *
121
+ * Contract struct AuthorizedSpend:
122
+ * - txid: string
123
+ * - vout: uint32
124
+ * - satoshis: uint256
125
+ * - authorizedAt: uint256 (timestamp)
126
+ *
127
+ * @param positionId - The position ID to query
128
+ * @returns Array of authorized spends for this position
129
+ */
130
+ protected getAuthorizedSpendsFromContract(positionId: string): Promise<AuthorizedSpend[]>;
131
+ }
132
+ /**
133
+ * Factory function to create VaultBalanceModule instance
134
+ *
135
+ * @param config - Configuration for the module
136
+ * @returns VaultBalanceModule instance
137
+ */
138
+ export declare function createVaultBalanceModule(config: VaultBalanceConfig): VaultBalanceModule;
139
+ /**
140
+ * USAGE EXAMPLE:
141
+ *
142
+ * ```typescript
143
+ * import { createVaultBalanceModule } from './vault-balance.module';
144
+ * import { BitcoinDataProvider } from './bitcoin-data-provider.module';
145
+ *
146
+ * const bitcoinProvider = new BitcoinDataProvider({
147
+ * providerUrl: 'https://blockstream.info/testnet/api'
148
+ * });
149
+ *
150
+ * const vaultBalance = createVaultBalanceModule({
151
+ * contractAddress: '0x...',
152
+ * chain: 'sepolia',
153
+ * bitcoinProvider,
154
+ * minConfirmations: 6
155
+ * });
156
+ *
157
+ * // Get complete balance breakdown
158
+ * const result = await vaultBalance.calculateTrustedBalance(
159
+ * 'position123',
160
+ * 'tb1q...'
161
+ * );
162
+ *
163
+ * console.log('Total Balance:', result.totalBalance, 'sats');
164
+ * console.log('Authorized Balance:', result.authorizedBalance, 'sats');
165
+ * console.log('Available Balance:', result.availableBalance, 'sats');
166
+ *
167
+ * // Or just get the available amount
168
+ * const available = await vaultBalance.getTrustedBalance('position123', 'tb1q...');
169
+ * console.log('Can authorize:', available, 'sats');
170
+ * ```
171
+ */
172
+ //# sourceMappingURL=vault-balance.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-balance.module.d.ts","sourceRoot":"","sources":["../../../src/modules/vault-balance.module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EACL,eAAe,EACf,IAAI,EAEJ,kBAAkB,EAClB,kBAAkB,EACnB,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAG5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,qBAAa,kBAAmB,YAAW,aAAa;IACtD,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,eAAe,CAAuB;gBAElC,MAAM,EAAE,kBAAkB;IAKtC;;;;;;;;;;;;;;OAcG;IACG,uBAAuB,CAC3B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,kBAAkB,CAAC;IA6E9B;;;;;;;;OAQG;IACG,iBAAiB,CACrB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,MAAM,CAAC;IAKlB;;;;;;;;OAQG;IACG,iBAAiB,CACrB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,EAAE,CAAC;IAKlB;;;;;;;;OAQG;IACG,eAAe,CACnB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,OAAO,CAAC;IAUnB;;;;;;OAMG;IACH,OAAO,CAAC,gBAAgB;IASxB;;;;;;;;;;;;;;OAcG;cACa,+BAA+B,CAC7C,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,eAAe,EAAE,CAAC;CA8F9B;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,kBAAkB,GACzB,kBAAkB,CAEpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG"}
@@ -0,0 +1,318 @@
1
+ "use strict";
2
+ /**
3
+ * Vault Balance Module - Super Module for Trusted Balance Calculation
4
+ *
5
+ * Combines Bitcoin network data with smart contract state to calculate
6
+ * the "trusted balance" - the amount available for new authorizations.
7
+ *
8
+ * Formula: Available Balance = Total UTXOs - Authorized Spends
9
+ *
10
+ * This is the primary source of truth for determining if a new spending
11
+ * authorization can be created for a given position.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.VaultBalanceModule = void 0;
15
+ exports.createVaultBalanceModule = createVaultBalanceModule;
16
+ /**
17
+ * Vault Balance Module
18
+ *
19
+ * This module is the "super module" that combines:
20
+ * 1. Bitcoin network data (UTXOs from bitcoin-data-provider)
21
+ * 2. Smart contract state (authorized spends from contract)
22
+ *
23
+ * To produce: Trusted Balance = Total - Authorized
24
+ *
25
+ * DESIGN NOTES:
26
+ *
27
+ * 1. Change Output Handling:
28
+ * When a UTXO is spent, the change output will have a DIFFERENT txid.
29
+ * This means we don't need special logic for change - it appears as a new UTXO.
30
+ * The old UTXO (original deposit) will be gone from Bitcoin queries.
31
+ *
32
+ * 2. Authorization is Commitment:
33
+ * Any UTXO in the AuthorizedSpend array is considered committed/consumed.
34
+ * Once authorized, it's the user's responsibility to sign and broadcast.
35
+ * If they don't, that's their problem - not the ecosystem's concern.
36
+ *
37
+ * 3. Security Model - Dual Source of Truth:
38
+ * - Bitcoin Network: What UTXOs actually exist
39
+ * - Smart Contract: What UTXOs are authorized to be spent
40
+ * - Available Balance = Total UTXOs - Authorized UTXOs
41
+ *
42
+ * This prevents double-spending by ensuring the same UTXO cannot be
43
+ * authorized twice. The contract's isAuthorizedSpend mapping enforces this.
44
+ *
45
+ * 4. Race Condition Protection:
46
+ * The FAFO (First Actor Fails Others) model protects against concurrent
47
+ * authorization attempts via state hash commitment in quantum time windows.
48
+ * This module provides the trusted balance INPUT to that authorization process.
49
+ *
50
+ * 5. UTXO Matching:
51
+ * We match authorized spends to UTXOs by (txid, vout) pair.
52
+ * A UTXO is available if it exists in Bitcoin but NOT in authorized spends.
53
+ *
54
+ * 6. Edge Cases:
55
+ * - UTXO authorized but not yet spent: Appears in Bitcoin, appears in contract
56
+ * - UTXO spent: Doesn't appear in Bitcoin, may still appear in contract
57
+ * - New deposit: Appears in Bitcoin, doesn't appear in contract
58
+ */
59
+ class VaultBalanceModule {
60
+ constructor(config) {
61
+ this.config = config;
62
+ this.bitcoinProvider = config.bitcoinProvider;
63
+ }
64
+ /**
65
+ * Calculate trusted balance for a position
66
+ *
67
+ * This is the main entry point for determining available balance.
68
+ *
69
+ * CRITICAL: Validates that all authorized spends have been properly broadcasted
70
+ * to Bitcoin network with sufficient confirmations. Throws errors if:
71
+ * 1. Authorized transaction not found on network (not broadcasted)
72
+ * 2. Authorized transaction has insufficient confirmations (network updating)
73
+ *
74
+ * @param positionId - The position ID to check
75
+ * @param vaultAddress - Bitcoin address of the vault
76
+ * @returns Complete breakdown of vault balance
77
+ * @throws Error if authorized spends are in invalid state
78
+ */
79
+ async calculateTrustedBalance(positionId, vaultAddress) {
80
+ const minConfirmations = this.config.minConfirmations || 6;
81
+ // 1. Get all UTXOs from Bitcoin network
82
+ const utxoSet = await this.bitcoinProvider.getUTXOSet(vaultAddress, minConfirmations);
83
+ // 2. Get authorized spends from smart contract
84
+ const authorizedSpends = await this.getAuthorizedSpendsFromContract(positionId);
85
+ // 3. CRITICAL: Validate each authorized spend state
86
+ // For each authorized spend, check if UTXO still exists as unspent
87
+ for (const spend of authorizedSpends) {
88
+ const utxoStillExists = utxoSet.utxos.some((utxo) => utxo.txid === spend.txid && utxo.vout === spend.vout);
89
+ if (utxoStillExists) {
90
+ // ERROR B: UTXO authorized in contract but still unspent on Bitcoin network
91
+ // This means SDK authorized but never signed/broadcasted the spending transaction
92
+ throw new Error(`Authorized UTXO ${spend.txid}:${spend.vout} not yet spent. ` +
93
+ `Transaction was authorized in smart contract but not signed and broadcasted to Bitcoin network. ` +
94
+ `Complete the transaction by signing and broadcasting it.`);
95
+ }
96
+ // UTXO no longer exists as unspent (it's been spent)
97
+ // This is expected - now we verify the spending transaction has enough confirmations
98
+ // Note: spend.txid refers to the INPUT UTXO, not the spending transaction
99
+ // We cannot validate spending transaction confirmations without storing its txid
100
+ // For now, we trust that if UTXO doesn't exist, it's been properly spent
101
+ // TODO: Consider adding spendingTxid to AuthorizedSpend for full validation
102
+ }
103
+ // 4. Calculate authorized balance (sum of all authorized spends)
104
+ const authorizedBalance = authorizedSpends.reduce((sum, spend) => sum + spend.satoshis, 0n);
105
+ // 5. Filter out UTXOs that are authorized (not available)
106
+ // Note: After validation above, authorized UTXOs should NOT appear in utxoSet
107
+ // But we filter anyway for safety
108
+ const availableUTXOs = utxoSet.utxos.filter((utxo) => !this.isUTXOAuthorized(utxo, authorizedSpends));
109
+ // 6. Calculate available balance
110
+ const availableBalance = availableUTXOs.reduce((sum, utxo) => sum + utxo.satoshis, 0n);
111
+ return {
112
+ // Total from Bitcoin
113
+ totalUTXOs: utxoSet.utxos,
114
+ totalBalance: utxoSet.totalBalance,
115
+ // Authorized from Contract
116
+ authorizedUTXOs: authorizedSpends,
117
+ authorizedBalance,
118
+ // Available = Total - Authorized (THIS IS THE TRUSTED BALANCE)
119
+ availableUTXOs,
120
+ availableBalance,
121
+ // Metadata
122
+ vaultAddress,
123
+ positionId,
124
+ timestamp: Date.now(),
125
+ };
126
+ }
127
+ /**
128
+ * Get just the trusted balance amount
129
+ *
130
+ * Convenience method when you only need the number.
131
+ *
132
+ * @param positionId - The position ID to check
133
+ * @param vaultAddress - Bitcoin address of the vault
134
+ * @returns Available balance in satoshis
135
+ */
136
+ async getTrustedBalance(positionId, vaultAddress) {
137
+ const result = await this.calculateTrustedBalance(positionId, vaultAddress);
138
+ return result.availableBalance;
139
+ }
140
+ /**
141
+ * Get available UTXOs that can be used for new authorizations
142
+ *
143
+ * This is useful when you need to select specific UTXOs for authorization.
144
+ *
145
+ * @param positionId - The position ID to check
146
+ * @param vaultAddress - Bitcoin address of the vault
147
+ * @returns Array of available UTXOs
148
+ */
149
+ async getAvailableUTXOs(positionId, vaultAddress) {
150
+ const result = await this.calculateTrustedBalance(positionId, vaultAddress);
151
+ return result.availableUTXOs;
152
+ }
153
+ /**
154
+ * Check if a specific UTXO is available for authorization
155
+ *
156
+ * @param positionId - The position ID to check
157
+ * @param vaultAddress - Bitcoin address of the vault
158
+ * @param txid - Transaction ID of the UTXO
159
+ * @param vout - Output index of the UTXO
160
+ * @returns True if UTXO is available, false otherwise
161
+ */
162
+ async isUTXOAvailable(positionId, vaultAddress, txid, vout) {
163
+ const availableUTXOs = await this.getAvailableUTXOs(positionId, vaultAddress);
164
+ return availableUTXOs.some((utxo) => utxo.txid === txid && utxo.vout === vout);
165
+ }
166
+ /**
167
+ * Check if a UTXO is authorized (appears in authorized spends)
168
+ *
169
+ * @param utxo - The UTXO to check
170
+ * @param authorizedSpends - List of authorized spends from contract
171
+ * @returns True if UTXO is authorized, false otherwise
172
+ */
173
+ isUTXOAuthorized(utxo, authorizedSpends) {
174
+ return authorizedSpends.some((spend) => spend.txid === utxo.txid && spend.vout === utxo.vout);
175
+ }
176
+ /**
177
+ * Query smart contract for authorized spends
178
+ *
179
+ * Calls the contract's getAuthorizedSpends(positionId) view function
180
+ * to retrieve all authorized Bitcoin spends for this position.
181
+ *
182
+ * Contract struct AuthorizedSpend:
183
+ * - txid: string
184
+ * - vout: uint32
185
+ * - satoshis: uint256
186
+ * - authorizedAt: uint256 (timestamp)
187
+ *
188
+ * @param positionId - The position ID to query
189
+ * @returns Array of authorized spends for this position
190
+ */
191
+ async getAuthorizedSpendsFromContract(positionId) {
192
+ // For LIT Actions, Lit.Actions.call provides RPC access
193
+ // This uses the ethers.js-like interface available in LIT runtime
194
+ // Convert positionId to bytes32 if it's a hex string
195
+ const positionIdBytes32 = positionId.startsWith("0x")
196
+ ? positionId
197
+ : `0x${positionId.padStart(64, "0")}`;
198
+ // ABI fragment for getAuthorizedSpends function
199
+ const getAuthorizedSpendsABI = [
200
+ {
201
+ inputs: [
202
+ { internalType: "bytes32", name: "positionId", type: "bytes32" },
203
+ ],
204
+ name: "getAuthorizedSpends",
205
+ outputs: [
206
+ {
207
+ components: [
208
+ { internalType: "string", name: "txid", type: "string" },
209
+ { internalType: "uint32", name: "vout", type: "uint32" },
210
+ { internalType: "uint256", name: "satoshis", type: "uint256" },
211
+ {
212
+ internalType: "string",
213
+ name: "targetAddress",
214
+ type: "string",
215
+ },
216
+ {
217
+ internalType: "uint256",
218
+ name: "targetAmount",
219
+ type: "uint256",
220
+ },
221
+ {
222
+ internalType: "string",
223
+ name: "changeAddress",
224
+ type: "string",
225
+ },
226
+ {
227
+ internalType: "uint256",
228
+ name: "authorizedAt",
229
+ type: "uint256",
230
+ },
231
+ ],
232
+ internalType: "struct LoanOperationsManager.AuthorizedSpend[]",
233
+ name: "",
234
+ type: "tuple[]",
235
+ },
236
+ ],
237
+ stateMutability: "view",
238
+ type: "function",
239
+ },
240
+ ];
241
+ // Get RPC URL - either from config (for local testing) or from Lit runtime
242
+ let rpcUrl;
243
+ if (this.config.rpcUrl) {
244
+ // Local testing: use provided RPC URL
245
+ rpcUrl = this.config.rpcUrl;
246
+ }
247
+ else {
248
+ // Production: get RPC from Lit runtime
249
+ // @ts-ignore - Lit.Actions is available in LIT runtime
250
+ rpcUrl = await Lit.Actions.getRpcUrl({ chain: this.config.chain });
251
+ }
252
+ // Use ethers.js to query the contract directly
253
+ // @ts-ignore - ethers is available in LIT runtime
254
+ // StaticJsonRpcProvider with explicit network bypasses auto-detection (required for ngrok URLs)
255
+ const provider = new ethers.providers.StaticJsonRpcProvider(rpcUrl, {
256
+ name: "any",
257
+ chainId: 1337,
258
+ });
259
+ // @ts-ignore - ethers is available in LIT runtime
260
+ const contract = new ethers.Contract(this.config.contractAddress, getAuthorizedSpendsABI, provider);
261
+ const result = await contract.getAuthorizedSpends(positionIdBytes32);
262
+ // Transform contract response to our interface
263
+ return result.map((spend) => ({
264
+ txid: spend.txid,
265
+ vout: Number(spend.vout),
266
+ satoshis: BigInt(spend.satoshis.toString()),
267
+ positionId: positionId,
268
+ targetAddress: spend.targetAddress,
269
+ targetAmount: BigInt(spend.targetAmount.toString()),
270
+ changeAddress: spend.changeAddress,
271
+ timestamp: Number(spend.authorizedAt),
272
+ }));
273
+ }
274
+ }
275
+ exports.VaultBalanceModule = VaultBalanceModule;
276
+ /**
277
+ * Factory function to create VaultBalanceModule instance
278
+ *
279
+ * @param config - Configuration for the module
280
+ * @returns VaultBalanceModule instance
281
+ */
282
+ function createVaultBalanceModule(config) {
283
+ return new VaultBalanceModule(config);
284
+ }
285
+ /**
286
+ * USAGE EXAMPLE:
287
+ *
288
+ * ```typescript
289
+ * import { createVaultBalanceModule } from './vault-balance.module';
290
+ * import { BitcoinDataProvider } from './bitcoin-data-provider.module';
291
+ *
292
+ * const bitcoinProvider = new BitcoinDataProvider({
293
+ * providerUrl: 'https://blockstream.info/testnet/api'
294
+ * });
295
+ *
296
+ * const vaultBalance = createVaultBalanceModule({
297
+ * contractAddress: '0x...',
298
+ * chain: 'sepolia',
299
+ * bitcoinProvider,
300
+ * minConfirmations: 6
301
+ * });
302
+ *
303
+ * // Get complete balance breakdown
304
+ * const result = await vaultBalance.calculateTrustedBalance(
305
+ * 'position123',
306
+ * 'tb1q...'
307
+ * );
308
+ *
309
+ * console.log('Total Balance:', result.totalBalance, 'sats');
310
+ * console.log('Authorized Balance:', result.authorizedBalance, 'sats');
311
+ * console.log('Available Balance:', result.availableBalance, 'sats');
312
+ *
313
+ * // Or just get the available amount
314
+ * const available = await vaultBalance.getTrustedBalance('position123', 'tb1q...');
315
+ * console.log('Can authorize:', available, 'sats');
316
+ * ```
317
+ */
318
+ //# sourceMappingURL=vault-balance.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-balance.module.js","sourceRoot":"","sources":["../../../src/modules/vault-balance.module.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;AAiWH,4DAIC;AAxVD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAa,kBAAkB;IAI7B,YAAY,MAA0B;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;IAChD,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,uBAAuB,CAC3B,UAAkB,EAClB,YAAoB;QAEpB,MAAM,gBAAgB,GAAG,IAAI,CAAC,MAAM,CAAC,gBAAgB,IAAI,CAAC,CAAC;QAE3D,wCAAwC;QACxC,MAAM,OAAO,GAAY,MAAM,IAAI,CAAC,eAAe,CAAC,UAAU,CAC5D,YAAY,EACZ,gBAAgB,CACjB,CAAC;QAEF,+CAA+C;QAC/C,MAAM,gBAAgB,GACpB,MAAM,IAAI,CAAC,+BAA+B,CAAC,UAAU,CAAC,CAAC;QAEzD,oDAAoD;QACpD,mEAAmE;QACnE,KAAK,MAAM,KAAK,IAAI,gBAAgB,EAAE,CAAC;YACrC,MAAM,eAAe,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CACxC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAC/D,CAAC;YAEF,IAAI,eAAe,EAAE,CAAC;gBACpB,4EAA4E;gBAC5E,kFAAkF;gBAClF,MA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E,CAAC;QAED,+CAA+C;QAC/C,kDAAkD;QAClD,gGAAgG;QAChG,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,qBAAqB,CAAC,MAAM,EAAE;YAClE,IAAI,EAAE,KAAK;YACX,OAAO,EAAE,IAAI;SACd,CAAC,CAAC;QAEH,kDAAkD;QAClD,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,QAAQ,CAClC,IAAI,CAAC,MAAM,CAAC,eAAe,EAC3B,sBAAsB,EACtB,QAAQ,CACT,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,mBAAmB,CAAC,iBAAiB,CAAC,CAAC;QAErE,+CAA+C;QAC/C,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,CAAC;YACjC,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YACxB,QAAQ,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC3C,UAAU,EAAE,UAAU;YACtB,aAAa,EAAE,KAAK,CAAC,aAAa;YAClC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC;YACnD,aAAa,EAAE,KAAK,CAAC,aAAa;YAClC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC;SACtC,CAAC,CAAC,CAAC;IACN,CAAC;CACF;AAjSD,gDAiSC;AAED;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,MAA0B;IAE1B,OAAO,IAAI,kBAAkB,CAAC,MAAM,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG"}
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Vault Snapshot Module - The Brain
3
+ *
4
+ * This module calculates ALL vault intelligence from simple contract state.
5
+ * It is the single source of truth for vault health metrics.
6
+ *
7
+ * Architecture:
8
+ * - Smart Contract: Stores simple state (ucdDebt, termStartTimestamp, btcCollateralSats)
9
+ * - This Module: Calculates everything else (ratios, thresholds, risk metrics)
10
+ *
11
+ * The vault-snapshot is used by:
12
+ * - Minting authorization (check if sufficient collateral)
13
+ * - Liquidation detection (check if under threshold)
14
+ * - UI display (show vault health to user)
15
+ * - Repayment calculations (how much can be repaid)
16
+ */
17
+ import { VaultSnapshot, VaultSnapshotConfig } from "../interfaces/chunks/vault-snapshot.i";
18
+ /**
19
+ * Vault Snapshot Module
20
+ *
21
+ * The intelligence layer that transforms simple contract state into
22
+ * comprehensive vault health metrics.
23
+ */
24
+ export declare class VaultSnapshotModule {
25
+ private config;
26
+ constructor(config: VaultSnapshotConfig);
27
+ /**
28
+ * Get complete vault snapshot
29
+ *
30
+ * This is the main entry point. Returns everything you need to know
31
+ * about a vault's health in a single call.
32
+ *
33
+ * @param positionId - Position to snapshot
34
+ * @returns Complete VaultSnapshot with all metrics
35
+ */
36
+ getVaultSnapshot(positionId: string): Promise<VaultSnapshot>;
37
+ /**
38
+ * Query position state from smart contract
39
+ *
40
+ * Gets the simple state that contract stores:
41
+ * - positionId, pkpId, borrower, vaultAddress
42
+ * - ucdDebt
43
+ * - termStartTimestamp
44
+ * - selectedTerm
45
+ * - status
46
+ */
47
+ protected queryPositionState(positionId: string): Promise<{
48
+ positionId: string;
49
+ pkpId: string;
50
+ borrower: string;
51
+ vaultAddress: string;
52
+ ucdDebt: bigint;
53
+ termStartTimestamp: number;
54
+ selectedTerm: number;
55
+ status: number;
56
+ }>;
57
+ /**
58
+ * Parse vault address based on format and network
59
+ *
60
+ * Supports two formats:
61
+ * 1. Single string: "mqM3ZR2N6DzZkafSdFqxEAkWovAU3uYTAN"
62
+ * 2. JSON object: {"mainnet":"bc1q...","testnet":"mqM3ZR2N6DzZkafSdFqxEAkWovAU3uYTAN"}
63
+ *
64
+ * @param rawVaultAddress Raw vault address from contract
65
+ * @param chain EVM chain (sepolia, ethereum)
66
+ * @returns Parsed Bitcoin address for the appropriate network
67
+ */
68
+ private parseVaultAddress;
69
+ /**
70
+ * Quick check if position is liquidatable
71
+ *
72
+ * Convenience method when you only need to know if position can be liquidated.
73
+ *
74
+ * @param positionId - Position to check
75
+ * @returns True if position is liquidatable
76
+ */
77
+ isLiquidatable(positionId: string): Promise<boolean>;
78
+ /**
79
+ * Get collateral ratio for position
80
+ *
81
+ * Convenience method when you only need the collateral ratio.
82
+ *
83
+ * @param positionId - Position to check
84
+ * @returns Collateral ratio in basis points
85
+ */
86
+ getCollateralRatio(positionId: string): Promise<number>;
87
+ /**
88
+ * Get available BTC balance
89
+ *
90
+ * Convenience method when you only need available balance.
91
+ *
92
+ * @param positionId - Position to check
93
+ * @returns Available BTC in satoshis
94
+ */
95
+ getAvailableBalance(positionId: string): Promise<bigint>;
96
+ /**
97
+ * Check if position has sufficient collateral for new debt
98
+ *
99
+ * Used by minting authorization to validate new mints.
100
+ *
101
+ * @param positionId - Position to check
102
+ * @param additionalDebt - Additional UCD debt to add
103
+ * @param requiredRatioBps - Required collateral ratio (e.g., 13000 for 130%)
104
+ * @returns True if sufficient collateral exists
105
+ */
106
+ hasSufficientCollateral(positionId: string, additionalDebt: bigint, requiredRatioBps: number): Promise<boolean>;
107
+ }
108
+ /**
109
+ * Factory function to create VaultSnapshotModule
110
+ *
111
+ * @param config - Configuration
112
+ * @returns VaultSnapshotModule instance
113
+ */
114
+ export declare function createVaultSnapshotModule(config: VaultSnapshotConfig): VaultSnapshotModule;
115
+ /**
116
+ * USAGE EXAMPLE:
117
+ *
118
+ * ```typescript
119
+ * import { createVaultSnapshotModule } from './modules/vault-snapshot';
120
+ * import { BitcoinDataProvider } from './modules/bitcoin-data-provider.module';
121
+ * import { PriceOracleModule } from './modules/price-oracle.module';
122
+ *
123
+ * // Initialize dependencies
124
+ * const bitcoinProvider = new BitcoinDataProvider({
125
+ * providerUrl: 'https://blockstream.info/testnet/api'
126
+ * });
127
+ *
128
+ * const priceOracle = new PriceOracleModule('mainnet');
129
+ *
130
+ * // Create vault balance module
131
+ * const vaultBalance = createVaultBalanceModule({
132
+ * contractAddress: '0x...',
133
+ * chain: 'sepolia',
134
+ * bitcoinProvider,
135
+ * minConfirmations: 6
136
+ * });
137
+ *
138
+ * // Create vault snapshot module
139
+ * const vaultSnapshot = createVaultSnapshotModule({
140
+ * contractAddress: '0x...',
141
+ * chain: 'sepolia',
142
+ * vaultBalance,
143
+ * priceOracle,
144
+ * });
145
+ *
146
+ * // Get complete snapshot
147
+ * const snapshot = await vaultSnapshot.getVaultSnapshot('position123');
148
+ *
149
+ * console.log('Position:', snapshot.positionId);
150
+ * console.log('Collateral Ratio:', snapshot.collateralRatioBps / 100, '%');
151
+ * console.log('Liquidatable:', snapshot.isLiquidatable);
152
+ * console.log('Days Until Expiry:', snapshot.daysUntilExpiry);
153
+ * console.log('Available BTC:', snapshot.availableBTCSats, 'sats');
154
+ *
155
+ * // Or use convenience methods
156
+ * const isLiquidatable = await vaultSnapshot.isLiquidatable('position123');
157
+ * const collateralRatio = await vaultSnapshot.getCollateralRatio('position123');
158
+ * const availableBalance = await vaultSnapshot.getAvailableBalance('position123');
159
+ *
160
+ * // Check if can mint more UCD
161
+ * const canMint = await vaultSnapshot.hasSufficientCollateral(
162
+ * 'position123',
163
+ * BigInt(5000), // Want to mint 5000 more UCD
164
+ * 13000 // Required 130% ratio
165
+ * );
166
+ * ```
167
+ */
168
+ //# sourceMappingURL=vault-snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-snapshot.d.ts","sourceRoot":"","sources":["../../../src/modules/vault-snapshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EACL,aAAa,EACb,mBAAmB,EACpB,MAAM,uCAAuC,CAAC;AAgB/C;;;;;GAKG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,MAAM,CAAsB;gBAExB,MAAM,EAAE,mBAAmB;IAIvC;;;;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