@pioneer-platform/pioneer-sdk 8.11.12 → 8.11.17
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 +330 -757
- package/dist/index.es.js +330 -757
- package/dist/index.js +330 -757
- package/package.json +2 -2
- package/src/charts/cosmos-staking.ts +32 -22
- package/src/charts/evm.ts +104 -37
- package/src/charts/maya.ts +9 -6
- package/src/index.ts +61 -13
- package/src/txbuilder/createUnsignedEvmTx.ts +4 -8
package/dist/index.js
CHANGED
|
@@ -367,7 +367,7 @@ class Pioneer {
|
|
|
367
367
|
|
|
368
368
|
// src/index.ts
|
|
369
369
|
import { addressNListToBIP32 as addressNListToBIP322, getPaths } from "@pioneer-platform/pioneer-coins";
|
|
370
|
-
import { assetData
|
|
370
|
+
import { assetData } from "@pioneer-platform/pioneer-discovery";
|
|
371
371
|
import { Events } from "@pioneer-platform/pioneer-events";
|
|
372
372
|
|
|
373
373
|
// node:events
|
|
@@ -566,467 +566,6 @@ function R(t) {
|
|
|
566
566
|
var A = o;
|
|
567
567
|
var P = o.prototype;
|
|
568
568
|
|
|
569
|
-
// src/charts/utils.ts
|
|
570
|
-
import { assetData } from "@pioneer-platform/pioneer-discovery";
|
|
571
|
-
function hydrateAssetData(caip) {
|
|
572
|
-
return assetData[caip] || assetData[caip.toLowerCase()];
|
|
573
|
-
}
|
|
574
|
-
function checkDuplicateBalance(balances, caip, pubkey, validator) {
|
|
575
|
-
return balances.some((b2) => b2.caip === caip && b2.pubkey === pubkey && (!validator || b2.validator === validator));
|
|
576
|
-
}
|
|
577
|
-
function createBalanceIdentifier(caip, pubkey) {
|
|
578
|
-
return `${caip}:${pubkey}`;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
// src/charts/custom-tokens.ts
|
|
582
|
-
var tag = "| charts-custom-tokens |";
|
|
583
|
-
async function fetchCustomTokens(params, balances) {
|
|
584
|
-
const { pioneer, pubkeys, blockchains, context } = params;
|
|
585
|
-
console.log(tag, "Fetching custom tokens...");
|
|
586
|
-
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
587
|
-
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
588
|
-
if (!primaryAddress) {
|
|
589
|
-
console.log(tag, "No EVM address found, skipping custom tokens");
|
|
590
|
-
return;
|
|
591
|
-
}
|
|
592
|
-
console.log(tag, "Using EVM address for custom tokens:", primaryAddress);
|
|
593
|
-
const supportedNetworks = blockchains.filter((net) => net.startsWith("eip155:"));
|
|
594
|
-
if (supportedNetworks.length === 0) {
|
|
595
|
-
console.log(tag, "No EVM networks for custom tokens");
|
|
596
|
-
return;
|
|
597
|
-
}
|
|
598
|
-
console.log(tag, `Checking custom tokens on ${supportedNetworks.length} networks`);
|
|
599
|
-
for (const networkId of supportedNetworks) {
|
|
600
|
-
try {
|
|
601
|
-
const response = await pioneer.GetCustomTokens({ networkId, userAddress: primaryAddress });
|
|
602
|
-
const customTokens = response?.data?.tokens || [];
|
|
603
|
-
console.log(tag, `Found ${customTokens.length} custom tokens on ${networkId}`);
|
|
604
|
-
for (const token of customTokens) {
|
|
605
|
-
const chartBalance = processCustomToken(token, primaryAddress, context, blockchains);
|
|
606
|
-
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
607
|
-
balances.push(chartBalance);
|
|
608
|
-
console.log(tag, `Added custom token: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
} catch (error) {
|
|
612
|
-
console.error(tag, `Error fetching custom tokens for ${networkId}:`, error.message);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
console.log(tag, `Custom tokens complete. Total balances: ${balances.length}`);
|
|
616
|
-
}
|
|
617
|
-
function processCustomToken(token, primaryAddress, context, blockchains) {
|
|
618
|
-
if (!token.assetCaip || !token.networkId) {
|
|
619
|
-
return null;
|
|
620
|
-
}
|
|
621
|
-
let extractedNetworkId = token.networkId;
|
|
622
|
-
if (token.assetCaip.includes("/denom:")) {
|
|
623
|
-
const parts = token.assetCaip.split("/denom:");
|
|
624
|
-
if (parts.length > 0) {
|
|
625
|
-
extractedNetworkId = parts[0];
|
|
626
|
-
}
|
|
627
|
-
} else if (token.networkId && token.networkId.includes("/")) {
|
|
628
|
-
extractedNetworkId = token.networkId.split("/")[0];
|
|
629
|
-
}
|
|
630
|
-
const isEip155 = extractedNetworkId.startsWith("eip155:");
|
|
631
|
-
if (!isEip155 && !blockchains.includes(extractedNetworkId)) {
|
|
632
|
-
return null;
|
|
633
|
-
}
|
|
634
|
-
const tokenAssetInfo = hydrateAssetData(token.assetCaip);
|
|
635
|
-
const tokenPubkey = token.pubkey || primaryAddress;
|
|
636
|
-
const chartBalance = {
|
|
637
|
-
context,
|
|
638
|
-
chart: "pioneer",
|
|
639
|
-
contextType: context.split(":")[0],
|
|
640
|
-
name: tokenAssetInfo?.name || token.token?.name || "Unknown Custom Token",
|
|
641
|
-
caip: token.assetCaip,
|
|
642
|
-
icon: tokenAssetInfo?.icon || token.token?.icon || "",
|
|
643
|
-
pubkey: tokenPubkey,
|
|
644
|
-
ticker: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
645
|
-
ref: `${context}${token.assetCaip}`,
|
|
646
|
-
identifier: createBalanceIdentifier(token.assetCaip, tokenPubkey),
|
|
647
|
-
networkId: extractedNetworkId,
|
|
648
|
-
symbol: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
649
|
-
type: tokenAssetInfo?.type || "erc20",
|
|
650
|
-
token: true,
|
|
651
|
-
decimal: tokenAssetInfo?.decimal || token.token?.decimal,
|
|
652
|
-
balance: token.token?.balance?.toString() || "0",
|
|
653
|
-
priceUsd: token.token?.price || 0,
|
|
654
|
-
valueUsd: token.token?.balanceUSD || 0,
|
|
655
|
-
updated: new Date().getTime()
|
|
656
|
-
};
|
|
657
|
-
return chartBalance;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
// src/charts/evm.ts
|
|
661
|
-
var tag2 = "| charts-evm |";
|
|
662
|
-
async function getEvmCharts(params) {
|
|
663
|
-
const { blockchains, pioneer, pubkeys, context } = params;
|
|
664
|
-
const balances = [];
|
|
665
|
-
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
666
|
-
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
667
|
-
console.log(tag2, "Total pubkeys available:", pubkeys.length);
|
|
668
|
-
console.log(tag2, "Blockchains to process:", blockchains);
|
|
669
|
-
if (!primaryAddress) {
|
|
670
|
-
console.log(tag2, "No EVM address found, skipping portfolio lookup (Zapper only supports Ethereum)");
|
|
671
|
-
return balances;
|
|
672
|
-
}
|
|
673
|
-
console.log(tag2, "Using EVM address for portfolio:", primaryAddress);
|
|
674
|
-
await fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context);
|
|
675
|
-
await fetchCustomTokens({ blockchains, pioneer, pubkeys, context }, balances);
|
|
676
|
-
try {
|
|
677
|
-
let portfolio = await pioneer.GetPortfolio({
|
|
678
|
-
networkId: "eip155:1",
|
|
679
|
-
address: primaryAddress
|
|
680
|
-
});
|
|
681
|
-
portfolio = portfolio.data?.data || portfolio.data;
|
|
682
|
-
if (!portfolio || !portfolio.balances) {
|
|
683
|
-
console.error(tag2, "No portfolio.balances found:", portfolio);
|
|
684
|
-
return balances;
|
|
685
|
-
}
|
|
686
|
-
console.log(tag2, `Portfolio returned ${portfolio.balances.length} balances`);
|
|
687
|
-
let processedCount = 0;
|
|
688
|
-
let skippedCount = 0;
|
|
689
|
-
for (const balance of portfolio.balances) {
|
|
690
|
-
const processedBalance = processPortfolioBalance(balance, primaryAddress, context, blockchains);
|
|
691
|
-
if (processedBalance) {
|
|
692
|
-
if (!checkDuplicateBalance(balances, processedBalance.caip, processedBalance.pubkey)) {
|
|
693
|
-
balances.push(processedBalance);
|
|
694
|
-
processedCount++;
|
|
695
|
-
}
|
|
696
|
-
} else {
|
|
697
|
-
skippedCount++;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
console.log(tag2, `Processed ${processedCount} balances, skipped ${skippedCount}`);
|
|
701
|
-
if (portfolio.tokens && portfolio.tokens.length > 0) {
|
|
702
|
-
console.log(tag2, "Processing portfolio.tokens:", portfolio.tokens.length);
|
|
703
|
-
for (const token of portfolio.tokens) {
|
|
704
|
-
const processedToken = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
705
|
-
if (processedToken && !checkDuplicateBalance(balances, processedToken.caip, processedToken.pubkey)) {
|
|
706
|
-
balances.push(processedToken);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
} catch (e) {
|
|
711
|
-
console.error(tag2, "Error fetching portfolio:", e);
|
|
712
|
-
}
|
|
713
|
-
return balances;
|
|
714
|
-
}
|
|
715
|
-
function processPortfolioBalance(balance, primaryAddress, context, blockchains) {
|
|
716
|
-
if (!balance.caip) {
|
|
717
|
-
console.error(tag2, "No caip found for:", balance);
|
|
718
|
-
return null;
|
|
719
|
-
}
|
|
720
|
-
const networkId = balance.caip.split("/")[0];
|
|
721
|
-
const isEip155 = networkId.startsWith("eip155:");
|
|
722
|
-
if (!isEip155 && !blockchains.includes(networkId)) {
|
|
723
|
-
return null;
|
|
724
|
-
}
|
|
725
|
-
const assetInfo = hydrateAssetData(balance.caip);
|
|
726
|
-
const balancePubkey = balance.pubkey || primaryAddress;
|
|
727
|
-
const balanceType = assetInfo?.type || balance.type || "native";
|
|
728
|
-
const isToken = balanceType !== "native" && balance.caip.includes("/erc20:");
|
|
729
|
-
let calculatedPrice = balance.priceUsd || balance.price || 0;
|
|
730
|
-
if ((!calculatedPrice || calculatedPrice === 0) && balance.valueUsd && balance.balance) {
|
|
731
|
-
const balanceNum = parseFloat(balance.balance.toString());
|
|
732
|
-
const valueNum = parseFloat(balance.valueUsd.toString());
|
|
733
|
-
if (balanceNum > 0 && valueNum > 0) {
|
|
734
|
-
calculatedPrice = valueNum / balanceNum;
|
|
735
|
-
console.log(tag2, `Calculated price from value/balance: ${calculatedPrice} for ${balance.caip}`);
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
const chartBalance = {
|
|
739
|
-
context,
|
|
740
|
-
chart: "pioneer",
|
|
741
|
-
contextType: "keepkey",
|
|
742
|
-
name: assetInfo?.name || balance.name || "Unknown",
|
|
743
|
-
caip: balance.caip,
|
|
744
|
-
icon: assetInfo?.icon || balance.icon || "",
|
|
745
|
-
pubkey: balancePubkey,
|
|
746
|
-
ticker: assetInfo?.symbol || balance.symbol || "UNK",
|
|
747
|
-
ref: `${context}${balance.caip}`,
|
|
748
|
-
identifier: createBalanceIdentifier(balance.caip, balancePubkey),
|
|
749
|
-
networkId,
|
|
750
|
-
chain: networkId,
|
|
751
|
-
symbol: assetInfo?.symbol || balance.symbol || "UNK",
|
|
752
|
-
type: balanceType,
|
|
753
|
-
token: isToken,
|
|
754
|
-
decimal: assetInfo?.decimal || balance.decimal,
|
|
755
|
-
balance: balance.balance.toString(),
|
|
756
|
-
price: calculatedPrice,
|
|
757
|
-
priceUsd: calculatedPrice,
|
|
758
|
-
valueUsd: balance.valueUsd.toString(),
|
|
759
|
-
updated: new Date().getTime()
|
|
760
|
-
};
|
|
761
|
-
if (balance.display) {
|
|
762
|
-
chartBalance.icon = ["multi", chartBalance.icon, balance.display.toString()].toString();
|
|
763
|
-
}
|
|
764
|
-
return chartBalance;
|
|
765
|
-
}
|
|
766
|
-
function processPortfolioToken(token, primaryAddress, context, blockchains) {
|
|
767
|
-
if (!token.assetCaip || !token.networkId) {
|
|
768
|
-
return null;
|
|
769
|
-
}
|
|
770
|
-
let extractedNetworkId = token.networkId;
|
|
771
|
-
if (token.assetCaip.includes("/denom:")) {
|
|
772
|
-
const parts = token.assetCaip.split("/denom:");
|
|
773
|
-
if (parts.length > 0) {
|
|
774
|
-
extractedNetworkId = parts[0];
|
|
775
|
-
}
|
|
776
|
-
} else if (token.networkId && token.networkId.includes("/")) {
|
|
777
|
-
extractedNetworkId = token.networkId.split("/")[0];
|
|
778
|
-
}
|
|
779
|
-
const isEip155 = extractedNetworkId.startsWith("eip155:");
|
|
780
|
-
if (!isEip155 && !blockchains.includes(extractedNetworkId)) {
|
|
781
|
-
return null;
|
|
782
|
-
}
|
|
783
|
-
const tokenAssetInfo = hydrateAssetData(token.assetCaip);
|
|
784
|
-
const tokenPubkey = token.pubkey || primaryAddress;
|
|
785
|
-
const chartBalance = {
|
|
786
|
-
context,
|
|
787
|
-
chart: "pioneer",
|
|
788
|
-
contextType: context.split(":")[0],
|
|
789
|
-
name: tokenAssetInfo?.name || token.token?.coingeckoId || token.token?.name || "Unknown",
|
|
790
|
-
caip: token.assetCaip,
|
|
791
|
-
icon: tokenAssetInfo?.icon || token.token?.icon || "",
|
|
792
|
-
pubkey: tokenPubkey,
|
|
793
|
-
ticker: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
794
|
-
ref: `${context}${token.assetCaip}`,
|
|
795
|
-
identifier: createBalanceIdentifier(token.assetCaip, tokenPubkey),
|
|
796
|
-
networkId: extractedNetworkId,
|
|
797
|
-
symbol: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
798
|
-
type: tokenAssetInfo?.type || "token",
|
|
799
|
-
token: true,
|
|
800
|
-
decimal: tokenAssetInfo?.decimal || token.token?.decimal,
|
|
801
|
-
balance: token.token?.balance?.toString() || "0",
|
|
802
|
-
priceUsd: token.token?.price || 0,
|
|
803
|
-
valueUsd: token.token?.balanceUSD || 0,
|
|
804
|
-
updated: new Date().getTime()
|
|
805
|
-
};
|
|
806
|
-
return chartBalance;
|
|
807
|
-
}
|
|
808
|
-
async function fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context) {
|
|
809
|
-
console.log(tag2, "Fetching stable coins for redundancy...");
|
|
810
|
-
const supportedNetworks = ["eip155:1", "eip155:137", "eip155:8453", "eip155:56"];
|
|
811
|
-
const networksToCheck = blockchains.filter((net) => supportedNetworks.includes(net));
|
|
812
|
-
if (networksToCheck.length === 0) {
|
|
813
|
-
console.log(tag2, "No supported networks for stable coins");
|
|
814
|
-
return;
|
|
815
|
-
}
|
|
816
|
-
console.log(tag2, `Checking stable coins on ${networksToCheck.length} networks`);
|
|
817
|
-
for (const networkId of networksToCheck) {
|
|
818
|
-
try {
|
|
819
|
-
const response = await pioneer.GetStableCoins({ networkId, address: primaryAddress });
|
|
820
|
-
const stableCoins = response?.data?.tokens || [];
|
|
821
|
-
console.log(tag2, `Found ${stableCoins.length} stable coins on ${networkId}`);
|
|
822
|
-
for (const token of stableCoins) {
|
|
823
|
-
const chartBalance = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
824
|
-
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
825
|
-
balances.push(chartBalance);
|
|
826
|
-
console.log(tag2, `Added stable coin: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
} catch (error) {
|
|
830
|
-
console.error(tag2, `Error fetching stable coins for ${networkId}:`, error.message);
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
console.log(tag2, `Stable coin redundancy complete. Total balances: ${balances.length}`);
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
// src/charts/maya.ts
|
|
837
|
-
var tag3 = "| charts-maya |";
|
|
838
|
-
async function getMayaCharts(params, existingBalances) {
|
|
839
|
-
const { blockchains, pioneer, pubkeys, context } = params;
|
|
840
|
-
const balances = [];
|
|
841
|
-
try {
|
|
842
|
-
const mayaPubkey = pubkeys.find((p) => p.networks && Array.isArray(p.networks) && p.networks.includes("cosmos:mayachain-mainnet-v1"));
|
|
843
|
-
if (!mayaPubkey || !mayaPubkey.address) {
|
|
844
|
-
console.log(tag3, "No MAYA pubkey found, skipping MAYA token fetch");
|
|
845
|
-
return balances;
|
|
846
|
-
}
|
|
847
|
-
if (!blockchains.includes("cosmos:mayachain-mainnet-v1")) {
|
|
848
|
-
console.log(tag3, "MAYA network not in blockchains list, skipping MAYA token fetch");
|
|
849
|
-
return balances;
|
|
850
|
-
}
|
|
851
|
-
const hasMayaToken = existingBalances.some((b2) => b2.caip === "cosmos:mayachain-mainnet-v1/denom:maya");
|
|
852
|
-
if (hasMayaToken) {
|
|
853
|
-
console.log(tag3, "MAYA token already exists in balances, skipping");
|
|
854
|
-
return balances;
|
|
855
|
-
}
|
|
856
|
-
console.log(tag3, "MAYA token not found in portfolio, fetching separately...");
|
|
857
|
-
console.log(tag3, "MAYA pubkey address:", mayaPubkey.address);
|
|
858
|
-
const mayaBalanceResponse = await pioneer.GetPortfolioBalances([
|
|
859
|
-
{
|
|
860
|
-
caip: "cosmos:mayachain-mainnet-v1/denom:maya",
|
|
861
|
-
pubkey: mayaPubkey.address
|
|
862
|
-
}
|
|
863
|
-
]);
|
|
864
|
-
console.log(tag3, "MAYA balance response:", JSON.stringify(mayaBalanceResponse?.data, null, 2));
|
|
865
|
-
if (!mayaBalanceResponse?.data || mayaBalanceResponse.data.length === 0) {
|
|
866
|
-
console.log(tag3, "No MAYA token balance returned from GetPortfolioBalances API");
|
|
867
|
-
return balances;
|
|
868
|
-
}
|
|
869
|
-
console.log(tag3, "Found MAYA token balances:", mayaBalanceResponse.data.length);
|
|
870
|
-
for (const mayaBalance of mayaBalanceResponse.data) {
|
|
871
|
-
if (mayaBalance.caip !== "cosmos:mayachain-mainnet-v1/denom:maya") {
|
|
872
|
-
console.log(tag3, "Unexpected balance in MAYA response:", mayaBalance);
|
|
873
|
-
continue;
|
|
874
|
-
}
|
|
875
|
-
const mayaAssetInfo = hydrateAssetData(mayaBalance.caip);
|
|
876
|
-
const isToken = mayaBalance.caip.includes("/denom:") && !mayaBalance.caip.endsWith("/denom:cacao");
|
|
877
|
-
const mayaTokenBalance = {
|
|
878
|
-
context,
|
|
879
|
-
chart: "pioneer",
|
|
880
|
-
contextType: context.split(":")[0],
|
|
881
|
-
name: mayaAssetInfo?.name || "Maya Token",
|
|
882
|
-
caip: mayaBalance.caip,
|
|
883
|
-
icon: mayaAssetInfo?.icon || "https://pioneers.dev/coins/maya.png",
|
|
884
|
-
pubkey: mayaPubkey.address,
|
|
885
|
-
ticker: mayaAssetInfo?.symbol || "MAYA",
|
|
886
|
-
ref: `${context}${mayaBalance.caip}`,
|
|
887
|
-
identifier: createBalanceIdentifier(mayaBalance.caip, mayaPubkey.address),
|
|
888
|
-
networkId: "cosmos:mayachain-mainnet-v1",
|
|
889
|
-
symbol: mayaAssetInfo?.symbol || "MAYA",
|
|
890
|
-
type: mayaAssetInfo?.type || "token",
|
|
891
|
-
token: isToken,
|
|
892
|
-
decimal: mayaAssetInfo?.decimal,
|
|
893
|
-
balance: mayaBalance.balance?.toString() || "0",
|
|
894
|
-
priceUsd: parseFloat(mayaBalance.priceUsd) || 0,
|
|
895
|
-
valueUsd: parseFloat(mayaBalance.valueUsd) || 0,
|
|
896
|
-
updated: new Date().getTime()
|
|
897
|
-
};
|
|
898
|
-
console.log(tag3, "Adding MAYA token to balances:", mayaTokenBalance);
|
|
899
|
-
balances.push(mayaTokenBalance);
|
|
900
|
-
}
|
|
901
|
-
} catch (mayaError) {
|
|
902
|
-
console.error(tag3, "Error fetching MAYA token balance:", mayaError);
|
|
903
|
-
}
|
|
904
|
-
return balances;
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
// src/charts/cosmos-staking.ts
|
|
908
|
-
var tag4 = "| charts-cosmos-staking |";
|
|
909
|
-
async function getCosmosStakingCharts(params) {
|
|
910
|
-
const { blockchains, pioneer, pubkeys, context } = params;
|
|
911
|
-
const balances = [];
|
|
912
|
-
try {
|
|
913
|
-
console.log(tag4, "Adding Cosmos staking positions to charts...");
|
|
914
|
-
const cosmosPubkeys = pubkeys.filter((p) => p.networks && Array.isArray(p.networks) && p.networks.some((n) => n.includes("cosmos:cosmoshub") || n.includes("cosmos:osmosis")));
|
|
915
|
-
if (cosmosPubkeys.length === 0) {
|
|
916
|
-
console.log(tag4, "No cosmos pubkeys found for staking positions");
|
|
917
|
-
return balances;
|
|
918
|
-
}
|
|
919
|
-
console.log(tag4, "Found cosmos pubkeys for staking:", cosmosPubkeys.length);
|
|
920
|
-
for (const cosmosPubkey of cosmosPubkeys) {
|
|
921
|
-
if (!cosmosPubkey.address) {
|
|
922
|
-
continue;
|
|
923
|
-
}
|
|
924
|
-
const cosmosNetworks = cosmosPubkey.networks.filter((n) => n.includes("cosmos:cosmoshub") || n.includes("cosmos:osmosis"));
|
|
925
|
-
for (const networkId of cosmosNetworks) {
|
|
926
|
-
if (!blockchains.includes(networkId)) {
|
|
927
|
-
continue;
|
|
928
|
-
}
|
|
929
|
-
await fetchStakingPositionsForNetwork(networkId, cosmosPubkey.address, context, pioneer, balances);
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
} catch (e) {
|
|
933
|
-
console.error(tag4, "Error adding cosmos staking positions:", e);
|
|
934
|
-
}
|
|
935
|
-
return balances;
|
|
936
|
-
}
|
|
937
|
-
async function fetchStakingPositionsForNetwork(networkId, address, context, pioneer, balances) {
|
|
938
|
-
try {
|
|
939
|
-
console.log(tag4, `Fetching staking positions for ${address} on ${networkId}...`);
|
|
940
|
-
let network;
|
|
941
|
-
if (networkId === "cosmos:cosmoshub-4") {
|
|
942
|
-
network = "cosmos";
|
|
943
|
-
} else if (networkId === "cosmos:osmosis-1") {
|
|
944
|
-
network = "osmosis";
|
|
945
|
-
} else {
|
|
946
|
-
console.error(tag4, `Unsupported networkId for staking: ${networkId}`);
|
|
947
|
-
return;
|
|
948
|
-
}
|
|
949
|
-
const stakingResponse = await pioneer.GetStakingPositions({
|
|
950
|
-
network,
|
|
951
|
-
address
|
|
952
|
-
});
|
|
953
|
-
if (!stakingResponse?.data || !Array.isArray(stakingResponse.data)) {
|
|
954
|
-
console.log(tag4, `No staking positions found for ${address} on ${networkId}`);
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
console.log(tag4, `Found ${stakingResponse.data.length} staking positions for ${networkId}`);
|
|
958
|
-
for (const position of stakingResponse.data) {
|
|
959
|
-
const processedPosition = processStakingPosition(position, address, context, networkId);
|
|
960
|
-
if (processedPosition && !checkDuplicateBalance(balances, processedPosition.caip, processedPosition.pubkey, processedPosition.validator)) {
|
|
961
|
-
balances.push(processedPosition);
|
|
962
|
-
console.log(tag4, `Added ${position.type} position:`, {
|
|
963
|
-
balance: processedPosition.balance,
|
|
964
|
-
ticker: processedPosition.ticker,
|
|
965
|
-
valueUsd: processedPosition.valueUsd,
|
|
966
|
-
validator: processedPosition.validator
|
|
967
|
-
});
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
} catch (stakingError) {
|
|
971
|
-
console.error(tag4, `Error fetching staking positions for ${address} on ${networkId}:`, stakingError);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
function processStakingPosition(position, address, context, networkId) {
|
|
975
|
-
if (!position.balance || position.balance <= 0 || !position.caip) {
|
|
976
|
-
return null;
|
|
977
|
-
}
|
|
978
|
-
const stakingAssetInfo = hydrateAssetData(position.caip);
|
|
979
|
-
const stakingBalance = {
|
|
980
|
-
context,
|
|
981
|
-
chart: "staking",
|
|
982
|
-
contextType: context.split(":")[0],
|
|
983
|
-
name: stakingAssetInfo?.name || position.name || `${position.type} Position`,
|
|
984
|
-
caip: position.caip,
|
|
985
|
-
icon: stakingAssetInfo?.icon || position.icon || "",
|
|
986
|
-
pubkey: address,
|
|
987
|
-
ticker: stakingAssetInfo?.symbol || position.ticker || position.symbol || "UNK",
|
|
988
|
-
ref: `${context}${position.caip}`,
|
|
989
|
-
identifier: createBalanceIdentifier(position.caip, address),
|
|
990
|
-
networkId,
|
|
991
|
-
symbol: stakingAssetInfo?.symbol || position.symbol || position.ticker || "UNK",
|
|
992
|
-
type: stakingAssetInfo?.type || position.type || "staking",
|
|
993
|
-
token: false,
|
|
994
|
-
decimal: stakingAssetInfo?.decimal,
|
|
995
|
-
balance: position.balance.toString(),
|
|
996
|
-
priceUsd: position.priceUsd || 0,
|
|
997
|
-
valueUsd: position.valueUsd || position.balance * (position.priceUsd || 0),
|
|
998
|
-
status: position.status || "active",
|
|
999
|
-
validator: position.validatorAddress || position.validator || "",
|
|
1000
|
-
updated: new Date().getTime()
|
|
1001
|
-
};
|
|
1002
|
-
return stakingBalance;
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
// src/charts/index.ts
|
|
1006
|
-
var tag5 = "| getCharts |";
|
|
1007
|
-
var getCharts = async (blockchains, pioneer, pubkeys, context) => {
|
|
1008
|
-
try {
|
|
1009
|
-
const balances = [];
|
|
1010
|
-
console.log(tag5, "init");
|
|
1011
|
-
const params = {
|
|
1012
|
-
blockchains,
|
|
1013
|
-
pioneer,
|
|
1014
|
-
pubkeys,
|
|
1015
|
-
context
|
|
1016
|
-
};
|
|
1017
|
-
const evmBalances = await getEvmCharts(params);
|
|
1018
|
-
balances.push(...evmBalances);
|
|
1019
|
-
const mayaBalances = await getMayaCharts(params, balances);
|
|
1020
|
-
balances.push(...mayaBalances);
|
|
1021
|
-
const stakingBalances = await getCosmosStakingCharts(params);
|
|
1022
|
-
balances.push(...stakingBalances);
|
|
1023
|
-
return balances;
|
|
1024
|
-
} catch (error) {
|
|
1025
|
-
console.error(tag5, "Error processing charts:", error);
|
|
1026
|
-
throw error;
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
|
|
1030
569
|
// src/getPubkey.ts
|
|
1031
570
|
import { NetworkIdToChain } from "@pioneer-platform/pioneer-caip";
|
|
1032
571
|
import {
|
|
@@ -1595,7 +1134,7 @@ async function fetchTokenPriceInUsd(pioneer, caip) {
|
|
|
1595
1134
|
}
|
|
1596
1135
|
}
|
|
1597
1136
|
async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, feeLevel = 5) {
|
|
1598
|
-
const
|
|
1137
|
+
const tag = TAG2 + " | createUnsignedEvmTx | ";
|
|
1599
1138
|
try {
|
|
1600
1139
|
if (!pioneer)
|
|
1601
1140
|
throw new Error("Failed to initialize Pioneer");
|
|
@@ -1616,11 +1155,11 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1616
1155
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
1617
1156
|
}
|
|
1618
1157
|
const address = pubkeyContext.address || pubkeyContext.pubkey;
|
|
1619
|
-
console.log(
|
|
1158
|
+
console.log(tag, "✅ Using FROM address from pubkeyContext:", address, "note:", pubkeyContext.note);
|
|
1620
1159
|
if (!address)
|
|
1621
1160
|
throw new Error("No address found for the specified network");
|
|
1622
1161
|
const gasPriceData = await pioneer.GetGasPriceByNetwork({ networkId });
|
|
1623
|
-
console.log(
|
|
1162
|
+
console.log(tag, "Gas price data from API:", JSON.stringify(gasPriceData.data));
|
|
1624
1163
|
let gasPrice;
|
|
1625
1164
|
const defaultGasPrices = {
|
|
1626
1165
|
1: 30,
|
|
@@ -1636,75 +1175,75 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1636
1175
|
let selectedGasPrice;
|
|
1637
1176
|
if (feeLevel <= 2) {
|
|
1638
1177
|
selectedGasPrice = gasPriceData.data.slow || gasPriceData.data.average || gasPriceData.data.fastest;
|
|
1639
|
-
console.log(
|
|
1178
|
+
console.log(tag, "Selecting SLOW gas price from API");
|
|
1640
1179
|
} else if (feeLevel >= 8) {
|
|
1641
1180
|
selectedGasPrice = gasPriceData.data.fastest || gasPriceData.data.fast || gasPriceData.data.average;
|
|
1642
|
-
console.log(
|
|
1181
|
+
console.log(tag, "Selecting FAST gas price from API");
|
|
1643
1182
|
} else {
|
|
1644
1183
|
selectedGasPrice = gasPriceData.data.average || gasPriceData.data.fast || gasPriceData.data.fastest;
|
|
1645
|
-
console.log(
|
|
1184
|
+
console.log(tag, "Selecting AVERAGE gas price from API");
|
|
1646
1185
|
}
|
|
1647
1186
|
let gasPriceNum;
|
|
1648
1187
|
if (selectedGasPrice === undefined || selectedGasPrice === null) {
|
|
1649
|
-
console.warn(
|
|
1188
|
+
console.warn(tag, "No valid gas price found in API response, using fallback:", fallbackGasGwei, "gwei");
|
|
1650
1189
|
gasPriceNum = fallbackGasGwei;
|
|
1651
1190
|
} else {
|
|
1652
1191
|
gasPriceNum = typeof selectedGasPrice === "string" ? parseFloat(selectedGasPrice) : selectedGasPrice;
|
|
1653
1192
|
if (isNaN(gasPriceNum) || !isFinite(gasPriceNum)) {
|
|
1654
|
-
console.warn(
|
|
1193
|
+
console.warn(tag, "Invalid gas price (NaN or Infinite):", selectedGasPrice, "- using fallback:", fallbackGasGwei, "gwei");
|
|
1655
1194
|
gasPriceNum = fallbackGasGwei;
|
|
1656
1195
|
}
|
|
1657
1196
|
}
|
|
1658
1197
|
gasPrice = BigInt(Math.round(gasPriceNum * 1e9));
|
|
1659
1198
|
if (gasPrice < MIN_GAS_PRICE_WEI) {
|
|
1660
|
-
console.warn(
|
|
1199
|
+
console.warn(tag, "Gas price from API too low:", gasPrice.toString(), "wei - using minimum:", MIN_GAS_PRICE_WEI.toString());
|
|
1661
1200
|
gasPrice = MIN_GAS_PRICE_WEI;
|
|
1662
1201
|
}
|
|
1663
1202
|
} else {
|
|
1664
1203
|
let gasPriceNum;
|
|
1665
1204
|
if (gasPriceData.data === undefined || gasPriceData.data === null) {
|
|
1666
|
-
console.warn(
|
|
1205
|
+
console.warn(tag, "Gas price API returned null/undefined, using fallback:", fallbackGasGwei, "gwei");
|
|
1667
1206
|
gasPriceNum = fallbackGasGwei;
|
|
1668
1207
|
} else {
|
|
1669
1208
|
gasPriceNum = typeof gasPriceData.data === "string" ? parseFloat(gasPriceData.data) : gasPriceData.data;
|
|
1670
1209
|
if (isNaN(gasPriceNum) || !isFinite(gasPriceNum)) {
|
|
1671
|
-
console.warn(
|
|
1210
|
+
console.warn(tag, "Invalid gas price (NaN or Infinite):", gasPriceData.data, "- using fallback:", fallbackGasGwei, "gwei");
|
|
1672
1211
|
gasPriceNum = fallbackGasGwei;
|
|
1673
1212
|
}
|
|
1674
1213
|
}
|
|
1675
1214
|
const baseGasPrice = BigInt(Math.round(gasPriceNum * 1e9));
|
|
1676
1215
|
if (feeLevel <= 2) {
|
|
1677
1216
|
gasPrice = baseGasPrice * BigInt(80) / BigInt(100);
|
|
1678
|
-
console.log(
|
|
1217
|
+
console.log(tag, "Using SLOW gas price (80% of base)");
|
|
1679
1218
|
} else if (feeLevel >= 8) {
|
|
1680
1219
|
gasPrice = baseGasPrice * BigInt(150) / BigInt(100);
|
|
1681
|
-
console.log(
|
|
1220
|
+
console.log(tag, "Using FAST gas price (150% of base)");
|
|
1682
1221
|
} else {
|
|
1683
1222
|
gasPrice = baseGasPrice;
|
|
1684
|
-
console.log(
|
|
1223
|
+
console.log(tag, "Using AVERAGE gas price (100% of base)");
|
|
1685
1224
|
}
|
|
1686
1225
|
if (gasPrice < MIN_GAS_PRICE_WEI) {
|
|
1687
|
-
console.warn(
|
|
1226
|
+
console.warn(tag, "Gas price too low:", gasPrice.toString(), "wei - using minimum:", MIN_GAS_PRICE_WEI.toString());
|
|
1688
1227
|
gasPrice = MIN_GAS_PRICE_WEI;
|
|
1689
1228
|
}
|
|
1690
1229
|
}
|
|
1691
|
-
console.log(
|
|
1230
|
+
console.log(tag, "Final gasPrice:", gasPrice.toString(), "wei (", Number(gasPrice) / 1e9, "gwei)");
|
|
1692
1231
|
let nonce;
|
|
1693
1232
|
try {
|
|
1694
1233
|
const nonceData = await pioneer.GetNonceByNetwork({ networkId, address });
|
|
1695
1234
|
nonce = nonceData.data.nonce;
|
|
1696
1235
|
if (nonce === undefined || nonce === null) {
|
|
1697
|
-
console.log(
|
|
1236
|
+
console.log(tag, "No nonce found for address (likely fresh address), defaulting to 0");
|
|
1698
1237
|
nonce = 0;
|
|
1699
1238
|
}
|
|
1700
1239
|
} catch (nonceError) {
|
|
1701
|
-
console.log(
|
|
1240
|
+
console.log(tag, "Failed to fetch nonce (likely fresh address):", nonceError.message, "- defaulting to 0");
|
|
1702
1241
|
nonce = 0;
|
|
1703
1242
|
}
|
|
1704
1243
|
const balanceData = await pioneer.GetBalanceAddressByNetwork({ networkId, address });
|
|
1705
1244
|
const balanceEth = parseFloat(balanceData.data.nativeBalance || balanceData.data.balance || "0");
|
|
1706
1245
|
const balance = BigInt(Math.round(balanceEth * 1000000000000000000));
|
|
1707
|
-
console.log(
|
|
1246
|
+
console.log(tag, "Native ETH balance from API:", balanceData.data.nativeBalance || balanceData.data.balance, "ETH (", balance.toString(), "wei)");
|
|
1708
1247
|
if (balance <= 0n)
|
|
1709
1248
|
throw new Error("Wallet balance is zero");
|
|
1710
1249
|
const assetType = classifyCaipEvm(caip);
|
|
@@ -1717,7 +1256,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1717
1256
|
let gasLimit;
|
|
1718
1257
|
if (isThorchainOperation) {
|
|
1719
1258
|
gasLimit = BigInt(120000);
|
|
1720
|
-
console.log(
|
|
1259
|
+
console.log(tag, "Using higher gas limit for THORChain swap:", gasLimit.toString());
|
|
1721
1260
|
} else {
|
|
1722
1261
|
gasLimit = chainId === 1 ? BigInt(21000) : BigInt(25000);
|
|
1723
1262
|
}
|
|
@@ -1733,7 +1272,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1733
1272
|
}
|
|
1734
1273
|
const buffer = BigInt(100);
|
|
1735
1274
|
amountWei = balance - gasFee - buffer;
|
|
1736
|
-
console.log(
|
|
1275
|
+
console.log(tag, "isMax calculation - balance:", balance.toString(), "gasFee:", gasFee.toString(), "buffer:", buffer.toString(), "amountWei:", amountWei.toString());
|
|
1737
1276
|
} else {
|
|
1738
1277
|
amountWei = BigInt(Math.round(amount * 1000000000000000000));
|
|
1739
1278
|
if (amountWei + gasFee > balance) {
|
|
@@ -1743,14 +1282,14 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1743
1282
|
const isThorchainSwap = memo && (memo.startsWith("=") || memo.startsWith("SWAP") || memo.includes(":"));
|
|
1744
1283
|
let txData = "0x";
|
|
1745
1284
|
if (isThorchainSwap) {
|
|
1746
|
-
console.log(
|
|
1285
|
+
console.log(tag, "Detected THORChain swap, encoding deposit data for memo:", memo);
|
|
1747
1286
|
let fixedMemo = memo;
|
|
1748
1287
|
if (memo.startsWith("=:b:") || memo.startsWith("=:btc:")) {
|
|
1749
1288
|
fixedMemo = memo.replace(/^=:(b|btc):/, "=:BTC.BTC:");
|
|
1750
|
-
console.log(
|
|
1289
|
+
console.log(tag, "Fixed Bitcoin swap memo from:", memo, "to:", fixedMemo);
|
|
1751
1290
|
} else if (memo.startsWith("=:e:") || memo.startsWith("=:eth:")) {
|
|
1752
1291
|
fixedMemo = memo.replace(/^=:(e|eth):/, "=:ETH.ETH:");
|
|
1753
|
-
console.log(
|
|
1292
|
+
console.log(tag, "Fixed Ethereum swap memo from:", memo, "to:", fixedMemo);
|
|
1754
1293
|
}
|
|
1755
1294
|
if (fixedMemo.length > 250) {
|
|
1756
1295
|
throw new Error(`Memo too long for THORChain: ${fixedMemo.length} bytes (max 250)`);
|
|
@@ -1766,14 +1305,14 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1766
1305
|
if (ethInbound) {
|
|
1767
1306
|
vaultAddress = ethInbound.address;
|
|
1768
1307
|
routerAddress = ethInbound.router || to;
|
|
1769
|
-
console.log(
|
|
1308
|
+
console.log(tag, "Using THORChain inbound addresses - vault:", vaultAddress, "router:", routerAddress);
|
|
1770
1309
|
to = routerAddress;
|
|
1771
1310
|
} else {
|
|
1772
1311
|
throw new Error("ETH inbound is halted or not found - cannot proceed with swap");
|
|
1773
1312
|
}
|
|
1774
1313
|
}
|
|
1775
1314
|
} catch (fetchError) {
|
|
1776
|
-
console.error(
|
|
1315
|
+
console.error(tag, "Failed to fetch inbound addresses:", fetchError);
|
|
1777
1316
|
throw new Error(`Cannot proceed with THORChain swap - failed to fetch inbound addresses: ${fetchError.message}`);
|
|
1778
1317
|
}
|
|
1779
1318
|
if (vaultAddress === "0x0000000000000000000000000000000000000000") {
|
|
@@ -1793,7 +1332,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1793
1332
|
const paddingLength = (32 - memoBytes.length % 32) % 32;
|
|
1794
1333
|
const memoPadded = memoHex + "0".repeat(paddingLength * 2);
|
|
1795
1334
|
txData = "0x" + functionSelector + vaultPadded + assetPadded + amountPadded + stringOffset + expiryPadded + stringLength + memoPadded;
|
|
1796
|
-
console.log(
|
|
1335
|
+
console.log(tag, "Encoded THORChain depositWithExpiry data:", {
|
|
1797
1336
|
functionSelector: "0x" + functionSelector,
|
|
1798
1337
|
vault: vaultAddress,
|
|
1799
1338
|
asset: assetAddress,
|
|
@@ -1803,9 +1342,9 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1803
1342
|
stringOffset: "0x" + stringOffset,
|
|
1804
1343
|
fullData: txData
|
|
1805
1344
|
});
|
|
1806
|
-
console.log(
|
|
1345
|
+
console.log(tag, "Native ETH swap - value will be set to:", amountWei.toString(), "wei");
|
|
1807
1346
|
} catch (error) {
|
|
1808
|
-
console.error(
|
|
1347
|
+
console.error(tag, "Error encoding THORChain deposit:", error);
|
|
1809
1348
|
throw new Error(`Failed to encode THORChain swap: ${error.message}`);
|
|
1810
1349
|
}
|
|
1811
1350
|
} else if (memo) {
|
|
@@ -1824,20 +1363,18 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1824
1363
|
}
|
|
1825
1364
|
case "erc20": {
|
|
1826
1365
|
const contractAddress = extractContractAddressFromCaip(caip);
|
|
1827
|
-
console.log(
|
|
1366
|
+
console.log(tag, "Fetching token decimals via pioneer-server API for", contractAddress, "on", networkId);
|
|
1828
1367
|
let tokenDecimals;
|
|
1829
1368
|
try {
|
|
1830
|
-
console.log(tag6, "Fetching token decimals via pioneer-server API for", contractAddress, "on", networkId);
|
|
1831
1369
|
const decimalsResponse = await pioneer.GetTokenDecimals({
|
|
1832
1370
|
networkId,
|
|
1833
1371
|
contractAddress
|
|
1834
1372
|
});
|
|
1835
1373
|
tokenDecimals = Number(decimalsResponse.data.decimals);
|
|
1836
|
-
console.log(
|
|
1374
|
+
console.log(tag, "✅ Fetched decimals from pioneer-server:", tokenDecimals);
|
|
1837
1375
|
} catch (error) {
|
|
1838
|
-
console.error(
|
|
1839
|
-
|
|
1840
|
-
tokenDecimals = 18;
|
|
1376
|
+
console.error(tag, "❌ CRITICAL ERROR: Failed to fetch token decimals from pioneer-server:", error);
|
|
1377
|
+
throw new Error(`Cannot build transaction: Failed to fetch decimals for token ${contractAddress} on ${networkId}. Error: ${error.message}`);
|
|
1841
1378
|
}
|
|
1842
1379
|
const tokenMultiplier = 10n ** BigInt(tokenDecimals);
|
|
1843
1380
|
let gasLimit = BigInt(1e5);
|
|
@@ -1857,7 +1394,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1857
1394
|
amountWei = tokenBalance;
|
|
1858
1395
|
} else {
|
|
1859
1396
|
amountWei = BigInt(Math.round(amount * Number(tokenMultiplier)));
|
|
1860
|
-
console.log(
|
|
1397
|
+
console.log(tag, "Token amount calculation:", {
|
|
1861
1398
|
inputAmount: amount,
|
|
1862
1399
|
decimals: tokenDecimals,
|
|
1863
1400
|
multiplier: tokenMultiplier,
|
|
@@ -1893,23 +1430,23 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1893
1430
|
}
|
|
1894
1431
|
if (pubkeyContext.addressNListMaster) {
|
|
1895
1432
|
unsignedTx.addressNList = pubkeyContext.addressNListMaster;
|
|
1896
|
-
console.log(
|
|
1433
|
+
console.log(tag, "✅ Using addressNListMaster from pubkey context:", unsignedTx.addressNList, "for address:", address);
|
|
1897
1434
|
} else if (pubkeyContext.pathMaster) {
|
|
1898
1435
|
unsignedTx.addressNList = bip32ToAddressNList(pubkeyContext.pathMaster);
|
|
1899
|
-
console.log(
|
|
1436
|
+
console.log(tag, "✅ Converted pathMaster to addressNList:", pubkeyContext.pathMaster, "→", unsignedTx.addressNList);
|
|
1900
1437
|
} else if (pubkeyContext.addressNList) {
|
|
1901
1438
|
unsignedTx.addressNList = pubkeyContext.addressNList;
|
|
1902
|
-
console.log(
|
|
1439
|
+
console.log(tag, "✅ Using addressNList from pubkey context:", unsignedTx.addressNList);
|
|
1903
1440
|
} else if (pubkeyContext.path) {
|
|
1904
1441
|
unsignedTx.addressNList = bip32ToAddressNList(pubkeyContext.path);
|
|
1905
|
-
console.log(
|
|
1442
|
+
console.log(tag, "⚠️ Using regular path (not master):", pubkeyContext.path, "→", unsignedTx.addressNList);
|
|
1906
1443
|
} else {
|
|
1907
1444
|
unsignedTx.addressNList = [2147483648 + 44, 2147483648 + 60, 2147483648, 0, 0];
|
|
1908
|
-
console.warn(
|
|
1445
|
+
console.warn(tag, "⚠️ No path info in pubkey context, using default account 0");
|
|
1909
1446
|
}
|
|
1910
1447
|
return unsignedTx;
|
|
1911
1448
|
} catch (error) {
|
|
1912
|
-
console.error(
|
|
1449
|
+
console.error(tag, "Error:", error.message);
|
|
1913
1450
|
throw error;
|
|
1914
1451
|
}
|
|
1915
1452
|
}
|
|
@@ -1918,7 +1455,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
1918
1455
|
import { caipToNetworkId as caipToNetworkId2 } from "@pioneer-platform/pioneer-caip";
|
|
1919
1456
|
var TAG3 = " | createUnsignedUxtoTx | ";
|
|
1920
1457
|
async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax) {
|
|
1921
|
-
let
|
|
1458
|
+
let tag = TAG3 + " | createUnsignedRippleTx | ";
|
|
1922
1459
|
try {
|
|
1923
1460
|
if (!pioneer)
|
|
1924
1461
|
throw new Error("Failed to init! pioneer");
|
|
@@ -1929,7 +1466,7 @@ async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer,
|
|
|
1929
1466
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
1930
1467
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
1931
1468
|
}
|
|
1932
|
-
console.log(
|
|
1469
|
+
console.log(tag, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
1933
1470
|
address: pubkeyContext.address
|
|
1934
1471
|
});
|
|
1935
1472
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
@@ -1997,7 +1534,7 @@ async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer,
|
|
|
1997
1534
|
};
|
|
1998
1535
|
return unsignedTx;
|
|
1999
1536
|
} catch (error) {
|
|
2000
|
-
console.error(
|
|
1537
|
+
console.error(tag, "Error:", error);
|
|
2001
1538
|
throw error;
|
|
2002
1539
|
}
|
|
2003
1540
|
}
|
|
@@ -2137,7 +1674,7 @@ var thorchainDepositTemplate = (params) => ({
|
|
|
2137
1674
|
// src/txbuilder/createUnsignedTendermintTx.ts
|
|
2138
1675
|
var TAG4 = " | createUnsignedTendermintTx | ";
|
|
2139
1676
|
async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, to) {
|
|
2140
|
-
const
|
|
1677
|
+
const tag = TAG4 + " | createUnsignedTendermintTx | ";
|
|
2141
1678
|
try {
|
|
2142
1679
|
if (!pioneer)
|
|
2143
1680
|
throw new Error("Failed to init! pioneer");
|
|
@@ -2148,7 +1685,7 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2148
1685
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
2149
1686
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2150
1687
|
}
|
|
2151
|
-
console.log(
|
|
1688
|
+
console.log(tag, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
2152
1689
|
address: pubkeyContext.address,
|
|
2153
1690
|
addressNList: pubkeyContext.addressNList || pubkeyContext.addressNListMaster
|
|
2154
1691
|
});
|
|
@@ -2171,11 +1708,11 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2171
1708
|
}
|
|
2172
1709
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
2173
1710
|
let asset = caip.split(":")[1];
|
|
2174
|
-
console.log(
|
|
1711
|
+
console.log(tag, `\uD83D\uDD0D Fetching account info for address: ${fromAddress}`);
|
|
2175
1712
|
const accountInfo = (await pioneer.GetAccountInfo({ network: chain, address: fromAddress })).data;
|
|
2176
|
-
console.log(
|
|
1713
|
+
console.log(tag, "\uD83D\uDCCB accountInfo:", JSON.stringify(accountInfo, null, 2));
|
|
2177
1714
|
let balanceInfo = await pioneer.GetPubkeyBalance({ asset: chain, pubkey: fromAddress });
|
|
2178
|
-
console.log(
|
|
1715
|
+
console.log(tag, `\uD83D\uDCB0 balanceInfo:`, balanceInfo);
|
|
2179
1716
|
let account_number, sequence;
|
|
2180
1717
|
if (accountInfo.account) {
|
|
2181
1718
|
account_number = accountInfo.account.account_number || "0";
|
|
@@ -2186,13 +1723,13 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2186
1723
|
} else {
|
|
2187
1724
|
throw new Error(`Unexpected account info format for ${networkId}: ${JSON.stringify(accountInfo)}`);
|
|
2188
1725
|
}
|
|
2189
|
-
console.log(
|
|
1726
|
+
console.log(tag, `\uD83D\uDCCA Extracted account_number: ${account_number}, sequence: ${sequence}`);
|
|
2190
1727
|
if (account_number === "0" || account_number === 0) {
|
|
2191
|
-
console.log(
|
|
2192
|
-
console.log(
|
|
2193
|
-
console.log(
|
|
2194
|
-
console.log(
|
|
2195
|
-
console.log(
|
|
1728
|
+
console.log(tag, `⚠️ WARNING: Account number is 0 from Pioneer API`);
|
|
1729
|
+
console.log(tag, ` This is likely due to stale Pioneer API cache`);
|
|
1730
|
+
console.log(tag, ` The mayachain-network module queries nodes directly but Pioneer API may be cached`);
|
|
1731
|
+
console.log(tag, ` Proceeding with account_number: 0 but transaction will likely fail`);
|
|
1732
|
+
console.log(tag, ` TODO: Fix Pioneer API to use fresh data from mayachain-network module`);
|
|
2196
1733
|
}
|
|
2197
1734
|
const fees = {
|
|
2198
1735
|
"cosmos:thorchain-mainnet-v1": 0.02,
|
|
@@ -2347,7 +1884,7 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2347
1884
|
}
|
|
2348
1885
|
}
|
|
2349
1886
|
} catch (error) {
|
|
2350
|
-
console.error(
|
|
1887
|
+
console.error(tag, "Error:", error);
|
|
2351
1888
|
throw error;
|
|
2352
1889
|
}
|
|
2353
1890
|
}
|
|
@@ -2376,7 +1913,7 @@ function getCoinTypeFromNetworkId(networkId) {
|
|
|
2376
1913
|
return coinType;
|
|
2377
1914
|
}
|
|
2378
1915
|
async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, feeLevel = 5, changeScriptType) {
|
|
2379
|
-
let
|
|
1916
|
+
let tag = " | createUnsignedUxtoTx | ";
|
|
2380
1917
|
try {
|
|
2381
1918
|
if (!pioneer)
|
|
2382
1919
|
throw Error("Failed to init! pioneer");
|
|
@@ -2387,7 +1924,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2387
1924
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
2388
1925
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2389
1926
|
}
|
|
2390
|
-
console.log(
|
|
1927
|
+
console.log(tag, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
2391
1928
|
address: pubkeyContext.address,
|
|
2392
1929
|
scriptType: pubkeyContext.scriptType
|
|
2393
1930
|
});
|
|
@@ -2398,15 +1935,15 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2398
1935
|
const isSegwit = segwitNetworks.includes(networkId);
|
|
2399
1936
|
let chain = NetworkIdToChain2[networkId];
|
|
2400
1937
|
const coinType = getCoinTypeFromNetworkId(networkId);
|
|
2401
|
-
console.log(`${
|
|
1938
|
+
console.log(`${tag}: Using SLIP-44 coin type ${coinType} for ${chain}`);
|
|
2402
1939
|
const utxos = [];
|
|
2403
1940
|
for (const pubkey of relevantPubkeys) {
|
|
2404
1941
|
try {
|
|
2405
1942
|
let utxosResp = await pioneer.ListUnspent({ network: chain, xpub: pubkey.pubkey });
|
|
2406
1943
|
utxosResp = utxosResp.data;
|
|
2407
|
-
console.log(`${
|
|
1944
|
+
console.log(`${tag}: ListUnspent response for ${pubkey.scriptType}:`, utxosResp?.length || 0, "UTXOs");
|
|
2408
1945
|
if (!utxosResp || !Array.isArray(utxosResp)) {
|
|
2409
|
-
console.warn(`${
|
|
1946
|
+
console.warn(`${tag}: Invalid or empty UTXO response for ${pubkey.pubkey}`);
|
|
2410
1947
|
continue;
|
|
2411
1948
|
}
|
|
2412
1949
|
let scriptType = pubkey.scriptType;
|
|
@@ -2415,10 +1952,10 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2415
1952
|
}
|
|
2416
1953
|
utxos.push(...utxosResp);
|
|
2417
1954
|
} catch (error) {
|
|
2418
|
-
console.error(`${
|
|
1955
|
+
console.error(`${tag}: Failed to fetch UTXOs for ${pubkey.pubkey}:`, error);
|
|
2419
1956
|
}
|
|
2420
1957
|
}
|
|
2421
|
-
console.log(`${
|
|
1958
|
+
console.log(`${tag}: Total UTXOs collected:`, utxos.length);
|
|
2422
1959
|
if (!utxos || utxos.length === 0)
|
|
2423
1960
|
throw Error("No UTXOs found across all addresses");
|
|
2424
1961
|
for (const utxo of utxos) {
|
|
@@ -2431,14 +1968,14 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2431
1968
|
}, {});
|
|
2432
1969
|
const mostCommonInputType = Object.entries(scriptTypeCount).sort(([, a2], [, b2]) => b2 - a2)[0]?.[0] || "p2pkh";
|
|
2433
1970
|
const actualChangeScriptType = changeScriptType || mostCommonInputType || relevantPubkeys[0]?.scriptType || "p2pkh";
|
|
2434
|
-
console.log(`${
|
|
2435
|
-
console.log(`${
|
|
1971
|
+
console.log(`${tag}: Input script types:`, scriptTypeCount);
|
|
1972
|
+
console.log(`${tag}: Using change script type: ${actualChangeScriptType} (matches inputs: ${mostCommonInputType})`);
|
|
2436
1973
|
const changeXpubInfo = relevantPubkeys.find((pk) => pk.scriptType === actualChangeScriptType);
|
|
2437
1974
|
if (!changeXpubInfo) {
|
|
2438
1975
|
throw new Error(`No ${actualChangeScriptType} xpub available for change address. ` + `Available types: ${relevantPubkeys.map((pk) => pk.scriptType).join(", ")}. ` + `Cannot create change output with mismatched script type.`);
|
|
2439
1976
|
}
|
|
2440
1977
|
const changeXpub = changeXpubInfo.pubkey;
|
|
2441
|
-
console.log(`${
|
|
1978
|
+
console.log(`${tag}: Change xpub selected for ${actualChangeScriptType}:`, changeXpub?.substring(0, 10) + "...");
|
|
2442
1979
|
let changeAddressIndex = await pioneer.GetChangeAddress({
|
|
2443
1980
|
network: chain,
|
|
2444
1981
|
xpub: changeXpub
|
|
@@ -2458,7 +1995,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2458
1995
|
break;
|
|
2459
1996
|
}
|
|
2460
1997
|
const path = bipPath;
|
|
2461
|
-
console.log(`${
|
|
1998
|
+
console.log(`${tag}: Change address path: ${path} (coin type: ${coinType}, index: ${changeAddressIndex})`);
|
|
2462
1999
|
const changeAddress = {
|
|
2463
2000
|
path,
|
|
2464
2001
|
isChange: true,
|
|
@@ -2468,7 +2005,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2468
2005
|
};
|
|
2469
2006
|
let feeRateFromNode;
|
|
2470
2007
|
if (networkId === "bip122:00000000001a91e3dace36e2be3bf030") {
|
|
2471
|
-
console.log(`${
|
|
2008
|
+
console.log(`${tag}: Using hardcoded fees for DOGE (10 sat/byte)`);
|
|
2472
2009
|
feeRateFromNode = {
|
|
2473
2010
|
slow: 10,
|
|
2474
2011
|
average: 10,
|
|
@@ -2481,19 +2018,19 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2481
2018
|
try {
|
|
2482
2019
|
let feeResponse;
|
|
2483
2020
|
if (pioneer.GetFeeRateByNetwork) {
|
|
2484
|
-
console.log(`${
|
|
2021
|
+
console.log(`${tag}: Trying GetFeeRateByNetwork for ${networkId}`);
|
|
2485
2022
|
feeResponse = await pioneer.GetFeeRateByNetwork({ networkId });
|
|
2486
2023
|
} else {
|
|
2487
|
-
console.log(`${
|
|
2024
|
+
console.log(`${tag}: Using GetFeeRate for ${networkId}`);
|
|
2488
2025
|
feeResponse = await pioneer.GetFeeRate({ networkId });
|
|
2489
2026
|
}
|
|
2490
2027
|
feeRateFromNode = feeResponse.data;
|
|
2491
|
-
console.log(`${
|
|
2028
|
+
console.log(`${tag}: Got fee rates from API:`, JSON.stringify(feeRateFromNode, null, 2));
|
|
2492
2029
|
const conversionThreshold = 500;
|
|
2493
2030
|
const needsConversion = feeRateFromNode.slow && feeRateFromNode.slow > conversionThreshold || feeRateFromNode.average && feeRateFromNode.average > conversionThreshold || feeRateFromNode.fast && feeRateFromNode.fast > conversionThreshold || feeRateFromNode.fastest && feeRateFromNode.fastest > conversionThreshold;
|
|
2494
2031
|
if (needsConversion) {
|
|
2495
|
-
console.warn(`${
|
|
2496
|
-
console.warn(`${
|
|
2032
|
+
console.warn(`${tag}: Detected wrong units - values appear to be in sat/kB instead of sat/byte`);
|
|
2033
|
+
console.warn(`${tag}: Original values:`, {
|
|
2497
2034
|
slow: feeRateFromNode.slow,
|
|
2498
2035
|
average: feeRateFromNode.average,
|
|
2499
2036
|
fast: feeRateFromNode.fast,
|
|
@@ -2507,12 +2044,12 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2507
2044
|
feeRateFromNode.fast = feeRateFromNode.fast / 1000;
|
|
2508
2045
|
if (feeRateFromNode.fastest)
|
|
2509
2046
|
feeRateFromNode.fastest = feeRateFromNode.fastest / 1000;
|
|
2510
|
-
console.warn(`${
|
|
2047
|
+
console.warn(`${tag}: Converted to sat/byte:`, feeRateFromNode);
|
|
2511
2048
|
}
|
|
2512
2049
|
if (!feeRateFromNode || typeof feeRateFromNode !== "object") {
|
|
2513
2050
|
throw new Error(`Invalid fee rate response from API: ${JSON.stringify(feeRateFromNode)}`);
|
|
2514
2051
|
}
|
|
2515
|
-
console.log(`${
|
|
2052
|
+
console.log(`${tag}: Available fee rates:`, {
|
|
2516
2053
|
slow: feeRateFromNode.slow,
|
|
2517
2054
|
average: feeRateFromNode.average,
|
|
2518
2055
|
fast: feeRateFromNode.fast,
|
|
@@ -2522,33 +2059,33 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2522
2059
|
throw new Error(`No valid fee rates in API response: ${JSON.stringify(feeRateFromNode)}`);
|
|
2523
2060
|
}
|
|
2524
2061
|
} catch (error) {
|
|
2525
|
-
console.error(`${
|
|
2062
|
+
console.error(`${tag}: Failed to get fee rates from Pioneer API:`, error.message || error);
|
|
2526
2063
|
throw new Error(`Unable to get fee rate for network ${networkId}: ${error.message || "API unavailable"}`);
|
|
2527
2064
|
}
|
|
2528
2065
|
}
|
|
2529
2066
|
let effectiveFeeRate;
|
|
2530
|
-
console.log(`${
|
|
2067
|
+
console.log(`${tag}: Using fee level ${feeLevel}`);
|
|
2531
2068
|
switch (feeLevel) {
|
|
2532
2069
|
case 1:
|
|
2533
2070
|
case 2:
|
|
2534
2071
|
effectiveFeeRate = feeRateFromNode.slow || feeRateFromNode.average;
|
|
2535
|
-
console.log(`${
|
|
2072
|
+
console.log(`${tag}: Using SLOW fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2536
2073
|
break;
|
|
2537
2074
|
case 3:
|
|
2538
2075
|
case 4:
|
|
2539
2076
|
effectiveFeeRate = feeRateFromNode.average || feeRateFromNode.fast;
|
|
2540
|
-
console.log(`${
|
|
2077
|
+
console.log(`${tag}: Using AVERAGE fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2541
2078
|
break;
|
|
2542
2079
|
case 5:
|
|
2543
2080
|
effectiveFeeRate = feeRateFromNode.fastest || feeRateFromNode.fast;
|
|
2544
|
-
console.log(`${
|
|
2081
|
+
console.log(`${tag}: Using FASTEST fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2545
2082
|
break;
|
|
2546
2083
|
default:
|
|
2547
2084
|
throw new Error(`Invalid fee level: ${feeLevel}. Must be 1-5`);
|
|
2548
2085
|
}
|
|
2549
2086
|
if (!effectiveFeeRate)
|
|
2550
2087
|
throw new Error("Unable to get fee rate for network");
|
|
2551
|
-
console.log(`${
|
|
2088
|
+
console.log(`${tag}: Using fee rate from API:`, {
|
|
2552
2089
|
feeLevel,
|
|
2553
2090
|
selectedRate: effectiveFeeRate,
|
|
2554
2091
|
description: feeLevel <= 2 ? "slow" : feeLevel <= 4 ? "average" : "fast"
|
|
@@ -2558,22 +2095,22 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2558
2095
|
effectiveFeeRate = Math.ceil(effectiveFeeRate);
|
|
2559
2096
|
const MIN_RELAY_FEE_RATE = 3;
|
|
2560
2097
|
if (effectiveFeeRate < MIN_RELAY_FEE_RATE) {
|
|
2561
|
-
console.log(`${
|
|
2098
|
+
console.log(`${tag}: Fee rate ${effectiveFeeRate} is below minimum relay fee, increasing to ${MIN_RELAY_FEE_RATE} sat/vB`);
|
|
2562
2099
|
effectiveFeeRate = MIN_RELAY_FEE_RATE;
|
|
2563
2100
|
}
|
|
2564
|
-
console.log(`${
|
|
2101
|
+
console.log(`${tag}: Final fee rate to use (rounded, with minimums): ${effectiveFeeRate} sat/vB`);
|
|
2565
2102
|
amount = parseInt(String(amount * 1e8));
|
|
2566
2103
|
if (amount <= 0 && !isMax)
|
|
2567
2104
|
throw Error("Invalid amount! 0");
|
|
2568
2105
|
const totalBalance = utxos.reduce((sum, utxo) => sum + utxo.value, 0);
|
|
2569
|
-
console.log(`${
|
|
2106
|
+
console.log(`${tag}: Coin selection inputs:`, {
|
|
2570
2107
|
utxoCount: utxos.length,
|
|
2571
2108
|
totalBalance: totalBalance / 1e8,
|
|
2572
2109
|
requestedAmount: amount / 1e8,
|
|
2573
2110
|
isMax,
|
|
2574
2111
|
feeRate: effectiveFeeRate
|
|
2575
2112
|
});
|
|
2576
|
-
console.log(`${
|
|
2113
|
+
console.log(`${tag}: UTXO details for coin selection:`, utxos.map((u) => ({
|
|
2577
2114
|
value: u.value,
|
|
2578
2115
|
txid: u.txid?.substring(0, 10) + "...",
|
|
2579
2116
|
vout: u.vout,
|
|
@@ -2587,8 +2124,8 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2587
2124
|
} else {
|
|
2588
2125
|
result = import_coinselect.default(utxos, [{ address: to, value: amount }], effectiveFeeRate);
|
|
2589
2126
|
}
|
|
2590
|
-
console.log(
|
|
2591
|
-
console.log(
|
|
2127
|
+
console.log(tag, "coinSelect result object:", result);
|
|
2128
|
+
console.log(tag, "coinSelect result.inputs:", result?.inputs);
|
|
2592
2129
|
if (!result || !result.inputs) {
|
|
2593
2130
|
const errorDetails = {
|
|
2594
2131
|
utxoCount: utxos.length,
|
|
@@ -2597,7 +2134,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2597
2134
|
feeRate: effectiveFeeRate,
|
|
2598
2135
|
insufficientFunds: totalBalance < amount
|
|
2599
2136
|
};
|
|
2600
|
-
console.error(`${
|
|
2137
|
+
console.error(`${tag}: Coin selection failed:`, errorDetails);
|
|
2601
2138
|
if (utxos.length === 0) {
|
|
2602
2139
|
throw Error("No UTXOs available for coin selection");
|
|
2603
2140
|
} else if (totalBalance < amount) {
|
|
@@ -2613,7 +2150,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2613
2150
|
throw Error("Failed to create transaction: Missing outputs");
|
|
2614
2151
|
if (fee === undefined)
|
|
2615
2152
|
throw Error("Failed to calculate transaction fee");
|
|
2616
|
-
console.log(`${
|
|
2153
|
+
console.log(`${tag}: Transaction built with:`, {
|
|
2617
2154
|
feeLevel,
|
|
2618
2155
|
effectiveFeeRate: `${effectiveFeeRate} sat/vB`,
|
|
2619
2156
|
calculatedFee: `${fee} satoshis`,
|
|
@@ -2621,8 +2158,8 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2621
2158
|
outputCount: outputs.length
|
|
2622
2159
|
});
|
|
2623
2160
|
const uniqueInputSet = new Set;
|
|
2624
|
-
console.log(
|
|
2625
|
-
console.log(
|
|
2161
|
+
console.log(tag, "inputs:", inputs);
|
|
2162
|
+
console.log(tag, "inputs:", inputs[0]);
|
|
2626
2163
|
const preparedInputs = inputs.map(transformInput).filter(({ hash, index }) => uniqueInputSet.has(`${hash}:${index}`) ? false : uniqueInputSet.add(`${hash}:${index}`)).map(({ value, index, hash, txHex, path: path2, scriptType }) => ({
|
|
2627
2164
|
addressNList: bip32ToAddressNList2(path2),
|
|
2628
2165
|
scriptType,
|
|
@@ -2751,7 +2288,7 @@ class TransactionManager {
|
|
|
2751
2288
|
throw new Error(`Unsupported CAIP: ${caipString}`);
|
|
2752
2289
|
}
|
|
2753
2290
|
async transfer({ caip, to, amount, memo, isMax = false, feeLevel = 5, changeScriptType }) {
|
|
2754
|
-
let
|
|
2291
|
+
let tag = TAG5 + " | transfer | ";
|
|
2755
2292
|
try {
|
|
2756
2293
|
if (!this.pioneer)
|
|
2757
2294
|
throw Error("Failed to init! pioneer");
|
|
@@ -2789,12 +2326,12 @@ class TransactionManager {
|
|
|
2789
2326
|
}
|
|
2790
2327
|
return unsignedTx;
|
|
2791
2328
|
} catch (e) {
|
|
2792
|
-
console.error(
|
|
2329
|
+
console.error(tag, e);
|
|
2793
2330
|
throw e;
|
|
2794
2331
|
}
|
|
2795
2332
|
}
|
|
2796
2333
|
async sign({ caip, unsignedTx }) {
|
|
2797
|
-
let
|
|
2334
|
+
let tag = TAG5 + " | sign | ";
|
|
2798
2335
|
try {
|
|
2799
2336
|
if (!this.pioneer)
|
|
2800
2337
|
throw Error("Failed to init! pioneer");
|
|
@@ -2880,20 +2417,20 @@ class TransactionManager {
|
|
|
2880
2417
|
}
|
|
2881
2418
|
case "cosmos:mayachain-mainnet-v1/slip44:931":
|
|
2882
2419
|
case "cosmos:mayachain-mainnet-v1/denom:maya": {
|
|
2883
|
-
console.log(
|
|
2884
|
-
console.log(
|
|
2885
|
-
console.log(
|
|
2886
|
-
console.log(
|
|
2887
|
-
console.log(
|
|
2888
|
-
console.log(
|
|
2889
|
-
console.log(
|
|
2890
|
-
console.log(
|
|
2891
|
-
console.log(
|
|
2420
|
+
console.log(tag, "\uD83D\uDD0D ===== MAYACHAIN SIGNING AUDIT =====");
|
|
2421
|
+
console.log(tag, "\uD83D\uDCCB unsignedTx:", JSON.stringify(unsignedTx, null, 2));
|
|
2422
|
+
console.log(tag, "\uD83D\uDD11 unsignedTx.addressNList:", unsignedTx.addressNList);
|
|
2423
|
+
console.log(tag, "\uD83D\uDCCD unsignedTx.signerAddress:", unsignedTx.signerAddress);
|
|
2424
|
+
console.log(tag, "\uD83C\uDF10 pubkeyContext:", this.pubkeyContext);
|
|
2425
|
+
console.log(tag, "\uD83D\uDD10 pubkeyContext.addressNList:", this.pubkeyContext?.addressNList);
|
|
2426
|
+
console.log(tag, "\uD83D\uDD10 pubkeyContext.addressNListMaster:", this.pubkeyContext?.addressNListMaster);
|
|
2427
|
+
console.log(tag, "\uD83D\uDCEC pubkeyContext.address:", this.pubkeyContext?.address);
|
|
2428
|
+
console.log(tag, "=======================================");
|
|
2892
2429
|
if (unsignedTx.signDoc.msgs[0].type === "mayachain/MsgSend") {
|
|
2893
2430
|
const responseSign = await this.keepKeySdk.mayachain.mayachainSignAminoTransfer(unsignedTx);
|
|
2894
2431
|
signedTx = responseSign.serialized;
|
|
2895
|
-
console.log(
|
|
2896
|
-
console.log(
|
|
2432
|
+
console.log(tag, "✅ Signing completed");
|
|
2433
|
+
console.log(tag, "\uD83D\uDCE6 responseSign:", responseSign);
|
|
2897
2434
|
} else if (unsignedTx.signDoc.msgs[0].type === "mayachain/MsgDeposit") {
|
|
2898
2435
|
const responseSign = await this.keepKeySdk.mayachain.mayachainSignAminoDeposit(unsignedTx);
|
|
2899
2436
|
signedTx = responseSign.serialized;
|
|
@@ -2915,11 +2452,11 @@ class TransactionManager {
|
|
|
2915
2452
|
if (serialized.length > 140) {
|
|
2916
2453
|
signedTx = serialized;
|
|
2917
2454
|
} else {
|
|
2918
|
-
console.error(
|
|
2455
|
+
console.error(tag, "EIP155 signing returned incomplete transaction - only signature components");
|
|
2919
2456
|
throw new Error("KeepKey returned incomplete transaction - cannot reconstruct without r,s,v components");
|
|
2920
2457
|
}
|
|
2921
2458
|
} else {
|
|
2922
|
-
console.error(
|
|
2459
|
+
console.error(tag, "EIP155 signing failed - no valid signature in response:", responseSign);
|
|
2923
2460
|
throw new Error("Failed to sign transaction - no valid signature in response");
|
|
2924
2461
|
}
|
|
2925
2462
|
break;
|
|
@@ -2940,19 +2477,19 @@ class TransactionManager {
|
|
|
2940
2477
|
}
|
|
2941
2478
|
}
|
|
2942
2479
|
if (!signedTx) {
|
|
2943
|
-
console.error(
|
|
2944
|
-
console.error(
|
|
2945
|
-
console.error(
|
|
2480
|
+
console.error(tag, "CRITICAL ERROR: signedTx is missing after signing process");
|
|
2481
|
+
console.error(tag, "CAIP:", caip);
|
|
2482
|
+
console.error(tag, "Type:", type);
|
|
2946
2483
|
throw Error("Failed to sign! missing signedTx");
|
|
2947
2484
|
}
|
|
2948
2485
|
return signedTx;
|
|
2949
2486
|
} catch (e) {
|
|
2950
|
-
console.error(
|
|
2487
|
+
console.error(tag, e);
|
|
2951
2488
|
throw e;
|
|
2952
2489
|
}
|
|
2953
2490
|
}
|
|
2954
2491
|
async broadcast({ networkId, serialized }) {
|
|
2955
|
-
let
|
|
2492
|
+
let tag = TAG5 + " | broadcast | ";
|
|
2956
2493
|
try {
|
|
2957
2494
|
if (!this.pioneer)
|
|
2958
2495
|
throw Error("Failed to init! pioneer");
|
|
@@ -2966,7 +2503,7 @@ class TransactionManager {
|
|
|
2966
2503
|
return result.txid;
|
|
2967
2504
|
}
|
|
2968
2505
|
} catch (e) {
|
|
2969
|
-
console.error(
|
|
2506
|
+
console.error(tag, e);
|
|
2970
2507
|
throw e;
|
|
2971
2508
|
}
|
|
2972
2509
|
}
|
|
@@ -3086,7 +2623,7 @@ var cosmosClaimAllRewardsTemplate = (params) => ({
|
|
|
3086
2623
|
// src/txbuilder/createUnsignedStakingTx.ts
|
|
3087
2624
|
var TAG6 = " | createUnsignedStakingTx | ";
|
|
3088
2625
|
async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyContext) {
|
|
3089
|
-
const
|
|
2626
|
+
const tag = TAG6 + " | createUnsignedStakingTx | ";
|
|
3090
2627
|
try {
|
|
3091
2628
|
if (!pioneer)
|
|
3092
2629
|
throw new Error("Failed to init! pioneer");
|
|
@@ -3097,7 +2634,7 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3097
2634
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
3098
2635
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
3099
2636
|
}
|
|
3100
|
-
console.log(
|
|
2637
|
+
console.log(tag, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
3101
2638
|
address: pubkeyContext.address
|
|
3102
2639
|
});
|
|
3103
2640
|
let chain;
|
|
@@ -3129,10 +2666,10 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3129
2666
|
default:
|
|
3130
2667
|
throw new Error(`Unsupported networkId for staking: ${networkId}`);
|
|
3131
2668
|
}
|
|
3132
|
-
console.log(
|
|
2669
|
+
console.log(tag, `Building ${params.type} transaction for ${chain}`);
|
|
3133
2670
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
3134
2671
|
const accountInfo = (await pioneer.GetAccountInfo({ network: chain, address: fromAddress })).data;
|
|
3135
|
-
console.log(
|
|
2672
|
+
console.log(tag, "accountInfo: ", accountInfo);
|
|
3136
2673
|
let account_number;
|
|
3137
2674
|
let sequence;
|
|
3138
2675
|
if (networkId === "cosmos:cosmoshub-4" || networkId === "cosmos:osmosis-1") {
|
|
@@ -3214,7 +2751,7 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3214
2751
|
throw new Error(`Unsupported staking transaction type: ${params.type}`);
|
|
3215
2752
|
}
|
|
3216
2753
|
} catch (error) {
|
|
3217
|
-
console.error(
|
|
2754
|
+
console.error(tag, "Error:", error);
|
|
3218
2755
|
throw error;
|
|
3219
2756
|
}
|
|
3220
2757
|
}
|
|
@@ -3260,16 +2797,16 @@ function withTimeout(promise, timeoutMs) {
|
|
|
3260
2797
|
]);
|
|
3261
2798
|
}
|
|
3262
2799
|
async function getFees(pioneer, networkId) {
|
|
3263
|
-
const
|
|
2800
|
+
const tag = TAG7 + " | getFees | ";
|
|
3264
2801
|
try {
|
|
3265
|
-
console.log(
|
|
2802
|
+
console.log(tag, `Fetching fees for network: ${networkId}`);
|
|
3266
2803
|
const networkType = getNetworkType(networkId);
|
|
3267
2804
|
if (networkType === "COSMOS") {
|
|
3268
|
-
console.log(
|
|
2805
|
+
console.log(tag, "Using hardcoded fees for Cosmos network:", networkId);
|
|
3269
2806
|
return getCosmosFees(networkId);
|
|
3270
2807
|
}
|
|
3271
2808
|
if (networkId === "bip122:00000000001a91e3dace36e2be3bf030") {
|
|
3272
|
-
console.log(
|
|
2809
|
+
console.log(tag, "Using hardcoded fees for Dogecoin: 10 sat/byte");
|
|
3273
2810
|
return {
|
|
3274
2811
|
slow: {
|
|
3275
2812
|
label: "Slow",
|
|
@@ -3306,7 +2843,7 @@ async function getFees(pioneer, networkId) {
|
|
|
3306
2843
|
const apiCall = pioneer.GetFeeRateByNetwork ? pioneer.GetFeeRateByNetwork({ networkId }) : pioneer.GetFeeRate({ networkId });
|
|
3307
2844
|
feeResponse = await withTimeout(apiCall, 3000);
|
|
3308
2845
|
} catch (timeoutError) {
|
|
3309
|
-
console.warn(
|
|
2846
|
+
console.warn(tag, "Dash fee API timeout, using fallback fees");
|
|
3310
2847
|
return {
|
|
3311
2848
|
slow: {
|
|
3312
2849
|
label: "Economy",
|
|
@@ -3339,10 +2876,10 @@ async function getFees(pioneer, networkId) {
|
|
|
3339
2876
|
}
|
|
3340
2877
|
} else {
|
|
3341
2878
|
if (networkType === "EVM") {
|
|
3342
|
-
console.log(
|
|
2879
|
+
console.log(tag, "Using GetGasPriceByNetwork for EVM network:", networkId);
|
|
3343
2880
|
feeResponse = await (pioneer.GetGasPriceByNetwork ? pioneer.GetGasPriceByNetwork({ networkId }) : Promise.reject(new Error("GetGasPriceByNetwork not available")));
|
|
3344
2881
|
} else {
|
|
3345
|
-
console.log(
|
|
2882
|
+
console.log(tag, "Using GetFeeRateByNetwork for UTXO network:", networkId);
|
|
3346
2883
|
feeResponse = await (pioneer.GetFeeRateByNetwork ? pioneer.GetFeeRateByNetwork({ networkId }) : pioneer.GetFeeRate({ networkId }));
|
|
3347
2884
|
}
|
|
3348
2885
|
}
|
|
@@ -3350,17 +2887,17 @@ async function getFees(pioneer, networkId) {
|
|
|
3350
2887
|
throw new Error(`No fee data returned for ${networkId}`);
|
|
3351
2888
|
}
|
|
3352
2889
|
const feeData = feeResponse.data;
|
|
3353
|
-
console.log(
|
|
2890
|
+
console.log(tag, "Raw fee data:", feeData);
|
|
3354
2891
|
const networkName = getNetworkName(networkId);
|
|
3355
2892
|
let normalizedFees = normalizeFeeData(feeData, networkType, networkName, networkId);
|
|
3356
2893
|
normalizedFees = ensureFeeDifferentiation(normalizedFees, networkType);
|
|
3357
2894
|
normalizedFees.networkId = networkId;
|
|
3358
2895
|
normalizedFees.networkType = networkType;
|
|
3359
2896
|
normalizedFees.raw = feeData;
|
|
3360
|
-
console.log(
|
|
2897
|
+
console.log(tag, "Normalized fees:", normalizedFees);
|
|
3361
2898
|
return normalizedFees;
|
|
3362
2899
|
} catch (error) {
|
|
3363
|
-
console.error(
|
|
2900
|
+
console.error(tag, "Failed to fetch fees:", error);
|
|
3364
2901
|
return getFallbackFees(networkId);
|
|
3365
2902
|
}
|
|
3366
2903
|
}
|
|
@@ -3669,7 +3206,7 @@ function estimateTransactionFee(feeRate, unit, networkType, txSize) {
|
|
|
3669
3206
|
|
|
3670
3207
|
// src/utils/kkapi-detection.ts
|
|
3671
3208
|
async function detectKkApiAvailability(forceLocalhost) {
|
|
3672
|
-
const
|
|
3209
|
+
const tag = " | detectKkApiAvailability | ";
|
|
3673
3210
|
try {
|
|
3674
3211
|
const isTauri = typeof window !== "undefined" && "__TAURI__" in window;
|
|
3675
3212
|
const isBrowser = typeof window !== "undefined";
|
|
@@ -3828,11 +3365,11 @@ function buildDashboardFromBalances(balances, blockchains, assetsMap) {
|
|
|
3828
3365
|
// src/utils/sync-market.ts
|
|
3829
3366
|
var TAG8 = " | sync-market | ";
|
|
3830
3367
|
async function syncMarket(balances, pioneer) {
|
|
3831
|
-
const
|
|
3368
|
+
const tag = `${TAG8} | syncMarket | `;
|
|
3832
3369
|
try {
|
|
3833
3370
|
const invalidBalances = balances.filter((b2) => !b2 || !b2.caip || typeof b2.caip !== "string" || !b2.caip.includes(":"));
|
|
3834
3371
|
if (invalidBalances.length > 0) {
|
|
3835
|
-
console.warn(
|
|
3372
|
+
console.warn(tag, `Found ${invalidBalances.length} balances with invalid CAIPs:`, invalidBalances.map((b2) => ({
|
|
3836
3373
|
caip: b2?.caip,
|
|
3837
3374
|
type: typeof b2?.caip,
|
|
3838
3375
|
symbol: b2?.symbol,
|
|
@@ -3847,7 +3384,7 @@ async function syncMarket(balances, pioneer) {
|
|
|
3847
3384
|
console.log("GetMarketInfo: payload length: ", allCaips.length);
|
|
3848
3385
|
const invalidEntries = allCaips.filter((caip) => typeof caip !== "string");
|
|
3849
3386
|
if (invalidEntries.length > 0) {
|
|
3850
|
-
console.error(
|
|
3387
|
+
console.error(tag, "CRITICAL: Invalid entries detected in allCaips:", invalidEntries);
|
|
3851
3388
|
throw new Error("Invalid CAIP entries detected - aborting market sync");
|
|
3852
3389
|
}
|
|
3853
3390
|
if (allCaips && allCaips.length > 0) {
|
|
@@ -3868,13 +3405,13 @@ async function syncMarket(balances, pioneer) {
|
|
|
3868
3405
|
}
|
|
3869
3406
|
}
|
|
3870
3407
|
} catch (apiError) {
|
|
3871
|
-
console.error(
|
|
3872
|
-
console.warn(
|
|
3408
|
+
console.error(tag, "API error fetching market info:", apiError);
|
|
3409
|
+
console.warn(tag, "Continuing without market prices");
|
|
3873
3410
|
}
|
|
3874
3411
|
}
|
|
3875
3412
|
return true;
|
|
3876
3413
|
} catch (e) {
|
|
3877
|
-
console.error(
|
|
3414
|
+
console.error(tag, "e:", e);
|
|
3878
3415
|
throw e;
|
|
3879
3416
|
}
|
|
3880
3417
|
}
|
|
@@ -3989,7 +3526,7 @@ class SDK {
|
|
|
3989
3526
|
this.appIcon = config.appIcon || "https://pioneers.dev/coins/keepkey.png";
|
|
3990
3527
|
this.spec = spec || config.spec || "https://api.keepkey.info/spec/swagger";
|
|
3991
3528
|
this.wss = config.wss || "wss://pioneers.dev";
|
|
3992
|
-
this.assets =
|
|
3529
|
+
this.assets = assetData;
|
|
3993
3530
|
this.assetsMap = new Map;
|
|
3994
3531
|
this.username = config.username;
|
|
3995
3532
|
this.queryKey = config.queryKey;
|
|
@@ -4067,7 +3604,7 @@ class SDK {
|
|
|
4067
3604
|
return true;
|
|
4068
3605
|
};
|
|
4069
3606
|
this.setPubkeys = (newPubkeys) => {
|
|
4070
|
-
const
|
|
3607
|
+
const tag = `${TAG9} | setPubkeys | `;
|
|
4071
3608
|
this.pubkeys = [];
|
|
4072
3609
|
this.pubkeySet.clear();
|
|
4073
3610
|
let added = 0;
|
|
@@ -4084,7 +3621,7 @@ class SDK {
|
|
|
4084
3621
|
this.pubkeySet.clear();
|
|
4085
3622
|
}
|
|
4086
3623
|
this.getUnifiedPortfolio = async function() {
|
|
4087
|
-
const
|
|
3624
|
+
const tag = `${TAG9} | getUnifiedPortfolio | `;
|
|
4088
3625
|
try {
|
|
4089
3626
|
const startTime = performance.now();
|
|
4090
3627
|
try {
|
|
@@ -4095,17 +3632,17 @@ class SDK {
|
|
|
4095
3632
|
signal: AbortSignal.timeout(2000)
|
|
4096
3633
|
});
|
|
4097
3634
|
if (!portfolioResponse.ok) {
|
|
4098
|
-
console.warn(
|
|
3635
|
+
console.warn(tag, "Portfolio endpoint returned", portfolioResponse.status);
|
|
4099
3636
|
return null;
|
|
4100
3637
|
}
|
|
4101
3638
|
const portfolioData = await portfolioResponse.json();
|
|
4102
3639
|
const loadTime = performance.now() - startTime;
|
|
4103
3640
|
if (!portfolioData.success) {
|
|
4104
|
-
console.warn(
|
|
3641
|
+
console.warn(tag, "Portfolio API returned success=false");
|
|
4105
3642
|
return null;
|
|
4106
3643
|
}
|
|
4107
3644
|
if (portfolioData.totalValueUsd === 0 || !portfolioData.totalValueUsd) {
|
|
4108
|
-
console.warn(
|
|
3645
|
+
console.warn(tag, "Portfolio value is $0.00 - may need device connection or sync");
|
|
4109
3646
|
return null;
|
|
4110
3647
|
}
|
|
4111
3648
|
let allBalances = [];
|
|
@@ -4192,14 +3729,14 @@ class SDK {
|
|
|
4192
3729
|
};
|
|
4193
3730
|
} catch (fetchError) {
|
|
4194
3731
|
if (fetchError.name === "AbortError") {
|
|
4195
|
-
console.log(
|
|
3732
|
+
console.log(tag, "Unified portfolio request timed out (this is normal if vault not running)");
|
|
4196
3733
|
} else {
|
|
4197
|
-
console.log(
|
|
3734
|
+
console.log(tag, "Failed to fetch unified portfolio:", fetchError.message);
|
|
4198
3735
|
}
|
|
4199
3736
|
return null;
|
|
4200
3737
|
}
|
|
4201
3738
|
} catch (e) {
|
|
4202
|
-
console.error(
|
|
3739
|
+
console.error(tag, "Error:", e);
|
|
4203
3740
|
return null;
|
|
4204
3741
|
}
|
|
4205
3742
|
};
|
|
@@ -4210,7 +3747,7 @@ class SDK {
|
|
|
4210
3747
|
return this.syncState.isSynced;
|
|
4211
3748
|
};
|
|
4212
3749
|
this.init = async function(walletsVerbose, setup) {
|
|
4213
|
-
const
|
|
3750
|
+
const tag = `${TAG9} | init | `;
|
|
4214
3751
|
try {
|
|
4215
3752
|
if (!this.username)
|
|
4216
3753
|
throw Error("username required!");
|
|
@@ -4266,11 +3803,11 @@ class SDK {
|
|
|
4266
3803
|
this.events.emit("message", request);
|
|
4267
3804
|
});
|
|
4268
3805
|
clientEvents.events.on("balance:update", (data) => {
|
|
4269
|
-
const
|
|
3806
|
+
const tag2 = TAG9 + " | balance:update | ";
|
|
4270
3807
|
try {
|
|
4271
3808
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
4272
3809
|
const balance = payload.balance;
|
|
4273
|
-
console.log(
|
|
3810
|
+
console.log(tag2, "Received balance update:", balance.caip);
|
|
4274
3811
|
const exists = this.balances.find((b2) => b2.caip === balance.caip && b2.pubkey === balance.pubkey);
|
|
4275
3812
|
if (!exists) {
|
|
4276
3813
|
this.balances.push(balance);
|
|
@@ -4280,27 +3817,27 @@ class SDK {
|
|
|
4280
3817
|
}
|
|
4281
3818
|
this.events.emit("BALANCE_UPDATE", balance);
|
|
4282
3819
|
} catch (e) {
|
|
4283
|
-
console.error(
|
|
3820
|
+
console.error(tag2, "Error processing balance update:", e);
|
|
4284
3821
|
}
|
|
4285
3822
|
});
|
|
4286
3823
|
clientEvents.events.on("sync:progress", (data) => {
|
|
4287
|
-
const
|
|
3824
|
+
const tag2 = TAG9 + " | sync:progress | ";
|
|
4288
3825
|
try {
|
|
4289
3826
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
4290
|
-
console.log(
|
|
3827
|
+
console.log(tag2, `Sync progress: ${payload.percentage}%`);
|
|
4291
3828
|
this.syncState.syncProgress = payload.percentage;
|
|
4292
3829
|
this.syncState.syncedChains = payload.completed;
|
|
4293
3830
|
this.syncState.totalChains = payload.total;
|
|
4294
3831
|
this.events.emit("SYNC_PROGRESS", this.syncState);
|
|
4295
3832
|
} catch (e) {
|
|
4296
|
-
console.error(
|
|
3833
|
+
console.error(tag2, "Error processing sync progress:", e);
|
|
4297
3834
|
}
|
|
4298
3835
|
});
|
|
4299
3836
|
clientEvents.events.on("sync:complete", (data) => {
|
|
4300
|
-
const
|
|
3837
|
+
const tag2 = TAG9 + " | sync:complete | ";
|
|
4301
3838
|
try {
|
|
4302
3839
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
|
4303
|
-
console.log(
|
|
3840
|
+
console.log(tag2, `Sync complete: ${payload.balances} balances in ${payload.duration}ms`);
|
|
4304
3841
|
this.syncState.isSynced = true;
|
|
4305
3842
|
this.syncState.isInitialSync = false;
|
|
4306
3843
|
this.syncState.syncProgress = 100;
|
|
@@ -4309,7 +3846,7 @@ class SDK {
|
|
|
4309
3846
|
this.events.emit("SYNC_COMPLETE", this.syncState);
|
|
4310
3847
|
this.events.emit("SYNC_STATE_CHANGED", this.syncState);
|
|
4311
3848
|
} catch (e) {
|
|
4312
|
-
console.error(
|
|
3849
|
+
console.error(tag2, "Error processing sync complete:", e);
|
|
4313
3850
|
}
|
|
4314
3851
|
});
|
|
4315
3852
|
this.events.emit("SET_STATUS", "init");
|
|
@@ -4341,7 +3878,7 @@ class SDK {
|
|
|
4341
3878
|
}
|
|
4342
3879
|
return this.pioneer;
|
|
4343
3880
|
} catch (e) {
|
|
4344
|
-
console.error(
|
|
3881
|
+
console.error(tag, "e: ", e);
|
|
4345
3882
|
throw e;
|
|
4346
3883
|
}
|
|
4347
3884
|
};
|
|
@@ -4352,7 +3889,7 @@ class SDK {
|
|
|
4352
3889
|
return syncMarket(this.balances, this.pioneer);
|
|
4353
3890
|
};
|
|
4354
3891
|
this.sync = async function() {
|
|
4355
|
-
const
|
|
3892
|
+
const tag = `${TAG9} | sync | `;
|
|
4356
3893
|
try {
|
|
4357
3894
|
const matchesNetwork = (item, networkId) => {
|
|
4358
3895
|
if (!item.networks || !Array.isArray(item.networks))
|
|
@@ -4484,7 +4021,7 @@ class SDK {
|
|
|
4484
4021
|
this.events.emit("SYNC_COMPLETE", this.syncState);
|
|
4485
4022
|
return true;
|
|
4486
4023
|
} catch (e) {
|
|
4487
|
-
console.error(
|
|
4024
|
+
console.error(tag, "Error in sync:", e);
|
|
4488
4025
|
throw e;
|
|
4489
4026
|
}
|
|
4490
4027
|
};
|
|
@@ -4498,7 +4035,7 @@ class SDK {
|
|
|
4498
4035
|
}
|
|
4499
4036
|
};
|
|
4500
4037
|
this.buildTx = async function(sendPayload) {
|
|
4501
|
-
let
|
|
4038
|
+
let tag = TAG9 + " | buildTx | ";
|
|
4502
4039
|
try {
|
|
4503
4040
|
const transactionDependencies = {
|
|
4504
4041
|
context: this.context,
|
|
@@ -4512,7 +4049,7 @@ class SDK {
|
|
|
4512
4049
|
};
|
|
4513
4050
|
let txManager = new TransactionManager(transactionDependencies, this.events);
|
|
4514
4051
|
let unsignedTx = await txManager.transfer(sendPayload);
|
|
4515
|
-
console.log(
|
|
4052
|
+
console.log(tag, "unsignedTx: ", unsignedTx);
|
|
4516
4053
|
return unsignedTx;
|
|
4517
4054
|
} catch (e) {
|
|
4518
4055
|
console.error(e);
|
|
@@ -4520,14 +4057,14 @@ class SDK {
|
|
|
4520
4057
|
}
|
|
4521
4058
|
};
|
|
4522
4059
|
this.buildDelegateTx = async function(caip, params) {
|
|
4523
|
-
let
|
|
4060
|
+
let tag = TAG9 + " | buildDelegateTx | ";
|
|
4524
4061
|
try {
|
|
4525
4062
|
const delegateParams = {
|
|
4526
4063
|
...params,
|
|
4527
4064
|
type: "delegate"
|
|
4528
4065
|
};
|
|
4529
4066
|
let unsignedTx = await createUnsignedStakingTx(caip, delegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4530
|
-
console.log(
|
|
4067
|
+
console.log(tag, "unsignedTx: ", unsignedTx);
|
|
4531
4068
|
return unsignedTx;
|
|
4532
4069
|
} catch (e) {
|
|
4533
4070
|
console.error(e);
|
|
@@ -4535,14 +4072,14 @@ class SDK {
|
|
|
4535
4072
|
}
|
|
4536
4073
|
};
|
|
4537
4074
|
this.buildUndelegateTx = async function(caip, params) {
|
|
4538
|
-
let
|
|
4075
|
+
let tag = TAG9 + " | buildUndelegateTx | ";
|
|
4539
4076
|
try {
|
|
4540
4077
|
const undelegateParams = {
|
|
4541
4078
|
...params,
|
|
4542
4079
|
type: "undelegate"
|
|
4543
4080
|
};
|
|
4544
4081
|
let unsignedTx = await createUnsignedStakingTx(caip, undelegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4545
|
-
console.log(
|
|
4082
|
+
console.log(tag, "unsignedTx: ", unsignedTx);
|
|
4546
4083
|
return unsignedTx;
|
|
4547
4084
|
} catch (e) {
|
|
4548
4085
|
console.error(e);
|
|
@@ -4550,14 +4087,14 @@ class SDK {
|
|
|
4550
4087
|
}
|
|
4551
4088
|
};
|
|
4552
4089
|
this.buildClaimRewardsTx = async function(caip, params) {
|
|
4553
|
-
let
|
|
4090
|
+
let tag = TAG9 + " | buildClaimRewardsTx | ";
|
|
4554
4091
|
try {
|
|
4555
4092
|
const claimParams = {
|
|
4556
4093
|
...params,
|
|
4557
4094
|
type: "claim_rewards"
|
|
4558
4095
|
};
|
|
4559
4096
|
let unsignedTx = await createUnsignedStakingTx(caip, claimParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4560
|
-
console.log(
|
|
4097
|
+
console.log(tag, "unsignedTx: ", unsignedTx);
|
|
4561
4098
|
return unsignedTx;
|
|
4562
4099
|
} catch (e) {
|
|
4563
4100
|
console.error(e);
|
|
@@ -4565,7 +4102,7 @@ class SDK {
|
|
|
4565
4102
|
}
|
|
4566
4103
|
};
|
|
4567
4104
|
this.buildClaimAllRewardsTx = async function(caip, params) {
|
|
4568
|
-
let
|
|
4105
|
+
let tag = TAG9 + " | buildClaimAllRewardsTx | ";
|
|
4569
4106
|
try {
|
|
4570
4107
|
const claimAllParams = {
|
|
4571
4108
|
...params,
|
|
@@ -4579,7 +4116,7 @@ class SDK {
|
|
|
4579
4116
|
}
|
|
4580
4117
|
};
|
|
4581
4118
|
this.signTx = async function(caip, unsignedTx) {
|
|
4582
|
-
let
|
|
4119
|
+
let tag = TAG9 + " | signTx | ";
|
|
4583
4120
|
try {
|
|
4584
4121
|
const transactionDependencies = {
|
|
4585
4122
|
context: this.context,
|
|
@@ -4600,7 +4137,7 @@ class SDK {
|
|
|
4600
4137
|
}
|
|
4601
4138
|
};
|
|
4602
4139
|
this.broadcastTx = async function(caip, signedTx) {
|
|
4603
|
-
let
|
|
4140
|
+
let tag = TAG9 + " | broadcastTx | ";
|
|
4604
4141
|
try {
|
|
4605
4142
|
const transactionDependencies = {
|
|
4606
4143
|
context: this.context,
|
|
@@ -4624,7 +4161,7 @@ class SDK {
|
|
|
4624
4161
|
}
|
|
4625
4162
|
};
|
|
4626
4163
|
this.swap = async function(swapPayload) {
|
|
4627
|
-
let
|
|
4164
|
+
let tag = `${TAG9} | swap | `;
|
|
4628
4165
|
try {
|
|
4629
4166
|
if (!swapPayload)
|
|
4630
4167
|
throw Error("swapPayload required!");
|
|
@@ -4673,15 +4210,15 @@ class SDK {
|
|
|
4673
4210
|
throw new Error(`Cannot use max amount: no balance found for ${swapPayload.caipIn}`);
|
|
4674
4211
|
}
|
|
4675
4212
|
let totalBalance = 0;
|
|
4676
|
-
console.log(
|
|
4213
|
+
console.log(tag, `Found ${inputBalances.length} balance entries for ${swapPayload.caipIn}`);
|
|
4677
4214
|
for (const balanceEntry of inputBalances) {
|
|
4678
4215
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
4679
4216
|
totalBalance += balance;
|
|
4680
|
-
console.log(
|
|
4217
|
+
console.log(tag, ` - ${balanceEntry.pubkey || balanceEntry.identifier}: ${balance}`);
|
|
4681
4218
|
}
|
|
4682
4219
|
this.assetContext.balance = totalBalance.toString();
|
|
4683
4220
|
this.assetContext.valueUsd = (totalBalance * parseFloat(this.assetContext.priceUsd || "0")).toFixed(2);
|
|
4684
|
-
console.log(
|
|
4221
|
+
console.log(tag, `Updated assetContext balance to aggregated total: ${totalBalance}`);
|
|
4685
4222
|
const feeReserves = {
|
|
4686
4223
|
"bip122:000000000019d6689c085ae165831e93/slip44:0": 0.00005,
|
|
4687
4224
|
"eip155:1/slip44:60": 0.001,
|
|
@@ -4692,7 +4229,7 @@ class SDK {
|
|
|
4692
4229
|
};
|
|
4693
4230
|
const reserve = feeReserves[swapPayload.caipIn] || 0.0001;
|
|
4694
4231
|
inputAmount = Math.max(0, totalBalance - reserve);
|
|
4695
|
-
console.log(
|
|
4232
|
+
console.log(tag, `Using max amount for swap: ${inputAmount} (total balance: ${totalBalance}, reserve: ${reserve})`);
|
|
4696
4233
|
} else {
|
|
4697
4234
|
inputAmount = typeof swapPayload.amount === "string" ? parseFloat(swapPayload.amount) : swapPayload.amount;
|
|
4698
4235
|
if (isNaN(inputAmount) || inputAmount <= 0) {
|
|
@@ -4712,7 +4249,7 @@ class SDK {
|
|
|
4712
4249
|
result = await this.pioneer.Quote(quote);
|
|
4713
4250
|
result = result.data;
|
|
4714
4251
|
} catch (e) {
|
|
4715
|
-
console.error(
|
|
4252
|
+
console.error(tag, "Failed to get quote: ", e);
|
|
4716
4253
|
throw Error("Quote API failed for path: " + quote.sellAsset + " -> " + quote.buyAsset + ". Error: " + e);
|
|
4717
4254
|
}
|
|
4718
4255
|
if (!result || result.length === 0)
|
|
@@ -4739,7 +4276,7 @@ class SDK {
|
|
|
4739
4276
|
if (tx.type === "deposit") {
|
|
4740
4277
|
unsignedTx = await createUnsignedTendermintTx(caip, tx.type, tx.txParams.amount, tx.txParams.memo, this.pubkeys, this.pioneer, this.pubkeyContext, false, undefined);
|
|
4741
4278
|
} else if (tx.type === "EVM" || tx.type === "evm") {
|
|
4742
|
-
console.log(
|
|
4279
|
+
console.log(tag, "Using pre-built EVM transaction from integration");
|
|
4743
4280
|
unsignedTx = tx.txParams;
|
|
4744
4281
|
} else {
|
|
4745
4282
|
if (!tx.txParams.memo)
|
|
@@ -4758,12 +4295,12 @@ class SDK {
|
|
|
4758
4295
|
return unsignedTx;
|
|
4759
4296
|
}
|
|
4760
4297
|
} catch (e) {
|
|
4761
|
-
console.error(
|
|
4298
|
+
console.error(tag, "Error: ", e);
|
|
4762
4299
|
throw e;
|
|
4763
4300
|
}
|
|
4764
4301
|
};
|
|
4765
4302
|
this.transfer = async function(sendPayload) {
|
|
4766
|
-
let
|
|
4303
|
+
let tag = `${TAG9} | transfer | `;
|
|
4767
4304
|
try {
|
|
4768
4305
|
if (!sendPayload)
|
|
4769
4306
|
throw Error("sendPayload required!");
|
|
@@ -4797,15 +4334,15 @@ class SDK {
|
|
|
4797
4334
|
return { txid, events: this.events };
|
|
4798
4335
|
} catch (error) {
|
|
4799
4336
|
if (error instanceof Error) {
|
|
4800
|
-
console.error(
|
|
4337
|
+
console.error(tag, "An error occurred during the transfer process:", error.message);
|
|
4801
4338
|
} else {
|
|
4802
|
-
console.error(
|
|
4339
|
+
console.error(tag, "An unknown error occurred during the transfer process");
|
|
4803
4340
|
}
|
|
4804
4341
|
throw error;
|
|
4805
4342
|
}
|
|
4806
4343
|
};
|
|
4807
4344
|
this.followTransaction = async function(caip, txid) {
|
|
4808
|
-
let
|
|
4345
|
+
let tag = " | followTransaction | ";
|
|
4809
4346
|
try {
|
|
4810
4347
|
const finalConfirmationBlocksByCaip = {
|
|
4811
4348
|
dogecoin: 3,
|
|
@@ -4835,7 +4372,7 @@ class SDK {
|
|
|
4835
4372
|
}
|
|
4836
4373
|
}
|
|
4837
4374
|
} catch (error) {
|
|
4838
|
-
console.error(
|
|
4375
|
+
console.error(tag, "Error:", error);
|
|
4839
4376
|
}
|
|
4840
4377
|
if (!isConfirmed) {
|
|
4841
4378
|
await new Promise((resolve) => setTimeout(resolve, 8000));
|
|
@@ -4853,18 +4390,18 @@ class SDK {
|
|
|
4853
4390
|
requiredConfirmations
|
|
4854
4391
|
};
|
|
4855
4392
|
} catch (error) {
|
|
4856
|
-
console.error(
|
|
4393
|
+
console.error(tag, "Error:", error);
|
|
4857
4394
|
throw new Error("Failed to follow transaction");
|
|
4858
4395
|
}
|
|
4859
4396
|
};
|
|
4860
4397
|
this.setBlockchains = async function(blockchains) {
|
|
4861
|
-
const
|
|
4398
|
+
const tag = `${TAG9} | setBlockchains | `;
|
|
4862
4399
|
try {
|
|
4863
4400
|
if (!blockchains)
|
|
4864
4401
|
throw Error("blockchains required!");
|
|
4865
4402
|
const uniqueBlockchains = [...new Set(blockchains)];
|
|
4866
4403
|
if (blockchains.length !== uniqueBlockchains.length) {
|
|
4867
|
-
console.warn(
|
|
4404
|
+
console.warn(tag, `Removed ${blockchains.length - uniqueBlockchains.length} duplicate blockchains`);
|
|
4868
4405
|
}
|
|
4869
4406
|
this.blockchains = uniqueBlockchains;
|
|
4870
4407
|
this.events.emit("SET_BLOCKCHAINS", this.blockchains);
|
|
@@ -4874,12 +4411,12 @@ class SDK {
|
|
|
4874
4411
|
}
|
|
4875
4412
|
};
|
|
4876
4413
|
this.addAsset = async function(caip, data) {
|
|
4877
|
-
let
|
|
4414
|
+
let tag = TAG9 + " | addAsset | ";
|
|
4878
4415
|
try {
|
|
4879
4416
|
let success = false;
|
|
4880
4417
|
if (!caip)
|
|
4881
4418
|
throw new Error("caip required!");
|
|
4882
|
-
let dataLocal =
|
|
4419
|
+
let dataLocal = assetData[caip];
|
|
4883
4420
|
if (!dataLocal) {
|
|
4884
4421
|
if (!data.networkId)
|
|
4885
4422
|
throw new Error("networkId required! can not build asset");
|
|
@@ -4912,22 +4449,22 @@ class SDK {
|
|
|
4912
4449
|
}
|
|
4913
4450
|
};
|
|
4914
4451
|
this.clearWalletState = async function() {
|
|
4915
|
-
const
|
|
4452
|
+
const tag = `${TAG9} | clearWalletState | `;
|
|
4916
4453
|
try {
|
|
4917
4454
|
this.context = null;
|
|
4918
4455
|
this.paths = [];
|
|
4919
4456
|
this.blockchains = [];
|
|
4920
4457
|
this.pubkeys = [];
|
|
4921
4458
|
this.pubkeySet.clear();
|
|
4922
|
-
console.log(
|
|
4459
|
+
console.log(tag, "Cleared wallet state including pubkeys and tracking set");
|
|
4923
4460
|
return true;
|
|
4924
4461
|
} catch (e) {
|
|
4925
|
-
console.error(
|
|
4462
|
+
console.error(tag, "e: ", e);
|
|
4926
4463
|
throw e;
|
|
4927
4464
|
}
|
|
4928
4465
|
};
|
|
4929
4466
|
this.addPath = async function(path) {
|
|
4930
|
-
const
|
|
4467
|
+
const tag = `${TAG9} | addPath | `;
|
|
4931
4468
|
try {
|
|
4932
4469
|
this.paths.push(path);
|
|
4933
4470
|
const pubkey = await getPubkey(path.networks[0], path, this.keepKeySdk, this.context);
|
|
@@ -4936,14 +4473,14 @@ class SDK {
|
|
|
4936
4473
|
this.buildDashboardFromBalances();
|
|
4937
4474
|
return { success: true, pubkey };
|
|
4938
4475
|
} catch (e) {
|
|
4939
|
-
console.error(
|
|
4476
|
+
console.error(tag, "Failed:", e);
|
|
4940
4477
|
throw e;
|
|
4941
4478
|
}
|
|
4942
4479
|
};
|
|
4943
4480
|
this.addPaths = async function(paths) {
|
|
4944
|
-
const
|
|
4481
|
+
const tag = `${TAG9} | addPaths | `;
|
|
4945
4482
|
try {
|
|
4946
|
-
console.log(
|
|
4483
|
+
console.log(tag, `Adding ${paths.length} paths in batch mode...`);
|
|
4947
4484
|
this.paths.push(...paths);
|
|
4948
4485
|
const newPubkeys = [];
|
|
4949
4486
|
for (const path of paths) {
|
|
@@ -4952,10 +4489,10 @@ class SDK {
|
|
|
4952
4489
|
this.addPubkey(pubkey);
|
|
4953
4490
|
newPubkeys.push(pubkey);
|
|
4954
4491
|
} catch (error) {
|
|
4955
|
-
console.warn(
|
|
4492
|
+
console.warn(tag, `Failed to get pubkey for path ${path.note}:`, error.message);
|
|
4956
4493
|
}
|
|
4957
4494
|
}
|
|
4958
|
-
console.log(
|
|
4495
|
+
console.log(tag, `Successfully added ${newPubkeys.length} pubkeys`);
|
|
4959
4496
|
const networkSet = new Set;
|
|
4960
4497
|
for (const path of paths) {
|
|
4961
4498
|
if (path.networks && Array.isArray(path.networks)) {
|
|
@@ -4963,13 +4500,13 @@ class SDK {
|
|
|
4963
4500
|
}
|
|
4964
4501
|
}
|
|
4965
4502
|
const uniqueNetworks = [...networkSet];
|
|
4966
|
-
console.log(
|
|
4503
|
+
console.log(tag, `Fetching balances for ${uniqueNetworks.length} unique networks in single API call...`);
|
|
4967
4504
|
await this.getBalancesForNetworks(uniqueNetworks);
|
|
4968
4505
|
this.buildDashboardFromBalances();
|
|
4969
|
-
console.log(
|
|
4506
|
+
console.log(tag, `Batch add complete: ${paths.length} paths, ${newPubkeys.length} pubkeys, ${this.balances?.length || 0} balances`);
|
|
4970
4507
|
return { success: true, pubkeys: newPubkeys };
|
|
4971
4508
|
} catch (e) {
|
|
4972
|
-
console.error(
|
|
4509
|
+
console.error(tag, "Failed:", e);
|
|
4973
4510
|
throw e;
|
|
4974
4511
|
}
|
|
4975
4512
|
};
|
|
@@ -4977,12 +4514,12 @@ class SDK {
|
|
|
4977
4514
|
return this.getGasAssets();
|
|
4978
4515
|
};
|
|
4979
4516
|
this.getGasAssets = async function() {
|
|
4980
|
-
const
|
|
4517
|
+
const tag = `${TAG9} | getGasAssets | `;
|
|
4981
4518
|
try {
|
|
4982
4519
|
for (let i = 0;i < this.blockchains.length; i++) {
|
|
4983
4520
|
let networkId = this.blockchains[i];
|
|
4984
4521
|
let caip = networkIdToCaip2(networkId);
|
|
4985
|
-
let asset = await
|
|
4522
|
+
let asset = await assetData[caip.toLowerCase()];
|
|
4986
4523
|
if (asset) {
|
|
4987
4524
|
asset.caip = caip.toLowerCase();
|
|
4988
4525
|
asset.networkId = networkId;
|
|
@@ -5011,7 +4548,7 @@ class SDK {
|
|
|
5011
4548
|
denom: "maya"
|
|
5012
4549
|
};
|
|
5013
4550
|
this.assetsMap.set(mayaTokenCaip, mayaToken);
|
|
5014
|
-
console.log(
|
|
4551
|
+
console.log(tag, "Added MAYA token to assetsMap");
|
|
5015
4552
|
}
|
|
5016
4553
|
return this.assetsMap;
|
|
5017
4554
|
} catch (e) {
|
|
@@ -5020,7 +4557,7 @@ class SDK {
|
|
|
5020
4557
|
}
|
|
5021
4558
|
};
|
|
5022
4559
|
this.getPubkeys = async function() {
|
|
5023
|
-
const
|
|
4560
|
+
const tag = `${TAG9} | getPubkeys | `;
|
|
5024
4561
|
try {
|
|
5025
4562
|
if (this.paths.length === 0)
|
|
5026
4563
|
throw new Error("No paths found!");
|
|
@@ -5034,41 +4571,41 @@ class SDK {
|
|
|
5034
4571
|
}
|
|
5035
4572
|
const pubkeysWithoutNetworks = this.pubkeys.filter((pk) => !pk.networks || !Array.isArray(pk.networks) || pk.networks.length === 0);
|
|
5036
4573
|
if (pubkeysWithoutNetworks.length > 0) {
|
|
5037
|
-
console.error(
|
|
5038
|
-
console.error(
|
|
4574
|
+
console.error(tag, "ERROR: Some pubkeys missing networks field!");
|
|
4575
|
+
console.error(tag, "Affected pubkeys:", pubkeysWithoutNetworks.length);
|
|
5039
4576
|
pubkeysWithoutNetworks.forEach((pk) => {
|
|
5040
|
-
console.error(
|
|
4577
|
+
console.error(tag, ` - ${pk.note || pk.pubkey}: networks=${pk.networks}`);
|
|
5041
4578
|
});
|
|
5042
4579
|
for (const pubkey of pubkeysWithoutNetworks) {
|
|
5043
4580
|
const matchingPath = this.paths.find((p) => JSON.stringify(p.addressNList) === JSON.stringify(pubkey.addressNList));
|
|
5044
4581
|
if (matchingPath && matchingPath.networks) {
|
|
5045
|
-
console.warn(
|
|
4582
|
+
console.warn(tag, ` ⚠️ Auto-fixing: Adding networks from path ${matchingPath.note}`);
|
|
5046
4583
|
pubkey.networks = matchingPath.networks;
|
|
5047
4584
|
}
|
|
5048
4585
|
}
|
|
5049
4586
|
const stillMissing = this.pubkeys.filter((pk) => !pk.networks || !Array.isArray(pk.networks) || pk.networks.length === 0);
|
|
5050
4587
|
if (stillMissing.length > 0) {
|
|
5051
|
-
console.error(
|
|
4588
|
+
console.error(tag, `❌ CRITICAL: ${stillMissing.length} pubkeys still missing networks after auto-fix!`);
|
|
5052
4589
|
stillMissing.forEach((pk) => {
|
|
5053
|
-
console.error(
|
|
4590
|
+
console.error(tag, ` - ${pk.note || pk.pubkey}`);
|
|
5054
4591
|
});
|
|
5055
4592
|
} else {
|
|
5056
|
-
console.log(
|
|
4593
|
+
console.log(tag, `✅ Auto-fix successful: All pubkeys now have networks field`);
|
|
5057
4594
|
}
|
|
5058
4595
|
}
|
|
5059
4596
|
this.events.emit("SET_PUBKEYS", this.pubkeys);
|
|
5060
4597
|
return pubkeys;
|
|
5061
4598
|
} catch (error) {
|
|
5062
4599
|
console.error("Error in getPubkeys:", error);
|
|
5063
|
-
console.error(
|
|
4600
|
+
console.error(tag, "Error in getPubkeys:", error);
|
|
5064
4601
|
throw error;
|
|
5065
4602
|
}
|
|
5066
4603
|
};
|
|
5067
4604
|
this.getBalancesForNetworks = async function(networkIds) {
|
|
5068
|
-
const
|
|
4605
|
+
const tag = `${TAG9} | getBalancesForNetworks | `;
|
|
5069
4606
|
try {
|
|
5070
4607
|
if (!this.pioneer) {
|
|
5071
|
-
console.error(
|
|
4608
|
+
console.error(tag, "ERROR: Pioneer client not initialized! this.pioneer is:", this.pioneer);
|
|
5072
4609
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
5073
4610
|
}
|
|
5074
4611
|
console.log("\uD83D\uDD0D [DIAGNOSTIC] Input networks:", networkIds);
|
|
@@ -5096,20 +4633,20 @@ class SDK {
|
|
|
5096
4633
|
return network === adjustedNetworkId;
|
|
5097
4634
|
}));
|
|
5098
4635
|
if (pubkeys.length === 0) {
|
|
5099
|
-
console.warn(
|
|
5100
|
-
console.warn(
|
|
4636
|
+
console.warn(tag, `⚠️ No pubkeys found for ${networkId} with networks field`);
|
|
4637
|
+
console.warn(tag, "Attempting fallback: finding pubkeys by path matching");
|
|
5101
4638
|
const pathsForNetwork = this.paths.filter((p) => p.networks?.includes(networkId) || networkId.startsWith("eip155:") && p.networks?.includes("eip155:*"));
|
|
5102
4639
|
for (const path of pathsForNetwork) {
|
|
5103
4640
|
const matchingPubkey = this.pubkeys.find((pk) => JSON.stringify(pk.addressNList) === JSON.stringify(path.addressNList));
|
|
5104
4641
|
if (matchingPubkey) {
|
|
5105
|
-
console.warn(
|
|
4642
|
+
console.warn(tag, ` ✓ Found pubkey via path matching: ${matchingPubkey.note || matchingPubkey.pubkey.slice(0, 10)}`);
|
|
5106
4643
|
pubkeys.push(matchingPubkey);
|
|
5107
4644
|
}
|
|
5108
4645
|
}
|
|
5109
4646
|
if (pubkeys.length > 0) {
|
|
5110
|
-
console.warn(
|
|
4647
|
+
console.warn(tag, ` ✅ Fallback successful: Found ${pubkeys.length} pubkeys for ${networkId}`);
|
|
5111
4648
|
} else {
|
|
5112
|
-
console.error(
|
|
4649
|
+
console.error(tag, ` ❌ Fallback failed: No pubkeys found for ${networkId}`);
|
|
5113
4650
|
}
|
|
5114
4651
|
}
|
|
5115
4652
|
const caipNative = await networkIdToCaip2(networkId);
|
|
@@ -5127,11 +4664,18 @@ class SDK {
|
|
|
5127
4664
|
caipCounts.forEach((count, caip) => {
|
|
5128
4665
|
console.log(` - ${caip}: ${count} queries`);
|
|
5129
4666
|
});
|
|
4667
|
+
console.log(`⏱️ [PERF] Starting GetPortfolioBalances API call...`);
|
|
4668
|
+
const apiCallStart = performance.now();
|
|
5130
4669
|
console.time("GetPortfolioBalances Response Time");
|
|
5131
4670
|
try {
|
|
5132
|
-
let marketInfo = await this.pioneer.GetPortfolioBalances(assetQuery);
|
|
4671
|
+
let marketInfo = await this.pioneer.GetPortfolioBalances({ pubkeys: assetQuery });
|
|
4672
|
+
const apiCallTime = performance.now() - apiCallStart;
|
|
5133
4673
|
console.timeEnd("GetPortfolioBalances Response Time");
|
|
4674
|
+
console.log(`⏱️ [PERF] API call completed in ${apiCallTime.toFixed(0)}ms`);
|
|
4675
|
+
const enrichStart = performance.now();
|
|
5134
4676
|
let balances = marketInfo.data;
|
|
4677
|
+
console.log(`⏱️ [PERF] Received ${balances?.length || 0} balances from server`);
|
|
4678
|
+
console.log(`⏱️ [PERF] Starting balance enrichment...`);
|
|
5135
4679
|
for (let balance of balances) {
|
|
5136
4680
|
const assetInfo = this.assetsMap.get(balance.caip.toLowerCase()) || this.assetsMap.get(balance.caip);
|
|
5137
4681
|
if (!assetInfo)
|
|
@@ -5145,47 +4689,50 @@ class SDK {
|
|
|
5145
4689
|
color
|
|
5146
4690
|
});
|
|
5147
4691
|
}
|
|
4692
|
+
const enrichTime = performance.now() - enrichStart;
|
|
4693
|
+
console.log(`⏱️ [PERF] Enrichment completed in ${enrichTime.toFixed(0)}ms`);
|
|
5148
4694
|
this.balances = balances;
|
|
5149
4695
|
this.events.emit("SET_BALANCES", this.balances);
|
|
4696
|
+
console.log(`⏱️ [PERF] Total getBalancesForNetworks: ${(performance.now() - apiCallStart).toFixed(0)}ms`);
|
|
5150
4697
|
return this.balances;
|
|
5151
4698
|
} catch (apiError) {
|
|
5152
|
-
console.error(
|
|
4699
|
+
console.error(tag, "GetPortfolioBalances API call failed:", apiError);
|
|
5153
4700
|
throw new Error(`GetPortfolioBalances API call failed: ${apiError?.message || "Unknown error"}`);
|
|
5154
4701
|
}
|
|
5155
4702
|
} catch (e) {
|
|
5156
|
-
console.error(
|
|
4703
|
+
console.error(tag, "Error: ", e);
|
|
5157
4704
|
throw e;
|
|
5158
4705
|
}
|
|
5159
4706
|
};
|
|
5160
4707
|
this.getBalances = async function() {
|
|
5161
|
-
const
|
|
4708
|
+
const tag = `${TAG9} | getBalances | `;
|
|
5162
4709
|
try {
|
|
5163
4710
|
return await this.getBalancesForNetworks(this.blockchains);
|
|
5164
4711
|
} catch (e) {
|
|
5165
|
-
console.error(
|
|
4712
|
+
console.error(tag, "Error in getBalances: ", e);
|
|
5166
4713
|
throw e;
|
|
5167
4714
|
}
|
|
5168
4715
|
};
|
|
5169
4716
|
this.getBalance = async function(networkId) {
|
|
5170
|
-
const
|
|
4717
|
+
const tag = `${TAG9} | getBalance | `;
|
|
5171
4718
|
try {
|
|
5172
4719
|
const results = await this.getBalancesForNetworks([networkId]);
|
|
5173
4720
|
const filtered = results.filter(async (b2) => b2.networkId === await networkIdToCaip2(networkId));
|
|
5174
4721
|
return filtered;
|
|
5175
4722
|
} catch (e) {
|
|
5176
|
-
console.error(
|
|
4723
|
+
console.error(tag, "Error: ", e);
|
|
5177
4724
|
throw e;
|
|
5178
4725
|
}
|
|
5179
4726
|
};
|
|
5180
4727
|
this.getFees = async function(networkId) {
|
|
5181
|
-
const
|
|
4728
|
+
const tag = `${TAG9} | getFees | `;
|
|
5182
4729
|
try {
|
|
5183
4730
|
if (!this.pioneer) {
|
|
5184
4731
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
5185
4732
|
}
|
|
5186
4733
|
return await getFees(this.pioneer, networkId);
|
|
5187
4734
|
} catch (e) {
|
|
5188
|
-
console.error(
|
|
4735
|
+
console.error(tag, "Error getting fees: ", e);
|
|
5189
4736
|
throw e;
|
|
5190
4737
|
}
|
|
5191
4738
|
};
|
|
@@ -5193,29 +4740,55 @@ class SDK {
|
|
|
5193
4740
|
return estimateTransactionFee(feeRate, unit, networkType, txSize);
|
|
5194
4741
|
};
|
|
5195
4742
|
this.getCharts = async function() {
|
|
5196
|
-
const
|
|
4743
|
+
const tag = `${TAG9} | getCharts | `;
|
|
5197
4744
|
try {
|
|
5198
|
-
console.log(
|
|
5199
|
-
const
|
|
5200
|
-
|
|
4745
|
+
console.log(tag, "Fetching charts from batch endpoint");
|
|
4746
|
+
const pubkeysForBatch = [];
|
|
4747
|
+
for (const pubkey of this.pubkeys) {
|
|
4748
|
+
const address = pubkey.address || pubkey.master || pubkey.pubkey;
|
|
4749
|
+
if (!address)
|
|
4750
|
+
continue;
|
|
4751
|
+
const supportedNetworks = pubkey.networks || [];
|
|
4752
|
+
for (const blockchain of this.blockchains) {
|
|
4753
|
+
const supportsNetwork = supportedNetworks.some((net) => net === blockchain || net.endsWith(":*") && blockchain.startsWith(net.replace(":*", ":")));
|
|
4754
|
+
if (supportsNetwork) {
|
|
4755
|
+
let caip;
|
|
4756
|
+
if (blockchain.startsWith("eip155:")) {
|
|
4757
|
+
caip = `${blockchain}/slip44:60`;
|
|
4758
|
+
} else {
|
|
4759
|
+
caip = `${blockchain}/slip44:0`;
|
|
4760
|
+
}
|
|
4761
|
+
pubkeysForBatch.push({
|
|
4762
|
+
pubkey: address,
|
|
4763
|
+
caip
|
|
4764
|
+
});
|
|
4765
|
+
}
|
|
4766
|
+
}
|
|
4767
|
+
}
|
|
4768
|
+
console.log(tag, `Calling batch GetCharts with ${pubkeysForBatch.length} pubkeys`);
|
|
4769
|
+
const chartsResponse = await this.pioneer.GetCharts({
|
|
4770
|
+
pubkeys: pubkeysForBatch
|
|
4771
|
+
});
|
|
4772
|
+
const newBalances = chartsResponse?.data?.balances || [];
|
|
4773
|
+
console.log(tag, `Received ${newBalances.length} balances from batch endpoint`);
|
|
5201
4774
|
const uniqueBalances = new Map([...this.balances, ...newBalances].map((balance) => [
|
|
5202
|
-
balance.identifier
|
|
4775
|
+
balance.identifier || `${balance.caip}:${balance.pubkey}`,
|
|
5203
4776
|
{
|
|
5204
4777
|
...balance,
|
|
5205
4778
|
type: balance.type || "balance"
|
|
5206
4779
|
}
|
|
5207
4780
|
]));
|
|
5208
|
-
console.log(
|
|
4781
|
+
console.log(tag, "uniqueBalances: ", uniqueBalances.size);
|
|
5209
4782
|
this.balances = Array.from(uniqueBalances.values());
|
|
5210
|
-
console.log(
|
|
4783
|
+
console.log(tag, "Updated this.balances: ", this.balances.length);
|
|
5211
4784
|
return this.balances;
|
|
5212
4785
|
} catch (e) {
|
|
5213
|
-
console.error(
|
|
4786
|
+
console.error(tag, "Error in getCharts:", e);
|
|
5214
4787
|
throw e;
|
|
5215
4788
|
}
|
|
5216
4789
|
};
|
|
5217
4790
|
this.setContext = async (context) => {
|
|
5218
|
-
const
|
|
4791
|
+
const tag = `${TAG9} | setContext | `;
|
|
5219
4792
|
try {
|
|
5220
4793
|
if (!context)
|
|
5221
4794
|
throw Error("context required!");
|
|
@@ -5223,12 +4796,12 @@ class SDK {
|
|
|
5223
4796
|
this.events.emit("SET_CONTEXT", context);
|
|
5224
4797
|
return { success: true };
|
|
5225
4798
|
} catch (e) {
|
|
5226
|
-
console.error(
|
|
4799
|
+
console.error(tag, "e: ", e);
|
|
5227
4800
|
return { success: false };
|
|
5228
4801
|
}
|
|
5229
4802
|
};
|
|
5230
4803
|
this.setContextType = async (contextType) => {
|
|
5231
|
-
const
|
|
4804
|
+
const tag = `${TAG9} | setContextType | `;
|
|
5232
4805
|
try {
|
|
5233
4806
|
if (!contextType)
|
|
5234
4807
|
throw Error("contextType required!");
|
|
@@ -5236,22 +4809,22 @@ class SDK {
|
|
|
5236
4809
|
this.events.emit("SET_CONTEXT_TYPE", contextType);
|
|
5237
4810
|
return { success: true };
|
|
5238
4811
|
} catch (e) {
|
|
5239
|
-
console.error(
|
|
4812
|
+
console.error(tag, "e: ", e);
|
|
5240
4813
|
return { success: false };
|
|
5241
4814
|
}
|
|
5242
4815
|
};
|
|
5243
4816
|
this.refresh = async () => {
|
|
5244
|
-
const
|
|
4817
|
+
const tag = `${TAG9} | refresh | `;
|
|
5245
4818
|
try {
|
|
5246
4819
|
await this.sync();
|
|
5247
4820
|
return this.balances;
|
|
5248
4821
|
} catch (e) {
|
|
5249
|
-
console.error(
|
|
4822
|
+
console.error(tag, "e: ", e);
|
|
5250
4823
|
throw e;
|
|
5251
4824
|
}
|
|
5252
4825
|
};
|
|
5253
4826
|
this.setAssetContext = async function(asset) {
|
|
5254
|
-
const
|
|
4827
|
+
const tag = `${TAG9} | setAssetContext | `;
|
|
5255
4828
|
try {
|
|
5256
4829
|
if (!asset) {
|
|
5257
4830
|
this.assetContext = null;
|
|
@@ -5263,7 +4836,7 @@ class SDK {
|
|
|
5263
4836
|
asset.networkId = caipToNetworkId7(asset.caip);
|
|
5264
4837
|
if (!this.pubkeys || this.pubkeys.length === 0) {
|
|
5265
4838
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no pubkeys loaded. Please initialize wallet first.`;
|
|
5266
|
-
console.error(
|
|
4839
|
+
console.error(tag, errorMsg);
|
|
5267
4840
|
throw new Error(errorMsg);
|
|
5268
4841
|
}
|
|
5269
4842
|
const pubkeysForNetwork = this.pubkeys.filter((e) => {
|
|
@@ -5278,8 +4851,8 @@ class SDK {
|
|
|
5278
4851
|
});
|
|
5279
4852
|
if (pubkeysForNetwork.length === 0) {
|
|
5280
4853
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no address/xpub found for network ${asset.networkId}`;
|
|
5281
|
-
console.error(
|
|
5282
|
-
console.error(
|
|
4854
|
+
console.error(tag, errorMsg);
|
|
4855
|
+
console.error(tag, "Available networks in pubkeys:", [
|
|
5283
4856
|
...new Set(this.pubkeys.flatMap((p) => p.networks || []))
|
|
5284
4857
|
]);
|
|
5285
4858
|
throw new Error(errorMsg);
|
|
@@ -5289,43 +4862,43 @@ class SDK {
|
|
|
5289
4862
|
const xpubFound = pubkeysForNetwork.some((p) => p.type === "xpub" && p.pubkey);
|
|
5290
4863
|
if (!xpubFound) {
|
|
5291
4864
|
const errorMsg = `Cannot set asset context for UTXO chain ${asset.caip} - xpub required but not found`;
|
|
5292
|
-
console.error(
|
|
4865
|
+
console.error(tag, errorMsg);
|
|
5293
4866
|
throw new Error(errorMsg);
|
|
5294
4867
|
}
|
|
5295
4868
|
}
|
|
5296
4869
|
const hasValidAddress = pubkeysForNetwork.some((p) => p.address || p.master || p.pubkey);
|
|
5297
4870
|
if (!hasValidAddress) {
|
|
5298
4871
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no valid address found in pubkeys`;
|
|
5299
|
-
console.error(
|
|
4872
|
+
console.error(tag, errorMsg);
|
|
5300
4873
|
throw new Error(errorMsg);
|
|
5301
4874
|
}
|
|
5302
|
-
console.log(
|
|
4875
|
+
console.log(tag, `✅ Validated: Found ${pubkeysForNetwork.length} addresses for ${asset.networkId}`);
|
|
5303
4876
|
let freshPriceUsd = 0;
|
|
5304
4877
|
try {
|
|
5305
4878
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
5306
|
-
console.warn(
|
|
4879
|
+
console.warn(tag, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
5307
4880
|
} else {
|
|
5308
|
-
console.log(
|
|
4881
|
+
console.log(tag, "Fetching fresh market price for:", asset.caip);
|
|
5309
4882
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
5310
|
-
console.log(
|
|
4883
|
+
console.log(tag, "Market data response:", marketData);
|
|
5311
4884
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
5312
4885
|
freshPriceUsd = marketData.data[0];
|
|
5313
|
-
console.log(
|
|
4886
|
+
console.log(tag, "✅ Fresh market price:", freshPriceUsd);
|
|
5314
4887
|
} else {
|
|
5315
|
-
console.warn(
|
|
4888
|
+
console.warn(tag, "No market data returned for:", asset.caip);
|
|
5316
4889
|
}
|
|
5317
4890
|
}
|
|
5318
4891
|
} catch (marketError) {
|
|
5319
|
-
console.error(
|
|
4892
|
+
console.error(tag, "Error fetching market price:", marketError);
|
|
5320
4893
|
}
|
|
5321
4894
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
5322
|
-
console.log(
|
|
5323
|
-
let assetInfoDiscovery =
|
|
5324
|
-
console.log(
|
|
4895
|
+
console.log(tag, "assetInfo: ", assetInfo);
|
|
4896
|
+
let assetInfoDiscovery = assetData[asset.caip];
|
|
4897
|
+
console.log(tag, "assetInfoDiscovery: ", assetInfoDiscovery);
|
|
5325
4898
|
if (assetInfoDiscovery)
|
|
5326
4899
|
assetInfo = assetInfoDiscovery;
|
|
5327
4900
|
if (!assetInfo) {
|
|
5328
|
-
console.log(
|
|
4901
|
+
console.log(tag, "Building placeholder asset!");
|
|
5329
4902
|
assetInfo = {
|
|
5330
4903
|
caip: asset.caip.toLowerCase(),
|
|
5331
4904
|
networkId: asset.networkId,
|
|
@@ -5342,30 +4915,30 @@ class SDK {
|
|
|
5342
4915
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
5343
4916
|
if (balance > 0 && valueUsd > 0) {
|
|
5344
4917
|
priceValue = valueUsd / balance;
|
|
5345
|
-
console.log(
|
|
4918
|
+
console.log(tag, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
5346
4919
|
}
|
|
5347
4920
|
}
|
|
5348
4921
|
if (priceValue && priceValue > 0) {
|
|
5349
|
-
console.log(
|
|
4922
|
+
console.log(tag, "detected priceUsd from balance:", priceValue);
|
|
5350
4923
|
assetInfo.priceUsd = priceValue;
|
|
5351
4924
|
}
|
|
5352
4925
|
}
|
|
5353
4926
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
5354
4927
|
assetInfo.priceUsd = freshPriceUsd;
|
|
5355
|
-
console.log(
|
|
4928
|
+
console.log(tag, "✅ Using fresh market price:", freshPriceUsd);
|
|
5356
4929
|
let totalBalance = 0;
|
|
5357
4930
|
let totalValueUsd = 0;
|
|
5358
|
-
console.log(
|
|
4931
|
+
console.log(tag, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
5359
4932
|
for (const balanceEntry of matchingBalances) {
|
|
5360
4933
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5361
4934
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5362
4935
|
totalBalance += balance;
|
|
5363
4936
|
totalValueUsd += valueUsd;
|
|
5364
|
-
console.log(
|
|
4937
|
+
console.log(tag, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5365
4938
|
}
|
|
5366
4939
|
assetInfo.balance = totalBalance.toString();
|
|
5367
4940
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5368
|
-
console.log(
|
|
4941
|
+
console.log(tag, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5369
4942
|
}
|
|
5370
4943
|
const assetBalances = this.balances.filter((b2) => b2.caip === asset.caip);
|
|
5371
4944
|
const assetPubkeys = this.pubkeys.filter((p) => p.networks && Array.isArray(p.networks) && p.networks.includes(caipToNetworkId7(asset.caip)) || caipToNetworkId7(asset.caip).includes("eip155") && p.networks && Array.isArray(p.networks) && p.networks.some((n) => n.startsWith("eip155")));
|
|
@@ -5385,7 +4958,7 @@ class SDK {
|
|
|
5385
4958
|
const balanceAmount = parseFloat(balance.balance || 0);
|
|
5386
4959
|
balance.valueUsd = (balanceAmount * freshPriceUsd).toString();
|
|
5387
4960
|
}
|
|
5388
|
-
console.log(
|
|
4961
|
+
console.log(tag, "Updated all balances with fresh price data");
|
|
5389
4962
|
}
|
|
5390
4963
|
this.assetContext = finalAssetContext;
|
|
5391
4964
|
if (asset.isToken || asset.type === "token" || assetInfo.isToken || assetInfo.type === "token") {
|
|
@@ -5437,20 +5010,20 @@ class SDK {
|
|
|
5437
5010
|
const currentContextValid = this.pubkeyContext?.networks?.includes(networkId);
|
|
5438
5011
|
if (!this.pubkeyContext || !currentContextValid) {
|
|
5439
5012
|
this.pubkeyContext = assetPubkeys[0];
|
|
5440
|
-
console.log(
|
|
5013
|
+
console.log(tag, "Auto-set pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey);
|
|
5441
5014
|
} else {
|
|
5442
|
-
console.log(
|
|
5015
|
+
console.log(tag, "Preserving existing pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey, `(addressNList: [${(this.pubkeyContext.addressNList || this.pubkeyContext.addressNListMaster).join(", ")}])`);
|
|
5443
5016
|
}
|
|
5444
5017
|
}
|
|
5445
5018
|
this.events.emit("SET_ASSET_CONTEXT", this.assetContext);
|
|
5446
5019
|
return this.assetContext;
|
|
5447
5020
|
} catch (e) {
|
|
5448
|
-
console.error(
|
|
5021
|
+
console.error(tag, "e: ", e);
|
|
5449
5022
|
throw e;
|
|
5450
5023
|
}
|
|
5451
5024
|
};
|
|
5452
5025
|
this.setPubkeyContext = async function(pubkey) {
|
|
5453
|
-
let
|
|
5026
|
+
let tag = `${TAG9} | setPubkeyContext | `;
|
|
5454
5027
|
try {
|
|
5455
5028
|
if (!pubkey)
|
|
5456
5029
|
throw Error("pubkey is required");
|
|
@@ -5458,31 +5031,31 @@ class SDK {
|
|
|
5458
5031
|
throw Error("invalid pubkey: missing pubkey or address");
|
|
5459
5032
|
const exists = this.pubkeys.some((pk) => pk.pubkey === pubkey.pubkey || pk.address === pubkey.address || pk.pubkey === pubkey.address);
|
|
5460
5033
|
if (!exists) {
|
|
5461
|
-
console.warn(
|
|
5034
|
+
console.warn(tag, "Pubkey not found in current pubkeys array");
|
|
5462
5035
|
}
|
|
5463
5036
|
this.pubkeyContext = pubkey;
|
|
5464
|
-
console.log(
|
|
5037
|
+
console.log(tag, "Pubkey context set to:", pubkey.address || pubkey.pubkey, "note:", pubkey.note, "addressNList:", pubkey.addressNList || pubkey.addressNListMaster);
|
|
5465
5038
|
return true;
|
|
5466
5039
|
} catch (e) {
|
|
5467
|
-
console.error(
|
|
5040
|
+
console.error(tag, "e: ", e);
|
|
5468
5041
|
throw e;
|
|
5469
5042
|
}
|
|
5470
5043
|
};
|
|
5471
5044
|
this.setOutboundAssetContext = async function(asset) {
|
|
5472
|
-
const
|
|
5045
|
+
const tag = `${TAG9} | setOutputAssetContext | `;
|
|
5473
5046
|
try {
|
|
5474
|
-
console.log(
|
|
5047
|
+
console.log(tag, "0. asset: ", asset);
|
|
5475
5048
|
if (!asset) {
|
|
5476
5049
|
this.outboundAssetContext = null;
|
|
5477
5050
|
return;
|
|
5478
5051
|
}
|
|
5479
|
-
console.log(
|
|
5052
|
+
console.log(tag, "1 asset: ", asset);
|
|
5480
5053
|
if (!asset.caip)
|
|
5481
5054
|
throw Error("Invalid Asset! missing caip!");
|
|
5482
5055
|
if (!asset.networkId)
|
|
5483
5056
|
asset.networkId = caipToNetworkId7(asset.caip);
|
|
5484
|
-
console.log(
|
|
5485
|
-
console.log(
|
|
5057
|
+
console.log(tag, "networkId: ", asset.networkId);
|
|
5058
|
+
console.log(tag, "this.pubkeys: ", this.pubkeys);
|
|
5486
5059
|
const pubkey = this.pubkeys.find((p) => {
|
|
5487
5060
|
if (!p.networks || !Array.isArray(p.networks))
|
|
5488
5061
|
return false;
|
|
@@ -5497,23 +5070,23 @@ class SDK {
|
|
|
5497
5070
|
let freshPriceUsd = 0;
|
|
5498
5071
|
try {
|
|
5499
5072
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
5500
|
-
console.warn(
|
|
5073
|
+
console.warn(tag, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
5501
5074
|
} else {
|
|
5502
|
-
console.log(
|
|
5075
|
+
console.log(tag, "Fetching fresh market price for:", asset.caip);
|
|
5503
5076
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
5504
|
-
console.log(
|
|
5077
|
+
console.log(tag, "Market data response:", marketData);
|
|
5505
5078
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
5506
5079
|
freshPriceUsd = marketData.data[0];
|
|
5507
|
-
console.log(
|
|
5080
|
+
console.log(tag, "✅ Fresh market price:", freshPriceUsd);
|
|
5508
5081
|
} else {
|
|
5509
|
-
console.warn(
|
|
5082
|
+
console.warn(tag, "No market data returned for:", asset.caip);
|
|
5510
5083
|
}
|
|
5511
5084
|
}
|
|
5512
5085
|
} catch (marketError) {
|
|
5513
|
-
console.error(
|
|
5086
|
+
console.error(tag, "Error fetching market price:", marketError);
|
|
5514
5087
|
}
|
|
5515
5088
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
5516
|
-
console.log(
|
|
5089
|
+
console.log(tag, "assetInfo: ", assetInfo);
|
|
5517
5090
|
if (!assetInfo) {
|
|
5518
5091
|
assetInfo = {
|
|
5519
5092
|
caip: asset.caip.toLowerCase(),
|
|
@@ -5531,45 +5104,45 @@ class SDK {
|
|
|
5531
5104
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
5532
5105
|
if (balance > 0 && valueUsd > 0) {
|
|
5533
5106
|
priceValue = valueUsd / balance;
|
|
5534
|
-
console.log(
|
|
5107
|
+
console.log(tag, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
5535
5108
|
}
|
|
5536
5109
|
}
|
|
5537
5110
|
if (priceValue && priceValue > 0) {
|
|
5538
|
-
console.log(
|
|
5111
|
+
console.log(tag, "detected priceUsd from balance:", priceValue);
|
|
5539
5112
|
assetInfo.priceUsd = priceValue;
|
|
5540
5113
|
}
|
|
5541
5114
|
}
|
|
5542
5115
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
5543
5116
|
assetInfo.priceUsd = freshPriceUsd;
|
|
5544
|
-
console.log(
|
|
5117
|
+
console.log(tag, "✅ Using fresh market price:", freshPriceUsd);
|
|
5545
5118
|
let totalBalance = 0;
|
|
5546
5119
|
let totalValueUsd = 0;
|
|
5547
|
-
console.log(
|
|
5120
|
+
console.log(tag, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
5548
5121
|
for (const balanceEntry of matchingBalances) {
|
|
5549
5122
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5550
5123
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5551
5124
|
totalBalance += balance;
|
|
5552
5125
|
totalValueUsd += valueUsd;
|
|
5553
|
-
console.log(
|
|
5126
|
+
console.log(tag, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5554
5127
|
}
|
|
5555
5128
|
assetInfo.balance = totalBalance.toString();
|
|
5556
5129
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5557
|
-
console.log(
|
|
5130
|
+
console.log(tag, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5558
5131
|
}
|
|
5559
|
-
console.log(
|
|
5132
|
+
console.log(tag, "CHECKPOINT 1");
|
|
5560
5133
|
this.outboundAssetContext = { ...assetInfo, ...asset, ...pubkey };
|
|
5561
|
-
console.log(
|
|
5562
|
-
console.log(
|
|
5134
|
+
console.log(tag, "CHECKPOINT 3");
|
|
5135
|
+
console.log(tag, "outboundAssetContext: assetInfo: ", assetInfo);
|
|
5563
5136
|
if (asset.caip) {
|
|
5564
5137
|
this.outboundBlockchainContext = caipToNetworkId7(asset.caip);
|
|
5565
5138
|
} else if (asset.networkId) {
|
|
5566
5139
|
this.outboundBlockchainContext = asset.networkId;
|
|
5567
5140
|
}
|
|
5568
|
-
console.log(
|
|
5141
|
+
console.log(tag, "CHECKPOINT 4");
|
|
5569
5142
|
this.events.emit("SET_OUTBOUND_ASSET_CONTEXT", this.outboundAssetContext);
|
|
5570
5143
|
return this.outboundAssetContext;
|
|
5571
5144
|
} catch (e) {
|
|
5572
|
-
console.error(
|
|
5145
|
+
console.error(tag, "e: ", e);
|
|
5573
5146
|
throw e;
|
|
5574
5147
|
}
|
|
5575
5148
|
};
|