@gvnrdao/dh-lit-actions 0.0.61 → 0.0.68
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.
- package/package.json +3 -3
- package/pkg-dist/pkg-src/constants/chunks/lit-actions-registry.d.ts.map +1 -0
- package/pkg-dist/{constants → pkg-src/constants}/chunks/lit-actions-registry.js +16 -16
- package/pkg-dist/pkg-src/constants/chunks/lit-actions-registry.js.map +1 -0
- package/pkg-dist/pkg-src/constants/chunks/package-registry.d.ts.map +1 -0
- package/pkg-dist/pkg-src/constants/chunks/package-registry.js.map +1 -0
- package/pkg-dist/pkg-src/constants/index.d.ts.map +1 -0
- package/pkg-dist/pkg-src/constants/index.js.map +1 -0
- package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.d.ts +40 -0
- package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.d.ts.map +1 -0
- package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.js +102 -0
- package/pkg-dist/pkg-src/executors/chunks/vault-snapshot-executor.js.map +1 -0
- package/pkg-dist/pkg-src/executors/index.d.ts +43 -0
- package/pkg-dist/pkg-src/executors/index.d.ts.map +1 -0
- package/pkg-dist/pkg-src/executors/index.js +48 -0
- package/pkg-dist/pkg-src/executors/index.js.map +1 -0
- package/pkg-dist/{index.d.ts → pkg-src/index.d.ts} +1 -2
- package/pkg-dist/pkg-src/index.d.ts.map +1 -0
- package/pkg-dist/{index.js → pkg-src/index.js} +1 -4
- package/pkg-dist/pkg-src/index.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/diamond-hands-lit-actions.i.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/diamond-hands-lit-actions.i.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-config.i.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-config.i.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-name.i.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-name.i.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-registry.i.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/lit-action-registry.i.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/pkp-info.i.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/chunks/pkp-info.i.js.map +1 -0
- package/pkg-dist/pkg-src/interfaces/index.d.ts.map +1 -0
- package/pkg-dist/pkg-src/interfaces/index.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/cid-utils.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/cid-utils.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/connection-helpers.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/connection-helpers.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/debug-logger.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/debug-logger.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/error-classification.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/error-classification.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/lit-action-helpers.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/lit-action-helpers.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/pkp-setup.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/pkp-setup.js.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/session-signature-cache.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/chunks/session-signature-cache.js.map +1 -0
- package/pkg-dist/pkg-src/utils/index.d.ts.map +1 -0
- package/pkg-dist/pkg-src/utils/index.js.map +1 -0
- package/pkg-dist/src/constants/chunks/bitcoin-network-config.d.ts +130 -0
- package/pkg-dist/src/constants/chunks/bitcoin-network-config.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/bitcoin-network-config.js +244 -0
- package/pkg-dist/src/constants/chunks/bitcoin-network-config.js.map +1 -0
- package/pkg-dist/src/constants/chunks/bitcoin.d.ts +42 -0
- package/pkg-dist/src/constants/chunks/bitcoin.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/bitcoin.js +45 -0
- package/pkg-dist/src/constants/chunks/bitcoin.js.map +1 -0
- package/pkg-dist/src/constants/chunks/decimals.d.ts +44 -0
- package/pkg-dist/src/constants/chunks/decimals.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/decimals.js +47 -0
- package/pkg-dist/src/constants/chunks/decimals.js.map +1 -0
- package/pkg-dist/src/constants/chunks/liquidation.d.ts +51 -0
- package/pkg-dist/src/constants/chunks/liquidation.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/liquidation.js +54 -0
- package/pkg-dist/src/constants/chunks/liquidation.js.map +1 -0
- package/pkg-dist/src/constants/chunks/loan-status.d.ts +57 -0
- package/pkg-dist/src/constants/chunks/loan-status.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/loan-status.js +95 -0
- package/pkg-dist/src/constants/chunks/loan-status.js.map +1 -0
- package/pkg-dist/src/constants/chunks/price-oracle.d.ts +46 -0
- package/pkg-dist/src/constants/chunks/price-oracle.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/price-oracle.js +49 -0
- package/pkg-dist/src/constants/chunks/price-oracle.js.map +1 -0
- package/pkg-dist/src/constants/chunks/quantum-time.d.ts +63 -0
- package/pkg-dist/src/constants/chunks/quantum-time.d.ts.map +1 -0
- package/pkg-dist/src/constants/chunks/quantum-time.js +66 -0
- package/pkg-dist/src/constants/chunks/quantum-time.js.map +1 -0
- package/pkg-dist/src/constants/index.d.ts +16 -0
- package/pkg-dist/src/constants/index.d.ts.map +1 -0
- package/pkg-dist/src/constants/index.js +57 -0
- package/pkg-dist/src/constants/index.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/authorization.i.d.ts +19 -0
- package/pkg-dist/src/interfaces/chunks/authorization.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/authorization.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/authorization.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.d.ts +60 -0
- package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/bitcoin-data-provider.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.d.ts +18 -0
- package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.js +8 -0
- package/pkg-dist/src/interfaces/chunks/circuit-breaker.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/mint-authorization.i.d.ts +24 -0
- package/pkg-dist/src/interfaces/chunks/mint-authorization.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/mint-authorization.i.js +5 -0
- package/pkg-dist/src/interfaces/chunks/mint-authorization.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/owner-authorization.d.ts +60 -0
- package/pkg-dist/src/interfaces/chunks/owner-authorization.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/owner-authorization.js +8 -0
- package/pkg-dist/src/interfaces/chunks/owner-authorization.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/price-oracle.i.d.ts +26 -0
- package/pkg-dist/src/interfaces/chunks/price-oracle.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/price-oracle.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/price-oracle.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.d.ts +23 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance-module.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance.i.d.ts +63 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/vault-balance.i.js.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.d.ts +51 -0
- package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.js +3 -0
- package/pkg-dist/src/interfaces/chunks/vault-snapshot.i.js.map +1 -0
- package/pkg-dist/src/interfaces/index.d.ts +15 -0
- package/pkg-dist/src/interfaces/index.d.ts.map +1 -0
- package/pkg-dist/src/interfaces/index.js +30 -0
- package/pkg-dist/src/interfaces/index.js.map +1 -0
- package/pkg-dist/src/modules/authorization.module.d.ts +160 -0
- package/pkg-dist/src/modules/authorization.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/authorization.module.js +317 -0
- package/pkg-dist/src/modules/authorization.module.js.map +1 -0
- package/pkg-dist/src/modules/bitcoin-data-provider.module.d.ts +91 -0
- package/pkg-dist/src/modules/bitcoin-data-provider.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/bitcoin-data-provider.module.js +261 -0
- package/pkg-dist/src/modules/bitcoin-data-provider.module.js.map +1 -0
- package/pkg-dist/src/modules/business-rules-math.module.d.ts +155 -0
- package/pkg-dist/src/modules/business-rules-math.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/business-rules-math.module.js +309 -0
- package/pkg-dist/src/modules/business-rules-math.module.js.map +1 -0
- package/pkg-dist/src/modules/price-oracle.module.d.ts +29 -0
- package/pkg-dist/src/modules/price-oracle.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/price-oracle.module.js +223 -0
- package/pkg-dist/src/modules/price-oracle.module.js.map +1 -0
- package/pkg-dist/src/modules/protocol-parameters.module.d.ts +58 -0
- package/pkg-dist/src/modules/protocol-parameters.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/protocol-parameters.module.js +125 -0
- package/pkg-dist/src/modules/protocol-parameters.module.js.map +1 -0
- package/pkg-dist/src/modules/quantum-time.module.d.ts +149 -0
- package/pkg-dist/src/modules/quantum-time.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/quantum-time.module.js +240 -0
- package/pkg-dist/src/modules/quantum-time.module.js.map +1 -0
- package/pkg-dist/src/modules/vault-balance.module.d.ts +172 -0
- package/pkg-dist/src/modules/vault-balance.module.d.ts.map +1 -0
- package/pkg-dist/src/modules/vault-balance.module.js +318 -0
- package/pkg-dist/src/modules/vault-balance.module.js.map +1 -0
- package/pkg-dist/src/modules/vault-snapshot.d.ts +168 -0
- package/pkg-dist/src/modules/vault-snapshot.d.ts.map +1 -0
- package/pkg-dist/src/modules/vault-snapshot.js +370 -0
- package/pkg-dist/src/modules/vault-snapshot.js.map +1 -0
- package/pkg-dist/constants/chunks/contract-deployments.d.ts +0 -65
- package/pkg-dist/constants/chunks/contract-deployments.d.ts.map +0 -1
- package/pkg-dist/constants/chunks/contract-deployments.js +0 -181
- package/pkg-dist/constants/chunks/contract-deployments.js.map +0 -1
- package/pkg-dist/constants/chunks/lit-actions-registry.d.ts.map +0 -1
- package/pkg-dist/constants/chunks/lit-actions-registry.js.map +0 -1
- package/pkg-dist/constants/chunks/package-registry.d.ts.map +0 -1
- package/pkg-dist/constants/chunks/package-registry.js.map +0 -1
- package/pkg-dist/constants/index.d.ts.map +0 -1
- package/pkg-dist/constants/index.js.map +0 -1
- package/pkg-dist/index.d.ts.map +0 -1
- package/pkg-dist/index.js.map +0 -1
- package/pkg-dist/interfaces/chunks/diamond-hands-lit-actions.i.d.ts.map +0 -1
- package/pkg-dist/interfaces/chunks/diamond-hands-lit-actions.i.js.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-config.i.d.ts.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-config.i.js.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-name.i.d.ts.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-name.i.js.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-registry.i.d.ts.map +0 -1
- package/pkg-dist/interfaces/chunks/lit-action-registry.i.js.map +0 -1
- package/pkg-dist/interfaces/chunks/pkp-info.i.d.ts.map +0 -1
- package/pkg-dist/interfaces/chunks/pkp-info.i.js.map +0 -1
- package/pkg-dist/interfaces/deployments.d.ts +0 -117
- package/pkg-dist/interfaces/deployments.d.ts.map +0 -1
- package/pkg-dist/interfaces/deployments.js +0 -7
- package/pkg-dist/interfaces/deployments.js.map +0 -1
- package/pkg-dist/interfaces/index.d.ts.map +0 -1
- package/pkg-dist/interfaces/index.js.map +0 -1
- package/pkg-dist/utils/chunks/cid-utils.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/cid-utils.js.map +0 -1
- package/pkg-dist/utils/chunks/connection-helpers.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/connection-helpers.js.map +0 -1
- package/pkg-dist/utils/chunks/debug-logger.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/debug-logger.js.map +0 -1
- package/pkg-dist/utils/chunks/error-classification.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/error-classification.js.map +0 -1
- package/pkg-dist/utils/chunks/lit-action-helpers.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/lit-action-helpers.js.map +0 -1
- package/pkg-dist/utils/chunks/pkp-setup.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/pkp-setup.js.map +0 -1
- package/pkg-dist/utils/chunks/session-signature-cache.d.ts.map +0 -1
- package/pkg-dist/utils/chunks/session-signature-cache.js.map +0 -1
- package/pkg-dist/utils/index.d.ts.map +0 -1
- package/pkg-dist/utils/index.js.map +0 -1
- /package/pkg-dist/{constants → pkg-src/constants}/chunks/lit-actions-registry.d.ts +0 -0
- /package/pkg-dist/{constants → pkg-src/constants}/chunks/package-registry.d.ts +0 -0
- /package/pkg-dist/{constants → pkg-src/constants}/chunks/package-registry.js +0 -0
- /package/pkg-dist/{constants → pkg-src/constants}/index.d.ts +0 -0
- /package/pkg-dist/{constants → pkg-src/constants}/index.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/diamond-hands-lit-actions.i.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/diamond-hands-lit-actions.i.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-config.i.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-config.i.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-name.i.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-name.i.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-registry.i.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/lit-action-registry.i.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/pkp-info.i.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/chunks/pkp-info.i.js +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/index.d.ts +0 -0
- /package/pkg-dist/{interfaces → pkg-src/interfaces}/index.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/cid-utils.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/cid-utils.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/connection-helpers.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/connection-helpers.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/debug-logger.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/debug-logger.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/error-classification.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/error-classification.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/lit-action-helpers.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/lit-action-helpers.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/pkp-setup.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/pkp-setup.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/session-signature-cache.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/chunks/session-signature-cache.js +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/index.d.ts +0 -0
- /package/pkg-dist/{utils → pkg-src/utils}/index.js +0 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Bitcoin Data Provider Module
|
|
4
|
+
*
|
|
5
|
+
* Provides Bitcoin network data with support for future consensus mechanisms.
|
|
6
|
+
* Current implementation: Single provider with fallback support
|
|
7
|
+
* Future implementation: 2-of-3 provider consensus
|
|
8
|
+
*
|
|
9
|
+
* This module centralizes all Bitcoin data queries to maintain a consistent
|
|
10
|
+
* interface that won't break when upgrading to multi-provider consensus.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.BitcoinDataProvider = void 0;
|
|
14
|
+
exports.createBitcoinDataProvider = createBitcoinDataProvider;
|
|
15
|
+
/**
|
|
16
|
+
* Bitcoin Data Provider
|
|
17
|
+
*
|
|
18
|
+
* Fetches UTXO data from Bitcoin network.
|
|
19
|
+
* Interface designed to remain stable when adding multi-provider consensus.
|
|
20
|
+
*/
|
|
21
|
+
class BitcoinDataProvider {
|
|
22
|
+
constructor(config) {
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.timeout = config.timeout || 10000; // Default 10 seconds
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Fetch all UTXOs for a Bitcoin address
|
|
28
|
+
*
|
|
29
|
+
* @param address - Bitcoin address (legacy, segwit, or taproot)
|
|
30
|
+
* @returns Array of UTXOs
|
|
31
|
+
*/
|
|
32
|
+
async getUTXOs(address) {
|
|
33
|
+
console.log(`[Bitcoin Data] Original address: "${address}"`);
|
|
34
|
+
// Strip network prefix if present (e.g., "REGTEST_address" -> "address")
|
|
35
|
+
const cleanAddress = this.stripNetworkPrefix(address);
|
|
36
|
+
console.log(`[Bitcoin Data] Cleaned address: "${cleanAddress}"`);
|
|
37
|
+
// If RPC helper is provided (for regtest/testing), use it directly
|
|
38
|
+
if (this.config.rpcHelper) {
|
|
39
|
+
return await this.fetchUTXOsFromRPC(cleanAddress);
|
|
40
|
+
}
|
|
41
|
+
// Try primary provider first
|
|
42
|
+
try {
|
|
43
|
+
return await this.fetchUTXOsFromProvider(this.config.providerUrl, cleanAddress);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.warn(`[Bitcoin Data] Primary provider failed: ${error.message}`);
|
|
47
|
+
// Try fallback providers if available
|
|
48
|
+
if (this.config.fallbackProviders &&
|
|
49
|
+
this.config.fallbackProviders.length > 0) {
|
|
50
|
+
const sortedFallbacks = [...this.config.fallbackProviders].sort((a, b) => a.priority - b.priority);
|
|
51
|
+
for (const fallback of sortedFallbacks) {
|
|
52
|
+
try {
|
|
53
|
+
console.log(`[Bitcoin Data] Trying fallback: ${fallback.name} (${fallback.url})`);
|
|
54
|
+
return await this.fetchUTXOsFromProvider(fallback.url, cleanAddress);
|
|
55
|
+
}
|
|
56
|
+
catch (fallbackError) {
|
|
57
|
+
console.warn(`[Bitcoin Data] Fallback ${fallback.name} failed: ${fallbackError.message}`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Failed to fetch UTXOs: ${error.message}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Strip network prefix from Bitcoin address
|
|
67
|
+
*
|
|
68
|
+
* Addresses may be stored with network prefix (e.g., "REGTEST_address")
|
|
69
|
+
* but APIs expect clean addresses without prefix.
|
|
70
|
+
*
|
|
71
|
+
* @param address - Address with optional prefix
|
|
72
|
+
* @returns Clean address without prefix
|
|
73
|
+
*/
|
|
74
|
+
stripNetworkPrefix(address) {
|
|
75
|
+
const prefixPattern = /^(REGTEST_|TESTNET_|MAINNET_)/;
|
|
76
|
+
return address.replace(prefixPattern, "");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Fetch UTXOs using Bitcoin RPC (for regtest/testing)
|
|
80
|
+
*/
|
|
81
|
+
async fetchUTXOsFromRPC(address) {
|
|
82
|
+
if (!this.config.rpcHelper) {
|
|
83
|
+
throw new Error("RPC helper not configured");
|
|
84
|
+
}
|
|
85
|
+
const wallet = this.config.rpcWallet || "";
|
|
86
|
+
const unspentOutputs = await this.config.rpcHelper.listUnspent(wallet, address);
|
|
87
|
+
const utxos = unspentOutputs.map((utxo) => ({
|
|
88
|
+
txid: utxo.txid,
|
|
89
|
+
vout: utxo.vout,
|
|
90
|
+
satoshis: BigInt(Math.round(utxo.amount * 100000000)), // BTC to satoshis as bigint
|
|
91
|
+
confirmations: utxo.confirmations,
|
|
92
|
+
}));
|
|
93
|
+
return utxos;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse Esplora/Blockstream/Mempool.space UTXO response
|
|
97
|
+
*
|
|
98
|
+
* Expected format: Array of {txid, vout, value, confirmations}
|
|
99
|
+
*
|
|
100
|
+
* @param data Raw response from provider API
|
|
101
|
+
* @returns Array of normalized UTXOs
|
|
102
|
+
*/
|
|
103
|
+
parseBlockstreamUTXOs(data) {
|
|
104
|
+
if (!Array.isArray(data)) {
|
|
105
|
+
throw new Error("Invalid Blockstream response format: expected an array");
|
|
106
|
+
}
|
|
107
|
+
// Normalize UTXO format (different providers may have different field names)
|
|
108
|
+
const utxos = data.map((utxo) => ({
|
|
109
|
+
txid: utxo.txid,
|
|
110
|
+
vout: utxo.vout !== undefined ? utxo.vout : utxo.n,
|
|
111
|
+
satoshis: BigInt(utxo.value !== undefined ? utxo.value : utxo.satoshis),
|
|
112
|
+
confirmations: utxo.confirmations || 0,
|
|
113
|
+
}));
|
|
114
|
+
return utxos;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Internal method to fetch UTXOs from a specific provider
|
|
118
|
+
*
|
|
119
|
+
* Supports standard Esplora API format:
|
|
120
|
+
* - Standard format: {providerUrl}/address/{address}/utxos
|
|
121
|
+
* - providerUrl must include full base path (e.g., "http://localhost:3002/api/esplora")
|
|
122
|
+
*/
|
|
123
|
+
async fetchUTXOsFromProvider(providerUrl, address) {
|
|
124
|
+
// Standard Esplora API format only
|
|
125
|
+
// providerUrl must include full base path (e.g., "http://localhost:3002/api/esplora")
|
|
126
|
+
const endpoint = `${providerUrl}/address/${address}/utxos`;
|
|
127
|
+
console.log(`Fetching UTXOs from ${endpoint}`);
|
|
128
|
+
const controller = new AbortController();
|
|
129
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
130
|
+
try {
|
|
131
|
+
const response = await fetch(endpoint, {
|
|
132
|
+
signal: controller.signal,
|
|
133
|
+
headers: {
|
|
134
|
+
Accept: "application/json",
|
|
135
|
+
"Content-Type": "application/json",
|
|
136
|
+
"User-Agent": "Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)",
|
|
137
|
+
"ngrok-skip-browser-warning": "true", // Skip ngrok browser warning page
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
clearTimeout(timeoutId);
|
|
141
|
+
if (!response.ok) {
|
|
142
|
+
throw new Error(`Bitcoin provider error: ${response.status} ${response.statusText}`);
|
|
143
|
+
}
|
|
144
|
+
const data = await response.json();
|
|
145
|
+
// Log the parsed data
|
|
146
|
+
console.log(`[Bitcoin Data] Raw API Response:`, JSON.stringify(data, null, 2));
|
|
147
|
+
// Parse standard Esplora/Blockstream format
|
|
148
|
+
return this.parseBlockstreamUTXOs(data);
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
clearTimeout(timeoutId);
|
|
152
|
+
if (error.name === "AbortError") {
|
|
153
|
+
throw new Error(`Request timeout after ${this.timeout}ms`);
|
|
154
|
+
}
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get complete UTXO set with balance summary
|
|
160
|
+
*
|
|
161
|
+
* This is the SINGLE method that vault-balance module should call.
|
|
162
|
+
* Returns everything needed to know about Bitcoin network state for an address.
|
|
163
|
+
*
|
|
164
|
+
* @param address - Bitcoin address
|
|
165
|
+
* @param minConfirmations - Minimum confirmations for "confirmed" status (REQUIRED - caller must specify)
|
|
166
|
+
* @returns Complete UTXO set with balance breakdown
|
|
167
|
+
*/
|
|
168
|
+
async getUTXOSet(address, minConfirmations) {
|
|
169
|
+
const utxos = await this.getUTXOs(address);
|
|
170
|
+
const totalBalance = utxos.reduce((sum, utxo) => sum + utxo.satoshis, 0n);
|
|
171
|
+
const confirmedBalance = utxos
|
|
172
|
+
.filter((utxo) => utxo.confirmations >= minConfirmations)
|
|
173
|
+
.reduce((sum, utxo) => sum + utxo.satoshis, 0n);
|
|
174
|
+
const unconfirmedBalance = totalBalance - confirmedBalance;
|
|
175
|
+
return {
|
|
176
|
+
utxos,
|
|
177
|
+
totalBalance,
|
|
178
|
+
totalUTXOs: utxos.length,
|
|
179
|
+
confirmedBalance,
|
|
180
|
+
unconfirmedBalance,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Get transaction with confirmation count
|
|
185
|
+
*
|
|
186
|
+
* Checks if transaction exists on Bitcoin network and returns confirmation count.
|
|
187
|
+
* Used to validate authorized spends have been broadcasted.
|
|
188
|
+
*
|
|
189
|
+
* @param txid - Transaction ID to check
|
|
190
|
+
* @returns Transaction with confirmation count, or null if not found
|
|
191
|
+
*/
|
|
192
|
+
async getTransaction(txid) {
|
|
193
|
+
// If RPC helper is provided (for regtest/testing), use it directly
|
|
194
|
+
if (this.config.rpcHelper) {
|
|
195
|
+
try {
|
|
196
|
+
const wallet = this.config.rpcWallet || "";
|
|
197
|
+
const tx = await this.config.rpcHelper.getTransaction(wallet, txid);
|
|
198
|
+
return {
|
|
199
|
+
txid: tx.txid,
|
|
200
|
+
confirmations: tx.confirmations || 0,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
// Transaction not found
|
|
205
|
+
if (error.message?.includes("Invalid or non-wallet transaction") ||
|
|
206
|
+
error.code === -5) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Try provider API
|
|
213
|
+
const controller = new AbortController();
|
|
214
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
215
|
+
try {
|
|
216
|
+
const response = await fetch(`${this.config.providerUrl}/api/tx/${txid}`, {
|
|
217
|
+
signal: controller.signal,
|
|
218
|
+
});
|
|
219
|
+
clearTimeout(timeoutId);
|
|
220
|
+
if (!response.ok) {
|
|
221
|
+
if (response.status === 404) {
|
|
222
|
+
return null; // Transaction not found
|
|
223
|
+
}
|
|
224
|
+
throw new Error(`Bitcoin provider error: ${response.status} ${response.statusText}`);
|
|
225
|
+
}
|
|
226
|
+
const data = await response.json();
|
|
227
|
+
// Blockstream API format
|
|
228
|
+
if (data.status) {
|
|
229
|
+
return {
|
|
230
|
+
txid: data.txid,
|
|
231
|
+
confirmations: data.status.confirmed
|
|
232
|
+
? data.status.block_height
|
|
233
|
+
? 1
|
|
234
|
+
: 0
|
|
235
|
+
: 0,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
clearTimeout(timeoutId);
|
|
242
|
+
if (error.name === "AbortError") {
|
|
243
|
+
throw new Error(`Request timeout after ${this.timeout}ms`);
|
|
244
|
+
}
|
|
245
|
+
// 404 means transaction not found
|
|
246
|
+
if (error.message?.includes("404")) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.BitcoinDataProvider = BitcoinDataProvider;
|
|
254
|
+
/**
|
|
255
|
+
* Factory function for creating Bitcoin Data Provider
|
|
256
|
+
* Useful for dependency injection and testing
|
|
257
|
+
*/
|
|
258
|
+
function createBitcoinDataProvider(config) {
|
|
259
|
+
return new BitcoinDataProvider(config);
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=bitcoin-data-provider.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcoin-data-provider.module.js","sourceRoot":"","sources":["../../../src/modules/bitcoin-data-provider.module.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAyUH,8DAIC;AApUD;;;;;GAKG;AACH,MAAa,mBAAmB;IAI9B,YAAY,MAAiC;QAC3C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC,qBAAqB;IAC/D,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,CAAC,OAAe;QAC5B,OAAO,CAAC,GAAG,CAAC,qCAAqC,OAAO,GAAG,CAAC,CAAC;QAE7D,yEAAyE;QACzE,MAAM,YAAY,GAAG,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAEtD,OAAO,CAAC,GAAG,CAAC,oCAAoC,YAAY,GAAG,CAAC,CAAC;QAEjE,mEAAmE;QACnE,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YAC1B,OAAO,MAAM,IAAI,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QACpD,CAAC;QAED,6BAA6B;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AC,IAAI,EAAE,CAAC;YAExC,yBAAyB;YACzB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBAChB,OAAO;oBACL,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,aAAa,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS;wBAClC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY;4BACxB,CAAC,CAAC,CAAC;4BACH,CAAC,CAAC,CAAC;wBACL,CAAC,CAAC,CAAC;iBACN,CAAC;YACJ,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAChC,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC;YAC7D,CAAC;YACD,kCAAkC;YAClC,IAAI,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;gBACnC,OAAO,IAAI,CAAC;YACd,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CAQF;AApTD,kDAoTC;AAED;;;GAGG;AACH,SAAgB,yBAAyB,CACvC,MAAiC;IAEjC,OAAO,IAAI,mBAAmB,CAAC,MAAM,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Business Rules Math Module
|
|
3
|
+
*
|
|
4
|
+
* Pure calculation functions for Diamond Hands Protocol business logic.
|
|
5
|
+
* These functions contain no side effects - they only perform calculations.
|
|
6
|
+
*
|
|
7
|
+
* Used by vault-snapshot and other modules for consistent math across the system.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Calculate term start timestamp from expiry timestamp and term length
|
|
11
|
+
*
|
|
12
|
+
* Contract stores expiryAt but not termStartTimestamp.
|
|
13
|
+
* We derive it: termStart = expiry - (termLength * 30 days)
|
|
14
|
+
*
|
|
15
|
+
* Uses constants to avoid magic numbers and ensure accuracy.
|
|
16
|
+
*
|
|
17
|
+
* @param expiryTimestamp - Unix timestamp when term expires (0 if no term)
|
|
18
|
+
* @param termLengthMonths - Term length in months
|
|
19
|
+
* @returns Unix timestamp when term started (0 if no term)
|
|
20
|
+
*/
|
|
21
|
+
export declare function calculateTermStartFromExpiry(expiryTimestamp: number, termLengthMonths: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* Calculate term status from timestamps
|
|
24
|
+
*
|
|
25
|
+
* Determines:
|
|
26
|
+
* - How long into the term we are
|
|
27
|
+
* - Whether term is expired
|
|
28
|
+
* - Days until expiry or days into grace period
|
|
29
|
+
*
|
|
30
|
+
* @param termStartTimestamp - Unix timestamp when term started (0 if not started)
|
|
31
|
+
* @param selectedTermMonths - Term length in months
|
|
32
|
+
* @returns Complete term status metrics
|
|
33
|
+
*/
|
|
34
|
+
export declare function calculateTermStatus(termStartTimestamp: number, selectedTermMonths: number): {
|
|
35
|
+
termDurationDays: number;
|
|
36
|
+
termLengthDays: number;
|
|
37
|
+
isExpired: boolean;
|
|
38
|
+
daysUntilExpiry: number;
|
|
39
|
+
daysIntoGracePeriod: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Calculate current liquidation threshold with grace period escalation
|
|
43
|
+
*
|
|
44
|
+
* Active loan: 130% (13000 bps)
|
|
45
|
+
* Expired:
|
|
46
|
+
* Day 0: 110% (11000 bps)
|
|
47
|
+
* Day 30: 200% (20000 bps)
|
|
48
|
+
* Quadratic escalation between
|
|
49
|
+
*
|
|
50
|
+
* Uses integer-only math for deterministic results across all platforms.
|
|
51
|
+
*
|
|
52
|
+
* @param isExpired - Whether loan is expired
|
|
53
|
+
* @param daysIntoGracePeriod - Days past expiry (0 if not expired)
|
|
54
|
+
* @returns Liquidation threshold in basis points
|
|
55
|
+
*/
|
|
56
|
+
export declare function calculateLiquidationThreshold(isExpired: boolean, daysIntoGracePeriod: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* Calculate collateral metrics from BTC balance and debt
|
|
59
|
+
*
|
|
60
|
+
* Converts BTC balance to USD value and calculates collateral ratio.
|
|
61
|
+
* Uses BigInt for all calculations to avoid JavaScript number precision issues.
|
|
62
|
+
*
|
|
63
|
+
* @param availableBTCSats - Available BTC balance in satoshis
|
|
64
|
+
* @param btcPriceUsd - BTC price with 8 decimals (e.g., 10000000000000 = $100,000)
|
|
65
|
+
* @param ucdDebt - UCD debt in wei (18 decimals)
|
|
66
|
+
* @returns Collateral value in USD and ratio in basis points
|
|
67
|
+
*/
|
|
68
|
+
export declare function calculateCollateralMetrics(availableBTCSats: bigint, btcPriceUsd: bigint, ucdDebt: bigint): {
|
|
69
|
+
collateralValueUsd: bigint;
|
|
70
|
+
collateralRatioBps: number;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Check if adding additional debt would maintain required collateral ratio
|
|
74
|
+
*
|
|
75
|
+
* Used by minting authorization to validate new mints.
|
|
76
|
+
*
|
|
77
|
+
* @param collateralValueUsd - Current collateral value in USD (whole dollars)
|
|
78
|
+
* @param currentDebt - Current UCD debt in wei (18 decimals)
|
|
79
|
+
* @param additionalDebt - Additional UCD debt to add in wei (18 decimals)
|
|
80
|
+
* @param requiredRatioBps - Required collateral ratio in basis points (e.g., 13000 for 130%)
|
|
81
|
+
* @returns True if sufficient collateral exists after adding debt
|
|
82
|
+
*/
|
|
83
|
+
export declare function hasSufficientCollateralForDebt(collateralValueUsd: bigint, currentDebt: bigint, additionalDebt: bigint, requiredRatioBps: number): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Calculate fee from amount using basis points
|
|
86
|
+
*
|
|
87
|
+
* Bulletproof fee calculation with validation.
|
|
88
|
+
* Multiply before divide to maintain precision.
|
|
89
|
+
*
|
|
90
|
+
* @param amount - Principal amount (in wei or sats)
|
|
91
|
+
* @param feeBps - Fee in basis points (e.g., 50 = 0.5%)
|
|
92
|
+
* @returns Fee amount in same units as input
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* calculateFeeBps(1000000000000000000n, 50) // 0.5% of 1 ETH
|
|
96
|
+
* // Returns: 5000000000000000n (0.005 ETH)
|
|
97
|
+
*/
|
|
98
|
+
export declare function calculateFeeBps(amount: bigint, feeBps: number): bigint;
|
|
99
|
+
/**
|
|
100
|
+
* Calculate USD value of BTC amount
|
|
101
|
+
*
|
|
102
|
+
* Converts satoshis to USD using price with 8 decimals.
|
|
103
|
+
* Result maintains 8 decimal precision.
|
|
104
|
+
*
|
|
105
|
+
* @param btcSats - Bitcoin amount in satoshis
|
|
106
|
+
* @param btcPriceUsd8Decimals - BTC price with 8 decimals (e.g., 9800000000000n = $98,000)
|
|
107
|
+
* @returns USD value with 8 decimals
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* calculateBtcValueUsd(100000000n, 9800000000000n) // 1 BTC at $98,000
|
|
111
|
+
* // Returns: 9800000000000n ($98,000 with 8 decimals)
|
|
112
|
+
*/
|
|
113
|
+
export declare function calculateBtcValueUsd(btcSats: bigint, btcPriceUsd8Decimals: bigint): bigint;
|
|
114
|
+
/**
|
|
115
|
+
* Calculate collateral ratio in basis points (with 8-decimal collateral and 18-decimal debt)
|
|
116
|
+
*
|
|
117
|
+
* Used for UCD mint validation where collateral is in USD (8 decimals)
|
|
118
|
+
* and debt is in UCD wei (18 decimals).
|
|
119
|
+
*
|
|
120
|
+
* @param collateralValueUsd8Decimals - Collateral value in USD with 8 decimals
|
|
121
|
+
* @param debtUsd18Decimals - Debt amount in USD with 18 decimals (UCD wei)
|
|
122
|
+
* @returns Collateral ratio in basis points (e.g., 20000 = 200%)
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* calculateCollateralRatioBps(9800000000000n, 4000000000000000000000n)
|
|
126
|
+
* // $98,000 collateral / $4,000 debt = 24.5 = 2450% = 24500 bps
|
|
127
|
+
* // Returns: 24500
|
|
128
|
+
*/
|
|
129
|
+
export declare function calculateCollateralRatioBps(collateralValueUsd8Decimals: bigint, debtUsd18Decimals: bigint): number;
|
|
130
|
+
/**
|
|
131
|
+
* Check if collateral ratio meets minimum requirement
|
|
132
|
+
*
|
|
133
|
+
* @param collateralValueUsd8Decimals - Collateral value in USD with 8 decimals
|
|
134
|
+
* @param debtUsd18Decimals - Debt amount in USD with 18 decimals
|
|
135
|
+
* @param minimumRatioBps - Minimum required ratio in basis points (e.g., 20000 = 200%)
|
|
136
|
+
* @returns True if collateral is sufficient
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* isCollateralSufficient(20000000000000n, 10000000000000000000000n, 20000)
|
|
140
|
+
* // $200,000 collateral / $10,000 debt = 2000% >= 200%
|
|
141
|
+
* // Returns: true
|
|
142
|
+
*/
|
|
143
|
+
export declare function isCollateralSufficient(collateralValueUsd8Decimals: bigint, debtUsd18Decimals: bigint, minimumRatioBps: number): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Convert basis points to percentage (for display)
|
|
146
|
+
*
|
|
147
|
+
* @param basisPoints - Value in basis points
|
|
148
|
+
* @returns Percentage as number (e.g., 20000 bps => 200)
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* bpsToPercent(20000) // Returns: 200
|
|
152
|
+
* bpsToPercent(50) // Returns: 0.5
|
|
153
|
+
*/
|
|
154
|
+
export declare function bpsToPercent(basisPoints: number): number;
|
|
155
|
+
//# sourceMappingURL=business-rules-math.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"business-rules-math.module.d.ts","sourceRoot":"","sources":["../../../src/modules/business-rules-math.module.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAYH;;;;;;;;;;;GAWG;AACH,wBAAgB,4BAA4B,CAC1C,eAAe,EAAE,MAAM,EACvB,gBAAgB,EAAE,MAAM,GACvB,MAAM,CAgBR;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,C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