@kaleidorg/wallet-protocols 1.0.0-beta.2
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/dist/adapters/ArkadeAdapter.d.ts +48 -0
- package/dist/adapters/ArkadeAdapter.d.ts.map +1 -0
- package/dist/adapters/ArkadeAdapter.js +314 -0
- package/dist/adapters/ArkadeAdapter.js.map +1 -0
- package/dist/adapters/IProtocolAdapter.d.ts +71 -0
- package/dist/adapters/IProtocolAdapter.d.ts.map +1 -0
- package/dist/adapters/IProtocolAdapter.js +29 -0
- package/dist/adapters/IProtocolAdapter.js.map +1 -0
- package/dist/adapters/RgbAdapter.d.ts +70 -0
- package/dist/adapters/RgbAdapter.d.ts.map +1 -0
- package/dist/adapters/RgbAdapter.js +818 -0
- package/dist/adapters/RgbAdapter.js.map +1 -0
- package/dist/adapters/SparkAdapter.d.ts +47 -0
- package/dist/adapters/SparkAdapter.d.ts.map +1 -0
- package/dist/adapters/SparkAdapter.js +390 -0
- package/dist/adapters/SparkAdapter.js.map +1 -0
- package/dist/adapters/wdk/ArkadeWdkAdapter.d.ts +78 -0
- package/dist/adapters/wdk/ArkadeWdkAdapter.d.ts.map +1 -0
- package/dist/adapters/wdk/ArkadeWdkAdapter.js +226 -0
- package/dist/adapters/wdk/ArkadeWdkAdapter.js.map +1 -0
- package/dist/adapters/wdk/LiquidWdkAdapter.d.ts +88 -0
- package/dist/adapters/wdk/LiquidWdkAdapter.d.ts.map +1 -0
- package/dist/adapters/wdk/LiquidWdkAdapter.js +272 -0
- package/dist/adapters/wdk/LiquidWdkAdapter.js.map +1 -0
- package/dist/adapters/wdk/RlnWdkAdapter.d.ts +85 -0
- package/dist/adapters/wdk/RlnWdkAdapter.d.ts.map +1 -0
- package/dist/adapters/wdk/RlnWdkAdapter.js +308 -0
- package/dist/adapters/wdk/RlnWdkAdapter.js.map +1 -0
- package/dist/adapters/wdk/SparkWdkAdapter.d.ts +79 -0
- package/dist/adapters/wdk/SparkWdkAdapter.d.ts.map +1 -0
- package/dist/adapters/wdk/SparkWdkAdapter.js +317 -0
- package/dist/adapters/wdk/SparkWdkAdapter.js.map +1 -0
- package/dist/adapters/wdk/moduleLoader.d.ts +22 -0
- package/dist/adapters/wdk/moduleLoader.d.ts.map +1 -0
- package/dist/adapters/wdk/moduleLoader.js +31 -0
- package/dist/adapters/wdk/moduleLoader.js.map +1 -0
- package/dist/capabilities/index.d.ts +46 -0
- package/dist/capabilities/index.d.ts.map +1 -0
- package/dist/capabilities/index.js +102 -0
- package/dist/capabilities/index.js.map +1 -0
- package/dist/disclosure/index.d.ts +51 -0
- package/dist/disclosure/index.d.ts.map +1 -0
- package/dist/disclosure/index.js +64 -0
- package/dist/disclosure/index.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/arkade-client-manager.d.ts +47 -0
- package/dist/lib/arkade-client-manager.d.ts.map +1 -0
- package/dist/lib/arkade-client-manager.js +121 -0
- package/dist/lib/arkade-client-manager.js.map +1 -0
- package/dist/lib/bolt11.d.ts +18 -0
- package/dist/lib/bolt11.d.ts.map +1 -0
- package/dist/lib/bolt11.js +31 -0
- package/dist/lib/bolt11.js.map +1 -0
- package/dist/lib/flashnet-client-manager.d.ts +29 -0
- package/dist/lib/flashnet-client-manager.d.ts.map +1 -0
- package/dist/lib/flashnet-client-manager.js +116 -0
- package/dist/lib/flashnet-client-manager.js.map +1 -0
- package/dist/lib/kaleido-client-manager.d.ts +26 -0
- package/dist/lib/kaleido-client-manager.d.ts.map +1 -0
- package/dist/lib/kaleido-client-manager.js +55 -0
- package/dist/lib/kaleido-client-manager.js.map +1 -0
- package/dist/lib/spark-client-manager.d.ts +41 -0
- package/dist/lib/spark-client-manager.d.ts.map +1 -0
- package/dist/lib/spark-client-manager.js +101 -0
- package/dist/lib/spark-client-manager.js.map +1 -0
- package/dist/manager/ProtocolManager.d.ts +57 -0
- package/dist/manager/ProtocolManager.d.ts.map +1 -0
- package/dist/manager/ProtocolManager.js +228 -0
- package/dist/manager/ProtocolManager.js.map +1 -0
- package/dist/ports/index.d.ts +29 -0
- package/dist/ports/index.d.ts.map +1 -0
- package/dist/ports/index.js +9 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/receive/unifiedReceive.d.ts +54 -0
- package/dist/receive/unifiedReceive.d.ts.map +1 -0
- package/dist/receive/unifiedReceive.js +102 -0
- package/dist/receive/unifiedReceive.js.map +1 -0
- package/dist/registry/createWdkRegistry.d.ts +20 -0
- package/dist/registry/createWdkRegistry.d.ts.map +1 -0
- package/dist/registry/createWdkRegistry.js +32 -0
- package/dist/registry/createWdkRegistry.js.map +1 -0
- package/dist/router/destination.d.ts +28 -0
- package/dist/router/destination.d.ts.map +1 -0
- package/dist/router/destination.js +72 -0
- package/dist/router/destination.js.map +1 -0
- package/dist/router/index.d.ts +55 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +67 -0
- package/dist/router/index.js.map +1 -0
- package/dist/swap/KaleidoswapSwap.d.ts +44 -0
- package/dist/swap/KaleidoswapSwap.d.ts.map +1 -0
- package/dist/swap/KaleidoswapSwap.js +123 -0
- package/dist/swap/KaleidoswapSwap.js.map +1 -0
- package/dist/types/arkade.d.ts +40 -0
- package/dist/types/arkade.d.ts.map +1 -0
- package/dist/types/arkade.js +6 -0
- package/dist/types/arkade.js.map +1 -0
- package/dist/types/base.d.ts +197 -0
- package/dist/types/base.d.ts.map +1 -0
- package/dist/types/base.js +27 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/cross-l2.d.ts +158 -0
- package/dist/types/cross-l2.d.ts.map +1 -0
- package/dist/types/cross-l2.js +16 -0
- package/dist/types/cross-l2.js.map +1 -0
- package/dist/types/flashnet.d.ts +31 -0
- package/dist/types/flashnet.d.ts.map +1 -0
- package/dist/types/flashnet.js +23 -0
- package/dist/types/flashnet.js.map +1 -0
- package/dist/types/rgb.d.ts +91 -0
- package/dist/types/rgb.d.ts.map +1 -0
- package/dist/types/rgb.js +6 -0
- package/dist/types/rgb.js.map +1 -0
- package/dist/types/spark.d.ts +59 -0
- package/dist/types/spark.d.ts.map +1 -0
- package/dist/types/spark.js +6 -0
- package/dist/types/spark.js.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +23 -0
- package/dist/utils.js.map +1 -0
- package/package.json +35 -0
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RGB Protocol Adapter
|
|
3
|
+
* Uses kaleido-sdk to implement the protocol adapter interface.
|
|
4
|
+
* Ported from rate-extension/src/protocols/adapters/RgbAdapter.ts
|
|
5
|
+
*/
|
|
6
|
+
import { kaleidoClientManager } from '../lib/kaleido-client-manager';
|
|
7
|
+
import { KaleidoError, APIError, NetworkError, NodeNotConfiguredError, QuoteExpiredError, InsufficientBalanceError as SdkInsufficientBalanceError, Layer as SdkLayer, } from 'kaleido-sdk';
|
|
8
|
+
import { ProtocolError, ConnectionError, InsufficientBalanceError, } from '../types/base';
|
|
9
|
+
export class RgbAdapter {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.protocolName = 'RGB';
|
|
12
|
+
this.supportedLayers = ['RGB_L1', 'RGB_LN', 'BTC_L1', 'BTC_LN'];
|
|
13
|
+
this.version = '1.0.0';
|
|
14
|
+
this.connected = false;
|
|
15
|
+
this.config = null;
|
|
16
|
+
this.swapAccessTokens = new Map();
|
|
17
|
+
}
|
|
18
|
+
// ========================================================================
|
|
19
|
+
// Connection Management
|
|
20
|
+
// ========================================================================
|
|
21
|
+
async connect(config) {
|
|
22
|
+
const rgbConfig = config;
|
|
23
|
+
if (!rgbConfig.nodeUrl) {
|
|
24
|
+
throw new ConnectionError('Node URL is required', 'RGB');
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
kaleidoClientManager.initialize({
|
|
28
|
+
baseUrl: rgbConfig.makerUrl || '',
|
|
29
|
+
nodeUrl: rgbConfig.nodeUrl,
|
|
30
|
+
apiKey: rgbConfig.apiKey,
|
|
31
|
+
});
|
|
32
|
+
const client = kaleidoClientManager.getClient();
|
|
33
|
+
await client.rln.getNodeInfo();
|
|
34
|
+
this.config = rgbConfig;
|
|
35
|
+
this.connected = true;
|
|
36
|
+
console.log('[RgbAdapter] Connected to RGB node via kaleido-sdk');
|
|
37
|
+
// Test maker connection (non-blocking)
|
|
38
|
+
if (rgbConfig.makerUrl) {
|
|
39
|
+
try {
|
|
40
|
+
await client.maker.listAssets();
|
|
41
|
+
console.log('[RgbAdapter] Maker API accessible');
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
45
|
+
console.warn('[RgbAdapter] Maker API not accessible (swaps will show error):', msg);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
51
|
+
throw new ConnectionError(`Failed to connect to RGB node: ${msg}`, 'RGB', error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async disconnect() {
|
|
55
|
+
kaleidoClientManager.reset();
|
|
56
|
+
this.connected = false;
|
|
57
|
+
this.config = null;
|
|
58
|
+
console.log('[RgbAdapter] Disconnected');
|
|
59
|
+
}
|
|
60
|
+
isConnected() {
|
|
61
|
+
return this.connected && kaleidoClientManager.isInitialized();
|
|
62
|
+
}
|
|
63
|
+
async getConnectionInfo() {
|
|
64
|
+
if (!this.isConnected()) {
|
|
65
|
+
throw new ProtocolError('Not connected', 'RGB', 'NOT_CONNECTED');
|
|
66
|
+
}
|
|
67
|
+
const info = {
|
|
68
|
+
protocol: 'RGB',
|
|
69
|
+
connected: true,
|
|
70
|
+
network: this.config?.network || 'regtest',
|
|
71
|
+
};
|
|
72
|
+
if (kaleidoClientManager.hasNode()) {
|
|
73
|
+
try {
|
|
74
|
+
const client = kaleidoClientManager.getClient();
|
|
75
|
+
const nodeInfo = await client.rln.getNodeInfo();
|
|
76
|
+
const networkInfo = await client.rln.getNetworkInfo();
|
|
77
|
+
info.nodeId = nodeInfo.pubkey || '';
|
|
78
|
+
info.blockHeight = networkInfo.height || 0;
|
|
79
|
+
info.syncStatus = { synced: true, progress: 100 };
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.warn('[RgbAdapter] Could not get node info:', error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return info;
|
|
86
|
+
}
|
|
87
|
+
// ========================================================================
|
|
88
|
+
// Asset Operations
|
|
89
|
+
// ========================================================================
|
|
90
|
+
async listAssets() {
|
|
91
|
+
if (!this.isConnected()) {
|
|
92
|
+
throw new ProtocolError('Not connected', 'RGB', 'NOT_CONNECTED');
|
|
93
|
+
}
|
|
94
|
+
const client = kaleidoClientManager.getClient();
|
|
95
|
+
let nodeAssetsArray = [];
|
|
96
|
+
if (kaleidoClientManager.hasNode()) {
|
|
97
|
+
try {
|
|
98
|
+
const nodeAssets = await client.rln.listAssets();
|
|
99
|
+
const response = nodeAssets;
|
|
100
|
+
nodeAssetsArray = [
|
|
101
|
+
...(response.nia || []),
|
|
102
|
+
...(response.uda || []),
|
|
103
|
+
...(response.cfa || []),
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
console.warn('[RgbAdapter] Could not get node assets:', error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (nodeAssetsArray.length === 0) {
|
|
111
|
+
// Return at least BTC if no RGB assets
|
|
112
|
+
try {
|
|
113
|
+
const btcBalance = await client.rln.getBtcBalance();
|
|
114
|
+
return [this.createBtcAsset(btcBalance)];
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Add BTC as first asset
|
|
121
|
+
const assets = [];
|
|
122
|
+
try {
|
|
123
|
+
const btcBalance = await client.rln.getBtcBalance();
|
|
124
|
+
assets.push(this.createBtcAsset(btcBalance));
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// Skip BTC if balance check fails
|
|
128
|
+
}
|
|
129
|
+
assets.push(...nodeAssetsArray.map(a => this.convertNodeAssetToUnified(a)));
|
|
130
|
+
return assets;
|
|
131
|
+
}
|
|
132
|
+
async getAsset(assetId) {
|
|
133
|
+
const assets = await this.listAssets();
|
|
134
|
+
const asset = assets.find(a => a.id === assetId || a.ticker === assetId);
|
|
135
|
+
if (!asset) {
|
|
136
|
+
throw new ProtocolError(`Asset not found: ${assetId}`, 'RGB', 'ASSET_NOT_FOUND');
|
|
137
|
+
}
|
|
138
|
+
return asset;
|
|
139
|
+
}
|
|
140
|
+
async getAssetBalance(assetId) {
|
|
141
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
142
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const client = kaleidoClientManager.getClient();
|
|
146
|
+
if (assetId === 'BTC' || assetId.toLowerCase() === 'btc') {
|
|
147
|
+
const btcBalance = await client.rln.getBtcBalance();
|
|
148
|
+
return this.convertBtcBalance(btcBalance);
|
|
149
|
+
}
|
|
150
|
+
const balanceData = await client.rln.getAssetBalance({ asset_id: assetId });
|
|
151
|
+
return this.convertSdkBalance(balanceData);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
throw this.handleSdkError(error, 'Failed to get asset balance');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async refreshBalances() { }
|
|
158
|
+
// ========================================================================
|
|
159
|
+
// Transaction Operations
|
|
160
|
+
// ========================================================================
|
|
161
|
+
async listTransactions(filter) {
|
|
162
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
163
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const client = kaleidoClientManager.getClient();
|
|
167
|
+
if (!filter?.asset) {
|
|
168
|
+
throw new ProtocolError('Asset ID is required for listing RGB transfers', 'RGB', 'ASSET_ID_REQUIRED');
|
|
169
|
+
}
|
|
170
|
+
const response = await client.rln.listTransfers({ asset_id: filter.asset });
|
|
171
|
+
const transfers = response.transfers || [];
|
|
172
|
+
return transfers
|
|
173
|
+
.map(t => this.convertTransferToTransaction(t))
|
|
174
|
+
.filter(tx => {
|
|
175
|
+
if (!filter)
|
|
176
|
+
return true;
|
|
177
|
+
if (filter.type && tx.type !== filter.type)
|
|
178
|
+
return false;
|
|
179
|
+
if (filter.status && tx.status !== filter.status)
|
|
180
|
+
return false;
|
|
181
|
+
if (filter.fromTimestamp && tx.timestamp < filter.fromTimestamp)
|
|
182
|
+
return false;
|
|
183
|
+
if (filter.toTimestamp && tx.timestamp > filter.toTimestamp)
|
|
184
|
+
return false;
|
|
185
|
+
return true;
|
|
186
|
+
})
|
|
187
|
+
.slice(filter?.offset || 0, (filter?.offset || 0) + (filter?.limit || 100));
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
throw this.handleSdkError(error, 'Failed to list transactions');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async getTransaction(txId, assetId) {
|
|
194
|
+
if (!assetId) {
|
|
195
|
+
throw new ProtocolError('Asset ID is required to look up an RGB transaction', 'RGB', 'ASSET_ID_REQUIRED');
|
|
196
|
+
}
|
|
197
|
+
const transactions = await this.listTransactions({ asset: assetId });
|
|
198
|
+
const tx = transactions.find(t => t.id === txId);
|
|
199
|
+
if (!tx) {
|
|
200
|
+
throw new ProtocolError(`Transaction not found: ${txId}`, 'RGB', 'TX_NOT_FOUND');
|
|
201
|
+
}
|
|
202
|
+
return tx;
|
|
203
|
+
}
|
|
204
|
+
// ========================================================================
|
|
205
|
+
// Payment Operations
|
|
206
|
+
// ========================================================================
|
|
207
|
+
async createInvoice(request) {
|
|
208
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
209
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
const client = kaleidoClientManager.getClient();
|
|
213
|
+
const lnInvoiceParams = {
|
|
214
|
+
expiry_sec: request.expirySeconds || 3600,
|
|
215
|
+
};
|
|
216
|
+
const isRgbInvoice = request.asset && request.asset !== 'BTC' && request.asset !== 'btc';
|
|
217
|
+
if (isRgbInvoice) {
|
|
218
|
+
lnInvoiceParams.asset_id = request.asset;
|
|
219
|
+
if (request.assetAmount && request.assetAmount > 0) {
|
|
220
|
+
lnInvoiceParams.asset_amount = request.assetAmount;
|
|
221
|
+
}
|
|
222
|
+
// RGB HTLC requires minimum 3000 sats in msats
|
|
223
|
+
const RGB_HTLC_MIN_MSAT = 3000000;
|
|
224
|
+
const requestedMsat = request.amount && request.amount > 0 ? request.amount * 1000 : 0;
|
|
225
|
+
lnInvoiceParams.amt_msat = Math.max(requestedMsat, RGB_HTLC_MIN_MSAT);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
if (request.amount && request.amount > 0) {
|
|
229
|
+
lnInvoiceParams.amt_msat = request.amount * 1000;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const lnInvoice = await client.rln.createLNInvoice(lnInvoiceParams);
|
|
233
|
+
return {
|
|
234
|
+
invoice: lnInvoice.invoice ?? '',
|
|
235
|
+
paymentHash: lnInvoice.payment_hash ?? '',
|
|
236
|
+
amount: request.amount,
|
|
237
|
+
expiresAt: Date.now() + (request.expirySeconds || 3600) * 1000,
|
|
238
|
+
description: request.description,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
throw this.handleSdkError(error, 'Failed to create invoice');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async decodeInvoice(invoice) {
|
|
246
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
247
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
const client = kaleidoClientManager.getClient();
|
|
251
|
+
const decoded = await client.rln.decodeLNInvoice({ invoice });
|
|
252
|
+
const amtMsat = decoded.amt_msat;
|
|
253
|
+
return {
|
|
254
|
+
paymentHash: decoded.payment_hash ?? '',
|
|
255
|
+
amount: amtMsat != null ? amtMsat / 1000 : undefined,
|
|
256
|
+
amountMsat: amtMsat ?? undefined,
|
|
257
|
+
description: decoded.description,
|
|
258
|
+
expiresAt: decoded.expiry_sec ? Date.now() + decoded.expiry_sec * 1000 : 0,
|
|
259
|
+
destination: decoded.payee_pubkey || '',
|
|
260
|
+
asset_id: decoded.asset_id ?? undefined,
|
|
261
|
+
asset_amount: decoded.asset_amount ?? undefined,
|
|
262
|
+
payment_hash: decoded.payment_hash,
|
|
263
|
+
amount_msat: decoded.amt_msat ?? undefined,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
throw this.handleSdkError(error, 'Failed to decode invoice');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async sendPayment(request) {
|
|
271
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
272
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
const client = kaleidoClientManager.getClient();
|
|
276
|
+
const sendParams = { invoice: request.invoice };
|
|
277
|
+
if (request.amount) {
|
|
278
|
+
sendParams.amt_msat = request.amount * 1000;
|
|
279
|
+
}
|
|
280
|
+
const result = await client.rln.sendPayment(sendParams);
|
|
281
|
+
return {
|
|
282
|
+
paymentHash: result.payment_hash ?? '',
|
|
283
|
+
preimage: result.payment_preimage,
|
|
284
|
+
amount: result.amount_msat ? result.amount_msat / 1000 : 0,
|
|
285
|
+
fee: result.fee_msat ? result.fee_msat / 1000 : 0,
|
|
286
|
+
status: this.mapPaymentStatus(result.status),
|
|
287
|
+
timestamp: Date.now(),
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
throw this.handleSdkError(error, 'Failed to send payment');
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async getPaymentStatus(paymentHash) {
|
|
295
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
296
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
const client = kaleidoClientManager.getClient();
|
|
300
|
+
const response = await client.rln.getPayment({ payment_hash: paymentHash });
|
|
301
|
+
const payment = (response.payment ?? response);
|
|
302
|
+
return {
|
|
303
|
+
paymentHash,
|
|
304
|
+
status: this.mapPaymentStatus(payment.status),
|
|
305
|
+
amount: payment.amount_msat ? payment.amount_msat / 1000 : undefined,
|
|
306
|
+
fee: payment.fee_msat ? payment.fee_msat / 1000 : undefined,
|
|
307
|
+
timestamp: payment.created_at,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
throw this.handleSdkError(error, 'Failed to get payment status');
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// ========================================================================
|
|
315
|
+
// Address Operations
|
|
316
|
+
// ========================================================================
|
|
317
|
+
async getReceiveAddress(assetId) {
|
|
318
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
319
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
320
|
+
}
|
|
321
|
+
try {
|
|
322
|
+
const client = kaleidoClientManager.getClient();
|
|
323
|
+
const addressData = await client.rln.getAddress();
|
|
324
|
+
return {
|
|
325
|
+
address: addressData.address ?? '',
|
|
326
|
+
format: 'BTC_ADDRESS',
|
|
327
|
+
asset: assetId,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
throw this.handleSdkError(error, 'Failed to get receive address');
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// ========================================================================
|
|
335
|
+
// Node & Balance Operations
|
|
336
|
+
// ========================================================================
|
|
337
|
+
async getNodeInfo() {
|
|
338
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
339
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
return await kaleidoClientManager.getClient().rln.getNodeInfo();
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
throw this.handleSdkError(error, 'Failed to get node info');
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async getBtcBalance() {
|
|
349
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
350
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
const client = kaleidoClientManager.getClient();
|
|
354
|
+
const btcBalance = await client.rln.getBtcBalance();
|
|
355
|
+
const vanilla = btcBalance?.vanilla || {};
|
|
356
|
+
const colored = btcBalance?.colored || {};
|
|
357
|
+
const confirmed = (vanilla.spendable || 0) + (colored.spendable || 0);
|
|
358
|
+
const futureTotal = (vanilla.future || 0) + (colored.future || 0);
|
|
359
|
+
const unconfirmed = Math.max(futureTotal - confirmed, 0);
|
|
360
|
+
return { confirmed, unconfirmed, total: futureTotal };
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
throw this.handleSdkError(error, 'Failed to get BTC balance');
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
async listChannels() {
|
|
367
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
368
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
const response = await kaleidoClientManager.getClient().rln.listChannels();
|
|
372
|
+
return response?.channels || response || [];
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
throw this.handleSdkError(error, 'Failed to list channels');
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async listPayments() {
|
|
379
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
380
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
381
|
+
}
|
|
382
|
+
try {
|
|
383
|
+
return await kaleidoClientManager.getClient().rln.listPayments();
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
throw this.handleSdkError(error, 'Failed to list payments');
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
async listTransfers(options) {
|
|
390
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
391
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
if (!options?.asset_id) {
|
|
395
|
+
return { transfers: [] };
|
|
396
|
+
}
|
|
397
|
+
return await kaleidoClientManager.getClient().rln.listTransfers({ asset_id: options.asset_id });
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
throw this.handleSdkError(error, 'Failed to list transfers');
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
// ========================================================================
|
|
404
|
+
// RGB-Specific Operations
|
|
405
|
+
// ========================================================================
|
|
406
|
+
async createRgbInvoice(params) {
|
|
407
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
408
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const durationSec = params.durationSeconds || params.duration_seconds || 3600;
|
|
412
|
+
return await kaleidoClientManager.getClient().rln.createRgbInvoice({
|
|
413
|
+
asset_id: params.assetId || params.asset_id,
|
|
414
|
+
expiration_timestamp: Math.floor(Date.now() / 1000) + durationSec,
|
|
415
|
+
min_confirmations: params.minConfirmations || params.min_confirmations || 1,
|
|
416
|
+
witness: params.witness ?? true,
|
|
417
|
+
...(params.assignment ? { assignment: params.assignment } : {}),
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
throw this.handleSdkError(error, 'Failed to create RGB invoice');
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
async decodeRgbInvoice(params) {
|
|
425
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
426
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
427
|
+
}
|
|
428
|
+
try {
|
|
429
|
+
return await kaleidoClientManager.getClient().rln.decodeRgbInvoice({
|
|
430
|
+
invoice: params.invoice || params,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
catch (error) {
|
|
434
|
+
throw this.handleSdkError(error, 'Failed to decode RGB invoice');
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
async getInvoiceStatus(params) {
|
|
438
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
439
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
440
|
+
}
|
|
441
|
+
try {
|
|
442
|
+
return await kaleidoClientManager.getClient().rln.getInvoiceStatus(params);
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
throw this.handleSdkError(error, 'Failed to get invoice status');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
async sendAsset(params) {
|
|
449
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
450
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
const client = kaleidoClientManager.getClient();
|
|
454
|
+
const assetId = params.assetId || params.asset_id;
|
|
455
|
+
const amount = params.amount ?? params.assignment?.value;
|
|
456
|
+
const assignment = params.assignment ?? (amount != null ? { type: 'Fungible', value: amount } : undefined);
|
|
457
|
+
return await client.rln.sendRgb({
|
|
458
|
+
donation: params.donation || false,
|
|
459
|
+
fee_rate: params.feeRate || params.fee_rate || 5,
|
|
460
|
+
min_confirmations: 1,
|
|
461
|
+
recipient_map: {
|
|
462
|
+
[assetId]: [{
|
|
463
|
+
recipient_id: params.recipientId || params.recipient_id,
|
|
464
|
+
assignment,
|
|
465
|
+
transport_endpoints: params.transportEndpoints || params.transport_endpoints || [],
|
|
466
|
+
...(params.witness_data ? { witness_data: params.witness_data } : {}),
|
|
467
|
+
}],
|
|
468
|
+
},
|
|
469
|
+
// skip_sync is a valid runtime param; cast bridges kaleido-sdk type drift.
|
|
470
|
+
skip_sync: false,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
catch (error) {
|
|
474
|
+
throw this.handleSdkError(error, 'Failed to send RGB asset');
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
async sendBtcOnchain(params) {
|
|
478
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
479
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
480
|
+
}
|
|
481
|
+
try {
|
|
482
|
+
return await kaleidoClientManager.getClient().rln.sendBtc({
|
|
483
|
+
address: params.address,
|
|
484
|
+
amount: params.amount,
|
|
485
|
+
fee_rate: params.feeRate || 5,
|
|
486
|
+
skip_sync: false,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
throw this.handleSdkError(error, 'Failed to send BTC on-chain');
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
// ========================================================================
|
|
494
|
+
// Swap Operations (via KaleidoSwap Maker API)
|
|
495
|
+
// ========================================================================
|
|
496
|
+
supportsSwaps() {
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
async getSwapQuote(request) {
|
|
500
|
+
if (!this.isConnected()) {
|
|
501
|
+
throw new ProtocolError('Not connected', 'RGB', 'NOT_CONNECTED');
|
|
502
|
+
}
|
|
503
|
+
if (!this.config?.makerUrl) {
|
|
504
|
+
throw new ProtocolError('Maker API not configured. Swaps not available in node-only mode.', 'RGB', 'MAKER_NOT_CONFIGURED');
|
|
505
|
+
}
|
|
506
|
+
try {
|
|
507
|
+
const client = kaleidoClientManager.getClient();
|
|
508
|
+
const quoteResponse = await client.maker.getQuote({
|
|
509
|
+
from_asset: {
|
|
510
|
+
asset_id: request.fromAsset,
|
|
511
|
+
layer: SdkLayer.RGB_LN,
|
|
512
|
+
amount: request.fromAmount,
|
|
513
|
+
},
|
|
514
|
+
to_asset: {
|
|
515
|
+
asset_id: request.toAsset,
|
|
516
|
+
layer: SdkLayer.RGB_LN,
|
|
517
|
+
amount: request.toAmount,
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
return {
|
|
521
|
+
id: quoteResponse.rfq_id,
|
|
522
|
+
fromAsset: quoteResponse.from_asset.asset_id,
|
|
523
|
+
fromAmount: Number(quoteResponse.from_asset.amount || 0),
|
|
524
|
+
toAsset: quoteResponse.to_asset.asset_id,
|
|
525
|
+
toAmount: Number(quoteResponse.to_asset.amount || 0),
|
|
526
|
+
price: quoteResponse.price,
|
|
527
|
+
fee: {
|
|
528
|
+
amount: quoteResponse.fee.final_fee,
|
|
529
|
+
asset: quoteResponse.fee.fee_asset,
|
|
530
|
+
breakdown: {
|
|
531
|
+
baseFee: quoteResponse.fee.base_fee,
|
|
532
|
+
variableFee: quoteResponse.fee.variable_fee,
|
|
533
|
+
networkFee: 0,
|
|
534
|
+
},
|
|
535
|
+
},
|
|
536
|
+
expiresAt: quoteResponse.expires_at,
|
|
537
|
+
provider: 'Kaleidoswap',
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
catch (error) {
|
|
541
|
+
throw this.handleSdkError(error, 'Maker API connection failed');
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async executeSwap(quote) {
|
|
545
|
+
if (!this.isConnected()) {
|
|
546
|
+
throw new ProtocolError('Not connected', 'RGB', 'NOT_CONNECTED');
|
|
547
|
+
}
|
|
548
|
+
if (!this.config?.makerUrl) {
|
|
549
|
+
throw new ProtocolError('Maker API not configured.', 'RGB', 'MAKER_NOT_CONFIGURED');
|
|
550
|
+
}
|
|
551
|
+
try {
|
|
552
|
+
const client = kaleidoClientManager.getClient();
|
|
553
|
+
const quoteAny = quote;
|
|
554
|
+
const orderRequest = {
|
|
555
|
+
rfq_id: quoteAny.rfqId || quote.id || '',
|
|
556
|
+
from_asset: { asset_id: quote.fromAsset, amount: quote.fromAmount, layer: quoteAny.fromLayer || 'RGB_LN' },
|
|
557
|
+
to_asset: { asset_id: quote.toAsset, amount: quote.toAmount, layer: quoteAny.toLayer || 'RGB_LN' },
|
|
558
|
+
receiver_address: { address: quoteAny.receiverAddress || '', format: 'BTC_ADDRESS' },
|
|
559
|
+
min_onchain_conf: 1,
|
|
560
|
+
refund_address: '',
|
|
561
|
+
email: '',
|
|
562
|
+
};
|
|
563
|
+
const result = await client.maker.createSwapOrder(orderRequest);
|
|
564
|
+
const swapResult = result;
|
|
565
|
+
const swapId = (swapResult.order_id ?? swapResult.id ?? '');
|
|
566
|
+
if (swapId && swapResult.access_token) {
|
|
567
|
+
this.swapAccessTokens.set(swapId, swapResult.access_token);
|
|
568
|
+
}
|
|
569
|
+
return {
|
|
570
|
+
swapId,
|
|
571
|
+
paymentHash: (swapResult.payment_hash ?? ''),
|
|
572
|
+
status: this.mapSwapStatus(swapResult.status),
|
|
573
|
+
quote,
|
|
574
|
+
timestamp: Date.now(),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
catch (error) {
|
|
578
|
+
throw this.handleSdkError(error, 'Failed to execute swap');
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
async getSwapStatus(swapId) {
|
|
582
|
+
if (!this.isConnected()) {
|
|
583
|
+
throw new ProtocolError('Not connected', 'RGB', 'NOT_CONNECTED');
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
const client = kaleidoClientManager.getClient();
|
|
587
|
+
const accessToken = this.swapAccessTokens.get(swapId);
|
|
588
|
+
if (!accessToken) {
|
|
589
|
+
throw new ProtocolError('Missing swap access token for status lookup', 'RGB', 'SWAP_ACCESS_TOKEN_MISSING');
|
|
590
|
+
}
|
|
591
|
+
const status = await client.maker.getSwapOrderStatus({
|
|
592
|
+
order_id: swapId,
|
|
593
|
+
access_token: accessToken,
|
|
594
|
+
});
|
|
595
|
+
const order = (status.order ?? status);
|
|
596
|
+
return {
|
|
597
|
+
swapId,
|
|
598
|
+
status: this.mapSwapStatus(order.status),
|
|
599
|
+
quote: {},
|
|
600
|
+
timestamp: order.created_at || Date.now(),
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
catch (error) {
|
|
604
|
+
throw this.handleSdkError(error, 'Failed to get swap status');
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
// ========================================================================
|
|
608
|
+
// Protocol-Specific Operations (escape hatch)
|
|
609
|
+
// ========================================================================
|
|
610
|
+
async executeProtocolOperation(operation, params) {
|
|
611
|
+
if (!kaleidoClientManager.hasNode()) {
|
|
612
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
613
|
+
}
|
|
614
|
+
const client = kaleidoClientManager.getClient();
|
|
615
|
+
switch (operation) {
|
|
616
|
+
case 'initNode':
|
|
617
|
+
return client.rln.initWallet(params);
|
|
618
|
+
case 'unlockNode':
|
|
619
|
+
return client.rln.unlockWallet(params);
|
|
620
|
+
case 'lockNode':
|
|
621
|
+
return client.rln.lockWallet();
|
|
622
|
+
case 'createUtxos':
|
|
623
|
+
return client.rln.createUtxos(params);
|
|
624
|
+
case 'issueAssetNIA':
|
|
625
|
+
return client.rln.issueAssetNIA(params);
|
|
626
|
+
case 'issueAssetCFA':
|
|
627
|
+
return client.rln.issueAssetCFA(params);
|
|
628
|
+
case 'estimateFee':
|
|
629
|
+
return client.rln.estimateFee(params);
|
|
630
|
+
case 'failTransfers':
|
|
631
|
+
return client.rln.failTransfers(params);
|
|
632
|
+
case 'refreshTransfers':
|
|
633
|
+
return client.rln.refreshTransfers(params);
|
|
634
|
+
case 'sync':
|
|
635
|
+
return client.rln.syncRgbWallet();
|
|
636
|
+
case 'connectPeer':
|
|
637
|
+
return client.rln.connectPeer(params);
|
|
638
|
+
case 'disconnectPeer':
|
|
639
|
+
return client.rln.disconnectPeer(params);
|
|
640
|
+
case 'listPeers':
|
|
641
|
+
return client.rln.listPeers();
|
|
642
|
+
case 'openChannel':
|
|
643
|
+
return client.rln.openChannel(params);
|
|
644
|
+
case 'closeChannel':
|
|
645
|
+
return client.rln.closeChannel(params);
|
|
646
|
+
case 'getAssetMetadata':
|
|
647
|
+
return client.rln.getAssetMetadata(params);
|
|
648
|
+
case 'getTakerPubkey':
|
|
649
|
+
return client.rln.getTakerPubkey();
|
|
650
|
+
case 'whitelistSwap':
|
|
651
|
+
return client.rln.whitelistSwap(params);
|
|
652
|
+
case 'initSwap':
|
|
653
|
+
return client.maker.initSwap(params);
|
|
654
|
+
case 'confirmSwap':
|
|
655
|
+
return client.maker.executeSwap(params);
|
|
656
|
+
case 'listSwaps':
|
|
657
|
+
return client.rln.listSwaps();
|
|
658
|
+
case 'getSwap':
|
|
659
|
+
return client.rln.getSwap(params);
|
|
660
|
+
case 'getLspInfo':
|
|
661
|
+
return client.maker.getLspInfo();
|
|
662
|
+
case 'createLspOrder':
|
|
663
|
+
return client.maker.createLspOrder(params);
|
|
664
|
+
case 'getLspOrder':
|
|
665
|
+
return client.maker.getLspOrder(params);
|
|
666
|
+
case 'estimateLspFees':
|
|
667
|
+
return client.maker.estimateLspFees(params);
|
|
668
|
+
default:
|
|
669
|
+
throw new ProtocolError(`Unknown operation: ${operation}`, 'RGB', 'UNKNOWN_OPERATION');
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
// ========================================================================
|
|
673
|
+
// Private Helpers
|
|
674
|
+
// ========================================================================
|
|
675
|
+
createBtcAsset(btcBalance) {
|
|
676
|
+
const balance = this.convertBtcBalance(btcBalance);
|
|
677
|
+
return {
|
|
678
|
+
id: 'BTC',
|
|
679
|
+
name: 'Bitcoin (RGB Node)',
|
|
680
|
+
ticker: 'BTC',
|
|
681
|
+
precision: 8,
|
|
682
|
+
protocol: 'RGB',
|
|
683
|
+
layer: 'BTC_L1',
|
|
684
|
+
balance,
|
|
685
|
+
capabilities: {
|
|
686
|
+
canSend: true, canReceive: true, canSwap: true,
|
|
687
|
+
supportsLightning: true, supportsOnchain: true,
|
|
688
|
+
},
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
convertNodeAssetToUnified(asset) {
|
|
692
|
+
return {
|
|
693
|
+
id: asset.asset_id,
|
|
694
|
+
name: asset.name,
|
|
695
|
+
ticker: asset.ticker,
|
|
696
|
+
precision: asset.precision || 8,
|
|
697
|
+
protocol: 'RGB',
|
|
698
|
+
layer: 'RGB_LN',
|
|
699
|
+
balance: this.convertNodeBalance(asset.balance),
|
|
700
|
+
capabilities: {
|
|
701
|
+
canSend: true, canReceive: true, canSwap: false,
|
|
702
|
+
supportsLightning: true, supportsOnchain: true,
|
|
703
|
+
},
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
convertBtcBalance(btcBalance) {
|
|
707
|
+
const vanilla = btcBalance.vanilla ?? { settled: 0, future: 0, spendable: 0 };
|
|
708
|
+
return {
|
|
709
|
+
total: vanilla.settled || 0,
|
|
710
|
+
available: vanilla.spendable || 0,
|
|
711
|
+
pending: vanilla.future || 0,
|
|
712
|
+
totalDisplay: this.formatAmount(vanilla.settled || 0, 8),
|
|
713
|
+
availableDisplay: this.formatAmount(vanilla.spendable || 0, 8),
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
convertSdkBalance(balance) {
|
|
717
|
+
return {
|
|
718
|
+
total: balance.settled || 0,
|
|
719
|
+
available: balance.spendable || 0,
|
|
720
|
+
pending: balance.future || 0,
|
|
721
|
+
locked: balance.offchain_outbound || 0,
|
|
722
|
+
totalDisplay: this.formatAmount(balance.settled || 0, 8),
|
|
723
|
+
availableDisplay: this.formatAmount(balance.spendable || 0, 8),
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
convertNodeBalance(balance) {
|
|
727
|
+
const total = balance?.settled || 0;
|
|
728
|
+
const available = balance?.spendable || 0;
|
|
729
|
+
const pending = balance?.future || 0;
|
|
730
|
+
return {
|
|
731
|
+
total, available, pending,
|
|
732
|
+
locked: balance?.offchain_outbound || 0,
|
|
733
|
+
totalDisplay: this.formatAmount(total, 8),
|
|
734
|
+
availableDisplay: this.formatAmount(available, 8),
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
convertTransferToTransaction(transfer) {
|
|
738
|
+
return {
|
|
739
|
+
id: transfer.txid || `tx_${Date.now()}`,
|
|
740
|
+
type: this.mapTransferType(transfer.kind),
|
|
741
|
+
status: this.mapTransferStatus(transfer.status),
|
|
742
|
+
timestamp: transfer.created_at || Date.now(),
|
|
743
|
+
amount: transfer.amount || 0,
|
|
744
|
+
amountDisplay: this.formatAmount(transfer.amount || 0, 8),
|
|
745
|
+
fee: transfer.fee,
|
|
746
|
+
feeDisplay: this.formatAmount(transfer.fee || 0, 8),
|
|
747
|
+
asset: {},
|
|
748
|
+
from: transfer.sender,
|
|
749
|
+
to: transfer.recipient,
|
|
750
|
+
protocolData: transfer,
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
mapTransferType(kind) {
|
|
754
|
+
if (!kind)
|
|
755
|
+
return 'send';
|
|
756
|
+
if (kind.includes('receive') || kind.includes('ReceiveAsset'))
|
|
757
|
+
return 'receive';
|
|
758
|
+
if (kind.includes('send') || kind.includes('SendAsset'))
|
|
759
|
+
return 'send';
|
|
760
|
+
return 'send';
|
|
761
|
+
}
|
|
762
|
+
mapTransferStatus(status) {
|
|
763
|
+
if (!status)
|
|
764
|
+
return 'pending';
|
|
765
|
+
if (status === 'Settled' || status === 'settled')
|
|
766
|
+
return 'confirmed';
|
|
767
|
+
if (status === 'Failed' || status === 'failed')
|
|
768
|
+
return 'failed';
|
|
769
|
+
return 'pending';
|
|
770
|
+
}
|
|
771
|
+
mapPaymentStatus(status) {
|
|
772
|
+
if (!status)
|
|
773
|
+
return 'pending';
|
|
774
|
+
if (status === 'succeeded' || status === 'success' || status === 'Succeeded')
|
|
775
|
+
return 'confirmed';
|
|
776
|
+
if (status === 'failed' || status === 'Failed')
|
|
777
|
+
return 'failed';
|
|
778
|
+
return 'pending';
|
|
779
|
+
}
|
|
780
|
+
mapSwapStatus(status) {
|
|
781
|
+
if (!status)
|
|
782
|
+
return 'pending';
|
|
783
|
+
if (status === 'completed' || status === 'success' || status === 'Completed')
|
|
784
|
+
return 'confirmed';
|
|
785
|
+
if (status === 'failed' || status === 'error' || status === 'Failed')
|
|
786
|
+
return 'failed';
|
|
787
|
+
return 'pending';
|
|
788
|
+
}
|
|
789
|
+
formatAmount(amount, precision) {
|
|
790
|
+
return (amount / Math.pow(10, precision)).toFixed(precision);
|
|
791
|
+
}
|
|
792
|
+
// ========================================================================
|
|
793
|
+
// Error Handling
|
|
794
|
+
// ========================================================================
|
|
795
|
+
handleSdkError(error, context) {
|
|
796
|
+
if (error instanceof NodeNotConfiguredError) {
|
|
797
|
+
throw new ProtocolError('Node not configured', 'RGB', 'NODE_NOT_CONFIGURED');
|
|
798
|
+
}
|
|
799
|
+
else if (error instanceof QuoteExpiredError) {
|
|
800
|
+
throw new ProtocolError('Quote expired', 'RGB', 'QUOTE_EXPIRED');
|
|
801
|
+
}
|
|
802
|
+
else if (error instanceof SdkInsufficientBalanceError) {
|
|
803
|
+
throw new InsufficientBalanceError('Insufficient balance', 'RGB', 0, 0);
|
|
804
|
+
}
|
|
805
|
+
else if (error instanceof APIError) {
|
|
806
|
+
throw new ProtocolError(`${context}: ${error.message}`, 'RGB', 'API_ERROR', error);
|
|
807
|
+
}
|
|
808
|
+
else if (error instanceof NetworkError) {
|
|
809
|
+
throw new ConnectionError(`${context}: Network error - ${error.message}`, 'RGB', error);
|
|
810
|
+
}
|
|
811
|
+
else if (error instanceof KaleidoError) {
|
|
812
|
+
throw new ProtocolError(`${context}: ${error.message}`, 'RGB', 'SDK_ERROR', error);
|
|
813
|
+
}
|
|
814
|
+
const msg = error instanceof Error ? error.message : 'Unknown error';
|
|
815
|
+
throw new ProtocolError(`${context}: ${msg}`, 'RGB', 'UNKNOWN_ERROR', error instanceof Error ? error : undefined);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
//# sourceMappingURL=RgbAdapter.js.map
|