@pioneer-platform/pioneer-sdk 8.14.0 → 8.15.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/index.cjs +802 -222
- package/dist/index.es.js +810 -222
- package/dist/index.js +810 -222
- package/package.json +2 -2
- package/src/fees/index.ts +49 -1
- package/src/index.ts +67 -60
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,7 @@ var __export = (target, all) => {
|
|
|
39
39
|
set: (newValue) => all[name] = () => newValue
|
|
40
40
|
});
|
|
41
41
|
};
|
|
42
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
42
43
|
|
|
43
44
|
// ../../../node_modules/coinselect/utils.js
|
|
44
45
|
var require_utils = __commonJS((exports2, module2) => {
|
|
@@ -236,6 +237,546 @@ var require_split = __commonJS((exports2, module2) => {
|
|
|
236
237
|
};
|
|
237
238
|
});
|
|
238
239
|
|
|
240
|
+
// src/charts/utils.ts
|
|
241
|
+
function hydrateAssetData(caip) {
|
|
242
|
+
return import_pioneer_discovery.assetData[caip] || import_pioneer_discovery.assetData[caip.toLowerCase()];
|
|
243
|
+
}
|
|
244
|
+
function checkDuplicateBalance(balances, caip, pubkey, validator) {
|
|
245
|
+
return balances.some((b) => b.caip === caip && b.pubkey === pubkey && (!validator || b.validator === validator));
|
|
246
|
+
}
|
|
247
|
+
function createBalanceIdentifier(caip, pubkey) {
|
|
248
|
+
return `${caip}:${pubkey}`;
|
|
249
|
+
}
|
|
250
|
+
var import_pioneer_discovery;
|
|
251
|
+
var init_utils = __esm(() => {
|
|
252
|
+
import_pioneer_discovery = require("@pioneer-platform/pioneer-discovery");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// src/charts/custom-tokens.ts
|
|
256
|
+
async function fetchCustomTokens(params, balances) {
|
|
257
|
+
const { pioneer, pubkeys, blockchains, context } = params;
|
|
258
|
+
console.log(tag, "Fetching custom tokens...");
|
|
259
|
+
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
260
|
+
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
261
|
+
if (!primaryAddress) {
|
|
262
|
+
console.log(tag, "No EVM address found, skipping custom tokens");
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
console.log(tag, "Using EVM address for custom tokens:", primaryAddress);
|
|
266
|
+
const supportedNetworks = blockchains.filter((net) => net.startsWith("eip155:"));
|
|
267
|
+
if (supportedNetworks.length === 0) {
|
|
268
|
+
console.log(tag, "No EVM networks for custom tokens");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
console.log(tag, `Checking custom tokens on ${supportedNetworks.length} networks`);
|
|
272
|
+
for (const networkId of supportedNetworks) {
|
|
273
|
+
try {
|
|
274
|
+
const response = await pioneer.GetCustomTokens({ networkId, userAddress: primaryAddress });
|
|
275
|
+
const customTokens = response?.data?.tokens || [];
|
|
276
|
+
console.log(tag, `Found ${customTokens.length} custom tokens on ${networkId}`);
|
|
277
|
+
for (const token of customTokens) {
|
|
278
|
+
const chartBalance = processCustomToken(token, primaryAddress, context, blockchains);
|
|
279
|
+
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
280
|
+
balances.push(chartBalance);
|
|
281
|
+
console.log(tag, `Added custom token: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.error(tag, `Error fetching custom tokens for ${networkId}:`, error.message);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
console.log(tag, `Custom tokens complete. Total balances: ${balances.length}`);
|
|
289
|
+
}
|
|
290
|
+
function processCustomToken(token, primaryAddress, context, blockchains) {
|
|
291
|
+
if (!token.assetCaip || !token.networkId) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
let extractedNetworkId = token.networkId;
|
|
295
|
+
if (token.assetCaip.includes("/denom:")) {
|
|
296
|
+
const parts = token.assetCaip.split("/denom:");
|
|
297
|
+
if (parts.length > 0) {
|
|
298
|
+
extractedNetworkId = parts[0];
|
|
299
|
+
}
|
|
300
|
+
} else if (token.networkId && token.networkId.includes("/")) {
|
|
301
|
+
extractedNetworkId = token.networkId.split("/")[0];
|
|
302
|
+
}
|
|
303
|
+
const isEip155 = extractedNetworkId.startsWith("eip155:");
|
|
304
|
+
if (!isEip155 && !blockchains.includes(extractedNetworkId)) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
const tokenAssetInfo = hydrateAssetData(token.assetCaip);
|
|
308
|
+
const tokenPubkey = token.pubkey || primaryAddress;
|
|
309
|
+
const chartBalance = {
|
|
310
|
+
context,
|
|
311
|
+
chart: "pioneer",
|
|
312
|
+
contextType: context.split(":")[0],
|
|
313
|
+
name: tokenAssetInfo?.name || token.token?.name || "Unknown Custom Token",
|
|
314
|
+
caip: token.assetCaip,
|
|
315
|
+
icon: tokenAssetInfo?.icon || token.token?.icon || "",
|
|
316
|
+
pubkey: tokenPubkey,
|
|
317
|
+
ticker: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
318
|
+
ref: `${context}${token.assetCaip}`,
|
|
319
|
+
identifier: createBalanceIdentifier(token.assetCaip, tokenPubkey),
|
|
320
|
+
networkId: extractedNetworkId,
|
|
321
|
+
symbol: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
322
|
+
type: tokenAssetInfo?.type || "erc20",
|
|
323
|
+
token: true,
|
|
324
|
+
decimal: tokenAssetInfo?.decimal || token.token?.decimal,
|
|
325
|
+
balance: token.token?.balance?.toString() || "0",
|
|
326
|
+
priceUsd: token.token?.price || 0,
|
|
327
|
+
valueUsd: token.token?.balanceUSD || 0,
|
|
328
|
+
updated: new Date().getTime()
|
|
329
|
+
};
|
|
330
|
+
return chartBalance;
|
|
331
|
+
}
|
|
332
|
+
var tag = "| charts-custom-tokens |";
|
|
333
|
+
var init_custom_tokens = __esm(() => {
|
|
334
|
+
init_utils();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// src/charts/evm.ts
|
|
338
|
+
async function getEvmCharts(params) {
|
|
339
|
+
const { blockchains, pioneer, pubkeys, context } = params;
|
|
340
|
+
const balances = [];
|
|
341
|
+
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
342
|
+
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
343
|
+
console.log(tag2, "Total pubkeys available:", pubkeys.length);
|
|
344
|
+
console.log(tag2, "Blockchains to process:", blockchains);
|
|
345
|
+
const pubkeysForBatch = [];
|
|
346
|
+
for (const pubkey of pubkeys) {
|
|
347
|
+
const address = pubkey.address || pubkey.master || pubkey.pubkey;
|
|
348
|
+
if (!address)
|
|
349
|
+
continue;
|
|
350
|
+
const supportedNetworks = pubkey.networks || [];
|
|
351
|
+
for (const blockchain of blockchains) {
|
|
352
|
+
const supportsNetwork = supportedNetworks.some((net) => net === blockchain || net.endsWith(":*") && blockchain.startsWith(net.replace(":*", ":")));
|
|
353
|
+
if (supportsNetwork) {
|
|
354
|
+
let caip;
|
|
355
|
+
if (blockchain.startsWith("eip155:")) {
|
|
356
|
+
caip = `${blockchain}/slip44:60`;
|
|
357
|
+
} else {
|
|
358
|
+
caip = `${blockchain}/slip44:0`;
|
|
359
|
+
}
|
|
360
|
+
pubkeysForBatch.push({
|
|
361
|
+
pubkey: address,
|
|
362
|
+
caip
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
console.log(tag2, `Built ${pubkeysForBatch.length} pubkey-chain combinations for batch request`);
|
|
368
|
+
if (pubkeysForBatch.length === 0) {
|
|
369
|
+
console.log(tag2, "No pubkeys to query, skipping portfolio lookup");
|
|
370
|
+
return balances;
|
|
371
|
+
}
|
|
372
|
+
if (primaryAddress) {
|
|
373
|
+
await fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context);
|
|
374
|
+
}
|
|
375
|
+
await fetchCustomTokens({ blockchains, pioneer, pubkeys, context }, balances);
|
|
376
|
+
try {
|
|
377
|
+
console.log(tag2, `Calling GetPortfolio with ${pubkeysForBatch.length} pubkeys (batch + non-blocking)`);
|
|
378
|
+
let portfolio = await pioneer.GetPortfolio({
|
|
379
|
+
pubkeys: pubkeysForBatch
|
|
380
|
+
});
|
|
381
|
+
portfolio = portfolio.data?.data || portfolio.data;
|
|
382
|
+
if (!portfolio || !portfolio.balances) {
|
|
383
|
+
console.error(tag2, "No portfolio.balances found:", portfolio);
|
|
384
|
+
return balances;
|
|
385
|
+
}
|
|
386
|
+
console.log(tag2, `Portfolio returned ${portfolio.balances.length} balances`);
|
|
387
|
+
let processedCount = 0;
|
|
388
|
+
let skippedCount = 0;
|
|
389
|
+
for (const balance of portfolio.balances) {
|
|
390
|
+
const processedBalance = processPortfolioBalance(balance, primaryAddress, context, blockchains);
|
|
391
|
+
if (processedBalance) {
|
|
392
|
+
if (!checkDuplicateBalance(balances, processedBalance.caip, processedBalance.pubkey)) {
|
|
393
|
+
balances.push(processedBalance);
|
|
394
|
+
processedCount++;
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
skippedCount++;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
console.log(tag2, `Processed ${processedCount} balances, skipped ${skippedCount}`);
|
|
401
|
+
if (portfolio.tokens && portfolio.tokens.length > 0) {
|
|
402
|
+
console.log(tag2, "Processing portfolio.tokens:", portfolio.tokens.length);
|
|
403
|
+
for (const token of portfolio.tokens) {
|
|
404
|
+
const processedToken = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
405
|
+
if (processedToken && !checkDuplicateBalance(balances, processedToken.caip, processedToken.pubkey)) {
|
|
406
|
+
balances.push(processedToken);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
console.log(tag2, `Total balances (native + tokens): ${balances.length}`);
|
|
411
|
+
} catch (e) {
|
|
412
|
+
console.error(tag2, "Error fetching portfolio:", e);
|
|
413
|
+
}
|
|
414
|
+
return balances;
|
|
415
|
+
}
|
|
416
|
+
function processPortfolioBalance(balance, primaryAddress, context, blockchains) {
|
|
417
|
+
if (!balance.caip) {
|
|
418
|
+
console.error(tag2, "No caip found for:", balance);
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
const networkId = balance.caip.split("/")[0];
|
|
422
|
+
const isEip155 = networkId.startsWith("eip155:");
|
|
423
|
+
if (!isEip155 && !blockchains.includes(networkId)) {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
const assetInfo = hydrateAssetData(balance.caip);
|
|
427
|
+
const balancePubkey = balance.pubkey || primaryAddress;
|
|
428
|
+
const balanceType = assetInfo?.type || balance.type || "native";
|
|
429
|
+
const isToken = balanceType !== "native" && balance.caip.includes("/erc20:");
|
|
430
|
+
let calculatedPrice = balance.priceUsd || balance.price || 0;
|
|
431
|
+
if ((!calculatedPrice || calculatedPrice === 0) && balance.valueUsd && balance.balance) {
|
|
432
|
+
const balanceNum = parseFloat(balance.balance.toString());
|
|
433
|
+
const valueNum = parseFloat(balance.valueUsd.toString());
|
|
434
|
+
if (balanceNum > 0 && valueNum > 0) {
|
|
435
|
+
calculatedPrice = valueNum / balanceNum;
|
|
436
|
+
console.log(tag2, `Calculated price from value/balance: ${calculatedPrice} for ${balance.caip}`);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const chartBalance = {
|
|
440
|
+
context,
|
|
441
|
+
chart: "pioneer",
|
|
442
|
+
contextType: "keepkey",
|
|
443
|
+
name: assetInfo?.name || balance.name || "Unknown",
|
|
444
|
+
caip: balance.caip,
|
|
445
|
+
icon: assetInfo?.icon || balance.icon || "",
|
|
446
|
+
pubkey: balancePubkey,
|
|
447
|
+
ticker: assetInfo?.symbol || balance.symbol || "UNK",
|
|
448
|
+
ref: `${context}${balance.caip}`,
|
|
449
|
+
identifier: createBalanceIdentifier(balance.caip, balancePubkey),
|
|
450
|
+
networkId,
|
|
451
|
+
chain: networkId,
|
|
452
|
+
symbol: assetInfo?.symbol || balance.symbol || "UNK",
|
|
453
|
+
type: balanceType,
|
|
454
|
+
token: isToken,
|
|
455
|
+
decimal: assetInfo?.decimal || balance.decimal,
|
|
456
|
+
balance: balance.balance.toString(),
|
|
457
|
+
price: calculatedPrice,
|
|
458
|
+
priceUsd: calculatedPrice,
|
|
459
|
+
valueUsd: balance.valueUsd.toString(),
|
|
460
|
+
updated: new Date().getTime()
|
|
461
|
+
};
|
|
462
|
+
if (balance.display) {
|
|
463
|
+
chartBalance.icon = ["multi", chartBalance.icon, balance.display.toString()].toString();
|
|
464
|
+
}
|
|
465
|
+
return chartBalance;
|
|
466
|
+
}
|
|
467
|
+
function processPortfolioToken(token, primaryAddress, context, blockchains) {
|
|
468
|
+
if (!token.assetCaip || !token.networkId) {
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
let extractedNetworkId = token.networkId;
|
|
472
|
+
if (token.assetCaip.includes("/denom:")) {
|
|
473
|
+
const parts = token.assetCaip.split("/denom:");
|
|
474
|
+
if (parts.length > 0) {
|
|
475
|
+
extractedNetworkId = parts[0];
|
|
476
|
+
}
|
|
477
|
+
} else if (token.networkId && token.networkId.includes("/")) {
|
|
478
|
+
extractedNetworkId = token.networkId.split("/")[0];
|
|
479
|
+
}
|
|
480
|
+
const isEip155 = extractedNetworkId.startsWith("eip155:");
|
|
481
|
+
if (!isEip155 && !blockchains.includes(extractedNetworkId)) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
const tokenAssetInfo = hydrateAssetData(token.assetCaip);
|
|
485
|
+
const tokenPubkey = token.pubkey || primaryAddress;
|
|
486
|
+
const chartBalance = {
|
|
487
|
+
context,
|
|
488
|
+
chart: "pioneer",
|
|
489
|
+
contextType: context.split(":")[0],
|
|
490
|
+
name: tokenAssetInfo?.name || token.token?.coingeckoId || token.token?.name || "Unknown",
|
|
491
|
+
caip: token.assetCaip,
|
|
492
|
+
icon: tokenAssetInfo?.icon || token.token?.icon || "",
|
|
493
|
+
pubkey: tokenPubkey,
|
|
494
|
+
ticker: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
495
|
+
ref: `${context}${token.assetCaip}`,
|
|
496
|
+
identifier: createBalanceIdentifier(token.assetCaip, tokenPubkey),
|
|
497
|
+
networkId: extractedNetworkId,
|
|
498
|
+
symbol: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
499
|
+
type: tokenAssetInfo?.type || "token",
|
|
500
|
+
token: true,
|
|
501
|
+
decimal: tokenAssetInfo?.decimal || token.token?.decimal,
|
|
502
|
+
balance: token.token?.balance?.toString() || "0",
|
|
503
|
+
priceUsd: token.token?.price || 0,
|
|
504
|
+
valueUsd: token.token?.balanceUSD || 0,
|
|
505
|
+
updated: new Date().getTime()
|
|
506
|
+
};
|
|
507
|
+
return chartBalance;
|
|
508
|
+
}
|
|
509
|
+
async function fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context) {
|
|
510
|
+
console.log(tag2, "Fetching stable coins for redundancy...");
|
|
511
|
+
try {
|
|
512
|
+
const fastFlag = typeof process !== "undefined" && process.env && process.env.PIONEER_FAST === "1";
|
|
513
|
+
const isTestContext = typeof context === "string" && /test|e2e/i.test(context);
|
|
514
|
+
if (fastFlag || isTestContext) {
|
|
515
|
+
console.log(tag2, "Fast mode detected (test/e2e). Skipping stable coin redundancy.");
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
} catch (_) {
|
|
519
|
+
}
|
|
520
|
+
const supportedNetworks = ["eip155:1", "eip155:137", "eip155:8453", "eip155:56"];
|
|
521
|
+
const networksToCheck = blockchains.filter((net) => supportedNetworks.includes(net));
|
|
522
|
+
if (networksToCheck.length === 0) {
|
|
523
|
+
console.log(tag2, "No supported networks for stable coins");
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
console.log(tag2, `Checking stable coins on ${networksToCheck.length} networks`);
|
|
527
|
+
const withTimeout2 = (p, ms) => {
|
|
528
|
+
return new Promise((resolve, reject) => {
|
|
529
|
+
const t = setTimeout(() => reject(new Error(`timeout ${ms}ms`)), ms);
|
|
530
|
+
p.then((v) => {
|
|
531
|
+
clearTimeout(t);
|
|
532
|
+
resolve(v);
|
|
533
|
+
}).catch((e) => {
|
|
534
|
+
clearTimeout(t);
|
|
535
|
+
reject(e);
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
const results = await Promise.allSettled(networksToCheck.map(async (networkId) => {
|
|
540
|
+
try {
|
|
541
|
+
const response = await withTimeout2(pioneer.GetStableCoins({ networkId, address: primaryAddress }), 2000);
|
|
542
|
+
const stableCoins = response?.data?.tokens || [];
|
|
543
|
+
console.log(tag2, `Found ${stableCoins.length} stable coins on ${networkId}`);
|
|
544
|
+
for (const token of stableCoins) {
|
|
545
|
+
const chartBalance = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
546
|
+
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
547
|
+
balances.push(chartBalance);
|
|
548
|
+
console.log(tag2, `Added stable coin: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
} catch (error) {
|
|
552
|
+
console.error(tag2, `Error fetching stable coins for ${networkId}:`, error?.message || error);
|
|
553
|
+
}
|
|
554
|
+
}));
|
|
555
|
+
const failures = results.filter((r) => r.status === "rejected").length;
|
|
556
|
+
if (failures > 0)
|
|
557
|
+
console.log(tag2, `Stable coin fetch had ${failures} failures (non-blocking)`);
|
|
558
|
+
console.log(tag2, `Stable coin redundancy complete. Total balances: ${balances.length}`);
|
|
559
|
+
}
|
|
560
|
+
var tag2 = "| charts-evm |";
|
|
561
|
+
var init_evm = __esm(() => {
|
|
562
|
+
init_utils();
|
|
563
|
+
init_custom_tokens();
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
// src/charts/maya.ts
|
|
567
|
+
async function getMayaCharts(params, existingBalances) {
|
|
568
|
+
const { blockchains, pioneer, pubkeys, context } = params;
|
|
569
|
+
const balances = [];
|
|
570
|
+
try {
|
|
571
|
+
const mayaPubkey = pubkeys.find((p) => p.networks && Array.isArray(p.networks) && p.networks.includes("cosmos:mayachain-mainnet-v1"));
|
|
572
|
+
if (!mayaPubkey || !mayaPubkey.address) {
|
|
573
|
+
console.log(tag3, "No MAYA pubkey found, skipping MAYA token fetch");
|
|
574
|
+
return balances;
|
|
575
|
+
}
|
|
576
|
+
if (!blockchains.includes("cosmos:mayachain-mainnet-v1")) {
|
|
577
|
+
console.log(tag3, "MAYA network not in blockchains list, skipping MAYA token fetch");
|
|
578
|
+
return balances;
|
|
579
|
+
}
|
|
580
|
+
const hasMayaToken = existingBalances.some((b) => b.caip === "cosmos:mayachain-mainnet-v1/denom:maya");
|
|
581
|
+
if (hasMayaToken) {
|
|
582
|
+
console.log(tag3, "MAYA token already exists in balances, skipping");
|
|
583
|
+
return balances;
|
|
584
|
+
}
|
|
585
|
+
console.log(tag3, "MAYA token not found in portfolio, fetching separately...");
|
|
586
|
+
console.log(tag3, "MAYA pubkey address:", mayaPubkey.address);
|
|
587
|
+
const mayaBalanceResponse = await pioneer.GetPortfolioBalances({
|
|
588
|
+
pubkeys: [
|
|
589
|
+
{
|
|
590
|
+
caip: "cosmos:mayachain-mainnet-v1/denom:maya",
|
|
591
|
+
pubkey: mayaPubkey.address
|
|
592
|
+
}
|
|
593
|
+
]
|
|
594
|
+
});
|
|
595
|
+
console.log(tag3, "MAYA balance response:", JSON.stringify(mayaBalanceResponse?.data, null, 2));
|
|
596
|
+
if (!mayaBalanceResponse?.data || mayaBalanceResponse.data.length === 0) {
|
|
597
|
+
console.log(tag3, "No MAYA token balance returned from GetPortfolioBalances API");
|
|
598
|
+
return balances;
|
|
599
|
+
}
|
|
600
|
+
console.log(tag3, "Found MAYA token balances:", mayaBalanceResponse.data.length);
|
|
601
|
+
for (const mayaBalance of mayaBalanceResponse.data) {
|
|
602
|
+
if (mayaBalance.caip !== "cosmos:mayachain-mainnet-v1/denom:maya") {
|
|
603
|
+
console.log(tag3, "Unexpected balance in MAYA response:", mayaBalance);
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
const mayaAssetInfo = hydrateAssetData(mayaBalance.caip);
|
|
607
|
+
const isToken = mayaBalance.caip.includes("/denom:") && !mayaBalance.caip.endsWith("/denom:cacao");
|
|
608
|
+
const mayaTokenBalance = {
|
|
609
|
+
context,
|
|
610
|
+
chart: "pioneer",
|
|
611
|
+
contextType: context.split(":")[0],
|
|
612
|
+
name: mayaAssetInfo?.name || "Maya Token",
|
|
613
|
+
caip: mayaBalance.caip,
|
|
614
|
+
icon: mayaAssetInfo?.icon || "https://pioneers.dev/coins/maya.png",
|
|
615
|
+
pubkey: mayaPubkey.address,
|
|
616
|
+
ticker: mayaAssetInfo?.symbol || "MAYA",
|
|
617
|
+
ref: `${context}${mayaBalance.caip}`,
|
|
618
|
+
identifier: createBalanceIdentifier(mayaBalance.caip, mayaPubkey.address),
|
|
619
|
+
networkId: "cosmos:mayachain-mainnet-v1",
|
|
620
|
+
symbol: mayaAssetInfo?.symbol || "MAYA",
|
|
621
|
+
type: mayaAssetInfo?.type || "token",
|
|
622
|
+
token: isToken,
|
|
623
|
+
decimal: mayaAssetInfo?.decimal,
|
|
624
|
+
balance: mayaBalance.balance?.toString() || "0",
|
|
625
|
+
priceUsd: parseFloat(mayaBalance.priceUsd) || 0,
|
|
626
|
+
valueUsd: parseFloat(mayaBalance.valueUsd) || 0,
|
|
627
|
+
updated: new Date().getTime()
|
|
628
|
+
};
|
|
629
|
+
console.log(tag3, "Adding MAYA token to balances:", mayaTokenBalance);
|
|
630
|
+
balances.push(mayaTokenBalance);
|
|
631
|
+
}
|
|
632
|
+
} catch (mayaError) {
|
|
633
|
+
console.error(tag3, "Error fetching MAYA token balance:", mayaError);
|
|
634
|
+
}
|
|
635
|
+
return balances;
|
|
636
|
+
}
|
|
637
|
+
var tag3 = "| charts-maya |";
|
|
638
|
+
var init_maya = __esm(() => {
|
|
639
|
+
init_utils();
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
// src/charts/cosmos-staking.ts
|
|
643
|
+
async function getCosmosStakingCharts(params) {
|
|
644
|
+
const { blockchains, pioneer, pubkeys, context } = params;
|
|
645
|
+
const balances = [];
|
|
646
|
+
try {
|
|
647
|
+
try {
|
|
648
|
+
const fastFlag = typeof process !== "undefined" && process.env && process.env.PIONEER_FAST === "1";
|
|
649
|
+
const isTestContext = typeof context === "string" && /test|e2e/i.test(context);
|
|
650
|
+
if (fastFlag || isTestContext) {
|
|
651
|
+
console.log(tag4, "Fast mode detected (test/e2e). Skipping cosmos staking fetch.");
|
|
652
|
+
return balances;
|
|
653
|
+
}
|
|
654
|
+
} catch (_) {
|
|
655
|
+
}
|
|
656
|
+
console.log(tag4, "Adding Cosmos staking positions to charts...");
|
|
657
|
+
const cosmosPubkeys = pubkeys.filter((p) => p.networks && Array.isArray(p.networks) && p.networks.some((n) => n.includes("cosmos:cosmoshub") || n.includes("cosmos:osmosis")));
|
|
658
|
+
if (cosmosPubkeys.length === 0) {
|
|
659
|
+
console.log(tag4, "No cosmos pubkeys found for staking positions");
|
|
660
|
+
return balances;
|
|
661
|
+
}
|
|
662
|
+
console.log(tag4, "Found cosmos pubkeys for staking:", cosmosPubkeys.length);
|
|
663
|
+
await Promise.allSettled(cosmosPubkeys.map(async (cosmosPubkey) => {
|
|
664
|
+
if (!cosmosPubkey.address)
|
|
665
|
+
return;
|
|
666
|
+
const cosmosNetworks = cosmosPubkey.networks.filter((n) => n.includes("cosmos:cosmoshub") || n.includes("cosmos:osmosis"));
|
|
667
|
+
await Promise.allSettled(cosmosNetworks.filter((networkId) => blockchains.includes(networkId)).map(async (networkId) => {
|
|
668
|
+
await fetchStakingPositionsForNetwork(networkId, cosmosPubkey.address, context, pioneer, balances);
|
|
669
|
+
}));
|
|
670
|
+
}));
|
|
671
|
+
} catch (e) {
|
|
672
|
+
console.error(tag4, "Error adding cosmos staking positions:", e);
|
|
673
|
+
}
|
|
674
|
+
return balances;
|
|
675
|
+
}
|
|
676
|
+
async function fetchStakingPositionsForNetwork(networkId, address, context, pioneer, balances) {
|
|
677
|
+
try {
|
|
678
|
+
console.log(tag4, `Fetching staking positions for ${address} on ${networkId}...`);
|
|
679
|
+
let network;
|
|
680
|
+
if (networkId === "cosmos:cosmoshub-4") {
|
|
681
|
+
network = "cosmos";
|
|
682
|
+
} else if (networkId === "cosmos:osmosis-1") {
|
|
683
|
+
network = "osmosis";
|
|
684
|
+
} else {
|
|
685
|
+
console.error(tag4, `Unsupported networkId for staking: ${networkId}`);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const stakingResponse = await pioneer.GetStakingPositions({
|
|
689
|
+
network,
|
|
690
|
+
address
|
|
691
|
+
});
|
|
692
|
+
if (!stakingResponse?.data || !Array.isArray(stakingResponse.data)) {
|
|
693
|
+
console.log(tag4, `No staking positions found for ${address} on ${networkId}`);
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
console.log(tag4, `Found ${stakingResponse.data.length} staking positions for ${networkId}`);
|
|
697
|
+
for (const position of stakingResponse.data) {
|
|
698
|
+
const processedPosition = processStakingPosition(position, address, context, networkId);
|
|
699
|
+
if (processedPosition && !checkDuplicateBalance(balances, processedPosition.caip, processedPosition.pubkey, processedPosition.validator)) {
|
|
700
|
+
balances.push(processedPosition);
|
|
701
|
+
console.log(tag4, `Added ${position.type} position:`, {
|
|
702
|
+
balance: processedPosition.balance,
|
|
703
|
+
ticker: processedPosition.ticker,
|
|
704
|
+
valueUsd: processedPosition.valueUsd,
|
|
705
|
+
validator: processedPosition.validator
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
} catch (stakingError) {
|
|
710
|
+
console.error(tag4, `Error fetching staking positions for ${address} on ${networkId}:`, stakingError);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function processStakingPosition(position, address, context, networkId) {
|
|
714
|
+
if (!position.balance || position.balance <= 0 || !position.caip) {
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
const stakingAssetInfo = hydrateAssetData(position.caip);
|
|
718
|
+
const stakingBalance = {
|
|
719
|
+
context,
|
|
720
|
+
chart: "staking",
|
|
721
|
+
contextType: context.split(":")[0],
|
|
722
|
+
name: stakingAssetInfo?.name || position.name || `${position.type} Position`,
|
|
723
|
+
caip: position.caip,
|
|
724
|
+
icon: stakingAssetInfo?.icon || position.icon || "",
|
|
725
|
+
pubkey: address,
|
|
726
|
+
ticker: stakingAssetInfo?.symbol || position.ticker || position.symbol || "UNK",
|
|
727
|
+
ref: `${context}${position.caip}`,
|
|
728
|
+
identifier: createBalanceIdentifier(position.caip, address),
|
|
729
|
+
networkId,
|
|
730
|
+
symbol: stakingAssetInfo?.symbol || position.symbol || position.ticker || "UNK",
|
|
731
|
+
type: stakingAssetInfo?.type || position.type || "staking",
|
|
732
|
+
token: false,
|
|
733
|
+
decimal: stakingAssetInfo?.decimal,
|
|
734
|
+
balance: position.balance.toString(),
|
|
735
|
+
priceUsd: position.priceUsd || 0,
|
|
736
|
+
valueUsd: position.valueUsd || position.balance * (position.priceUsd || 0),
|
|
737
|
+
status: position.status || "active",
|
|
738
|
+
validator: position.validatorAddress || position.validator || "",
|
|
739
|
+
updated: new Date().getTime()
|
|
740
|
+
};
|
|
741
|
+
return stakingBalance;
|
|
742
|
+
}
|
|
743
|
+
var tag4 = "| charts-cosmos-staking |";
|
|
744
|
+
var init_cosmos_staking = __esm(() => {
|
|
745
|
+
init_utils();
|
|
746
|
+
});
|
|
747
|
+
// src/charts/index.ts
|
|
748
|
+
var exports_charts = {};
|
|
749
|
+
__export(exports_charts, {
|
|
750
|
+
getCharts: () => getCharts
|
|
751
|
+
});
|
|
752
|
+
var tag5 = "| getCharts |", getCharts = async (blockchains, pioneer, pubkeys, context) => {
|
|
753
|
+
try {
|
|
754
|
+
const balances = [];
|
|
755
|
+
console.log(tag5, "init");
|
|
756
|
+
const params = {
|
|
757
|
+
blockchains,
|
|
758
|
+
pioneer,
|
|
759
|
+
pubkeys,
|
|
760
|
+
context
|
|
761
|
+
};
|
|
762
|
+
const evmBalances = await getEvmCharts(params);
|
|
763
|
+
balances.push(...evmBalances);
|
|
764
|
+
const mayaBalances = await getMayaCharts(params, balances);
|
|
765
|
+
balances.push(...mayaBalances);
|
|
766
|
+
const stakingBalances = await getCosmosStakingCharts(params);
|
|
767
|
+
balances.push(...stakingBalances);
|
|
768
|
+
return balances;
|
|
769
|
+
} catch (error) {
|
|
770
|
+
console.error(tag5, "Error processing charts:", error);
|
|
771
|
+
throw error;
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
var init_charts = __esm(() => {
|
|
775
|
+
init_evm();
|
|
776
|
+
init_maya();
|
|
777
|
+
init_cosmos_staking();
|
|
778
|
+
});
|
|
779
|
+
|
|
239
780
|
// src/index.ts
|
|
240
781
|
var exports_src = {};
|
|
241
782
|
__export(exports_src, {
|
|
@@ -390,7 +931,7 @@ class Pioneer {
|
|
|
390
931
|
|
|
391
932
|
// src/index.ts
|
|
392
933
|
var import_pioneer_coins4 = require("@pioneer-platform/pioneer-coins");
|
|
393
|
-
var
|
|
934
|
+
var import_pioneer_discovery2 = require("@pioneer-platform/pioneer-discovery");
|
|
394
935
|
var import_pioneer_events = require("@pioneer-platform/pioneer-events");
|
|
395
936
|
var import_events = __toESM(require("events"));
|
|
396
937
|
|
|
@@ -2769,6 +3310,10 @@ async function getFees(pioneer, networkId) {
|
|
|
2769
3310
|
console.log(tag, "Using hardcoded fees for Cosmos network:", networkId);
|
|
2770
3311
|
return getCosmosFees(networkId);
|
|
2771
3312
|
}
|
|
3313
|
+
if (networkId === "ripple:4109c6f2045fc7eff4cde8f9905d19c2") {
|
|
3314
|
+
console.log(tag, "Using hardcoded fees for Ripple: 0.00001 XRP");
|
|
3315
|
+
return getRippleFees(networkId);
|
|
3316
|
+
}
|
|
2772
3317
|
if (networkId === "bip122:00000000001a91e3dace36e2be3bf030") {
|
|
2773
3318
|
console.log(tag, "Using hardcoded fees for Dogecoin: 10 sat/byte");
|
|
2774
3319
|
return {
|
|
@@ -3095,6 +3640,39 @@ function getCosmosFees(networkId) {
|
|
|
3095
3640
|
raw: { hardcoded: true, base: feeConfig.base, unit: feeConfig.unit, denom: feeConfig.denom }
|
|
3096
3641
|
};
|
|
3097
3642
|
}
|
|
3643
|
+
function getRippleFees(networkId) {
|
|
3644
|
+
const networkName = getNetworkName(networkId);
|
|
3645
|
+
const standardFee = "0.00001";
|
|
3646
|
+
return {
|
|
3647
|
+
slow: {
|
|
3648
|
+
label: "Standard",
|
|
3649
|
+
value: standardFee,
|
|
3650
|
+
unit: "XRP",
|
|
3651
|
+
description: `Fixed fee for ${networkName}. XRP uses a deterministic fee model.`,
|
|
3652
|
+
estimatedTime: "~4 seconds",
|
|
3653
|
+
priority: "low"
|
|
3654
|
+
},
|
|
3655
|
+
average: {
|
|
3656
|
+
label: "Standard",
|
|
3657
|
+
value: standardFee,
|
|
3658
|
+
unit: "XRP",
|
|
3659
|
+
description: `Fixed fee for ${networkName}. XRP uses a deterministic fee model.`,
|
|
3660
|
+
estimatedTime: "~4 seconds",
|
|
3661
|
+
priority: "medium"
|
|
3662
|
+
},
|
|
3663
|
+
fastest: {
|
|
3664
|
+
label: "Standard",
|
|
3665
|
+
value: standardFee,
|
|
3666
|
+
unit: "XRP",
|
|
3667
|
+
description: `Fixed fee for ${networkName}. XRP uses a deterministic fee model.`,
|
|
3668
|
+
estimatedTime: "~4 seconds",
|
|
3669
|
+
priority: "high"
|
|
3670
|
+
},
|
|
3671
|
+
networkId,
|
|
3672
|
+
networkType: "RIPPLE",
|
|
3673
|
+
raw: { hardcoded: true, fee: standardFee, unit: "XRP" }
|
|
3674
|
+
};
|
|
3675
|
+
}
|
|
3098
3676
|
function getFallbackFees(networkId) {
|
|
3099
3677
|
const networkType = getNetworkType(networkId);
|
|
3100
3678
|
const networkName = getNetworkName(networkId);
|
|
@@ -3493,7 +4071,7 @@ class SDK {
|
|
|
3493
4071
|
this.appIcon = config.appIcon || "https://pioneers.dev/coins/keepkey.png";
|
|
3494
4072
|
this.spec = spec || config.spec || "https://api.keepkey.info/spec/swagger";
|
|
3495
4073
|
this.wss = config.wss || "wss://api.keepkey.info";
|
|
3496
|
-
this.assets =
|
|
4074
|
+
this.assets = import_pioneer_discovery2.assetData;
|
|
3497
4075
|
this.assetsMap = new Map;
|
|
3498
4076
|
this.username = config.username;
|
|
3499
4077
|
this.queryKey = config.queryKey;
|
|
@@ -3544,9 +4122,9 @@ class SDK {
|
|
|
3544
4122
|
fallbackToRemote: true
|
|
3545
4123
|
}) : null;
|
|
3546
4124
|
this.pairWallet = async (options) => {
|
|
3547
|
-
const
|
|
4125
|
+
const tag6 = TAG9 + " | pairWallet | ";
|
|
3548
4126
|
if (this.viewOnlyMode || this.skipDevicePairing) {
|
|
3549
|
-
console.log(
|
|
4127
|
+
console.log(tag6, "\uD83D\uDC41️ [VIEW-ONLY] Skipping device pairing");
|
|
3550
4128
|
return {
|
|
3551
4129
|
success: false,
|
|
3552
4130
|
reason: "view-only-mode",
|
|
@@ -3587,7 +4165,7 @@ class SDK {
|
|
|
3587
4165
|
return true;
|
|
3588
4166
|
};
|
|
3589
4167
|
this.setPubkeys = (newPubkeys) => {
|
|
3590
|
-
const
|
|
4168
|
+
const tag6 = `${TAG9} | setPubkeys | `;
|
|
3591
4169
|
this.pubkeys = [];
|
|
3592
4170
|
this.pubkeySet.clear();
|
|
3593
4171
|
let added = 0;
|
|
@@ -3613,7 +4191,7 @@ class SDK {
|
|
|
3613
4191
|
return !!this.keepkeyEndpoint && this.keepkeyEndpoint.isAvailable;
|
|
3614
4192
|
};
|
|
3615
4193
|
this.getUnifiedPortfolio = async function() {
|
|
3616
|
-
const
|
|
4194
|
+
const tag6 = `${TAG9} | getUnifiedPortfolio | `;
|
|
3617
4195
|
try {
|
|
3618
4196
|
const startTime = performance.now();
|
|
3619
4197
|
try {
|
|
@@ -3624,17 +4202,17 @@ class SDK {
|
|
|
3624
4202
|
signal: AbortSignal.timeout(2000)
|
|
3625
4203
|
});
|
|
3626
4204
|
if (!portfolioResponse.ok) {
|
|
3627
|
-
console.warn(
|
|
4205
|
+
console.warn(tag6, "Portfolio endpoint returned", portfolioResponse.status);
|
|
3628
4206
|
return null;
|
|
3629
4207
|
}
|
|
3630
4208
|
const portfolioData = await portfolioResponse.json();
|
|
3631
4209
|
const loadTime = performance.now() - startTime;
|
|
3632
4210
|
if (!portfolioData.success) {
|
|
3633
|
-
console.warn(
|
|
4211
|
+
console.warn(tag6, "Portfolio API returned success=false");
|
|
3634
4212
|
return null;
|
|
3635
4213
|
}
|
|
3636
4214
|
if (portfolioData.totalValueUsd === 0 || !portfolioData.totalValueUsd) {
|
|
3637
|
-
console.warn(
|
|
4215
|
+
console.warn(tag6, "Portfolio value is $0.00 - may need device connection or sync");
|
|
3638
4216
|
return null;
|
|
3639
4217
|
}
|
|
3640
4218
|
let allBalances = [];
|
|
@@ -3721,14 +4299,14 @@ class SDK {
|
|
|
3721
4299
|
};
|
|
3722
4300
|
} catch (fetchError) {
|
|
3723
4301
|
if (fetchError.name === "AbortError") {
|
|
3724
|
-
console.log(
|
|
4302
|
+
console.log(tag6, "Unified portfolio request timed out (this is normal if vault not running)");
|
|
3725
4303
|
} else {
|
|
3726
|
-
console.log(
|
|
4304
|
+
console.log(tag6, "Failed to fetch unified portfolio:", fetchError.message);
|
|
3727
4305
|
}
|
|
3728
4306
|
return null;
|
|
3729
4307
|
}
|
|
3730
4308
|
} catch (e) {
|
|
3731
|
-
console.error(
|
|
4309
|
+
console.error(tag6, "Error:", e);
|
|
3732
4310
|
return null;
|
|
3733
4311
|
}
|
|
3734
4312
|
};
|
|
@@ -3739,7 +4317,7 @@ class SDK {
|
|
|
3739
4317
|
return this.syncState.isSynced;
|
|
3740
4318
|
};
|
|
3741
4319
|
this.init = async function(walletsVerbose, setup) {
|
|
3742
|
-
const
|
|
4320
|
+
const tag6 = `${TAG9} | init | `;
|
|
3743
4321
|
try {
|
|
3744
4322
|
if (!this.username)
|
|
3745
4323
|
throw Error("username required!");
|
|
@@ -3804,11 +4382,11 @@ class SDK {
|
|
|
3804
4382
|
this.events.emit("message", request);
|
|
3805
4383
|
});
|
|
3806
4384
|
clientEvents.events.on("balance:update", (data) => {
|
|
3807
|
-
const
|
|
4385
|
+
const tag7 = TAG9 + " | balance:update | ";
|
|
3808
4386
|
try {
|
|
3809
4387
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
3810
4388
|
const balance = payload.balance;
|
|
3811
|
-
console.log(
|
|
4389
|
+
console.log(tag7, "Received balance update:", balance.caip);
|
|
3812
4390
|
const exists = this.balances.find((b) => b.caip === balance.caip && b.pubkey === balance.pubkey);
|
|
3813
4391
|
if (!exists) {
|
|
3814
4392
|
this.balances.push(balance);
|
|
@@ -3818,27 +4396,27 @@ class SDK {
|
|
|
3818
4396
|
}
|
|
3819
4397
|
this.events.emit("BALANCE_UPDATE", balance);
|
|
3820
4398
|
} catch (e) {
|
|
3821
|
-
console.error(
|
|
4399
|
+
console.error(tag7, "Error processing balance update:", e);
|
|
3822
4400
|
}
|
|
3823
4401
|
});
|
|
3824
4402
|
clientEvents.events.on("sync:progress", (data) => {
|
|
3825
|
-
const
|
|
4403
|
+
const tag7 = TAG9 + " | sync:progress | ";
|
|
3826
4404
|
try {
|
|
3827
4405
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
3828
|
-
console.log(
|
|
4406
|
+
console.log(tag7, `Sync progress: ${payload.percentage}%`);
|
|
3829
4407
|
this.syncState.syncProgress = payload.percentage;
|
|
3830
4408
|
this.syncState.syncedChains = payload.completed;
|
|
3831
4409
|
this.syncState.totalChains = payload.total;
|
|
3832
4410
|
this.events.emit("SYNC_PROGRESS", this.syncState);
|
|
3833
4411
|
} catch (e) {
|
|
3834
|
-
console.error(
|
|
4412
|
+
console.error(tag7, "Error processing sync progress:", e);
|
|
3835
4413
|
}
|
|
3836
4414
|
});
|
|
3837
4415
|
clientEvents.events.on("sync:complete", (data) => {
|
|
3838
|
-
const
|
|
4416
|
+
const tag7 = TAG9 + " | sync:complete | ";
|
|
3839
4417
|
try {
|
|
3840
4418
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
3841
|
-
console.log(
|
|
4419
|
+
console.log(tag7, `Sync complete: ${payload.balances} balances in ${payload.duration}ms`);
|
|
3842
4420
|
this.syncState.isSynced = true;
|
|
3843
4421
|
this.syncState.isInitialSync = false;
|
|
3844
4422
|
this.syncState.syncProgress = 100;
|
|
@@ -3847,7 +4425,7 @@ class SDK {
|
|
|
3847
4425
|
this.events.emit("SYNC_COMPLETE", this.syncState);
|
|
3848
4426
|
this.events.emit("SYNC_STATE_CHANGED", this.syncState);
|
|
3849
4427
|
} catch (e) {
|
|
3850
|
-
console.error(
|
|
4428
|
+
console.error(tag7, "Error processing sync complete:", e);
|
|
3851
4429
|
}
|
|
3852
4430
|
});
|
|
3853
4431
|
this.events.emit("SET_STATUS", "init");
|
|
@@ -3879,18 +4457,27 @@ class SDK {
|
|
|
3879
4457
|
}
|
|
3880
4458
|
return this.pioneer;
|
|
3881
4459
|
} catch (e) {
|
|
3882
|
-
console.error(
|
|
4460
|
+
console.error(tag6, "e: ", e);
|
|
3883
4461
|
throw e;
|
|
3884
4462
|
}
|
|
3885
4463
|
};
|
|
3886
4464
|
this.buildDashboardFromBalances = function() {
|
|
3887
4465
|
return buildDashboardFromBalances(this.balances, this.blockchains, this.assetsMap);
|
|
3888
4466
|
};
|
|
4467
|
+
this.inferTypeFromCaip = function(caip) {
|
|
4468
|
+
if (caip.includes("/slip44:"))
|
|
4469
|
+
return "native";
|
|
4470
|
+
if (caip.includes("/erc20:") || caip.includes("/bep20:") || caip.includes("/spl:"))
|
|
4471
|
+
return "token";
|
|
4472
|
+
if (caip.includes("/denom:"))
|
|
4473
|
+
return "native";
|
|
4474
|
+
return "unknown";
|
|
4475
|
+
};
|
|
3889
4476
|
this.syncMarket = async function() {
|
|
3890
4477
|
return syncMarket(this.balances, this.pioneer);
|
|
3891
4478
|
};
|
|
3892
4479
|
this.sync = async function() {
|
|
3893
|
-
const
|
|
4480
|
+
const tag6 = `${TAG9} | sync | `;
|
|
3894
4481
|
try {
|
|
3895
4482
|
const matchesNetwork = (item, networkId) => {
|
|
3896
4483
|
if (!item.networks || !Array.isArray(item.networks))
|
|
@@ -3934,9 +4521,9 @@ class SDK {
|
|
|
3934
4521
|
}
|
|
3935
4522
|
}
|
|
3936
4523
|
await this.getBalances();
|
|
3937
|
-
console.log(
|
|
4524
|
+
console.log(tag6, "Loading charts (tokens + portfolio)...");
|
|
3938
4525
|
await this.getCharts();
|
|
3939
|
-
console.log(
|
|
4526
|
+
console.log(tag6, `Charts loaded. Total balances: ${this.balances.length}`);
|
|
3940
4527
|
await this.syncMarket();
|
|
3941
4528
|
const dashboardData = {
|
|
3942
4529
|
networks: [],
|
|
@@ -4025,7 +4612,7 @@ class SDK {
|
|
|
4025
4612
|
this.events.emit("SYNC_COMPLETE", this.syncState);
|
|
4026
4613
|
return true;
|
|
4027
4614
|
} catch (e) {
|
|
4028
|
-
console.error(
|
|
4615
|
+
console.error(tag6, "Error in sync:", e);
|
|
4029
4616
|
throw e;
|
|
4030
4617
|
}
|
|
4031
4618
|
};
|
|
@@ -4039,7 +4626,7 @@ class SDK {
|
|
|
4039
4626
|
}
|
|
4040
4627
|
};
|
|
4041
4628
|
this.buildTx = async function(sendPayload) {
|
|
4042
|
-
let
|
|
4629
|
+
let tag6 = TAG9 + " | buildTx | ";
|
|
4043
4630
|
try {
|
|
4044
4631
|
const transactionDependencies = {
|
|
4045
4632
|
context: this.context,
|
|
@@ -4053,7 +4640,7 @@ class SDK {
|
|
|
4053
4640
|
};
|
|
4054
4641
|
let txManager = new TransactionManager(transactionDependencies, this.events);
|
|
4055
4642
|
let unsignedTx = await txManager.transfer(sendPayload);
|
|
4056
|
-
console.log(
|
|
4643
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4057
4644
|
return unsignedTx;
|
|
4058
4645
|
} catch (e) {
|
|
4059
4646
|
console.error(e);
|
|
@@ -4061,14 +4648,14 @@ class SDK {
|
|
|
4061
4648
|
}
|
|
4062
4649
|
};
|
|
4063
4650
|
this.buildDelegateTx = async function(caip, params) {
|
|
4064
|
-
let
|
|
4651
|
+
let tag6 = TAG9 + " | buildDelegateTx | ";
|
|
4065
4652
|
try {
|
|
4066
4653
|
const delegateParams = {
|
|
4067
4654
|
...params,
|
|
4068
4655
|
type: "delegate"
|
|
4069
4656
|
};
|
|
4070
4657
|
let unsignedTx = await createUnsignedStakingTx(caip, delegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4071
|
-
console.log(
|
|
4658
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4072
4659
|
return unsignedTx;
|
|
4073
4660
|
} catch (e) {
|
|
4074
4661
|
console.error(e);
|
|
@@ -4076,14 +4663,14 @@ class SDK {
|
|
|
4076
4663
|
}
|
|
4077
4664
|
};
|
|
4078
4665
|
this.buildUndelegateTx = async function(caip, params) {
|
|
4079
|
-
let
|
|
4666
|
+
let tag6 = TAG9 + " | buildUndelegateTx | ";
|
|
4080
4667
|
try {
|
|
4081
4668
|
const undelegateParams = {
|
|
4082
4669
|
...params,
|
|
4083
4670
|
type: "undelegate"
|
|
4084
4671
|
};
|
|
4085
4672
|
let unsignedTx = await createUnsignedStakingTx(caip, undelegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4086
|
-
console.log(
|
|
4673
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4087
4674
|
return unsignedTx;
|
|
4088
4675
|
} catch (e) {
|
|
4089
4676
|
console.error(e);
|
|
@@ -4091,14 +4678,14 @@ class SDK {
|
|
|
4091
4678
|
}
|
|
4092
4679
|
};
|
|
4093
4680
|
this.buildClaimRewardsTx = async function(caip, params) {
|
|
4094
|
-
let
|
|
4681
|
+
let tag6 = TAG9 + " | buildClaimRewardsTx | ";
|
|
4095
4682
|
try {
|
|
4096
4683
|
const claimParams = {
|
|
4097
4684
|
...params,
|
|
4098
4685
|
type: "claim_rewards"
|
|
4099
4686
|
};
|
|
4100
4687
|
let unsignedTx = await createUnsignedStakingTx(caip, claimParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4101
|
-
console.log(
|
|
4688
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4102
4689
|
return unsignedTx;
|
|
4103
4690
|
} catch (e) {
|
|
4104
4691
|
console.error(e);
|
|
@@ -4106,7 +4693,7 @@ class SDK {
|
|
|
4106
4693
|
}
|
|
4107
4694
|
};
|
|
4108
4695
|
this.buildClaimAllRewardsTx = async function(caip, params) {
|
|
4109
|
-
let
|
|
4696
|
+
let tag6 = TAG9 + " | buildClaimAllRewardsTx | ";
|
|
4110
4697
|
try {
|
|
4111
4698
|
const claimAllParams = {
|
|
4112
4699
|
...params,
|
|
@@ -4120,7 +4707,7 @@ class SDK {
|
|
|
4120
4707
|
}
|
|
4121
4708
|
};
|
|
4122
4709
|
this.signTx = async function(caip, unsignedTx) {
|
|
4123
|
-
let
|
|
4710
|
+
let tag6 = TAG9 + " | signTx | ";
|
|
4124
4711
|
try {
|
|
4125
4712
|
const transactionDependencies = {
|
|
4126
4713
|
context: this.context,
|
|
@@ -4141,7 +4728,7 @@ class SDK {
|
|
|
4141
4728
|
}
|
|
4142
4729
|
};
|
|
4143
4730
|
this.broadcastTx = async function(caip, signedTx) {
|
|
4144
|
-
let
|
|
4731
|
+
let tag6 = TAG9 + " | broadcastTx | ";
|
|
4145
4732
|
try {
|
|
4146
4733
|
const transactionDependencies = {
|
|
4147
4734
|
context: this.context,
|
|
@@ -4165,7 +4752,7 @@ class SDK {
|
|
|
4165
4752
|
}
|
|
4166
4753
|
};
|
|
4167
4754
|
this.swap = async function(swapPayload) {
|
|
4168
|
-
let
|
|
4755
|
+
let tag6 = `${TAG9} | swap | `;
|
|
4169
4756
|
try {
|
|
4170
4757
|
if (!swapPayload)
|
|
4171
4758
|
throw Error("swapPayload required!");
|
|
@@ -4214,15 +4801,15 @@ class SDK {
|
|
|
4214
4801
|
throw new Error(`Cannot use max amount: no balance found for ${swapPayload.caipIn}`);
|
|
4215
4802
|
}
|
|
4216
4803
|
let totalBalance = 0;
|
|
4217
|
-
console.log(
|
|
4804
|
+
console.log(tag6, `Found ${inputBalances.length} balance entries for ${swapPayload.caipIn}`);
|
|
4218
4805
|
for (const balanceEntry of inputBalances) {
|
|
4219
4806
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
4220
4807
|
totalBalance += balance;
|
|
4221
|
-
console.log(
|
|
4808
|
+
console.log(tag6, ` - ${balanceEntry.pubkey || balanceEntry.identifier}: ${balance}`);
|
|
4222
4809
|
}
|
|
4223
4810
|
this.assetContext.balance = totalBalance.toString();
|
|
4224
4811
|
this.assetContext.valueUsd = (totalBalance * parseFloat(this.assetContext.priceUsd || "0")).toFixed(2);
|
|
4225
|
-
console.log(
|
|
4812
|
+
console.log(tag6, `Updated assetContext balance to aggregated total: ${totalBalance}`);
|
|
4226
4813
|
const feeReserves = {
|
|
4227
4814
|
"bip122:000000000019d6689c085ae165831e93/slip44:0": 0.00005,
|
|
4228
4815
|
"eip155:1/slip44:60": 0.001,
|
|
@@ -4233,7 +4820,7 @@ class SDK {
|
|
|
4233
4820
|
};
|
|
4234
4821
|
const reserve = feeReserves[swapPayload.caipIn] || 0.0001;
|
|
4235
4822
|
inputAmount = Math.max(0, totalBalance - reserve);
|
|
4236
|
-
console.log(
|
|
4823
|
+
console.log(tag6, `Using max amount for swap: ${inputAmount} (total balance: ${totalBalance}, reserve: ${reserve})`);
|
|
4237
4824
|
} else {
|
|
4238
4825
|
inputAmount = typeof swapPayload.amount === "string" ? parseFloat(swapPayload.amount) : swapPayload.amount;
|
|
4239
4826
|
if (isNaN(inputAmount) || inputAmount <= 0) {
|
|
@@ -4253,7 +4840,7 @@ class SDK {
|
|
|
4253
4840
|
result = await this.pioneer.Quote(quote);
|
|
4254
4841
|
result = result.data;
|
|
4255
4842
|
} catch (e) {
|
|
4256
|
-
console.error(
|
|
4843
|
+
console.error(tag6, "Failed to get quote: ", e);
|
|
4257
4844
|
throw Error("Quote API failed for path: " + quote.sellAsset + " -> " + quote.buyAsset + ". Error: " + e);
|
|
4258
4845
|
}
|
|
4259
4846
|
if (!result || result.length === 0)
|
|
@@ -4280,16 +4867,16 @@ class SDK {
|
|
|
4280
4867
|
if (tx.type === "deposit") {
|
|
4281
4868
|
unsignedTx = await createUnsignedTendermintTx(caip, tx.type, tx.txParams.amount, tx.txParams.memo, this.pubkeys, this.pioneer, this.pubkeyContext, false, undefined);
|
|
4282
4869
|
} else if (tx.type === "EVM" || tx.type === "evm") {
|
|
4283
|
-
console.log(
|
|
4870
|
+
console.log(tag6, "Building EVM swap transaction with createUnsignedEvmTx");
|
|
4284
4871
|
unsignedTx = await createUnsignedEvmTx(caip, tx.txParams.recipientAddress, parseFloat(tx.txParams.amount), tx.txParams.memo, this.pubkeys, this.pioneer, this.pubkeyContext, false);
|
|
4285
|
-
console.log(
|
|
4872
|
+
console.log(tag6, "✅ Built complete EVM transaction:", {
|
|
4286
4873
|
to: unsignedTx.to,
|
|
4287
4874
|
from: this.pubkeyContext?.address,
|
|
4288
4875
|
value: unsignedTx.value,
|
|
4289
4876
|
chainId: unsignedTx.chainId,
|
|
4290
4877
|
hasData: !!unsignedTx.data
|
|
4291
4878
|
});
|
|
4292
|
-
console.log(
|
|
4879
|
+
console.log(tag6, "\uD83D\uDEE1️ Running Tenderly simulation for EVM swap...");
|
|
4293
4880
|
try {
|
|
4294
4881
|
const insightResult = await this.pioneer.Insight({
|
|
4295
4882
|
tx: unsignedTx,
|
|
@@ -4297,15 +4884,15 @@ class SDK {
|
|
|
4297
4884
|
isThorchainSwap: tx.txParams.isThorchainSwap || false
|
|
4298
4885
|
});
|
|
4299
4886
|
const insight = insightResult.body;
|
|
4300
|
-
console.log(
|
|
4887
|
+
console.log(tag6, "Simulation result:", insight?.simulation);
|
|
4301
4888
|
if (!insight || !insight.simulation) {
|
|
4302
|
-
console.warn(
|
|
4889
|
+
console.warn(tag6, "⚠️ WARNING: Tenderly simulation unavailable - proceeding without validation");
|
|
4303
4890
|
} else if (!insight.simulation.success) {
|
|
4304
|
-
console.warn(
|
|
4305
|
-
console.warn(
|
|
4891
|
+
console.warn(tag6, `⚠️ WARNING: Swap simulation FAILED - ${insight.simulation.error || "Transaction may revert"}`);
|
|
4892
|
+
console.warn(tag6, "⚠️ Proceeding anyway - USE CAUTION");
|
|
4306
4893
|
} else {
|
|
4307
4894
|
if (tx.txParams.isThorchainSwap) {
|
|
4308
|
-
console.log(
|
|
4895
|
+
console.log(tag6, "\uD83D\uDD0D Verifying THORChain swap parameters...");
|
|
4309
4896
|
const method = insight.simulation.method;
|
|
4310
4897
|
if (!method || !method.toLowerCase().includes("deposit")) {
|
|
4311
4898
|
throw new Error(`❌ CRITICAL: Invalid THORChain swap method: ${method} - expected depositWithExpiry`);
|
|
@@ -4316,20 +4903,20 @@ class SDK {
|
|
|
4316
4903
|
if (routerAddress.toLowerCase() === vaultAddress.toLowerCase()) {
|
|
4317
4904
|
throw new Error(`❌ CRITICAL: Sending directly to vault ${vaultAddress} instead of router!`);
|
|
4318
4905
|
}
|
|
4319
|
-
console.log(
|
|
4320
|
-
console.log(
|
|
4906
|
+
console.log(tag6, `✅ Router: ${routerAddress}`);
|
|
4907
|
+
console.log(tag6, `✅ Vault: ${vaultAddress}`);
|
|
4321
4908
|
}
|
|
4322
4909
|
if (insight.simulation.addresses && insight.simulation.addresses.length > 0) {
|
|
4323
|
-
console.log(
|
|
4910
|
+
console.log(tag6, `✅ Addresses involved: ${insight.simulation.addresses.length}`);
|
|
4324
4911
|
} else {
|
|
4325
|
-
console.log(
|
|
4912
|
+
console.log(tag6, "⚠️ WARNING: No addresses detected in simulation");
|
|
4326
4913
|
}
|
|
4327
4914
|
}
|
|
4328
|
-
console.log(
|
|
4329
|
-
console.log(
|
|
4915
|
+
console.log(tag6, `✅ Simulation PASSED - Gas used: ${insight.simulation.gasUsed}`);
|
|
4916
|
+
console.log(tag6, `✅ Method: ${insight.simulation.method}`);
|
|
4330
4917
|
}
|
|
4331
4918
|
} catch (e) {
|
|
4332
|
-
console.error(
|
|
4919
|
+
console.error(tag6, "❌ Simulation validation failed:", e.message);
|
|
4333
4920
|
throw new Error(`Swap blocked by simulation failure: ${e.message}`);
|
|
4334
4921
|
}
|
|
4335
4922
|
} else {
|
|
@@ -4349,12 +4936,12 @@ class SDK {
|
|
|
4349
4936
|
return unsignedTx;
|
|
4350
4937
|
}
|
|
4351
4938
|
} catch (e) {
|
|
4352
|
-
console.error(
|
|
4939
|
+
console.error(tag6, "Error: ", e);
|
|
4353
4940
|
throw e;
|
|
4354
4941
|
}
|
|
4355
4942
|
};
|
|
4356
4943
|
this.transfer = async function(sendPayload) {
|
|
4357
|
-
let
|
|
4944
|
+
let tag6 = `${TAG9} | transfer | `;
|
|
4358
4945
|
try {
|
|
4359
4946
|
if (!sendPayload)
|
|
4360
4947
|
throw Error("sendPayload required!");
|
|
@@ -4388,15 +4975,15 @@ class SDK {
|
|
|
4388
4975
|
return { txid, events: this.events };
|
|
4389
4976
|
} catch (error) {
|
|
4390
4977
|
if (error instanceof Error) {
|
|
4391
|
-
console.error(
|
|
4978
|
+
console.error(tag6, "An error occurred during the transfer process:", error.message);
|
|
4392
4979
|
} else {
|
|
4393
|
-
console.error(
|
|
4980
|
+
console.error(tag6, "An unknown error occurred during the transfer process");
|
|
4394
4981
|
}
|
|
4395
4982
|
throw error;
|
|
4396
4983
|
}
|
|
4397
4984
|
};
|
|
4398
4985
|
this.followTransaction = async function(caip, txid) {
|
|
4399
|
-
let
|
|
4986
|
+
let tag6 = " | followTransaction | ";
|
|
4400
4987
|
try {
|
|
4401
4988
|
const finalConfirmationBlocksByCaip = {
|
|
4402
4989
|
dogecoin: 3,
|
|
@@ -4426,7 +5013,7 @@ class SDK {
|
|
|
4426
5013
|
}
|
|
4427
5014
|
}
|
|
4428
5015
|
} catch (error) {
|
|
4429
|
-
console.error(
|
|
5016
|
+
console.error(tag6, "Error:", error);
|
|
4430
5017
|
}
|
|
4431
5018
|
if (!isConfirmed) {
|
|
4432
5019
|
await new Promise((resolve) => setTimeout(resolve, 8000));
|
|
@@ -4444,18 +5031,18 @@ class SDK {
|
|
|
4444
5031
|
requiredConfirmations
|
|
4445
5032
|
};
|
|
4446
5033
|
} catch (error) {
|
|
4447
|
-
console.error(
|
|
5034
|
+
console.error(tag6, "Error:", error);
|
|
4448
5035
|
throw new Error("Failed to follow transaction");
|
|
4449
5036
|
}
|
|
4450
5037
|
};
|
|
4451
5038
|
this.setBlockchains = async function(blockchains) {
|
|
4452
|
-
const
|
|
5039
|
+
const tag6 = `${TAG9} | setBlockchains | `;
|
|
4453
5040
|
try {
|
|
4454
5041
|
if (!blockchains)
|
|
4455
5042
|
throw Error("blockchains required!");
|
|
4456
5043
|
const uniqueBlockchains = [...new Set(blockchains)];
|
|
4457
5044
|
if (blockchains.length !== uniqueBlockchains.length) {
|
|
4458
|
-
console.warn(
|
|
5045
|
+
console.warn(tag6, `Removed ${blockchains.length - uniqueBlockchains.length} duplicate blockchains`);
|
|
4459
5046
|
}
|
|
4460
5047
|
this.blockchains = uniqueBlockchains;
|
|
4461
5048
|
this.events.emit("SET_BLOCKCHAINS", this.blockchains);
|
|
@@ -4465,12 +5052,12 @@ class SDK {
|
|
|
4465
5052
|
}
|
|
4466
5053
|
};
|
|
4467
5054
|
this.addAsset = async function(caip, data) {
|
|
4468
|
-
let
|
|
5055
|
+
let tag6 = TAG9 + " | addAsset | ";
|
|
4469
5056
|
try {
|
|
4470
5057
|
let success = false;
|
|
4471
5058
|
if (!caip)
|
|
4472
5059
|
throw new Error("caip required!");
|
|
4473
|
-
let dataLocal =
|
|
5060
|
+
let dataLocal = import_pioneer_discovery2.assetData[caip];
|
|
4474
5061
|
if (!dataLocal) {
|
|
4475
5062
|
if (!data.networkId)
|
|
4476
5063
|
throw new Error("networkId required! can not build asset");
|
|
@@ -4503,22 +5090,22 @@ class SDK {
|
|
|
4503
5090
|
}
|
|
4504
5091
|
};
|
|
4505
5092
|
this.clearWalletState = async function() {
|
|
4506
|
-
const
|
|
5093
|
+
const tag6 = `${TAG9} | clearWalletState | `;
|
|
4507
5094
|
try {
|
|
4508
5095
|
this.context = null;
|
|
4509
5096
|
this.paths = [];
|
|
4510
5097
|
this.blockchains = [];
|
|
4511
5098
|
this.pubkeys = [];
|
|
4512
5099
|
this.pubkeySet.clear();
|
|
4513
|
-
console.log(
|
|
5100
|
+
console.log(tag6, "Cleared wallet state including pubkeys and tracking set");
|
|
4514
5101
|
return true;
|
|
4515
5102
|
} catch (e) {
|
|
4516
|
-
console.error(
|
|
5103
|
+
console.error(tag6, "e: ", e);
|
|
4517
5104
|
throw e;
|
|
4518
5105
|
}
|
|
4519
5106
|
};
|
|
4520
5107
|
this.addPath = async function(path) {
|
|
4521
|
-
const
|
|
5108
|
+
const tag6 = `${TAG9} | addPath | `;
|
|
4522
5109
|
try {
|
|
4523
5110
|
this.paths.push(path);
|
|
4524
5111
|
const pubkey = await getPubkey(path.networks[0], path, this.keepKeySdk, this.context);
|
|
@@ -4527,14 +5114,14 @@ class SDK {
|
|
|
4527
5114
|
this.buildDashboardFromBalances();
|
|
4528
5115
|
return { success: true, pubkey };
|
|
4529
5116
|
} catch (e) {
|
|
4530
|
-
console.error(
|
|
5117
|
+
console.error(tag6, "Failed:", e);
|
|
4531
5118
|
throw e;
|
|
4532
5119
|
}
|
|
4533
5120
|
};
|
|
4534
5121
|
this.addPaths = async function(paths) {
|
|
4535
|
-
const
|
|
5122
|
+
const tag6 = `${TAG9} | addPaths | `;
|
|
4536
5123
|
try {
|
|
4537
|
-
console.log(
|
|
5124
|
+
console.log(tag6, `Adding ${paths.length} paths in batch mode...`);
|
|
4538
5125
|
this.paths.push(...paths);
|
|
4539
5126
|
const newPubkeys = [];
|
|
4540
5127
|
for (const path of paths) {
|
|
@@ -4543,10 +5130,10 @@ class SDK {
|
|
|
4543
5130
|
this.addPubkey(pubkey);
|
|
4544
5131
|
newPubkeys.push(pubkey);
|
|
4545
5132
|
} catch (error) {
|
|
4546
|
-
console.warn(
|
|
5133
|
+
console.warn(tag6, `Failed to get pubkey for path ${path.note}:`, error.message);
|
|
4547
5134
|
}
|
|
4548
5135
|
}
|
|
4549
|
-
console.log(
|
|
5136
|
+
console.log(tag6, `Successfully added ${newPubkeys.length} pubkeys`);
|
|
4550
5137
|
const networkSet = new Set;
|
|
4551
5138
|
for (const path of paths) {
|
|
4552
5139
|
if (path.networks && Array.isArray(path.networks)) {
|
|
@@ -4554,13 +5141,13 @@ class SDK {
|
|
|
4554
5141
|
}
|
|
4555
5142
|
}
|
|
4556
5143
|
const uniqueNetworks = [...networkSet];
|
|
4557
|
-
console.log(
|
|
5144
|
+
console.log(tag6, `Fetching balances for ${uniqueNetworks.length} unique networks in single API call...`);
|
|
4558
5145
|
await this.getBalancesForNetworks(uniqueNetworks);
|
|
4559
5146
|
this.buildDashboardFromBalances();
|
|
4560
|
-
console.log(
|
|
5147
|
+
console.log(tag6, `Batch add complete: ${paths.length} paths, ${newPubkeys.length} pubkeys, ${this.balances?.length || 0} balances`);
|
|
4561
5148
|
return { success: true, pubkeys: newPubkeys };
|
|
4562
5149
|
} catch (e) {
|
|
4563
|
-
console.error(
|
|
5150
|
+
console.error(tag6, "Failed:", e);
|
|
4564
5151
|
throw e;
|
|
4565
5152
|
}
|
|
4566
5153
|
};
|
|
@@ -4568,12 +5155,12 @@ class SDK {
|
|
|
4568
5155
|
return this.getGasAssets();
|
|
4569
5156
|
};
|
|
4570
5157
|
this.getGasAssets = async function() {
|
|
4571
|
-
const
|
|
5158
|
+
const tag6 = `${TAG9} | getGasAssets | `;
|
|
4572
5159
|
try {
|
|
4573
5160
|
for (let i = 0;i < this.blockchains.length; i++) {
|
|
4574
5161
|
let networkId = this.blockchains[i];
|
|
4575
5162
|
let caip = import_pioneer_caip8.networkIdToCaip(networkId);
|
|
4576
|
-
let asset = await
|
|
5163
|
+
let asset = await import_pioneer_discovery2.assetData[caip.toLowerCase()];
|
|
4577
5164
|
if (asset) {
|
|
4578
5165
|
asset.caip = caip.toLowerCase();
|
|
4579
5166
|
asset.networkId = networkId;
|
|
@@ -4602,7 +5189,7 @@ class SDK {
|
|
|
4602
5189
|
denom: "maya"
|
|
4603
5190
|
};
|
|
4604
5191
|
this.assetsMap.set(mayaTokenCaip, mayaToken);
|
|
4605
|
-
console.log(
|
|
5192
|
+
console.log(tag6, "Added MAYA token to assetsMap");
|
|
4606
5193
|
}
|
|
4607
5194
|
return this.assetsMap;
|
|
4608
5195
|
} catch (e) {
|
|
@@ -4611,7 +5198,7 @@ class SDK {
|
|
|
4611
5198
|
}
|
|
4612
5199
|
};
|
|
4613
5200
|
this.getPubkeys = async function() {
|
|
4614
|
-
const
|
|
5201
|
+
const tag6 = `${TAG9} | getPubkeys | `;
|
|
4615
5202
|
try {
|
|
4616
5203
|
if (this.paths.length === 0)
|
|
4617
5204
|
throw new Error("No paths found!");
|
|
@@ -4625,43 +5212,46 @@ class SDK {
|
|
|
4625
5212
|
}
|
|
4626
5213
|
const pubkeysWithoutNetworks = this.pubkeys.filter((pk) => !pk.networks || !Array.isArray(pk.networks) || pk.networks.length === 0);
|
|
4627
5214
|
if (pubkeysWithoutNetworks.length > 0) {
|
|
4628
|
-
console.error(
|
|
4629
|
-
console.error(
|
|
5215
|
+
console.error(tag6, "ERROR: Some pubkeys missing networks field!");
|
|
5216
|
+
console.error(tag6, "Affected pubkeys:", pubkeysWithoutNetworks.length);
|
|
4630
5217
|
pubkeysWithoutNetworks.forEach((pk) => {
|
|
4631
|
-
console.error(
|
|
5218
|
+
console.error(tag6, ` - ${pk.note || pk.pubkey}: networks=${pk.networks}`);
|
|
4632
5219
|
});
|
|
4633
5220
|
for (const pubkey of pubkeysWithoutNetworks) {
|
|
4634
5221
|
const matchingPath = this.paths.find((p) => JSON.stringify(p.addressNList) === JSON.stringify(pubkey.addressNList));
|
|
4635
5222
|
if (matchingPath && matchingPath.networks) {
|
|
4636
|
-
console.warn(
|
|
5223
|
+
console.warn(tag6, ` ⚠️ Auto-fixing: Adding networks from path ${matchingPath.note}`);
|
|
4637
5224
|
pubkey.networks = matchingPath.networks;
|
|
4638
5225
|
}
|
|
4639
5226
|
}
|
|
4640
5227
|
const stillMissing = this.pubkeys.filter((pk) => !pk.networks || !Array.isArray(pk.networks) || pk.networks.length === 0);
|
|
4641
5228
|
if (stillMissing.length > 0) {
|
|
4642
|
-
console.error(
|
|
5229
|
+
console.error(tag6, `❌ CRITICAL: ${stillMissing.length} pubkeys still missing networks after auto-fix!`);
|
|
4643
5230
|
stillMissing.forEach((pk) => {
|
|
4644
|
-
console.error(
|
|
5231
|
+
console.error(tag6, ` - ${pk.note || pk.pubkey}`);
|
|
4645
5232
|
});
|
|
4646
5233
|
} else {
|
|
4647
|
-
console.log(
|
|
5234
|
+
console.log(tag6, `✅ Auto-fix successful: All pubkeys now have networks field`);
|
|
4648
5235
|
}
|
|
4649
5236
|
}
|
|
4650
5237
|
this.events.emit("SET_PUBKEYS", this.pubkeys);
|
|
4651
5238
|
return pubkeys;
|
|
4652
5239
|
} catch (error) {
|
|
4653
5240
|
console.error("Error in getPubkeys:", error);
|
|
4654
|
-
console.error(
|
|
5241
|
+
console.error(tag6, "Error in getPubkeys:", error);
|
|
4655
5242
|
throw error;
|
|
4656
5243
|
}
|
|
4657
5244
|
};
|
|
4658
|
-
this.getBalancesForNetworks = async function(networkIds) {
|
|
4659
|
-
const
|
|
5245
|
+
this.getBalancesForNetworks = async function(networkIds, forceRefresh) {
|
|
5246
|
+
const tag6 = `${TAG9} | getBalancesForNetworks | `;
|
|
4660
5247
|
try {
|
|
4661
5248
|
if (!this.pioneer) {
|
|
4662
|
-
console.error(
|
|
5249
|
+
console.error(tag6, "ERROR: Pioneer client not initialized! this.pioneer is:", this.pioneer);
|
|
4663
5250
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
4664
5251
|
}
|
|
5252
|
+
if (forceRefresh) {
|
|
5253
|
+
console.log(tag6, "\uD83D\uDD04 Force refresh requested - bypassing balance cache");
|
|
5254
|
+
}
|
|
4665
5255
|
console.log("\uD83D\uDD0D [DIAGNOSTIC] Input networks:", networkIds);
|
|
4666
5256
|
console.log("\uD83D\uDD0D [DIAGNOSTIC] Total pubkeys:", this.pubkeys.length);
|
|
4667
5257
|
const pubkeysWithNetworks = this.pubkeys.filter((p) => p.networks && Array.isArray(p.networks));
|
|
@@ -4687,20 +5277,20 @@ class SDK {
|
|
|
4687
5277
|
return network === adjustedNetworkId;
|
|
4688
5278
|
}));
|
|
4689
5279
|
if (pubkeys.length === 0) {
|
|
4690
|
-
console.warn(
|
|
4691
|
-
console.warn(
|
|
5280
|
+
console.warn(tag6, `⚠️ No pubkeys found for ${networkId} with networks field`);
|
|
5281
|
+
console.warn(tag6, "Attempting fallback: finding pubkeys by path matching");
|
|
4692
5282
|
const pathsForNetwork = this.paths.filter((p) => p.networks?.includes(networkId) || networkId.startsWith("eip155:") && p.networks?.includes("eip155:*"));
|
|
4693
5283
|
for (const path of pathsForNetwork) {
|
|
4694
5284
|
const matchingPubkey = this.pubkeys.find((pk) => JSON.stringify(pk.addressNList) === JSON.stringify(path.addressNList));
|
|
4695
5285
|
if (matchingPubkey) {
|
|
4696
|
-
console.warn(
|
|
5286
|
+
console.warn(tag6, ` ✓ Found pubkey via path matching: ${matchingPubkey.note || matchingPubkey.pubkey.slice(0, 10)}`);
|
|
4697
5287
|
pubkeys.push(matchingPubkey);
|
|
4698
5288
|
}
|
|
4699
5289
|
}
|
|
4700
5290
|
if (pubkeys.length > 0) {
|
|
4701
|
-
console.warn(
|
|
5291
|
+
console.warn(tag6, ` ✅ Fallback successful: Found ${pubkeys.length} pubkeys for ${networkId}`);
|
|
4702
5292
|
} else {
|
|
4703
|
-
console.error(
|
|
5293
|
+
console.error(tag6, ` ❌ Fallback failed: No pubkeys found for ${networkId}`);
|
|
4704
5294
|
}
|
|
4705
5295
|
}
|
|
4706
5296
|
const caipNative = await import_pioneer_caip8.networkIdToCaip(networkId);
|
|
@@ -4722,7 +5312,7 @@ class SDK {
|
|
|
4722
5312
|
const apiCallStart = performance.now();
|
|
4723
5313
|
console.time("GetPortfolioBalances Response Time");
|
|
4724
5314
|
try {
|
|
4725
|
-
let marketInfo = await this.pioneer.GetPortfolioBalances({ pubkeys: assetQuery });
|
|
5315
|
+
let marketInfo = await this.pioneer.GetPortfolioBalances({ pubkeys: assetQuery }, forceRefresh ? { forceRefresh: true } : undefined);
|
|
4726
5316
|
const apiCallTime = performance.now() - apiCallStart;
|
|
4727
5317
|
console.timeEnd("GetPortfolioBalances Response Time");
|
|
4728
5318
|
console.log(`⏱️ [PERF] API call completed in ${apiCallTime.toFixed(0)}ms`);
|
|
@@ -4732,10 +5322,23 @@ class SDK {
|
|
|
4732
5322
|
console.log(`⏱️ [PERF] Starting balance enrichment...`);
|
|
4733
5323
|
for (let balance of balances) {
|
|
4734
5324
|
const assetInfo = this.assetsMap.get(balance.caip.toLowerCase()) || this.assetsMap.get(balance.caip);
|
|
4735
|
-
if (!assetInfo)
|
|
5325
|
+
if (!assetInfo) {
|
|
5326
|
+
console.warn(`⚠️ [ENRICHMENT] Asset metadata missing for ${balance.caip}, using fallback enrichment`);
|
|
5327
|
+
const inferredType = this.inferTypeFromCaip(balance.caip);
|
|
5328
|
+
Object.assign(balance, {
|
|
5329
|
+
type: balance.type || inferredType,
|
|
5330
|
+
isNative: balance.isNative ?? inferredType === "native",
|
|
5331
|
+
networkId: import_pioneer_caip8.caipToNetworkId(balance.caip),
|
|
5332
|
+
icon: "https://pioneers.dev/coins/unknown.png",
|
|
5333
|
+
identifier: `${balance.caip}:${balance.pubkey}`,
|
|
5334
|
+
updated: Date.now()
|
|
5335
|
+
});
|
|
4736
5336
|
continue;
|
|
5337
|
+
}
|
|
4737
5338
|
const color = ASSET_COLORS[balance.caip] || assetInfo.color;
|
|
4738
5339
|
Object.assign(balance, assetInfo, {
|
|
5340
|
+
type: balance.type || assetInfo.type,
|
|
5341
|
+
isNative: balance.isNative ?? assetInfo.isNative,
|
|
4739
5342
|
networkId: import_pioneer_caip8.caipToNetworkId(balance.caip),
|
|
4740
5343
|
icon: assetInfo.icon || "https://pioneers.dev/coins/etherum.png",
|
|
4741
5344
|
identifier: `${balance.caip}:${balance.pubkey}`,
|
|
@@ -4757,43 +5360,43 @@ class SDK {
|
|
|
4757
5360
|
console.log(`⏱️ [PERF] Total getBalancesForNetworks: ${(performance.now() - apiCallStart).toFixed(0)}ms`);
|
|
4758
5361
|
return this.balances;
|
|
4759
5362
|
} catch (apiError) {
|
|
4760
|
-
console.error(
|
|
5363
|
+
console.error(tag6, "GetPortfolioBalances API call failed:", apiError);
|
|
4761
5364
|
throw new Error(`GetPortfolioBalances API call failed: ${apiError?.message || "Unknown error"}`);
|
|
4762
5365
|
}
|
|
4763
5366
|
} catch (e) {
|
|
4764
|
-
console.error(
|
|
5367
|
+
console.error(tag6, "Error: ", e);
|
|
4765
5368
|
throw e;
|
|
4766
5369
|
}
|
|
4767
5370
|
};
|
|
4768
|
-
this.getBalances = async function() {
|
|
4769
|
-
const
|
|
5371
|
+
this.getBalances = async function(forceRefresh) {
|
|
5372
|
+
const tag6 = `${TAG9} | getBalances | `;
|
|
4770
5373
|
try {
|
|
4771
|
-
return await this.getBalancesForNetworks(this.blockchains);
|
|
5374
|
+
return await this.getBalancesForNetworks(this.blockchains, forceRefresh);
|
|
4772
5375
|
} catch (e) {
|
|
4773
|
-
console.error(
|
|
5376
|
+
console.error(tag6, "Error in getBalances: ", e);
|
|
4774
5377
|
throw e;
|
|
4775
5378
|
}
|
|
4776
5379
|
};
|
|
4777
5380
|
this.getBalance = async function(networkId) {
|
|
4778
|
-
const
|
|
5381
|
+
const tag6 = `${TAG9} | getBalance | `;
|
|
4779
5382
|
try {
|
|
4780
5383
|
const results = await this.getBalancesForNetworks([networkId]);
|
|
4781
5384
|
const filtered = results.filter(async (b) => b.networkId === await import_pioneer_caip8.networkIdToCaip(networkId));
|
|
4782
5385
|
return filtered;
|
|
4783
5386
|
} catch (e) {
|
|
4784
|
-
console.error(
|
|
5387
|
+
console.error(tag6, "Error: ", e);
|
|
4785
5388
|
throw e;
|
|
4786
5389
|
}
|
|
4787
5390
|
};
|
|
4788
5391
|
this.getFees = async function(networkId) {
|
|
4789
|
-
const
|
|
5392
|
+
const tag6 = `${TAG9} | getFees | `;
|
|
4790
5393
|
try {
|
|
4791
5394
|
if (!this.pioneer) {
|
|
4792
5395
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
4793
5396
|
}
|
|
4794
5397
|
return await getFees(this.pioneer, networkId);
|
|
4795
5398
|
} catch (e) {
|
|
4796
|
-
console.error(
|
|
5399
|
+
console.error(tag6, "Error getting fees: ", e);
|
|
4797
5400
|
throw e;
|
|
4798
5401
|
}
|
|
4799
5402
|
};
|
|
@@ -4801,42 +5404,12 @@ class SDK {
|
|
|
4801
5404
|
return estimateTransactionFee(feeRate, unit, networkType, txSize);
|
|
4802
5405
|
};
|
|
4803
5406
|
this.getCharts = async function() {
|
|
4804
|
-
const
|
|
5407
|
+
const tag6 = `${TAG9} | getCharts | `;
|
|
4805
5408
|
try {
|
|
4806
|
-
console.log(
|
|
4807
|
-
const
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
if (!address)
|
|
4811
|
-
continue;
|
|
4812
|
-
const supportedNetworks = pubkey.networks || [];
|
|
4813
|
-
for (const blockchain of this.blockchains) {
|
|
4814
|
-
const supportsNetwork = supportedNetworks.some((net) => net === blockchain || net.endsWith(":*") && blockchain.startsWith(net.replace(":*", ":")));
|
|
4815
|
-
if (supportsNetwork) {
|
|
4816
|
-
let caip;
|
|
4817
|
-
if (blockchain.startsWith("eip155:")) {
|
|
4818
|
-
caip = `${blockchain}/slip44:60`;
|
|
4819
|
-
} else {
|
|
4820
|
-
caip = `${blockchain}/slip44:0`;
|
|
4821
|
-
}
|
|
4822
|
-
pubkeysForBatch.push({
|
|
4823
|
-
pubkey: address,
|
|
4824
|
-
caip
|
|
4825
|
-
});
|
|
4826
|
-
}
|
|
4827
|
-
}
|
|
4828
|
-
}
|
|
4829
|
-
console.log(tag, `Calling batch GetCharts with ${pubkeysForBatch.length} pubkeys`);
|
|
4830
|
-
let newBalances = [];
|
|
4831
|
-
try {
|
|
4832
|
-
const chartsResponse = await this.pioneer.GetCharts({
|
|
4833
|
-
pubkeys: pubkeysForBatch
|
|
4834
|
-
});
|
|
4835
|
-
newBalances = chartsResponse?.data?.balances || [];
|
|
4836
|
-
console.log(tag, `Received ${newBalances.length} balances from batch endpoint`);
|
|
4837
|
-
} catch (chartsError) {
|
|
4838
|
-
console.warn(tag, `GetCharts API not available (${chartsError.message}), continuing without charts data`);
|
|
4839
|
-
}
|
|
5409
|
+
console.log(tag6, "Fetching charts (portfolio + tokens + staking)...");
|
|
5410
|
+
const { getCharts: getChartsModular } = await Promise.resolve().then(() => (init_charts(), exports_charts));
|
|
5411
|
+
const newBalances = await getChartsModular(this.blockchains, this.pioneer, this.pubkeys, this.context);
|
|
5412
|
+
console.log(tag6, `Modular charts returned ${newBalances.length} balances (native + tokens + staking)`);
|
|
4840
5413
|
const uniqueBalances = new Map([...this.balances, ...newBalances].map((balance) => [
|
|
4841
5414
|
balance.identifier || `${balance.caip}:${balance.pubkey}`,
|
|
4842
5415
|
{
|
|
@@ -4844,17 +5417,19 @@ class SDK {
|
|
|
4844
5417
|
type: balance.type || "balance"
|
|
4845
5418
|
}
|
|
4846
5419
|
]));
|
|
4847
|
-
console.log(
|
|
5420
|
+
console.log(tag6, "Deduplicated to:", uniqueBalances.size, "unique balances");
|
|
4848
5421
|
this.balances = Array.from(uniqueBalances.values());
|
|
4849
|
-
|
|
5422
|
+
const tokens = this.balances.filter((b) => b.token === true);
|
|
5423
|
+
const native = this.balances.filter((b) => b.token !== true);
|
|
5424
|
+
console.log(tag6, `Balance breakdown: ${native.length} native + ${tokens.length} tokens = ${this.balances.length} total`);
|
|
4850
5425
|
return this.balances;
|
|
4851
5426
|
} catch (e) {
|
|
4852
|
-
console.error(
|
|
5427
|
+
console.error(tag6, "Error in getCharts:", e);
|
|
4853
5428
|
throw e;
|
|
4854
5429
|
}
|
|
4855
5430
|
};
|
|
4856
5431
|
this.setContext = async (context) => {
|
|
4857
|
-
const
|
|
5432
|
+
const tag6 = `${TAG9} | setContext | `;
|
|
4858
5433
|
try {
|
|
4859
5434
|
if (!context)
|
|
4860
5435
|
throw Error("context required!");
|
|
@@ -4862,12 +5437,12 @@ class SDK {
|
|
|
4862
5437
|
this.events.emit("SET_CONTEXT", context);
|
|
4863
5438
|
return { success: true };
|
|
4864
5439
|
} catch (e) {
|
|
4865
|
-
console.error(
|
|
5440
|
+
console.error(tag6, "e: ", e);
|
|
4866
5441
|
return { success: false };
|
|
4867
5442
|
}
|
|
4868
5443
|
};
|
|
4869
5444
|
this.setContextType = async (contextType) => {
|
|
4870
|
-
const
|
|
5445
|
+
const tag6 = `${TAG9} | setContextType | `;
|
|
4871
5446
|
try {
|
|
4872
5447
|
if (!contextType)
|
|
4873
5448
|
throw Error("contextType required!");
|
|
@@ -4875,22 +5450,27 @@ class SDK {
|
|
|
4875
5450
|
this.events.emit("SET_CONTEXT_TYPE", contextType);
|
|
4876
5451
|
return { success: true };
|
|
4877
5452
|
} catch (e) {
|
|
4878
|
-
console.error(
|
|
5453
|
+
console.error(tag6, "e: ", e);
|
|
4879
5454
|
return { success: false };
|
|
4880
5455
|
}
|
|
4881
5456
|
};
|
|
4882
|
-
this.refresh = async () => {
|
|
4883
|
-
const
|
|
5457
|
+
this.refresh = async (forceRefresh) => {
|
|
5458
|
+
const tag6 = `${TAG9} | refresh | `;
|
|
4884
5459
|
try {
|
|
4885
|
-
|
|
5460
|
+
if (forceRefresh) {
|
|
5461
|
+
console.log(tag6, "\uD83D\uDD04 Force refresh - fetching fresh balances from blockchain");
|
|
5462
|
+
await this.getBalances(true);
|
|
5463
|
+
} else {
|
|
5464
|
+
await this.sync();
|
|
5465
|
+
}
|
|
4886
5466
|
return this.balances;
|
|
4887
5467
|
} catch (e) {
|
|
4888
|
-
console.error(
|
|
5468
|
+
console.error(tag6, "e: ", e);
|
|
4889
5469
|
throw e;
|
|
4890
5470
|
}
|
|
4891
5471
|
};
|
|
4892
5472
|
this.setAssetContext = async function(asset) {
|
|
4893
|
-
const
|
|
5473
|
+
const tag6 = `${TAG9} | setAssetContext | `;
|
|
4894
5474
|
try {
|
|
4895
5475
|
if (!asset) {
|
|
4896
5476
|
this.assetContext = null;
|
|
@@ -4902,7 +5482,7 @@ class SDK {
|
|
|
4902
5482
|
asset.networkId = import_pioneer_caip8.caipToNetworkId(asset.caip);
|
|
4903
5483
|
if (!this.pubkeys || this.pubkeys.length === 0) {
|
|
4904
5484
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no pubkeys loaded. Please initialize wallet first.`;
|
|
4905
|
-
console.error(
|
|
5485
|
+
console.error(tag6, errorMsg);
|
|
4906
5486
|
throw new Error(errorMsg);
|
|
4907
5487
|
}
|
|
4908
5488
|
const pubkeysForNetwork = this.pubkeys.filter((e) => {
|
|
@@ -4917,8 +5497,8 @@ class SDK {
|
|
|
4917
5497
|
});
|
|
4918
5498
|
if (pubkeysForNetwork.length === 0) {
|
|
4919
5499
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no address/xpub found for network ${asset.networkId}`;
|
|
4920
|
-
console.error(
|
|
4921
|
-
console.error(
|
|
5500
|
+
console.error(tag6, errorMsg);
|
|
5501
|
+
console.error(tag6, "Available networks in pubkeys:", [
|
|
4922
5502
|
...new Set(this.pubkeys.flatMap((p) => p.networks || []))
|
|
4923
5503
|
]);
|
|
4924
5504
|
throw new Error(errorMsg);
|
|
@@ -4928,43 +5508,43 @@ class SDK {
|
|
|
4928
5508
|
const xpubFound = pubkeysForNetwork.some((p) => p.type === "xpub" && p.pubkey);
|
|
4929
5509
|
if (!xpubFound) {
|
|
4930
5510
|
const errorMsg = `Cannot set asset context for UTXO chain ${asset.caip} - xpub required but not found`;
|
|
4931
|
-
console.error(
|
|
5511
|
+
console.error(tag6, errorMsg);
|
|
4932
5512
|
throw new Error(errorMsg);
|
|
4933
5513
|
}
|
|
4934
5514
|
}
|
|
4935
5515
|
const hasValidAddress = pubkeysForNetwork.some((p) => p.address || p.master || p.pubkey);
|
|
4936
5516
|
if (!hasValidAddress) {
|
|
4937
5517
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no valid address found in pubkeys`;
|
|
4938
|
-
console.error(
|
|
5518
|
+
console.error(tag6, errorMsg);
|
|
4939
5519
|
throw new Error(errorMsg);
|
|
4940
5520
|
}
|
|
4941
|
-
console.log(
|
|
5521
|
+
console.log(tag6, `✅ Validated: Found ${pubkeysForNetwork.length} addresses for ${asset.networkId}`);
|
|
4942
5522
|
let freshPriceUsd = 0;
|
|
4943
5523
|
try {
|
|
4944
5524
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
4945
|
-
console.warn(
|
|
5525
|
+
console.warn(tag6, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
4946
5526
|
} else {
|
|
4947
|
-
console.log(
|
|
5527
|
+
console.log(tag6, "Fetching fresh market price for:", asset.caip);
|
|
4948
5528
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
4949
|
-
console.log(
|
|
5529
|
+
console.log(tag6, "Market data response:", marketData);
|
|
4950
5530
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
4951
5531
|
freshPriceUsd = marketData.data[0];
|
|
4952
|
-
console.log(
|
|
5532
|
+
console.log(tag6, "✅ Fresh market price:", freshPriceUsd);
|
|
4953
5533
|
} else {
|
|
4954
|
-
console.warn(
|
|
5534
|
+
console.warn(tag6, "No market data returned for:", asset.caip);
|
|
4955
5535
|
}
|
|
4956
5536
|
}
|
|
4957
5537
|
} catch (marketError) {
|
|
4958
|
-
console.error(
|
|
5538
|
+
console.error(tag6, "Error fetching market price:", marketError);
|
|
4959
5539
|
}
|
|
4960
5540
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
4961
|
-
console.log(
|
|
4962
|
-
let assetInfoDiscovery =
|
|
4963
|
-
console.log(
|
|
5541
|
+
console.log(tag6, "assetInfo: ", assetInfo);
|
|
5542
|
+
let assetInfoDiscovery = import_pioneer_discovery2.assetData[asset.caip];
|
|
5543
|
+
console.log(tag6, "assetInfoDiscovery: ", assetInfoDiscovery);
|
|
4964
5544
|
if (assetInfoDiscovery)
|
|
4965
5545
|
assetInfo = assetInfoDiscovery;
|
|
4966
5546
|
if (!assetInfo) {
|
|
4967
|
-
console.log(
|
|
5547
|
+
console.log(tag6, "Building placeholder asset!");
|
|
4968
5548
|
assetInfo = {
|
|
4969
5549
|
caip: asset.caip.toLowerCase(),
|
|
4970
5550
|
networkId: asset.networkId,
|
|
@@ -4981,30 +5561,30 @@ class SDK {
|
|
|
4981
5561
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
4982
5562
|
if (balance > 0 && valueUsd > 0) {
|
|
4983
5563
|
priceValue = valueUsd / balance;
|
|
4984
|
-
console.log(
|
|
5564
|
+
console.log(tag6, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
4985
5565
|
}
|
|
4986
5566
|
}
|
|
4987
5567
|
if (priceValue && priceValue > 0) {
|
|
4988
|
-
console.log(
|
|
5568
|
+
console.log(tag6, "detected priceUsd from balance:", priceValue);
|
|
4989
5569
|
assetInfo.priceUsd = priceValue;
|
|
4990
5570
|
}
|
|
4991
5571
|
}
|
|
4992
5572
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
4993
5573
|
assetInfo.priceUsd = freshPriceUsd;
|
|
4994
|
-
console.log(
|
|
5574
|
+
console.log(tag6, "✅ Using fresh market price:", freshPriceUsd);
|
|
4995
5575
|
let totalBalance = 0;
|
|
4996
5576
|
let totalValueUsd = 0;
|
|
4997
|
-
console.log(
|
|
5577
|
+
console.log(tag6, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
4998
5578
|
for (const balanceEntry of matchingBalances) {
|
|
4999
5579
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5000
5580
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5001
5581
|
totalBalance += balance;
|
|
5002
5582
|
totalValueUsd += valueUsd;
|
|
5003
|
-
console.log(
|
|
5583
|
+
console.log(tag6, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5004
5584
|
}
|
|
5005
5585
|
assetInfo.balance = totalBalance.toString();
|
|
5006
5586
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5007
|
-
console.log(
|
|
5587
|
+
console.log(tag6, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5008
5588
|
}
|
|
5009
5589
|
const assetBalances = this.balances.filter((b) => b.caip === asset.caip);
|
|
5010
5590
|
const assetPubkeys = this.pubkeys.filter((p) => p.networks && Array.isArray(p.networks) && p.networks.includes(import_pioneer_caip8.caipToNetworkId(asset.caip)) || import_pioneer_caip8.caipToNetworkId(asset.caip).includes("eip155") && p.networks && Array.isArray(p.networks) && p.networks.some((n) => n.startsWith("eip155")));
|
|
@@ -5024,7 +5604,7 @@ class SDK {
|
|
|
5024
5604
|
const balanceAmount = parseFloat(balance.balance || 0);
|
|
5025
5605
|
balance.valueUsd = (balanceAmount * freshPriceUsd).toString();
|
|
5026
5606
|
}
|
|
5027
|
-
console.log(
|
|
5607
|
+
console.log(tag6, "Updated all balances with fresh price data");
|
|
5028
5608
|
}
|
|
5029
5609
|
this.assetContext = finalAssetContext;
|
|
5030
5610
|
if (asset.isToken || asset.type === "token" || assetInfo.isToken || assetInfo.type === "token") {
|
|
@@ -5076,20 +5656,20 @@ class SDK {
|
|
|
5076
5656
|
const currentContextValid = this.pubkeyContext?.networks?.includes(networkId);
|
|
5077
5657
|
if (!this.pubkeyContext || !currentContextValid) {
|
|
5078
5658
|
this.pubkeyContext = assetPubkeys[0];
|
|
5079
|
-
console.log(
|
|
5659
|
+
console.log(tag6, "Auto-set pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey);
|
|
5080
5660
|
} else {
|
|
5081
|
-
console.log(
|
|
5661
|
+
console.log(tag6, "Preserving existing pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey, `(addressNList: [${(this.pubkeyContext.addressNList || this.pubkeyContext.addressNListMaster).join(", ")}])`);
|
|
5082
5662
|
}
|
|
5083
5663
|
}
|
|
5084
5664
|
this.events.emit("SET_ASSET_CONTEXT", this.assetContext);
|
|
5085
5665
|
return this.assetContext;
|
|
5086
5666
|
} catch (e) {
|
|
5087
|
-
console.error(
|
|
5667
|
+
console.error(tag6, "e: ", e);
|
|
5088
5668
|
throw e;
|
|
5089
5669
|
}
|
|
5090
5670
|
};
|
|
5091
5671
|
this.setPubkeyContext = async function(pubkey) {
|
|
5092
|
-
let
|
|
5672
|
+
let tag6 = `${TAG9} | setPubkeyContext | `;
|
|
5093
5673
|
try {
|
|
5094
5674
|
if (!pubkey)
|
|
5095
5675
|
throw Error("pubkey is required");
|
|
@@ -5097,31 +5677,31 @@ class SDK {
|
|
|
5097
5677
|
throw Error("invalid pubkey: missing pubkey or address");
|
|
5098
5678
|
const exists = this.pubkeys.some((pk) => pk.pubkey === pubkey.pubkey || pk.address === pubkey.address || pk.pubkey === pubkey.address);
|
|
5099
5679
|
if (!exists) {
|
|
5100
|
-
console.warn(
|
|
5680
|
+
console.warn(tag6, "Pubkey not found in current pubkeys array");
|
|
5101
5681
|
}
|
|
5102
5682
|
this.pubkeyContext = pubkey;
|
|
5103
|
-
console.log(
|
|
5683
|
+
console.log(tag6, "Pubkey context set to:", pubkey.address || pubkey.pubkey, "note:", pubkey.note, "addressNList:", pubkey.addressNList || pubkey.addressNListMaster);
|
|
5104
5684
|
return true;
|
|
5105
5685
|
} catch (e) {
|
|
5106
|
-
console.error(
|
|
5686
|
+
console.error(tag6, "e: ", e);
|
|
5107
5687
|
throw e;
|
|
5108
5688
|
}
|
|
5109
5689
|
};
|
|
5110
5690
|
this.setOutboundAssetContext = async function(asset) {
|
|
5111
|
-
const
|
|
5691
|
+
const tag6 = `${TAG9} | setOutputAssetContext | `;
|
|
5112
5692
|
try {
|
|
5113
|
-
console.log(
|
|
5693
|
+
console.log(tag6, "0. asset: ", asset);
|
|
5114
5694
|
if (!asset) {
|
|
5115
5695
|
this.outboundAssetContext = null;
|
|
5116
5696
|
return;
|
|
5117
5697
|
}
|
|
5118
|
-
console.log(
|
|
5698
|
+
console.log(tag6, "1 asset: ", asset);
|
|
5119
5699
|
if (!asset.caip)
|
|
5120
5700
|
throw Error("Invalid Asset! missing caip!");
|
|
5121
5701
|
if (!asset.networkId)
|
|
5122
5702
|
asset.networkId = import_pioneer_caip8.caipToNetworkId(asset.caip);
|
|
5123
|
-
console.log(
|
|
5124
|
-
console.log(
|
|
5703
|
+
console.log(tag6, "networkId: ", asset.networkId);
|
|
5704
|
+
console.log(tag6, "this.pubkeys: ", this.pubkeys);
|
|
5125
5705
|
const pubkey = this.pubkeys.find((p) => {
|
|
5126
5706
|
if (!p.networks || !Array.isArray(p.networks))
|
|
5127
5707
|
return false;
|
|
@@ -5136,23 +5716,23 @@ class SDK {
|
|
|
5136
5716
|
let freshPriceUsd = 0;
|
|
5137
5717
|
try {
|
|
5138
5718
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
5139
|
-
console.warn(
|
|
5719
|
+
console.warn(tag6, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
5140
5720
|
} else {
|
|
5141
|
-
console.log(
|
|
5721
|
+
console.log(tag6, "Fetching fresh market price for:", asset.caip);
|
|
5142
5722
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
5143
|
-
console.log(
|
|
5723
|
+
console.log(tag6, "Market data response:", marketData);
|
|
5144
5724
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
5145
5725
|
freshPriceUsd = marketData.data[0];
|
|
5146
|
-
console.log(
|
|
5726
|
+
console.log(tag6, "✅ Fresh market price:", freshPriceUsd);
|
|
5147
5727
|
} else {
|
|
5148
|
-
console.warn(
|
|
5728
|
+
console.warn(tag6, "No market data returned for:", asset.caip);
|
|
5149
5729
|
}
|
|
5150
5730
|
}
|
|
5151
5731
|
} catch (marketError) {
|
|
5152
|
-
console.error(
|
|
5732
|
+
console.error(tag6, "Error fetching market price:", marketError);
|
|
5153
5733
|
}
|
|
5154
5734
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
5155
|
-
console.log(
|
|
5735
|
+
console.log(tag6, "assetInfo: ", assetInfo);
|
|
5156
5736
|
if (!assetInfo) {
|
|
5157
5737
|
assetInfo = {
|
|
5158
5738
|
caip: asset.caip.toLowerCase(),
|
|
@@ -5170,70 +5750,70 @@ class SDK {
|
|
|
5170
5750
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
5171
5751
|
if (balance > 0 && valueUsd > 0) {
|
|
5172
5752
|
priceValue = valueUsd / balance;
|
|
5173
|
-
console.log(
|
|
5753
|
+
console.log(tag6, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
5174
5754
|
}
|
|
5175
5755
|
}
|
|
5176
5756
|
if (priceValue && priceValue > 0) {
|
|
5177
|
-
console.log(
|
|
5757
|
+
console.log(tag6, "detected priceUsd from balance:", priceValue);
|
|
5178
5758
|
assetInfo.priceUsd = priceValue;
|
|
5179
5759
|
}
|
|
5180
5760
|
}
|
|
5181
5761
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
5182
5762
|
assetInfo.priceUsd = freshPriceUsd;
|
|
5183
|
-
console.log(
|
|
5763
|
+
console.log(tag6, "✅ Using fresh market price:", freshPriceUsd);
|
|
5184
5764
|
let totalBalance = 0;
|
|
5185
5765
|
let totalValueUsd = 0;
|
|
5186
|
-
console.log(
|
|
5766
|
+
console.log(tag6, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
5187
5767
|
for (const balanceEntry of matchingBalances) {
|
|
5188
5768
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5189
5769
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5190
5770
|
totalBalance += balance;
|
|
5191
5771
|
totalValueUsd += valueUsd;
|
|
5192
|
-
console.log(
|
|
5772
|
+
console.log(tag6, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5193
5773
|
}
|
|
5194
5774
|
assetInfo.balance = totalBalance.toString();
|
|
5195
5775
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5196
|
-
console.log(
|
|
5776
|
+
console.log(tag6, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5197
5777
|
}
|
|
5198
|
-
console.log(
|
|
5778
|
+
console.log(tag6, "CHECKPOINT 1");
|
|
5199
5779
|
this.outboundAssetContext = { ...assetInfo, ...asset, ...pubkey };
|
|
5200
|
-
console.log(
|
|
5201
|
-
console.log(
|
|
5780
|
+
console.log(tag6, "CHECKPOINT 3");
|
|
5781
|
+
console.log(tag6, "outboundAssetContext: assetInfo: ", assetInfo);
|
|
5202
5782
|
if (asset.caip) {
|
|
5203
5783
|
this.outboundBlockchainContext = import_pioneer_caip8.caipToNetworkId(asset.caip);
|
|
5204
5784
|
} else if (asset.networkId) {
|
|
5205
5785
|
this.outboundBlockchainContext = asset.networkId;
|
|
5206
5786
|
}
|
|
5207
|
-
console.log(
|
|
5787
|
+
console.log(tag6, "CHECKPOINT 4");
|
|
5208
5788
|
this.events.emit("SET_OUTBOUND_ASSET_CONTEXT", this.outboundAssetContext);
|
|
5209
5789
|
return this.outboundAssetContext;
|
|
5210
5790
|
} catch (e) {
|
|
5211
|
-
console.error(
|
|
5791
|
+
console.error(tag6, "e: ", e);
|
|
5212
5792
|
throw e;
|
|
5213
5793
|
}
|
|
5214
5794
|
};
|
|
5215
5795
|
}
|
|
5216
5796
|
CheckERC20Allowance = async (params) => {
|
|
5217
|
-
const
|
|
5797
|
+
const tag6 = TAG9 + " | CheckERC20Allowance | ";
|
|
5218
5798
|
try {
|
|
5219
|
-
console.log(
|
|
5799
|
+
console.log(tag6, "Checking ERC20 allowance:", params);
|
|
5220
5800
|
const result = await this.pioneer.GetTokenAllowance(params);
|
|
5221
|
-
console.log(
|
|
5801
|
+
console.log(tag6, "Allowance result:", result);
|
|
5222
5802
|
return result.data;
|
|
5223
5803
|
} catch (e) {
|
|
5224
|
-
console.error(
|
|
5804
|
+
console.error(tag6, "Error checking ERC20 allowance:", e);
|
|
5225
5805
|
throw e;
|
|
5226
5806
|
}
|
|
5227
5807
|
};
|
|
5228
5808
|
BuildERC20ApprovalTx = async (params) => {
|
|
5229
|
-
const
|
|
5809
|
+
const tag6 = TAG9 + " | BuildERC20ApprovalTx | ";
|
|
5230
5810
|
try {
|
|
5231
|
-
console.log(
|
|
5811
|
+
console.log(tag6, "Building ERC20 approval transaction:", params);
|
|
5232
5812
|
const result = await this.pioneer.BuildApprovalTransaction(params);
|
|
5233
|
-
console.log(
|
|
5813
|
+
console.log(tag6, "Approval tx built:", result);
|
|
5234
5814
|
return result.data;
|
|
5235
5815
|
} catch (e) {
|
|
5236
|
-
console.error(
|
|
5816
|
+
console.error(tag6, "Error building approval transaction:", e);
|
|
5237
5817
|
throw e;
|
|
5238
5818
|
}
|
|
5239
5819
|
};
|