@pioneer-platform/pioneer-sdk 4.21.1 → 4.21.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +418 -329
- package/dist/index.es.js +418 -329
- package/dist/index.js +418 -329
- package/package.json +1 -1
- package/src/charts/custom-tokens.ts +144 -0
- package/src/charts/evm.ts +5 -0
- package/src/index.ts +19 -10
- package/src/utils/build-dashboard.ts +7 -0
package/dist/index.js
CHANGED
|
@@ -243,7 +243,7 @@ var require_lib = __commonJS((exports) => {
|
|
|
243
243
|
this.spec = spec;
|
|
244
244
|
this.queryKey = config.queryKey;
|
|
245
245
|
this.pioneer = {};
|
|
246
|
-
this.timeout = config.
|
|
246
|
+
this.timeout = config.timeout || 45000;
|
|
247
247
|
}
|
|
248
248
|
Pioneer2.prototype.init = function() {
|
|
249
249
|
return __awaiter(this, undefined, undefined, function() {
|
|
@@ -837,21 +837,101 @@ function createBalanceIdentifier(caip, pubkey) {
|
|
|
837
837
|
return `${caip}:${pubkey}`;
|
|
838
838
|
}
|
|
839
839
|
|
|
840
|
+
// src/charts/custom-tokens.ts
|
|
841
|
+
var tag = "| charts-custom-tokens |";
|
|
842
|
+
async function fetchCustomTokens(params, balances) {
|
|
843
|
+
const { pioneer, pubkeys, blockchains, context } = params;
|
|
844
|
+
console.log(tag, "Fetching custom tokens...");
|
|
845
|
+
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
846
|
+
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
847
|
+
if (!primaryAddress) {
|
|
848
|
+
console.log(tag, "No EVM address found, skipping custom tokens");
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
console.log(tag, "Using EVM address for custom tokens:", primaryAddress);
|
|
852
|
+
const supportedNetworks = blockchains.filter((net) => net.startsWith("eip155:"));
|
|
853
|
+
if (supportedNetworks.length === 0) {
|
|
854
|
+
console.log(tag, "No EVM networks for custom tokens");
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
console.log(tag, `Checking custom tokens on ${supportedNetworks.length} networks`);
|
|
858
|
+
for (const networkId of supportedNetworks) {
|
|
859
|
+
try {
|
|
860
|
+
const response = await pioneer.GetCustomTokens({ networkId, address: primaryAddress });
|
|
861
|
+
const customTokens = response?.data?.tokens || [];
|
|
862
|
+
console.log(tag, `Found ${customTokens.length} custom tokens on ${networkId}`);
|
|
863
|
+
for (const token of customTokens) {
|
|
864
|
+
const chartBalance = processCustomToken(token, primaryAddress, context, blockchains);
|
|
865
|
+
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
866
|
+
balances.push(chartBalance);
|
|
867
|
+
console.log(tag, `Added custom token: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
} catch (error) {
|
|
871
|
+
console.error(tag, `Error fetching custom tokens for ${networkId}:`, error.message);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
console.log(tag, `Custom tokens complete. Total balances: ${balances.length}`);
|
|
875
|
+
}
|
|
876
|
+
function processCustomToken(token, primaryAddress, context, blockchains) {
|
|
877
|
+
if (!token.assetCaip || !token.networkId) {
|
|
878
|
+
return null;
|
|
879
|
+
}
|
|
880
|
+
let extractedNetworkId = token.networkId;
|
|
881
|
+
if (token.assetCaip.includes("/denom:")) {
|
|
882
|
+
const parts = token.assetCaip.split("/denom:");
|
|
883
|
+
if (parts.length > 0) {
|
|
884
|
+
extractedNetworkId = parts[0];
|
|
885
|
+
}
|
|
886
|
+
} else if (token.networkId && token.networkId.includes("/")) {
|
|
887
|
+
extractedNetworkId = token.networkId.split("/")[0];
|
|
888
|
+
}
|
|
889
|
+
const isEip155 = extractedNetworkId.startsWith("eip155:");
|
|
890
|
+
if (!isEip155 && !blockchains.includes(extractedNetworkId)) {
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
const tokenAssetInfo = hydrateAssetData(token.assetCaip);
|
|
894
|
+
const tokenPubkey = token.pubkey || primaryAddress;
|
|
895
|
+
const chartBalance = {
|
|
896
|
+
context,
|
|
897
|
+
chart: "pioneer",
|
|
898
|
+
contextType: context.split(":")[0],
|
|
899
|
+
name: tokenAssetInfo?.name || token.token?.name || "Unknown Custom Token",
|
|
900
|
+
caip: token.assetCaip,
|
|
901
|
+
icon: tokenAssetInfo?.icon || token.token?.icon || "",
|
|
902
|
+
pubkey: tokenPubkey,
|
|
903
|
+
ticker: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
904
|
+
ref: `${context}${token.assetCaip}`,
|
|
905
|
+
identifier: createBalanceIdentifier(token.assetCaip, tokenPubkey),
|
|
906
|
+
networkId: extractedNetworkId,
|
|
907
|
+
symbol: tokenAssetInfo?.symbol || token.token?.symbol || "UNK",
|
|
908
|
+
type: tokenAssetInfo?.type || "erc20",
|
|
909
|
+
token: true,
|
|
910
|
+
decimal: tokenAssetInfo?.decimal || token.token?.decimal,
|
|
911
|
+
balance: token.token?.balance?.toString() || "0",
|
|
912
|
+
priceUsd: token.token?.price || 0,
|
|
913
|
+
valueUsd: token.token?.balanceUSD || 0,
|
|
914
|
+
updated: new Date().getTime()
|
|
915
|
+
};
|
|
916
|
+
return chartBalance;
|
|
917
|
+
}
|
|
918
|
+
|
|
840
919
|
// src/charts/evm.ts
|
|
841
|
-
var
|
|
920
|
+
var tag2 = "| charts-evm |";
|
|
842
921
|
async function getEvmCharts(params) {
|
|
843
922
|
const { blockchains, pioneer, pubkeys, context } = params;
|
|
844
923
|
const balances = [];
|
|
845
924
|
const evmPubkey = pubkeys.find((e) => e.networks && Array.isArray(e.networks) && e.networks.includes("eip155:*"));
|
|
846
925
|
const primaryAddress = evmPubkey?.address || evmPubkey?.master;
|
|
847
|
-
console.log(
|
|
848
|
-
console.log(
|
|
926
|
+
console.log(tag2, "Total pubkeys available:", pubkeys.length);
|
|
927
|
+
console.log(tag2, "Blockchains to process:", blockchains);
|
|
849
928
|
if (!primaryAddress) {
|
|
850
|
-
console.log(
|
|
929
|
+
console.log(tag2, "No EVM address found, skipping portfolio lookup (Zapper only supports Ethereum)");
|
|
851
930
|
return balances;
|
|
852
931
|
}
|
|
853
|
-
console.log(
|
|
932
|
+
console.log(tag2, "Using EVM address for portfolio:", primaryAddress);
|
|
854
933
|
await fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context);
|
|
934
|
+
await fetchCustomTokens({ blockchains, pioneer, pubkeys, context }, balances);
|
|
855
935
|
try {
|
|
856
936
|
let portfolio = await pioneer.GetPortfolio({
|
|
857
937
|
networkId: "eip155:1",
|
|
@@ -859,10 +939,10 @@ async function getEvmCharts(params) {
|
|
|
859
939
|
});
|
|
860
940
|
portfolio = portfolio.data?.data || portfolio.data;
|
|
861
941
|
if (!portfolio || !portfolio.balances) {
|
|
862
|
-
console.error(
|
|
942
|
+
console.error(tag2, "No portfolio.balances found:", portfolio);
|
|
863
943
|
return balances;
|
|
864
944
|
}
|
|
865
|
-
console.log(
|
|
945
|
+
console.log(tag2, `Portfolio returned ${portfolio.balances.length} balances`);
|
|
866
946
|
let processedCount = 0;
|
|
867
947
|
let skippedCount = 0;
|
|
868
948
|
for (const balance of portfolio.balances) {
|
|
@@ -876,9 +956,9 @@ async function getEvmCharts(params) {
|
|
|
876
956
|
skippedCount++;
|
|
877
957
|
}
|
|
878
958
|
}
|
|
879
|
-
console.log(
|
|
959
|
+
console.log(tag2, `Processed ${processedCount} balances, skipped ${skippedCount}`);
|
|
880
960
|
if (portfolio.tokens && portfolio.tokens.length > 0) {
|
|
881
|
-
console.log(
|
|
961
|
+
console.log(tag2, "Processing portfolio.tokens:", portfolio.tokens.length);
|
|
882
962
|
for (const token of portfolio.tokens) {
|
|
883
963
|
const processedToken = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
884
964
|
if (processedToken && !checkDuplicateBalance(balances, processedToken.caip, processedToken.pubkey)) {
|
|
@@ -887,13 +967,13 @@ async function getEvmCharts(params) {
|
|
|
887
967
|
}
|
|
888
968
|
}
|
|
889
969
|
} catch (e) {
|
|
890
|
-
console.error(
|
|
970
|
+
console.error(tag2, "Error fetching portfolio:", e);
|
|
891
971
|
}
|
|
892
972
|
return balances;
|
|
893
973
|
}
|
|
894
974
|
function processPortfolioBalance(balance, primaryAddress, context, blockchains) {
|
|
895
975
|
if (!balance.caip) {
|
|
896
|
-
console.error(
|
|
976
|
+
console.error(tag2, "No caip found for:", balance);
|
|
897
977
|
return null;
|
|
898
978
|
}
|
|
899
979
|
const networkId = balance.caip.split("/")[0];
|
|
@@ -911,7 +991,7 @@ function processPortfolioBalance(balance, primaryAddress, context, blockchains)
|
|
|
911
991
|
const valueNum = parseFloat(balance.valueUsd.toString());
|
|
912
992
|
if (balanceNum > 0 && valueNum > 0) {
|
|
913
993
|
calculatedPrice = valueNum / balanceNum;
|
|
914
|
-
console.log(
|
|
994
|
+
console.log(tag2, `Calculated price from value/balance: ${calculatedPrice} for ${balance.caip}`);
|
|
915
995
|
}
|
|
916
996
|
}
|
|
917
997
|
const chartBalance = {
|
|
@@ -985,70 +1065,70 @@ function processPortfolioToken(token, primaryAddress, context, blockchains) {
|
|
|
985
1065
|
return chartBalance;
|
|
986
1066
|
}
|
|
987
1067
|
async function fetchStableCoins(pioneer, primaryAddress, blockchains, balances, context) {
|
|
988
|
-
console.log(
|
|
1068
|
+
console.log(tag2, "Fetching stable coins for redundancy...");
|
|
989
1069
|
const supportedNetworks = ["eip155:1", "eip155:137", "eip155:8453", "eip155:56"];
|
|
990
1070
|
const networksToCheck = blockchains.filter((net) => supportedNetworks.includes(net));
|
|
991
1071
|
if (networksToCheck.length === 0) {
|
|
992
|
-
console.log(
|
|
1072
|
+
console.log(tag2, "No supported networks for stable coins");
|
|
993
1073
|
return;
|
|
994
1074
|
}
|
|
995
|
-
console.log(
|
|
1075
|
+
console.log(tag2, `Checking stable coins on ${networksToCheck.length} networks`);
|
|
996
1076
|
for (const networkId of networksToCheck) {
|
|
997
1077
|
try {
|
|
998
1078
|
const response = await pioneer.GetStableCoins({ networkId, address: primaryAddress });
|
|
999
1079
|
const stableCoins = response?.data?.tokens || [];
|
|
1000
|
-
console.log(
|
|
1080
|
+
console.log(tag2, `Found ${stableCoins.length} stable coins on ${networkId}`);
|
|
1001
1081
|
for (const token of stableCoins) {
|
|
1002
1082
|
const chartBalance = processPortfolioToken(token, primaryAddress, context, blockchains);
|
|
1003
1083
|
if (chartBalance && !checkDuplicateBalance(balances, chartBalance.caip, chartBalance.pubkey)) {
|
|
1004
1084
|
balances.push(chartBalance);
|
|
1005
|
-
console.log(
|
|
1085
|
+
console.log(tag2, `Added stable coin: ${chartBalance.symbol} = ${chartBalance.balance}`);
|
|
1006
1086
|
}
|
|
1007
1087
|
}
|
|
1008
1088
|
} catch (error) {
|
|
1009
|
-
console.error(
|
|
1089
|
+
console.error(tag2, `Error fetching stable coins for ${networkId}:`, error.message);
|
|
1010
1090
|
}
|
|
1011
1091
|
}
|
|
1012
|
-
console.log(
|
|
1092
|
+
console.log(tag2, `Stable coin redundancy complete. Total balances: ${balances.length}`);
|
|
1013
1093
|
}
|
|
1014
1094
|
|
|
1015
1095
|
// src/charts/maya.ts
|
|
1016
|
-
var
|
|
1096
|
+
var tag3 = "| charts-maya |";
|
|
1017
1097
|
async function getMayaCharts(params, existingBalances) {
|
|
1018
1098
|
const { blockchains, pioneer, pubkeys, context } = params;
|
|
1019
1099
|
const balances = [];
|
|
1020
1100
|
try {
|
|
1021
1101
|
const mayaPubkey = pubkeys.find((p) => p.networks && Array.isArray(p.networks) && p.networks.includes("cosmos:mayachain-mainnet-v1"));
|
|
1022
1102
|
if (!mayaPubkey || !mayaPubkey.address) {
|
|
1023
|
-
console.log(
|
|
1103
|
+
console.log(tag3, "No MAYA pubkey found, skipping MAYA token fetch");
|
|
1024
1104
|
return balances;
|
|
1025
1105
|
}
|
|
1026
1106
|
if (!blockchains.includes("cosmos:mayachain-mainnet-v1")) {
|
|
1027
|
-
console.log(
|
|
1107
|
+
console.log(tag3, "MAYA network not in blockchains list, skipping MAYA token fetch");
|
|
1028
1108
|
return balances;
|
|
1029
1109
|
}
|
|
1030
1110
|
const hasMayaToken = existingBalances.some((b2) => b2.caip === "cosmos:mayachain-mainnet-v1/denom:maya");
|
|
1031
1111
|
if (hasMayaToken) {
|
|
1032
|
-
console.log(
|
|
1112
|
+
console.log(tag3, "MAYA token already exists in balances, skipping");
|
|
1033
1113
|
return balances;
|
|
1034
1114
|
}
|
|
1035
|
-
console.log(
|
|
1036
|
-
console.log(
|
|
1115
|
+
console.log(tag3, "MAYA token not found in portfolio, fetching separately...");
|
|
1116
|
+
console.log(tag3, "MAYA pubkey address:", mayaPubkey.address);
|
|
1037
1117
|
const mayaBalanceResponse = await pioneer.GetPortfolioBalances([
|
|
1038
1118
|
{
|
|
1039
1119
|
caip: "cosmos:mayachain-mainnet-v1/denom:maya",
|
|
1040
1120
|
pubkey: mayaPubkey.address
|
|
1041
1121
|
}
|
|
1042
1122
|
]);
|
|
1043
|
-
console.log(
|
|
1123
|
+
console.log(tag3, "MAYA balance response:", JSON.stringify(mayaBalanceResponse?.data, null, 2));
|
|
1044
1124
|
if (!mayaBalanceResponse?.data || mayaBalanceResponse.data.length === 0) {
|
|
1045
|
-
console.log(
|
|
1125
|
+
console.log(tag3, "No MAYA token balance returned from GetPortfolioBalances API");
|
|
1046
1126
|
return balances;
|
|
1047
1127
|
}
|
|
1048
|
-
console.log(
|
|
1128
|
+
console.log(tag3, "Found MAYA token balances:", mayaBalanceResponse.data.length);
|
|
1049
1129
|
for (const mayaBalance of mayaBalanceResponse.data) {
|
|
1050
1130
|
if (mayaBalance.caip !== "cosmos:mayachain-mainnet-v1/denom:maya") {
|
|
1051
|
-
console.log(
|
|
1131
|
+
console.log(tag3, "Unexpected balance in MAYA response:", mayaBalance);
|
|
1052
1132
|
continue;
|
|
1053
1133
|
}
|
|
1054
1134
|
const mayaAssetInfo = hydrateAssetData(mayaBalance.caip);
|
|
@@ -1074,28 +1154,28 @@ async function getMayaCharts(params, existingBalances) {
|
|
|
1074
1154
|
valueUsd: parseFloat(mayaBalance.valueUsd) || 0,
|
|
1075
1155
|
updated: new Date().getTime()
|
|
1076
1156
|
};
|
|
1077
|
-
console.log(
|
|
1157
|
+
console.log(tag3, "Adding MAYA token to balances:", mayaTokenBalance);
|
|
1078
1158
|
balances.push(mayaTokenBalance);
|
|
1079
1159
|
}
|
|
1080
1160
|
} catch (mayaError) {
|
|
1081
|
-
console.error(
|
|
1161
|
+
console.error(tag3, "Error fetching MAYA token balance:", mayaError);
|
|
1082
1162
|
}
|
|
1083
1163
|
return balances;
|
|
1084
1164
|
}
|
|
1085
1165
|
|
|
1086
1166
|
// src/charts/cosmos-staking.ts
|
|
1087
|
-
var
|
|
1167
|
+
var tag4 = "| charts-cosmos-staking |";
|
|
1088
1168
|
async function getCosmosStakingCharts(params) {
|
|
1089
1169
|
const { blockchains, pioneer, pubkeys, context } = params;
|
|
1090
1170
|
const balances = [];
|
|
1091
1171
|
try {
|
|
1092
|
-
console.log(
|
|
1172
|
+
console.log(tag4, "Adding Cosmos staking positions to charts...");
|
|
1093
1173
|
const cosmosPubkeys = pubkeys.filter((p) => p.networks && Array.isArray(p.networks) && p.networks.some((n) => n.includes("cosmos:cosmoshub") || n.includes("cosmos:osmosis")));
|
|
1094
1174
|
if (cosmosPubkeys.length === 0) {
|
|
1095
|
-
console.log(
|
|
1175
|
+
console.log(tag4, "No cosmos pubkeys found for staking positions");
|
|
1096
1176
|
return balances;
|
|
1097
1177
|
}
|
|
1098
|
-
console.log(
|
|
1178
|
+
console.log(tag4, "Found cosmos pubkeys for staking:", cosmosPubkeys.length);
|
|
1099
1179
|
for (const cosmosPubkey of cosmosPubkeys) {
|
|
1100
1180
|
if (!cosmosPubkey.address) {
|
|
1101
1181
|
continue;
|
|
@@ -1109,20 +1189,20 @@ async function getCosmosStakingCharts(params) {
|
|
|
1109
1189
|
}
|
|
1110
1190
|
}
|
|
1111
1191
|
} catch (e) {
|
|
1112
|
-
console.error(
|
|
1192
|
+
console.error(tag4, "Error adding cosmos staking positions:", e);
|
|
1113
1193
|
}
|
|
1114
1194
|
return balances;
|
|
1115
1195
|
}
|
|
1116
1196
|
async function fetchStakingPositionsForNetwork(networkId, address, context, pioneer, balances) {
|
|
1117
1197
|
try {
|
|
1118
|
-
console.log(
|
|
1198
|
+
console.log(tag4, `Fetching staking positions for ${address} on ${networkId}...`);
|
|
1119
1199
|
let network;
|
|
1120
1200
|
if (networkId === "cosmos:cosmoshub-4") {
|
|
1121
1201
|
network = "cosmos";
|
|
1122
1202
|
} else if (networkId === "cosmos:osmosis-1") {
|
|
1123
1203
|
network = "osmosis";
|
|
1124
1204
|
} else {
|
|
1125
|
-
console.error(
|
|
1205
|
+
console.error(tag4, `Unsupported networkId for staking: ${networkId}`);
|
|
1126
1206
|
return;
|
|
1127
1207
|
}
|
|
1128
1208
|
const stakingResponse = await pioneer.GetStakingPositions({
|
|
@@ -1130,15 +1210,15 @@ async function fetchStakingPositionsForNetwork(networkId, address, context, pion
|
|
|
1130
1210
|
address
|
|
1131
1211
|
});
|
|
1132
1212
|
if (!stakingResponse?.data || !Array.isArray(stakingResponse.data)) {
|
|
1133
|
-
console.log(
|
|
1213
|
+
console.log(tag4, `No staking positions found for ${address} on ${networkId}`);
|
|
1134
1214
|
return;
|
|
1135
1215
|
}
|
|
1136
|
-
console.log(
|
|
1216
|
+
console.log(tag4, `Found ${stakingResponse.data.length} staking positions for ${networkId}`);
|
|
1137
1217
|
for (const position of stakingResponse.data) {
|
|
1138
1218
|
const processedPosition = processStakingPosition(position, address, context, networkId);
|
|
1139
1219
|
if (processedPosition && !checkDuplicateBalance(balances, processedPosition.caip, processedPosition.pubkey, processedPosition.validator)) {
|
|
1140
1220
|
balances.push(processedPosition);
|
|
1141
|
-
console.log(
|
|
1221
|
+
console.log(tag4, `Added ${position.type} position:`, {
|
|
1142
1222
|
balance: processedPosition.balance,
|
|
1143
1223
|
ticker: processedPosition.ticker,
|
|
1144
1224
|
valueUsd: processedPosition.valueUsd,
|
|
@@ -1147,7 +1227,7 @@ async function fetchStakingPositionsForNetwork(networkId, address, context, pion
|
|
|
1147
1227
|
}
|
|
1148
1228
|
}
|
|
1149
1229
|
} catch (stakingError) {
|
|
1150
|
-
console.error(
|
|
1230
|
+
console.error(tag4, `Error fetching staking positions for ${address} on ${networkId}:`, stakingError);
|
|
1151
1231
|
}
|
|
1152
1232
|
}
|
|
1153
1233
|
function processStakingPosition(position, address, context, networkId) {
|
|
@@ -1182,11 +1262,11 @@ function processStakingPosition(position, address, context, networkId) {
|
|
|
1182
1262
|
}
|
|
1183
1263
|
|
|
1184
1264
|
// src/charts/index.ts
|
|
1185
|
-
var
|
|
1265
|
+
var tag5 = "| getCharts |";
|
|
1186
1266
|
var getCharts = async (blockchains, pioneer, pubkeys, context) => {
|
|
1187
1267
|
try {
|
|
1188
1268
|
const balances = [];
|
|
1189
|
-
console.log(
|
|
1269
|
+
console.log(tag5, "init");
|
|
1190
1270
|
const params = {
|
|
1191
1271
|
blockchains,
|
|
1192
1272
|
pioneer,
|
|
@@ -1201,7 +1281,7 @@ var getCharts = async (blockchains, pioneer, pubkeys, context) => {
|
|
|
1201
1281
|
balances.push(...stakingBalances);
|
|
1202
1282
|
return balances;
|
|
1203
1283
|
} catch (error) {
|
|
1204
|
-
console.error(
|
|
1284
|
+
console.error(tag5, "Error processing charts:", error);
|
|
1205
1285
|
throw error;
|
|
1206
1286
|
}
|
|
1207
1287
|
};
|
|
@@ -1983,7 +2063,7 @@ async function fetchTokenPriceInUsd(pioneer, caip) {
|
|
|
1983
2063
|
}
|
|
1984
2064
|
}
|
|
1985
2065
|
async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, feeLevel = 5) {
|
|
1986
|
-
const
|
|
2066
|
+
const tag6 = TAG + " | createUnsignedEvmTx | ";
|
|
1987
2067
|
try {
|
|
1988
2068
|
if (!pioneer)
|
|
1989
2069
|
throw new Error("Failed to initialize Pioneer");
|
|
@@ -2004,11 +2084,11 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2004
2084
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2005
2085
|
}
|
|
2006
2086
|
const address = pubkeyContext.address || pubkeyContext.pubkey;
|
|
2007
|
-
console.log(
|
|
2087
|
+
console.log(tag6, "✅ Using FROM address from pubkeyContext:", address, "note:", pubkeyContext.note);
|
|
2008
2088
|
if (!address)
|
|
2009
2089
|
throw new Error("No address found for the specified network");
|
|
2010
2090
|
const gasPriceData = await pioneer.GetGasPriceByNetwork({ networkId });
|
|
2011
|
-
console.log(
|
|
2091
|
+
console.log(tag6, "Gas price data from API:", JSON.stringify(gasPriceData.data));
|
|
2012
2092
|
let gasPrice;
|
|
2013
2093
|
const defaultGasPrices = {
|
|
2014
2094
|
1: 30,
|
|
@@ -2024,75 +2104,75 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2024
2104
|
let selectedGasPrice;
|
|
2025
2105
|
if (feeLevel <= 2) {
|
|
2026
2106
|
selectedGasPrice = gasPriceData.data.slow || gasPriceData.data.average || gasPriceData.data.fastest;
|
|
2027
|
-
console.log(
|
|
2107
|
+
console.log(tag6, "Selecting SLOW gas price from API");
|
|
2028
2108
|
} else if (feeLevel >= 8) {
|
|
2029
2109
|
selectedGasPrice = gasPriceData.data.fastest || gasPriceData.data.fast || gasPriceData.data.average;
|
|
2030
|
-
console.log(
|
|
2110
|
+
console.log(tag6, "Selecting FAST gas price from API");
|
|
2031
2111
|
} else {
|
|
2032
2112
|
selectedGasPrice = gasPriceData.data.average || gasPriceData.data.fast || gasPriceData.data.fastest;
|
|
2033
|
-
console.log(
|
|
2113
|
+
console.log(tag6, "Selecting AVERAGE gas price from API");
|
|
2034
2114
|
}
|
|
2035
2115
|
let gasPriceNum;
|
|
2036
2116
|
if (selectedGasPrice === undefined || selectedGasPrice === null) {
|
|
2037
|
-
console.warn(
|
|
2117
|
+
console.warn(tag6, "No valid gas price found in API response, using fallback:", fallbackGasGwei, "gwei");
|
|
2038
2118
|
gasPriceNum = fallbackGasGwei;
|
|
2039
2119
|
} else {
|
|
2040
2120
|
gasPriceNum = typeof selectedGasPrice === "string" ? parseFloat(selectedGasPrice) : selectedGasPrice;
|
|
2041
2121
|
if (isNaN(gasPriceNum) || !isFinite(gasPriceNum)) {
|
|
2042
|
-
console.warn(
|
|
2122
|
+
console.warn(tag6, "Invalid gas price (NaN or Infinite):", selectedGasPrice, "- using fallback:", fallbackGasGwei, "gwei");
|
|
2043
2123
|
gasPriceNum = fallbackGasGwei;
|
|
2044
2124
|
}
|
|
2045
2125
|
}
|
|
2046
2126
|
gasPrice = BigInt(Math.round(gasPriceNum * 1e9));
|
|
2047
2127
|
if (gasPrice < MIN_GAS_PRICE_WEI) {
|
|
2048
|
-
console.warn(
|
|
2128
|
+
console.warn(tag6, "Gas price from API too low:", gasPrice.toString(), "wei - using minimum:", MIN_GAS_PRICE_WEI.toString());
|
|
2049
2129
|
gasPrice = MIN_GAS_PRICE_WEI;
|
|
2050
2130
|
}
|
|
2051
2131
|
} else {
|
|
2052
2132
|
let gasPriceNum;
|
|
2053
2133
|
if (gasPriceData.data === undefined || gasPriceData.data === null) {
|
|
2054
|
-
console.warn(
|
|
2134
|
+
console.warn(tag6, "Gas price API returned null/undefined, using fallback:", fallbackGasGwei, "gwei");
|
|
2055
2135
|
gasPriceNum = fallbackGasGwei;
|
|
2056
2136
|
} else {
|
|
2057
2137
|
gasPriceNum = typeof gasPriceData.data === "string" ? parseFloat(gasPriceData.data) : gasPriceData.data;
|
|
2058
2138
|
if (isNaN(gasPriceNum) || !isFinite(gasPriceNum)) {
|
|
2059
|
-
console.warn(
|
|
2139
|
+
console.warn(tag6, "Invalid gas price (NaN or Infinite):", gasPriceData.data, "- using fallback:", fallbackGasGwei, "gwei");
|
|
2060
2140
|
gasPriceNum = fallbackGasGwei;
|
|
2061
2141
|
}
|
|
2062
2142
|
}
|
|
2063
2143
|
const baseGasPrice = BigInt(Math.round(gasPriceNum * 1e9));
|
|
2064
2144
|
if (feeLevel <= 2) {
|
|
2065
2145
|
gasPrice = baseGasPrice * BigInt(80) / BigInt(100);
|
|
2066
|
-
console.log(
|
|
2146
|
+
console.log(tag6, "Using SLOW gas price (80% of base)");
|
|
2067
2147
|
} else if (feeLevel >= 8) {
|
|
2068
2148
|
gasPrice = baseGasPrice * BigInt(150) / BigInt(100);
|
|
2069
|
-
console.log(
|
|
2149
|
+
console.log(tag6, "Using FAST gas price (150% of base)");
|
|
2070
2150
|
} else {
|
|
2071
2151
|
gasPrice = baseGasPrice;
|
|
2072
|
-
console.log(
|
|
2152
|
+
console.log(tag6, "Using AVERAGE gas price (100% of base)");
|
|
2073
2153
|
}
|
|
2074
2154
|
if (gasPrice < MIN_GAS_PRICE_WEI) {
|
|
2075
|
-
console.warn(
|
|
2155
|
+
console.warn(tag6, "Gas price too low:", gasPrice.toString(), "wei - using minimum:", MIN_GAS_PRICE_WEI.toString());
|
|
2076
2156
|
gasPrice = MIN_GAS_PRICE_WEI;
|
|
2077
2157
|
}
|
|
2078
2158
|
}
|
|
2079
|
-
console.log(
|
|
2159
|
+
console.log(tag6, "Final gasPrice:", gasPrice.toString(), "wei (", Number(gasPrice) / 1e9, "gwei)");
|
|
2080
2160
|
let nonce;
|
|
2081
2161
|
try {
|
|
2082
2162
|
const nonceData = await pioneer.GetNonceByNetwork({ networkId, address });
|
|
2083
2163
|
nonce = nonceData.data.nonce;
|
|
2084
2164
|
if (nonce === undefined || nonce === null) {
|
|
2085
|
-
console.log(
|
|
2165
|
+
console.log(tag6, "No nonce found for address (likely fresh address), defaulting to 0");
|
|
2086
2166
|
nonce = 0;
|
|
2087
2167
|
}
|
|
2088
2168
|
} catch (nonceError) {
|
|
2089
|
-
console.log(
|
|
2169
|
+
console.log(tag6, "Failed to fetch nonce (likely fresh address):", nonceError.message, "- defaulting to 0");
|
|
2090
2170
|
nonce = 0;
|
|
2091
2171
|
}
|
|
2092
2172
|
const balanceData = await pioneer.GetBalanceAddressByNetwork({ networkId, address });
|
|
2093
2173
|
const balanceEth = parseFloat(balanceData.data.nativeBalance || balanceData.data.balance || "0");
|
|
2094
2174
|
const balance = BigInt(Math.round(balanceEth * 1000000000000000000));
|
|
2095
|
-
console.log(
|
|
2175
|
+
console.log(tag6, "Native ETH balance from API:", balanceData.data.nativeBalance || balanceData.data.balance, "ETH (", balance.toString(), "wei)");
|
|
2096
2176
|
if (balance <= 0n)
|
|
2097
2177
|
throw new Error("Wallet balance is zero");
|
|
2098
2178
|
const assetType = classifyCaipEvm(caip);
|
|
@@ -2105,7 +2185,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2105
2185
|
let gasLimit;
|
|
2106
2186
|
if (isThorchainOperation) {
|
|
2107
2187
|
gasLimit = BigInt(120000);
|
|
2108
|
-
console.log(
|
|
2188
|
+
console.log(tag6, "Using higher gas limit for THORChain swap:", gasLimit.toString());
|
|
2109
2189
|
} else {
|
|
2110
2190
|
gasLimit = chainId === 1 ? BigInt(21000) : BigInt(25000);
|
|
2111
2191
|
}
|
|
@@ -2121,7 +2201,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2121
2201
|
}
|
|
2122
2202
|
const buffer = BigInt(100);
|
|
2123
2203
|
amountWei = balance - gasFee - buffer;
|
|
2124
|
-
console.log(
|
|
2204
|
+
console.log(tag6, "isMax calculation - balance:", balance.toString(), "gasFee:", gasFee.toString(), "buffer:", buffer.toString(), "amountWei:", amountWei.toString());
|
|
2125
2205
|
} else {
|
|
2126
2206
|
amountWei = BigInt(Math.round(amount * 1000000000000000000));
|
|
2127
2207
|
if (amountWei + gasFee > balance) {
|
|
@@ -2131,14 +2211,14 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2131
2211
|
const isThorchainSwap = memo && (memo.startsWith("=") || memo.startsWith("SWAP") || memo.includes(":"));
|
|
2132
2212
|
let txData = "0x";
|
|
2133
2213
|
if (isThorchainSwap) {
|
|
2134
|
-
console.log(
|
|
2214
|
+
console.log(tag6, "Detected THORChain swap, encoding deposit data for memo:", memo);
|
|
2135
2215
|
let fixedMemo = memo;
|
|
2136
2216
|
if (memo.startsWith("=:b:") || memo.startsWith("=:btc:")) {
|
|
2137
2217
|
fixedMemo = memo.replace(/^=:(b|btc):/, "=:BTC.BTC:");
|
|
2138
|
-
console.log(
|
|
2218
|
+
console.log(tag6, "Fixed Bitcoin swap memo from:", memo, "to:", fixedMemo);
|
|
2139
2219
|
} else if (memo.startsWith("=:e:") || memo.startsWith("=:eth:")) {
|
|
2140
2220
|
fixedMemo = memo.replace(/^=:(e|eth):/, "=:ETH.ETH:");
|
|
2141
|
-
console.log(
|
|
2221
|
+
console.log(tag6, "Fixed Ethereum swap memo from:", memo, "to:", fixedMemo);
|
|
2142
2222
|
}
|
|
2143
2223
|
if (fixedMemo.length > 250) {
|
|
2144
2224
|
throw new Error(`Memo too long for THORChain: ${fixedMemo.length} bytes (max 250)`);
|
|
@@ -2154,14 +2234,14 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2154
2234
|
if (ethInbound) {
|
|
2155
2235
|
vaultAddress = ethInbound.address;
|
|
2156
2236
|
routerAddress = ethInbound.router || to;
|
|
2157
|
-
console.log(
|
|
2237
|
+
console.log(tag6, "Using THORChain inbound addresses - vault:", vaultAddress, "router:", routerAddress);
|
|
2158
2238
|
to = routerAddress;
|
|
2159
2239
|
} else {
|
|
2160
2240
|
throw new Error("ETH inbound is halted or not found - cannot proceed with swap");
|
|
2161
2241
|
}
|
|
2162
2242
|
}
|
|
2163
2243
|
} catch (fetchError) {
|
|
2164
|
-
console.error(
|
|
2244
|
+
console.error(tag6, "Failed to fetch inbound addresses:", fetchError);
|
|
2165
2245
|
throw new Error(`Cannot proceed with THORChain swap - failed to fetch inbound addresses: ${fetchError.message}`);
|
|
2166
2246
|
}
|
|
2167
2247
|
if (vaultAddress === "0x0000000000000000000000000000000000000000") {
|
|
@@ -2181,7 +2261,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2181
2261
|
const paddingLength = (32 - memoBytes.length % 32) % 32;
|
|
2182
2262
|
const memoPadded = memoHex + "0".repeat(paddingLength * 2);
|
|
2183
2263
|
txData = "0x" + functionSelector + vaultPadded + assetPadded + amountPadded + stringOffset + expiryPadded + stringLength + memoPadded;
|
|
2184
|
-
console.log(
|
|
2264
|
+
console.log(tag6, "Encoded THORChain depositWithExpiry data:", {
|
|
2185
2265
|
functionSelector: "0x" + functionSelector,
|
|
2186
2266
|
vault: vaultAddress,
|
|
2187
2267
|
asset: assetAddress,
|
|
@@ -2191,9 +2271,9 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2191
2271
|
stringOffset: "0x" + stringOffset,
|
|
2192
2272
|
fullData: txData
|
|
2193
2273
|
});
|
|
2194
|
-
console.log(
|
|
2274
|
+
console.log(tag6, "Native ETH swap - value will be set to:", amountWei.toString(), "wei");
|
|
2195
2275
|
} catch (error) {
|
|
2196
|
-
console.error(
|
|
2276
|
+
console.error(tag6, "Error encoding THORChain deposit:", error);
|
|
2197
2277
|
throw new Error(`Failed to encode THORChain swap: ${error.message}`);
|
|
2198
2278
|
}
|
|
2199
2279
|
} else if (memo) {
|
|
@@ -2212,19 +2292,19 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2212
2292
|
}
|
|
2213
2293
|
case "erc20": {
|
|
2214
2294
|
const contractAddress = extractContractAddressFromCaip(caip);
|
|
2215
|
-
console.log(
|
|
2295
|
+
console.log(tag6, "Fetching token decimals from contract:", contractAddress, "on network:", networkId);
|
|
2216
2296
|
let tokenDecimals;
|
|
2217
2297
|
try {
|
|
2218
|
-
console.log(
|
|
2298
|
+
console.log(tag6, "Fetching token decimals via pioneer-server API for", contractAddress, "on", networkId);
|
|
2219
2299
|
const decimalsResponse = await pioneer.GetTokenDecimals({
|
|
2220
2300
|
networkId,
|
|
2221
2301
|
contractAddress
|
|
2222
2302
|
});
|
|
2223
2303
|
tokenDecimals = Number(decimalsResponse.data.decimals);
|
|
2224
|
-
console.log(
|
|
2304
|
+
console.log(tag6, "✅ Fetched decimals from pioneer-server:", tokenDecimals);
|
|
2225
2305
|
} catch (error) {
|
|
2226
|
-
console.error(
|
|
2227
|
-
console.warn(
|
|
2306
|
+
console.error(tag6, "Failed to fetch token decimals from pioneer-server:", error);
|
|
2307
|
+
console.warn(tag6, "⚠️ FALLBACK: Using default 18 decimals - THIS MAY BE INCORRECT!");
|
|
2228
2308
|
tokenDecimals = 18;
|
|
2229
2309
|
}
|
|
2230
2310
|
const tokenMultiplier = 10n ** BigInt(tokenDecimals);
|
|
@@ -2245,7 +2325,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2245
2325
|
amountWei = tokenBalance;
|
|
2246
2326
|
} else {
|
|
2247
2327
|
amountWei = BigInt(Math.round(amount * Number(tokenMultiplier)));
|
|
2248
|
-
console.log(
|
|
2328
|
+
console.log(tag6, "Token amount calculation:", {
|
|
2249
2329
|
inputAmount: amount,
|
|
2250
2330
|
decimals: tokenDecimals,
|
|
2251
2331
|
multiplier: tokenMultiplier,
|
|
@@ -2281,23 +2361,23 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2281
2361
|
}
|
|
2282
2362
|
if (pubkeyContext.addressNListMaster) {
|
|
2283
2363
|
unsignedTx.addressNList = pubkeyContext.addressNListMaster;
|
|
2284
|
-
console.log(
|
|
2364
|
+
console.log(tag6, "✅ Using addressNListMaster from pubkey context:", unsignedTx.addressNList, "for address:", address);
|
|
2285
2365
|
} else if (pubkeyContext.pathMaster) {
|
|
2286
2366
|
unsignedTx.addressNList = bip32ToAddressNList(pubkeyContext.pathMaster);
|
|
2287
|
-
console.log(
|
|
2367
|
+
console.log(tag6, "✅ Converted pathMaster to addressNList:", pubkeyContext.pathMaster, "→", unsignedTx.addressNList);
|
|
2288
2368
|
} else if (pubkeyContext.addressNList) {
|
|
2289
2369
|
unsignedTx.addressNList = pubkeyContext.addressNList;
|
|
2290
|
-
console.log(
|
|
2370
|
+
console.log(tag6, "✅ Using addressNList from pubkey context:", unsignedTx.addressNList);
|
|
2291
2371
|
} else if (pubkeyContext.path) {
|
|
2292
2372
|
unsignedTx.addressNList = bip32ToAddressNList(pubkeyContext.path);
|
|
2293
|
-
console.log(
|
|
2373
|
+
console.log(tag6, "⚠️ Using regular path (not master):", pubkeyContext.path, "→", unsignedTx.addressNList);
|
|
2294
2374
|
} else {
|
|
2295
2375
|
unsignedTx.addressNList = [2147483648 + 44, 2147483648 + 60, 2147483648, 0, 0];
|
|
2296
|
-
console.warn(
|
|
2376
|
+
console.warn(tag6, "⚠️ No path info in pubkey context, using default account 0");
|
|
2297
2377
|
}
|
|
2298
2378
|
return unsignedTx;
|
|
2299
2379
|
} catch (error) {
|
|
2300
|
-
console.error(
|
|
2380
|
+
console.error(tag6, "Error:", error.message);
|
|
2301
2381
|
throw error;
|
|
2302
2382
|
}
|
|
2303
2383
|
}
|
|
@@ -2306,7 +2386,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
|
|
|
2306
2386
|
import { caipToNetworkId as caipToNetworkId2 } from "@pioneer-platform/pioneer-caip";
|
|
2307
2387
|
var TAG2 = " | createUnsignedUxtoTx | ";
|
|
2308
2388
|
async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax) {
|
|
2309
|
-
let
|
|
2389
|
+
let tag6 = TAG2 + " | createUnsignedRippleTx | ";
|
|
2310
2390
|
try {
|
|
2311
2391
|
if (!pioneer)
|
|
2312
2392
|
throw new Error("Failed to init! pioneer");
|
|
@@ -2317,7 +2397,7 @@ async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer,
|
|
|
2317
2397
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
2318
2398
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2319
2399
|
}
|
|
2320
|
-
console.log(
|
|
2400
|
+
console.log(tag6, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
2321
2401
|
address: pubkeyContext.address
|
|
2322
2402
|
});
|
|
2323
2403
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
@@ -2385,7 +2465,7 @@ async function createUnsignedRippleTx(caip, to, amount, memo, pubkeys, pioneer,
|
|
|
2385
2465
|
};
|
|
2386
2466
|
return unsignedTx;
|
|
2387
2467
|
} catch (error) {
|
|
2388
|
-
console.error(
|
|
2468
|
+
console.error(tag6, "Error:", error);
|
|
2389
2469
|
throw error;
|
|
2390
2470
|
}
|
|
2391
2471
|
}
|
|
@@ -2525,7 +2605,7 @@ var thorchainDepositTemplate = (params) => ({
|
|
|
2525
2605
|
// src/txbuilder/createUnsignedTendermintTx.ts
|
|
2526
2606
|
var TAG3 = " | createUnsignedTendermintTx | ";
|
|
2527
2607
|
async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, to) {
|
|
2528
|
-
const
|
|
2608
|
+
const tag6 = TAG3 + " | createUnsignedTendermintTx | ";
|
|
2529
2609
|
try {
|
|
2530
2610
|
if (!pioneer)
|
|
2531
2611
|
throw new Error("Failed to init! pioneer");
|
|
@@ -2536,7 +2616,7 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2536
2616
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
2537
2617
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2538
2618
|
}
|
|
2539
|
-
console.log(
|
|
2619
|
+
console.log(tag6, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
2540
2620
|
address: pubkeyContext.address,
|
|
2541
2621
|
addressNList: pubkeyContext.addressNList || pubkeyContext.addressNListMaster
|
|
2542
2622
|
});
|
|
@@ -2559,11 +2639,11 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2559
2639
|
}
|
|
2560
2640
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
2561
2641
|
let asset = caip.split(":")[1];
|
|
2562
|
-
console.log(
|
|
2642
|
+
console.log(tag6, `\uD83D\uDD0D Fetching account info for address: ${fromAddress}`);
|
|
2563
2643
|
const accountInfo = (await pioneer.GetAccountInfo({ network: chain, address: fromAddress })).data;
|
|
2564
|
-
console.log(
|
|
2644
|
+
console.log(tag6, "\uD83D\uDCCB accountInfo:", JSON.stringify(accountInfo, null, 2));
|
|
2565
2645
|
let balanceInfo = await pioneer.GetPubkeyBalance({ asset: chain, pubkey: fromAddress });
|
|
2566
|
-
console.log(
|
|
2646
|
+
console.log(tag6, `\uD83D\uDCB0 balanceInfo:`, balanceInfo);
|
|
2567
2647
|
let account_number, sequence;
|
|
2568
2648
|
if (accountInfo.account) {
|
|
2569
2649
|
account_number = accountInfo.account.account_number || "0";
|
|
@@ -2574,13 +2654,13 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2574
2654
|
} else {
|
|
2575
2655
|
throw new Error(`Unexpected account info format for ${networkId}: ${JSON.stringify(accountInfo)}`);
|
|
2576
2656
|
}
|
|
2577
|
-
console.log(
|
|
2657
|
+
console.log(tag6, `\uD83D\uDCCA Extracted account_number: ${account_number}, sequence: ${sequence}`);
|
|
2578
2658
|
if (account_number === "0" || account_number === 0) {
|
|
2579
|
-
console.log(
|
|
2580
|
-
console.log(
|
|
2581
|
-
console.log(
|
|
2582
|
-
console.log(
|
|
2583
|
-
console.log(
|
|
2659
|
+
console.log(tag6, `⚠️ WARNING: Account number is 0 from Pioneer API`);
|
|
2660
|
+
console.log(tag6, ` This is likely due to stale Pioneer API cache`);
|
|
2661
|
+
console.log(tag6, ` The mayachain-network module queries nodes directly but Pioneer API may be cached`);
|
|
2662
|
+
console.log(tag6, ` Proceeding with account_number: 0 but transaction will likely fail`);
|
|
2663
|
+
console.log(tag6, ` TODO: Fix Pioneer API to use fresh data from mayachain-network module`);
|
|
2584
2664
|
}
|
|
2585
2665
|
const fees = {
|
|
2586
2666
|
"cosmos:thorchain-mainnet-v1": 0.02,
|
|
@@ -2735,7 +2815,7 @@ async function createUnsignedTendermintTx(caip, type, amount, memo, pubkeys, pio
|
|
|
2735
2815
|
}
|
|
2736
2816
|
}
|
|
2737
2817
|
} catch (error) {
|
|
2738
|
-
console.error(
|
|
2818
|
+
console.error(tag6, "Error:", error);
|
|
2739
2819
|
throw error;
|
|
2740
2820
|
}
|
|
2741
2821
|
}
|
|
@@ -2764,7 +2844,7 @@ function getCoinTypeFromNetworkId(networkId) {
|
|
|
2764
2844
|
return coinType;
|
|
2765
2845
|
}
|
|
2766
2846
|
async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pubkeyContext, isMax, feeLevel = 5, changeScriptType) {
|
|
2767
|
-
let
|
|
2847
|
+
let tag6 = " | createUnsignedUxtoTx | ";
|
|
2768
2848
|
try {
|
|
2769
2849
|
if (!pioneer)
|
|
2770
2850
|
throw Error("Failed to init! pioneer");
|
|
@@ -2775,7 +2855,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2775
2855
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
2776
2856
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
2777
2857
|
}
|
|
2778
|
-
console.log(
|
|
2858
|
+
console.log(tag6, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
2779
2859
|
address: pubkeyContext.address,
|
|
2780
2860
|
scriptType: pubkeyContext.scriptType
|
|
2781
2861
|
});
|
|
@@ -2786,15 +2866,15 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2786
2866
|
const isSegwit = segwitNetworks.includes(networkId);
|
|
2787
2867
|
let chain = NetworkIdToChain[networkId];
|
|
2788
2868
|
const coinType = getCoinTypeFromNetworkId(networkId);
|
|
2789
|
-
console.log(`${
|
|
2869
|
+
console.log(`${tag6}: Using SLIP-44 coin type ${coinType} for ${chain}`);
|
|
2790
2870
|
const utxos = [];
|
|
2791
2871
|
for (const pubkey of relevantPubkeys) {
|
|
2792
2872
|
try {
|
|
2793
2873
|
let utxosResp = await pioneer.ListUnspent({ network: chain, xpub: pubkey.pubkey });
|
|
2794
2874
|
utxosResp = utxosResp.data;
|
|
2795
|
-
console.log(`${
|
|
2875
|
+
console.log(`${tag6}: ListUnspent response for ${pubkey.scriptType}:`, utxosResp?.length || 0, "UTXOs");
|
|
2796
2876
|
if (!utxosResp || !Array.isArray(utxosResp)) {
|
|
2797
|
-
console.warn(`${
|
|
2877
|
+
console.warn(`${tag6}: Invalid or empty UTXO response for ${pubkey.pubkey}`);
|
|
2798
2878
|
continue;
|
|
2799
2879
|
}
|
|
2800
2880
|
let scriptType = pubkey.scriptType;
|
|
@@ -2803,10 +2883,10 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2803
2883
|
}
|
|
2804
2884
|
utxos.push(...utxosResp);
|
|
2805
2885
|
} catch (error) {
|
|
2806
|
-
console.error(`${
|
|
2886
|
+
console.error(`${tag6}: Failed to fetch UTXOs for ${pubkey.pubkey}:`, error);
|
|
2807
2887
|
}
|
|
2808
2888
|
}
|
|
2809
|
-
console.log(`${
|
|
2889
|
+
console.log(`${tag6}: Total UTXOs collected:`, utxos.length);
|
|
2810
2890
|
if (!utxos || utxos.length === 0)
|
|
2811
2891
|
throw Error("No UTXOs found across all addresses");
|
|
2812
2892
|
for (const utxo of utxos) {
|
|
@@ -2819,14 +2899,14 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2819
2899
|
}, {});
|
|
2820
2900
|
const mostCommonInputType = Object.entries(scriptTypeCount).sort(([, a2], [, b3]) => b3 - a2)[0]?.[0] || "p2pkh";
|
|
2821
2901
|
const actualChangeScriptType = changeScriptType || mostCommonInputType || relevantPubkeys[0]?.scriptType || "p2pkh";
|
|
2822
|
-
console.log(`${
|
|
2823
|
-
console.log(`${
|
|
2902
|
+
console.log(`${tag6}: Input script types:`, scriptTypeCount);
|
|
2903
|
+
console.log(`${tag6}: Using change script type: ${actualChangeScriptType} (matches inputs: ${mostCommonInputType})`);
|
|
2824
2904
|
const changeXpubInfo = relevantPubkeys.find((pk) => pk.scriptType === actualChangeScriptType);
|
|
2825
2905
|
if (!changeXpubInfo) {
|
|
2826
2906
|
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.`);
|
|
2827
2907
|
}
|
|
2828
2908
|
const changeXpub = changeXpubInfo.pubkey;
|
|
2829
|
-
console.log(`${
|
|
2909
|
+
console.log(`${tag6}: Change xpub selected for ${actualChangeScriptType}:`, changeXpub?.substring(0, 10) + "...");
|
|
2830
2910
|
let changeAddressIndex = await pioneer.GetChangeAddress({
|
|
2831
2911
|
network: chain,
|
|
2832
2912
|
xpub: changeXpub
|
|
@@ -2846,7 +2926,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2846
2926
|
break;
|
|
2847
2927
|
}
|
|
2848
2928
|
const path = bipPath;
|
|
2849
|
-
console.log(`${
|
|
2929
|
+
console.log(`${tag6}: Change address path: ${path} (coin type: ${coinType}, index: ${changeAddressIndex})`);
|
|
2850
2930
|
const changeAddress = {
|
|
2851
2931
|
path,
|
|
2852
2932
|
isChange: true,
|
|
@@ -2856,7 +2936,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2856
2936
|
};
|
|
2857
2937
|
let feeRateFromNode;
|
|
2858
2938
|
if (networkId === "bip122:00000000001a91e3dace36e2be3bf030") {
|
|
2859
|
-
console.log(`${
|
|
2939
|
+
console.log(`${tag6}: Using hardcoded fees for DOGE (10 sat/byte)`);
|
|
2860
2940
|
feeRateFromNode = {
|
|
2861
2941
|
slow: 10,
|
|
2862
2942
|
average: 10,
|
|
@@ -2869,19 +2949,19 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2869
2949
|
try {
|
|
2870
2950
|
let feeResponse;
|
|
2871
2951
|
if (pioneer.GetFeeRateByNetwork) {
|
|
2872
|
-
console.log(`${
|
|
2952
|
+
console.log(`${tag6}: Trying GetFeeRateByNetwork for ${networkId}`);
|
|
2873
2953
|
feeResponse = await pioneer.GetFeeRateByNetwork({ networkId });
|
|
2874
2954
|
} else {
|
|
2875
|
-
console.log(`${
|
|
2955
|
+
console.log(`${tag6}: Using GetFeeRate for ${networkId}`);
|
|
2876
2956
|
feeResponse = await pioneer.GetFeeRate({ networkId });
|
|
2877
2957
|
}
|
|
2878
2958
|
feeRateFromNode = feeResponse.data;
|
|
2879
|
-
console.log(`${
|
|
2959
|
+
console.log(`${tag6}: Got fee rates from API:`, JSON.stringify(feeRateFromNode, null, 2));
|
|
2880
2960
|
const conversionThreshold = 500;
|
|
2881
2961
|
const needsConversion = feeRateFromNode.slow && feeRateFromNode.slow > conversionThreshold || feeRateFromNode.average && feeRateFromNode.average > conversionThreshold || feeRateFromNode.fast && feeRateFromNode.fast > conversionThreshold || feeRateFromNode.fastest && feeRateFromNode.fastest > conversionThreshold;
|
|
2882
2962
|
if (needsConversion) {
|
|
2883
|
-
console.warn(`${
|
|
2884
|
-
console.warn(`${
|
|
2963
|
+
console.warn(`${tag6}: Detected wrong units - values appear to be in sat/kB instead of sat/byte`);
|
|
2964
|
+
console.warn(`${tag6}: Original values:`, {
|
|
2885
2965
|
slow: feeRateFromNode.slow,
|
|
2886
2966
|
average: feeRateFromNode.average,
|
|
2887
2967
|
fast: feeRateFromNode.fast,
|
|
@@ -2895,12 +2975,12 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2895
2975
|
feeRateFromNode.fast = feeRateFromNode.fast / 1000;
|
|
2896
2976
|
if (feeRateFromNode.fastest)
|
|
2897
2977
|
feeRateFromNode.fastest = feeRateFromNode.fastest / 1000;
|
|
2898
|
-
console.warn(`${
|
|
2978
|
+
console.warn(`${tag6}: Converted to sat/byte:`, feeRateFromNode);
|
|
2899
2979
|
}
|
|
2900
2980
|
if (!feeRateFromNode || typeof feeRateFromNode !== "object") {
|
|
2901
2981
|
throw new Error(`Invalid fee rate response from API: ${JSON.stringify(feeRateFromNode)}`);
|
|
2902
2982
|
}
|
|
2903
|
-
console.log(`${
|
|
2983
|
+
console.log(`${tag6}: Available fee rates:`, {
|
|
2904
2984
|
slow: feeRateFromNode.slow,
|
|
2905
2985
|
average: feeRateFromNode.average,
|
|
2906
2986
|
fast: feeRateFromNode.fast,
|
|
@@ -2910,33 +2990,33 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2910
2990
|
throw new Error(`No valid fee rates in API response: ${JSON.stringify(feeRateFromNode)}`);
|
|
2911
2991
|
}
|
|
2912
2992
|
} catch (error) {
|
|
2913
|
-
console.error(`${
|
|
2993
|
+
console.error(`${tag6}: Failed to get fee rates from Pioneer API:`, error.message || error);
|
|
2914
2994
|
throw new Error(`Unable to get fee rate for network ${networkId}: ${error.message || "API unavailable"}`);
|
|
2915
2995
|
}
|
|
2916
2996
|
}
|
|
2917
2997
|
let effectiveFeeRate;
|
|
2918
|
-
console.log(`${
|
|
2998
|
+
console.log(`${tag6}: Using fee level ${feeLevel}`);
|
|
2919
2999
|
switch (feeLevel) {
|
|
2920
3000
|
case 1:
|
|
2921
3001
|
case 2:
|
|
2922
3002
|
effectiveFeeRate = feeRateFromNode.slow || feeRateFromNode.average;
|
|
2923
|
-
console.log(`${
|
|
3003
|
+
console.log(`${tag6}: Using SLOW fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2924
3004
|
break;
|
|
2925
3005
|
case 3:
|
|
2926
3006
|
case 4:
|
|
2927
3007
|
effectiveFeeRate = feeRateFromNode.average || feeRateFromNode.fast;
|
|
2928
|
-
console.log(`${
|
|
3008
|
+
console.log(`${tag6}: Using AVERAGE fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2929
3009
|
break;
|
|
2930
3010
|
case 5:
|
|
2931
3011
|
effectiveFeeRate = feeRateFromNode.fastest || feeRateFromNode.fast;
|
|
2932
|
-
console.log(`${
|
|
3012
|
+
console.log(`${tag6}: Using FASTEST fee rate: ${effectiveFeeRate} sat/vB`);
|
|
2933
3013
|
break;
|
|
2934
3014
|
default:
|
|
2935
3015
|
throw new Error(`Invalid fee level: ${feeLevel}. Must be 1-5`);
|
|
2936
3016
|
}
|
|
2937
3017
|
if (!effectiveFeeRate)
|
|
2938
3018
|
throw new Error("Unable to get fee rate for network");
|
|
2939
|
-
console.log(`${
|
|
3019
|
+
console.log(`${tag6}: Using fee rate from API:`, {
|
|
2940
3020
|
feeLevel,
|
|
2941
3021
|
selectedRate: effectiveFeeRate,
|
|
2942
3022
|
description: feeLevel <= 2 ? "slow" : feeLevel <= 4 ? "average" : "fast"
|
|
@@ -2946,22 +3026,22 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2946
3026
|
effectiveFeeRate = Math.ceil(effectiveFeeRate);
|
|
2947
3027
|
const MIN_RELAY_FEE_RATE = 3;
|
|
2948
3028
|
if (effectiveFeeRate < MIN_RELAY_FEE_RATE) {
|
|
2949
|
-
console.log(`${
|
|
3029
|
+
console.log(`${tag6}: Fee rate ${effectiveFeeRate} is below minimum relay fee, increasing to ${MIN_RELAY_FEE_RATE} sat/vB`);
|
|
2950
3030
|
effectiveFeeRate = MIN_RELAY_FEE_RATE;
|
|
2951
3031
|
}
|
|
2952
|
-
console.log(`${
|
|
3032
|
+
console.log(`${tag6}: Final fee rate to use (rounded, with minimums): ${effectiveFeeRate} sat/vB`);
|
|
2953
3033
|
amount = parseInt(String(amount * 1e8));
|
|
2954
3034
|
if (amount <= 0 && !isMax)
|
|
2955
3035
|
throw Error("Invalid amount! 0");
|
|
2956
3036
|
const totalBalance = utxos.reduce((sum, utxo) => sum + utxo.value, 0);
|
|
2957
|
-
console.log(`${
|
|
3037
|
+
console.log(`${tag6}: Coin selection inputs:`, {
|
|
2958
3038
|
utxoCount: utxos.length,
|
|
2959
3039
|
totalBalance: totalBalance / 1e8,
|
|
2960
3040
|
requestedAmount: amount / 1e8,
|
|
2961
3041
|
isMax,
|
|
2962
3042
|
feeRate: effectiveFeeRate
|
|
2963
3043
|
});
|
|
2964
|
-
console.log(`${
|
|
3044
|
+
console.log(`${tag6}: UTXO details for coin selection:`, utxos.map((u) => ({
|
|
2965
3045
|
value: u.value,
|
|
2966
3046
|
txid: u.txid?.substring(0, 10) + "...",
|
|
2967
3047
|
vout: u.vout,
|
|
@@ -2975,8 +3055,8 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2975
3055
|
} else {
|
|
2976
3056
|
result = import_coinselect.default(utxos, [{ address: to, value: amount }], effectiveFeeRate);
|
|
2977
3057
|
}
|
|
2978
|
-
console.log(
|
|
2979
|
-
console.log(
|
|
3058
|
+
console.log(tag6, "coinSelect result object:", result);
|
|
3059
|
+
console.log(tag6, "coinSelect result.inputs:", result?.inputs);
|
|
2980
3060
|
if (!result || !result.inputs) {
|
|
2981
3061
|
const errorDetails = {
|
|
2982
3062
|
utxoCount: utxos.length,
|
|
@@ -2985,7 +3065,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
2985
3065
|
feeRate: effectiveFeeRate,
|
|
2986
3066
|
insufficientFunds: totalBalance < amount
|
|
2987
3067
|
};
|
|
2988
|
-
console.error(`${
|
|
3068
|
+
console.error(`${tag6}: Coin selection failed:`, errorDetails);
|
|
2989
3069
|
if (utxos.length === 0) {
|
|
2990
3070
|
throw Error("No UTXOs available for coin selection");
|
|
2991
3071
|
} else if (totalBalance < amount) {
|
|
@@ -3001,7 +3081,7 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
3001
3081
|
throw Error("Failed to create transaction: Missing outputs");
|
|
3002
3082
|
if (fee === undefined)
|
|
3003
3083
|
throw Error("Failed to calculate transaction fee");
|
|
3004
|
-
console.log(`${
|
|
3084
|
+
console.log(`${tag6}: Transaction built with:`, {
|
|
3005
3085
|
feeLevel,
|
|
3006
3086
|
effectiveFeeRate: `${effectiveFeeRate} sat/vB`,
|
|
3007
3087
|
calculatedFee: `${fee} satoshis`,
|
|
@@ -3009,8 +3089,8 @@ async function createUnsignedUxtoTx(caip, to, amount, memo, pubkeys, pioneer, pu
|
|
|
3009
3089
|
outputCount: outputs.length
|
|
3010
3090
|
});
|
|
3011
3091
|
const uniqueInputSet = new Set;
|
|
3012
|
-
console.log(
|
|
3013
|
-
console.log(
|
|
3092
|
+
console.log(tag6, "inputs:", inputs);
|
|
3093
|
+
console.log(tag6, "inputs:", inputs[0]);
|
|
3014
3094
|
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 }) => ({
|
|
3015
3095
|
addressNList: bip32ToAddressNList2(path2),
|
|
3016
3096
|
scriptType,
|
|
@@ -3118,7 +3198,7 @@ class TransactionManager {
|
|
|
3118
3198
|
throw new Error(`Unsupported CAIP: ${caip}`);
|
|
3119
3199
|
}
|
|
3120
3200
|
async transfer({ caip, to, amount, memo, isMax = false, feeLevel = 5, changeScriptType }) {
|
|
3121
|
-
let
|
|
3201
|
+
let tag6 = TAG4 + " | transfer | ";
|
|
3122
3202
|
try {
|
|
3123
3203
|
if (!this.pioneer)
|
|
3124
3204
|
throw Error("Failed to init! pioneer");
|
|
@@ -3156,12 +3236,12 @@ class TransactionManager {
|
|
|
3156
3236
|
}
|
|
3157
3237
|
return unsignedTx;
|
|
3158
3238
|
} catch (e) {
|
|
3159
|
-
console.error(
|
|
3239
|
+
console.error(tag6, e);
|
|
3160
3240
|
throw e;
|
|
3161
3241
|
}
|
|
3162
3242
|
}
|
|
3163
3243
|
async sign({ caip, unsignedTx }) {
|
|
3164
|
-
let
|
|
3244
|
+
let tag6 = TAG4 + " | sign | ";
|
|
3165
3245
|
try {
|
|
3166
3246
|
if (!this.pioneer)
|
|
3167
3247
|
throw Error("Failed to init! pioneer");
|
|
@@ -3247,20 +3327,20 @@ class TransactionManager {
|
|
|
3247
3327
|
}
|
|
3248
3328
|
case "cosmos:mayachain-mainnet-v1/slip44:931":
|
|
3249
3329
|
case "cosmos:mayachain-mainnet-v1/denom:maya": {
|
|
3250
|
-
console.log(
|
|
3251
|
-
console.log(
|
|
3252
|
-
console.log(
|
|
3253
|
-
console.log(
|
|
3254
|
-
console.log(
|
|
3255
|
-
console.log(
|
|
3256
|
-
console.log(
|
|
3257
|
-
console.log(
|
|
3258
|
-
console.log(
|
|
3330
|
+
console.log(tag6, "\uD83D\uDD0D ===== MAYACHAIN SIGNING AUDIT =====");
|
|
3331
|
+
console.log(tag6, "\uD83D\uDCCB unsignedTx:", JSON.stringify(unsignedTx, null, 2));
|
|
3332
|
+
console.log(tag6, "\uD83D\uDD11 unsignedTx.addressNList:", unsignedTx.addressNList);
|
|
3333
|
+
console.log(tag6, "\uD83D\uDCCD unsignedTx.signerAddress:", unsignedTx.signerAddress);
|
|
3334
|
+
console.log(tag6, "\uD83C\uDF10 pubkeyContext:", this.pubkeyContext);
|
|
3335
|
+
console.log(tag6, "\uD83D\uDD10 pubkeyContext.addressNList:", this.pubkeyContext?.addressNList);
|
|
3336
|
+
console.log(tag6, "\uD83D\uDD10 pubkeyContext.addressNListMaster:", this.pubkeyContext?.addressNListMaster);
|
|
3337
|
+
console.log(tag6, "\uD83D\uDCEC pubkeyContext.address:", this.pubkeyContext?.address);
|
|
3338
|
+
console.log(tag6, "=======================================");
|
|
3259
3339
|
if (unsignedTx.signDoc.msgs[0].type === "mayachain/MsgSend") {
|
|
3260
3340
|
const responseSign = await this.keepKeySdk.mayachain.mayachainSignAminoTransfer(unsignedTx);
|
|
3261
3341
|
signedTx = responseSign.serialized;
|
|
3262
|
-
console.log(
|
|
3263
|
-
console.log(
|
|
3342
|
+
console.log(tag6, "✅ Signing completed");
|
|
3343
|
+
console.log(tag6, "\uD83D\uDCE6 responseSign:", responseSign);
|
|
3264
3344
|
} else if (unsignedTx.signDoc.msgs[0].type === "mayachain/MsgDeposit") {
|
|
3265
3345
|
const responseSign = await this.keepKeySdk.mayachain.mayachainSignAminoDeposit(unsignedTx);
|
|
3266
3346
|
signedTx = responseSign.serialized;
|
|
@@ -3282,11 +3362,11 @@ class TransactionManager {
|
|
|
3282
3362
|
if (serialized.length > 140) {
|
|
3283
3363
|
signedTx = serialized;
|
|
3284
3364
|
} else {
|
|
3285
|
-
console.error(
|
|
3365
|
+
console.error(tag6, "EIP155 signing returned incomplete transaction - only signature components");
|
|
3286
3366
|
throw new Error("KeepKey returned incomplete transaction - cannot reconstruct without r,s,v components");
|
|
3287
3367
|
}
|
|
3288
3368
|
} else {
|
|
3289
|
-
console.error(
|
|
3369
|
+
console.error(tag6, "EIP155 signing failed - no valid signature in response:", responseSign);
|
|
3290
3370
|
throw new Error("Failed to sign transaction - no valid signature in response");
|
|
3291
3371
|
}
|
|
3292
3372
|
break;
|
|
@@ -3307,19 +3387,19 @@ class TransactionManager {
|
|
|
3307
3387
|
}
|
|
3308
3388
|
}
|
|
3309
3389
|
if (!signedTx) {
|
|
3310
|
-
console.error(
|
|
3311
|
-
console.error(
|
|
3312
|
-
console.error(
|
|
3390
|
+
console.error(tag6, "CRITICAL ERROR: signedTx is missing after signing process");
|
|
3391
|
+
console.error(tag6, "CAIP:", caip);
|
|
3392
|
+
console.error(tag6, "Type:", type);
|
|
3313
3393
|
throw Error("Failed to sign! missing signedTx");
|
|
3314
3394
|
}
|
|
3315
3395
|
return signedTx;
|
|
3316
3396
|
} catch (e) {
|
|
3317
|
-
console.error(
|
|
3397
|
+
console.error(tag6, e);
|
|
3318
3398
|
throw e;
|
|
3319
3399
|
}
|
|
3320
3400
|
}
|
|
3321
3401
|
async broadcast({ networkId, serialized }) {
|
|
3322
|
-
let
|
|
3402
|
+
let tag6 = TAG4 + " | broadcast | ";
|
|
3323
3403
|
try {
|
|
3324
3404
|
if (!this.pioneer)
|
|
3325
3405
|
throw Error("Failed to init! pioneer");
|
|
@@ -3333,7 +3413,7 @@ class TransactionManager {
|
|
|
3333
3413
|
return result.txid;
|
|
3334
3414
|
}
|
|
3335
3415
|
} catch (e) {
|
|
3336
|
-
console.error(
|
|
3416
|
+
console.error(tag6, e);
|
|
3337
3417
|
throw e;
|
|
3338
3418
|
}
|
|
3339
3419
|
}
|
|
@@ -3453,7 +3533,7 @@ var cosmosClaimAllRewardsTemplate = (params) => ({
|
|
|
3453
3533
|
// src/txbuilder/createUnsignedStakingTx.ts
|
|
3454
3534
|
var TAG5 = " | createUnsignedStakingTx | ";
|
|
3455
3535
|
async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyContext) {
|
|
3456
|
-
const
|
|
3536
|
+
const tag6 = TAG5 + " | createUnsignedStakingTx | ";
|
|
3457
3537
|
try {
|
|
3458
3538
|
if (!pioneer)
|
|
3459
3539
|
throw new Error("Failed to init! pioneer");
|
|
@@ -3464,7 +3544,7 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3464
3544
|
if (!pubkeyContext.networks?.includes(networkId)) {
|
|
3465
3545
|
throw new Error(`Pubkey context is for wrong network. Expected ${networkId}, got ${pubkeyContext.networks}`);
|
|
3466
3546
|
}
|
|
3467
|
-
console.log(
|
|
3547
|
+
console.log(tag6, `✅ Using pubkeyContext for network ${networkId}:`, {
|
|
3468
3548
|
address: pubkeyContext.address
|
|
3469
3549
|
});
|
|
3470
3550
|
let chain;
|
|
@@ -3496,10 +3576,10 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3496
3576
|
default:
|
|
3497
3577
|
throw new Error(`Unsupported networkId for staking: ${networkId}`);
|
|
3498
3578
|
}
|
|
3499
|
-
console.log(
|
|
3579
|
+
console.log(tag6, `Building ${params.type} transaction for ${chain}`);
|
|
3500
3580
|
const fromAddress = pubkeyContext.address || pubkeyContext.pubkey;
|
|
3501
3581
|
const accountInfo = (await pioneer.GetAccountInfo({ network: chain, address: fromAddress })).data;
|
|
3502
|
-
console.log(
|
|
3582
|
+
console.log(tag6, "accountInfo: ", accountInfo);
|
|
3503
3583
|
let account_number;
|
|
3504
3584
|
let sequence;
|
|
3505
3585
|
if (networkId === "cosmos:cosmoshub-4" || networkId === "cosmos:osmosis-1") {
|
|
@@ -3581,7 +3661,7 @@ async function createUnsignedStakingTx(caip, params, pubkeys, pioneer, pubkeyCon
|
|
|
3581
3661
|
throw new Error(`Unsupported staking transaction type: ${params.type}`);
|
|
3582
3662
|
}
|
|
3583
3663
|
} catch (error) {
|
|
3584
|
-
console.error(
|
|
3664
|
+
console.error(tag6, "Error:", error);
|
|
3585
3665
|
throw error;
|
|
3586
3666
|
}
|
|
3587
3667
|
}
|
|
@@ -3627,16 +3707,16 @@ function withTimeout(promise, timeoutMs) {
|
|
|
3627
3707
|
]);
|
|
3628
3708
|
}
|
|
3629
3709
|
async function getFees(pioneer, networkId) {
|
|
3630
|
-
const
|
|
3710
|
+
const tag6 = TAG6 + " | getFees | ";
|
|
3631
3711
|
try {
|
|
3632
|
-
console.log(
|
|
3712
|
+
console.log(tag6, `Fetching fees for network: ${networkId}`);
|
|
3633
3713
|
const networkType = getNetworkType(networkId);
|
|
3634
3714
|
if (networkType === "COSMOS") {
|
|
3635
|
-
console.log(
|
|
3715
|
+
console.log(tag6, "Using hardcoded fees for Cosmos network:", networkId);
|
|
3636
3716
|
return getCosmosFees(networkId);
|
|
3637
3717
|
}
|
|
3638
3718
|
if (networkId === "bip122:00000000001a91e3dace36e2be3bf030") {
|
|
3639
|
-
console.log(
|
|
3719
|
+
console.log(tag6, "Using hardcoded fees for Dogecoin: 10 sat/byte");
|
|
3640
3720
|
return {
|
|
3641
3721
|
slow: {
|
|
3642
3722
|
label: "Slow",
|
|
@@ -3673,7 +3753,7 @@ async function getFees(pioneer, networkId) {
|
|
|
3673
3753
|
const apiCall = pioneer.GetFeeRateByNetwork ? pioneer.GetFeeRateByNetwork({ networkId }) : pioneer.GetFeeRate({ networkId });
|
|
3674
3754
|
feeResponse = await withTimeout(apiCall, 3000);
|
|
3675
3755
|
} catch (timeoutError) {
|
|
3676
|
-
console.warn(
|
|
3756
|
+
console.warn(tag6, "Dash fee API timeout, using fallback fees");
|
|
3677
3757
|
return {
|
|
3678
3758
|
slow: {
|
|
3679
3759
|
label: "Economy",
|
|
@@ -3711,17 +3791,17 @@ async function getFees(pioneer, networkId) {
|
|
|
3711
3791
|
throw new Error(`No fee data returned for ${networkId}`);
|
|
3712
3792
|
}
|
|
3713
3793
|
const feeData = feeResponse.data;
|
|
3714
|
-
console.log(
|
|
3794
|
+
console.log(tag6, "Raw fee data:", feeData);
|
|
3715
3795
|
const networkName = getNetworkName(networkId);
|
|
3716
3796
|
let normalizedFees = normalizeFeeData(feeData, networkType, networkName, networkId);
|
|
3717
3797
|
normalizedFees = ensureFeeDifferentiation(normalizedFees, networkType);
|
|
3718
3798
|
normalizedFees.networkId = networkId;
|
|
3719
3799
|
normalizedFees.networkType = networkType;
|
|
3720
3800
|
normalizedFees.raw = feeData;
|
|
3721
|
-
console.log(
|
|
3801
|
+
console.log(tag6, "Normalized fees:", normalizedFees);
|
|
3722
3802
|
return normalizedFees;
|
|
3723
3803
|
} catch (error) {
|
|
3724
|
-
console.error(
|
|
3804
|
+
console.error(tag6, "Failed to fetch fees:", error);
|
|
3725
3805
|
return getFallbackFees(networkId);
|
|
3726
3806
|
}
|
|
3727
3807
|
}
|
|
@@ -4030,7 +4110,7 @@ function estimateTransactionFee(feeRate, unit, networkType, txSize) {
|
|
|
4030
4110
|
|
|
4031
4111
|
// src/utils/kkapi-detection.ts
|
|
4032
4112
|
async function detectKkApiAvailability(forceLocalhost) {
|
|
4033
|
-
const
|
|
4113
|
+
const tag6 = " | detectKkApiAvailability | ";
|
|
4034
4114
|
try {
|
|
4035
4115
|
const isTauri = typeof window !== "undefined" && "__TAURI__" in window;
|
|
4036
4116
|
const isBrowser = typeof window !== "undefined";
|
|
@@ -4158,6 +4238,11 @@ function buildDashboardFromBalances(balances, blockchains, assetsMap) {
|
|
|
4158
4238
|
return sum + balanceNum;
|
|
4159
4239
|
}, 0).toString();
|
|
4160
4240
|
const assetInfo = nativeAssetCaip ? assetsMap.get(nativeAssetCaip) : null;
|
|
4241
|
+
console.log(TAG7, `[DEBUG] Network: ${blockchain}`);
|
|
4242
|
+
console.log(TAG7, `[DEBUG] nativeAssetCaip: ${nativeAssetCaip}`);
|
|
4243
|
+
console.log(TAG7, `[DEBUG] assetInfo:`, assetInfo);
|
|
4244
|
+
console.log(TAG7, `[DEBUG] gasAsset:`, gasAsset);
|
|
4245
|
+
console.log(TAG7, `[DEBUG] Resolved name: ${gasAsset?.name || assetInfo?.name || "NO NAME"}`);
|
|
4161
4246
|
networksTemp.push({
|
|
4162
4247
|
networkId: blockchain,
|
|
4163
4248
|
totalValueUsd: networkTotal,
|
|
@@ -4183,11 +4268,11 @@ function buildDashboardFromBalances(balances, blockchains, assetsMap) {
|
|
|
4183
4268
|
// src/utils/sync-market.ts
|
|
4184
4269
|
var TAG8 = " | sync-market | ";
|
|
4185
4270
|
async function syncMarket(balances, pioneer) {
|
|
4186
|
-
const
|
|
4271
|
+
const tag6 = `${TAG8} | syncMarket | `;
|
|
4187
4272
|
try {
|
|
4188
4273
|
const invalidBalances = balances.filter((b3) => !b3 || !b3.caip || typeof b3.caip !== "string" || !b3.caip.includes(":"));
|
|
4189
4274
|
if (invalidBalances.length > 0) {
|
|
4190
|
-
console.warn(
|
|
4275
|
+
console.warn(tag6, `Found ${invalidBalances.length} balances with invalid CAIPs:`, invalidBalances.map((b3) => ({
|
|
4191
4276
|
caip: b3?.caip,
|
|
4192
4277
|
type: typeof b3?.caip,
|
|
4193
4278
|
symbol: b3?.symbol,
|
|
@@ -4202,7 +4287,7 @@ async function syncMarket(balances, pioneer) {
|
|
|
4202
4287
|
console.log("GetMarketInfo: payload length: ", allCaips.length);
|
|
4203
4288
|
const invalidEntries = allCaips.filter((caip) => typeof caip !== "string");
|
|
4204
4289
|
if (invalidEntries.length > 0) {
|
|
4205
|
-
console.error(
|
|
4290
|
+
console.error(tag6, "CRITICAL: Invalid entries detected in allCaips:", invalidEntries);
|
|
4206
4291
|
throw new Error("Invalid CAIP entries detected - aborting market sync");
|
|
4207
4292
|
}
|
|
4208
4293
|
if (allCaips && allCaips.length > 0) {
|
|
@@ -4223,13 +4308,13 @@ async function syncMarket(balances, pioneer) {
|
|
|
4223
4308
|
}
|
|
4224
4309
|
}
|
|
4225
4310
|
} catch (apiError) {
|
|
4226
|
-
console.error(
|
|
4227
|
-
console.warn(
|
|
4311
|
+
console.error(tag6, "API error fetching market info:", apiError);
|
|
4312
|
+
console.warn(tag6, "Continuing without market prices");
|
|
4228
4313
|
}
|
|
4229
4314
|
}
|
|
4230
4315
|
return true;
|
|
4231
4316
|
} catch (e) {
|
|
4232
|
-
console.error(
|
|
4317
|
+
console.error(tag6, "e:", e);
|
|
4233
4318
|
throw e;
|
|
4234
4319
|
}
|
|
4235
4320
|
}
|
|
@@ -4399,7 +4484,7 @@ class SDK {
|
|
|
4399
4484
|
return true;
|
|
4400
4485
|
};
|
|
4401
4486
|
this.setPubkeys = (newPubkeys) => {
|
|
4402
|
-
const
|
|
4487
|
+
const tag6 = `${TAG9} | setPubkeys | `;
|
|
4403
4488
|
this.pubkeys = [];
|
|
4404
4489
|
this.pubkeySet.clear();
|
|
4405
4490
|
let added = 0;
|
|
@@ -4410,7 +4495,7 @@ class SDK {
|
|
|
4410
4495
|
}
|
|
4411
4496
|
};
|
|
4412
4497
|
this.getUnifiedPortfolio = async function() {
|
|
4413
|
-
const
|
|
4498
|
+
const tag6 = `${TAG9} | getUnifiedPortfolio | `;
|
|
4414
4499
|
try {
|
|
4415
4500
|
const startTime = performance.now();
|
|
4416
4501
|
try {
|
|
@@ -4421,17 +4506,17 @@ class SDK {
|
|
|
4421
4506
|
signal: AbortSignal.timeout(2000)
|
|
4422
4507
|
});
|
|
4423
4508
|
if (!portfolioResponse.ok) {
|
|
4424
|
-
console.warn(
|
|
4509
|
+
console.warn(tag6, "Portfolio endpoint returned", portfolioResponse.status);
|
|
4425
4510
|
return null;
|
|
4426
4511
|
}
|
|
4427
4512
|
const portfolioData = await portfolioResponse.json();
|
|
4428
4513
|
const loadTime = performance.now() - startTime;
|
|
4429
4514
|
if (!portfolioData.success) {
|
|
4430
|
-
console.warn(
|
|
4515
|
+
console.warn(tag6, "Portfolio API returned success=false");
|
|
4431
4516
|
return null;
|
|
4432
4517
|
}
|
|
4433
4518
|
if (portfolioData.totalValueUsd === 0 || !portfolioData.totalValueUsd) {
|
|
4434
|
-
console.warn(
|
|
4519
|
+
console.warn(tag6, "Portfolio value is $0.00 - may need device connection or sync");
|
|
4435
4520
|
return null;
|
|
4436
4521
|
}
|
|
4437
4522
|
let allBalances = [];
|
|
@@ -4506,19 +4591,19 @@ class SDK {
|
|
|
4506
4591
|
};
|
|
4507
4592
|
} catch (fetchError) {
|
|
4508
4593
|
if (fetchError.name === "AbortError") {
|
|
4509
|
-
console.log(
|
|
4594
|
+
console.log(tag6, "Unified portfolio request timed out (this is normal if vault not running)");
|
|
4510
4595
|
} else {
|
|
4511
|
-
console.log(
|
|
4596
|
+
console.log(tag6, "Failed to fetch unified portfolio:", fetchError.message);
|
|
4512
4597
|
}
|
|
4513
4598
|
return null;
|
|
4514
4599
|
}
|
|
4515
4600
|
} catch (e) {
|
|
4516
|
-
console.error(
|
|
4601
|
+
console.error(tag6, "Error:", e);
|
|
4517
4602
|
return null;
|
|
4518
4603
|
}
|
|
4519
4604
|
};
|
|
4520
4605
|
this.init = async function(walletsVerbose, setup) {
|
|
4521
|
-
const
|
|
4606
|
+
const tag6 = `${TAG9} | init | `;
|
|
4522
4607
|
try {
|
|
4523
4608
|
if (!this.username)
|
|
4524
4609
|
throw Error("username required!");
|
|
@@ -4601,7 +4686,7 @@ class SDK {
|
|
|
4601
4686
|
}
|
|
4602
4687
|
return this.pioneer;
|
|
4603
4688
|
} catch (e) {
|
|
4604
|
-
console.error(
|
|
4689
|
+
console.error(tag6, "e: ", e);
|
|
4605
4690
|
throw e;
|
|
4606
4691
|
}
|
|
4607
4692
|
};
|
|
@@ -4612,7 +4697,7 @@ class SDK {
|
|
|
4612
4697
|
return syncMarket(this.balances, this.pioneer);
|
|
4613
4698
|
};
|
|
4614
4699
|
this.sync = async function() {
|
|
4615
|
-
const
|
|
4700
|
+
const tag6 = `${TAG9} | sync | `;
|
|
4616
4701
|
try {
|
|
4617
4702
|
const matchesNetwork = (item, networkId) => {
|
|
4618
4703
|
if (!item.networks || !Array.isArray(item.networks))
|
|
@@ -4664,16 +4749,16 @@ class SDK {
|
|
|
4664
4749
|
let totalPortfolioValue = 0;
|
|
4665
4750
|
const networksTemp = [];
|
|
4666
4751
|
const uniqueBlockchains = [...new Set(this.blockchains)];
|
|
4667
|
-
console.log(
|
|
4752
|
+
console.log(tag6, "uniqueBlockchains: ", uniqueBlockchains);
|
|
4668
4753
|
for (const blockchain of uniqueBlockchains) {
|
|
4669
4754
|
const filteredBalances = this.balances.filter((b3) => {
|
|
4670
4755
|
const networkId = caipToNetworkId7(b3.caip);
|
|
4671
4756
|
return networkId === blockchain || blockchain === "eip155:*" && networkId.startsWith("eip155:");
|
|
4672
4757
|
});
|
|
4673
|
-
console.log(
|
|
4674
|
-
console.log(
|
|
4758
|
+
console.log(tag6, `Filtering for blockchain: ${blockchain}`);
|
|
4759
|
+
console.log(tag6, `Found ${filteredBalances.length} balances before deduplication`);
|
|
4675
4760
|
filteredBalances.forEach((balance, idx) => {
|
|
4676
|
-
console.log(
|
|
4761
|
+
console.log(tag6, `Balance[${idx}]:`, {
|
|
4677
4762
|
caip: balance.caip,
|
|
4678
4763
|
pubkey: balance.pubkey,
|
|
4679
4764
|
balance: balance.balance,
|
|
@@ -4683,7 +4768,7 @@ class SDK {
|
|
|
4683
4768
|
const balanceMap = new Map;
|
|
4684
4769
|
const isBitcoin = blockchain.includes("bip122:000000000019d6689c085ae165831e93");
|
|
4685
4770
|
if (isBitcoin) {
|
|
4686
|
-
console.log(
|
|
4771
|
+
console.log(tag6, "Bitcoin network detected - checking for duplicate balances");
|
|
4687
4772
|
const bitcoinByValue = new Map;
|
|
4688
4773
|
filteredBalances.forEach((balance) => {
|
|
4689
4774
|
const valueKey = `${balance.balance}_${balance.valueUsd}`;
|
|
@@ -4694,7 +4779,7 @@ class SDK {
|
|
|
4694
4779
|
});
|
|
4695
4780
|
for (const [valueKey, balances] of bitcoinByValue.entries()) {
|
|
4696
4781
|
if (balances.length === 3 && parseFloat(balances[0].valueUsd || "0") > 0) {
|
|
4697
|
-
console.log(
|
|
4782
|
+
console.log(tag6, "BITCOIN API BUG DETECTED: All 3 address types have same balance, keeping only xpub");
|
|
4698
4783
|
const xpubBalance = balances.find((b3) => b3.pubkey?.startsWith("xpub")) || balances[0];
|
|
4699
4784
|
const key = `${xpubBalance.caip}_${xpubBalance.pubkey || "default"}`;
|
|
4700
4785
|
balanceMap.set(key, xpubBalance);
|
|
@@ -4714,13 +4799,13 @@ class SDK {
|
|
|
4714
4799
|
});
|
|
4715
4800
|
}
|
|
4716
4801
|
const networkBalances = Array.from(balanceMap.values());
|
|
4717
|
-
console.log(
|
|
4718
|
-
console.log(
|
|
4802
|
+
console.log(tag6, "networkBalances (deduplicated): ", networkBalances);
|
|
4803
|
+
console.log(tag6, "networkBalances count: ", networkBalances.length);
|
|
4719
4804
|
const networkTotal = networkBalances.reduce((sum, balance, idx) => {
|
|
4720
4805
|
const valueUsd = typeof balance.valueUsd === "string" ? parseFloat(balance.valueUsd) : balance.valueUsd || 0;
|
|
4721
|
-
console.log(
|
|
4806
|
+
console.log(tag6, `[${idx}] valueUsd:`, balance.valueUsd, "→ parsed:", valueUsd, "| running sum:", sum + valueUsd);
|
|
4722
4807
|
if (blockchain.includes("bip122:000000000019d6689c085ae165831e93")) {
|
|
4723
|
-
console.log(
|
|
4808
|
+
console.log(tag6, `[BITCOIN DEBUG ${idx}] pubkey:`, balance.pubkey?.substring(0, 10) + "...", "| balance:", balance.balance, "| valueUsd:", balance.valueUsd, "→ parsed:", valueUsd);
|
|
4724
4809
|
}
|
|
4725
4810
|
return sum + valueUsd;
|
|
4726
4811
|
}, 0);
|
|
@@ -4751,7 +4836,7 @@ class SDK {
|
|
|
4751
4836
|
this.dashboard = dashboardData;
|
|
4752
4837
|
return true;
|
|
4753
4838
|
} catch (e) {
|
|
4754
|
-
console.error(
|
|
4839
|
+
console.error(tag6, "Error in sync:", e);
|
|
4755
4840
|
throw e;
|
|
4756
4841
|
}
|
|
4757
4842
|
};
|
|
@@ -4765,7 +4850,7 @@ class SDK {
|
|
|
4765
4850
|
}
|
|
4766
4851
|
};
|
|
4767
4852
|
this.buildTx = async function(sendPayload) {
|
|
4768
|
-
let
|
|
4853
|
+
let tag6 = TAG9 + " | buildTx | ";
|
|
4769
4854
|
try {
|
|
4770
4855
|
const transactionDependencies = {
|
|
4771
4856
|
context: this.context,
|
|
@@ -4779,7 +4864,7 @@ class SDK {
|
|
|
4779
4864
|
};
|
|
4780
4865
|
let txManager = new TransactionManager(transactionDependencies, this.events);
|
|
4781
4866
|
let unsignedTx = await txManager.transfer(sendPayload);
|
|
4782
|
-
console.log(
|
|
4867
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4783
4868
|
return unsignedTx;
|
|
4784
4869
|
} catch (e) {
|
|
4785
4870
|
console.error(e);
|
|
@@ -4787,14 +4872,14 @@ class SDK {
|
|
|
4787
4872
|
}
|
|
4788
4873
|
};
|
|
4789
4874
|
this.buildDelegateTx = async function(caip, params) {
|
|
4790
|
-
let
|
|
4875
|
+
let tag6 = TAG9 + " | buildDelegateTx | ";
|
|
4791
4876
|
try {
|
|
4792
4877
|
const delegateParams = {
|
|
4793
4878
|
...params,
|
|
4794
4879
|
type: "delegate"
|
|
4795
4880
|
};
|
|
4796
4881
|
let unsignedTx = await createUnsignedStakingTx(caip, delegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4797
|
-
console.log(
|
|
4882
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4798
4883
|
return unsignedTx;
|
|
4799
4884
|
} catch (e) {
|
|
4800
4885
|
console.error(e);
|
|
@@ -4802,14 +4887,14 @@ class SDK {
|
|
|
4802
4887
|
}
|
|
4803
4888
|
};
|
|
4804
4889
|
this.buildUndelegateTx = async function(caip, params) {
|
|
4805
|
-
let
|
|
4890
|
+
let tag6 = TAG9 + " | buildUndelegateTx | ";
|
|
4806
4891
|
try {
|
|
4807
4892
|
const undelegateParams = {
|
|
4808
4893
|
...params,
|
|
4809
4894
|
type: "undelegate"
|
|
4810
4895
|
};
|
|
4811
4896
|
let unsignedTx = await createUnsignedStakingTx(caip, undelegateParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4812
|
-
console.log(
|
|
4897
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4813
4898
|
return unsignedTx;
|
|
4814
4899
|
} catch (e) {
|
|
4815
4900
|
console.error(e);
|
|
@@ -4817,14 +4902,14 @@ class SDK {
|
|
|
4817
4902
|
}
|
|
4818
4903
|
};
|
|
4819
4904
|
this.buildClaimRewardsTx = async function(caip, params) {
|
|
4820
|
-
let
|
|
4905
|
+
let tag6 = TAG9 + " | buildClaimRewardsTx | ";
|
|
4821
4906
|
try {
|
|
4822
4907
|
const claimParams = {
|
|
4823
4908
|
...params,
|
|
4824
4909
|
type: "claim_rewards"
|
|
4825
4910
|
};
|
|
4826
4911
|
let unsignedTx = await createUnsignedStakingTx(caip, claimParams, this.pubkeys, this.pioneer, this.pubkeyContext);
|
|
4827
|
-
console.log(
|
|
4912
|
+
console.log(tag6, "unsignedTx: ", unsignedTx);
|
|
4828
4913
|
return unsignedTx;
|
|
4829
4914
|
} catch (e) {
|
|
4830
4915
|
console.error(e);
|
|
@@ -4832,7 +4917,7 @@ class SDK {
|
|
|
4832
4917
|
}
|
|
4833
4918
|
};
|
|
4834
4919
|
this.buildClaimAllRewardsTx = async function(caip, params) {
|
|
4835
|
-
let
|
|
4920
|
+
let tag6 = TAG9 + " | buildClaimAllRewardsTx | ";
|
|
4836
4921
|
try {
|
|
4837
4922
|
const claimAllParams = {
|
|
4838
4923
|
...params,
|
|
@@ -4845,8 +4930,8 @@ class SDK {
|
|
|
4845
4930
|
throw e;
|
|
4846
4931
|
}
|
|
4847
4932
|
};
|
|
4848
|
-
this.signTx = async function(unsignedTx) {
|
|
4849
|
-
let
|
|
4933
|
+
this.signTx = async function(caip, unsignedTx) {
|
|
4934
|
+
let tag6 = TAG9 + " | signTx | ";
|
|
4850
4935
|
try {
|
|
4851
4936
|
const transactionDependencies = {
|
|
4852
4937
|
context: this.context,
|
|
@@ -4859,7 +4944,7 @@ class SDK {
|
|
|
4859
4944
|
keepKeySdk: this.keepKeySdk
|
|
4860
4945
|
};
|
|
4861
4946
|
let txManager = new TransactionManager(transactionDependencies, this.events);
|
|
4862
|
-
let signedTx = await txManager.sign(unsignedTx);
|
|
4947
|
+
let signedTx = await txManager.sign({ caip, unsignedTx });
|
|
4863
4948
|
return signedTx;
|
|
4864
4949
|
} catch (e) {
|
|
4865
4950
|
console.error(e);
|
|
@@ -4867,7 +4952,7 @@ class SDK {
|
|
|
4867
4952
|
}
|
|
4868
4953
|
};
|
|
4869
4954
|
this.broadcastTx = async function(caip, signedTx) {
|
|
4870
|
-
let
|
|
4955
|
+
let tag6 = TAG9 + " | broadcastTx | ";
|
|
4871
4956
|
try {
|
|
4872
4957
|
const transactionDependencies = {
|
|
4873
4958
|
context: this.context,
|
|
@@ -4891,7 +4976,7 @@ class SDK {
|
|
|
4891
4976
|
}
|
|
4892
4977
|
};
|
|
4893
4978
|
this.swap = async function(swapPayload) {
|
|
4894
|
-
let
|
|
4979
|
+
let tag6 = `${TAG9} | swap | `;
|
|
4895
4980
|
try {
|
|
4896
4981
|
if (!swapPayload)
|
|
4897
4982
|
throw Error("swapPayload required!");
|
|
@@ -4940,15 +5025,15 @@ class SDK {
|
|
|
4940
5025
|
throw new Error(`Cannot use max amount: no balance found for ${swapPayload.caipIn}`);
|
|
4941
5026
|
}
|
|
4942
5027
|
let totalBalance = 0;
|
|
4943
|
-
console.log(
|
|
5028
|
+
console.log(tag6, `Found ${inputBalances.length} balance entries for ${swapPayload.caipIn}`);
|
|
4944
5029
|
for (const balanceEntry of inputBalances) {
|
|
4945
5030
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
4946
5031
|
totalBalance += balance;
|
|
4947
|
-
console.log(
|
|
5032
|
+
console.log(tag6, ` - ${balanceEntry.pubkey || balanceEntry.identifier}: ${balance}`);
|
|
4948
5033
|
}
|
|
4949
5034
|
this.assetContext.balance = totalBalance.toString();
|
|
4950
5035
|
this.assetContext.valueUsd = (totalBalance * parseFloat(this.assetContext.priceUsd || "0")).toFixed(2);
|
|
4951
|
-
console.log(
|
|
5036
|
+
console.log(tag6, `Updated assetContext balance to aggregated total: ${totalBalance}`);
|
|
4952
5037
|
const feeReserves = {
|
|
4953
5038
|
"bip122:000000000019d6689c085ae165831e93/slip44:0": 0.00005,
|
|
4954
5039
|
"eip155:1/slip44:60": 0.001,
|
|
@@ -4959,7 +5044,7 @@ class SDK {
|
|
|
4959
5044
|
};
|
|
4960
5045
|
const reserve = feeReserves[swapPayload.caipIn] || 0.0001;
|
|
4961
5046
|
inputAmount = Math.max(0, totalBalance - reserve);
|
|
4962
|
-
console.log(
|
|
5047
|
+
console.log(tag6, `Using max amount for swap: ${inputAmount} (total balance: ${totalBalance}, reserve: ${reserve})`);
|
|
4963
5048
|
} else {
|
|
4964
5049
|
inputAmount = typeof swapPayload.amount === "string" ? parseFloat(swapPayload.amount) : swapPayload.amount;
|
|
4965
5050
|
if (isNaN(inputAmount) || inputAmount <= 0) {
|
|
@@ -4967,23 +5052,23 @@ class SDK {
|
|
|
4967
5052
|
}
|
|
4968
5053
|
}
|
|
4969
5054
|
let quote = {
|
|
4970
|
-
|
|
4971
|
-
sellAsset: this.assetContext,
|
|
5055
|
+
sellAsset: this.assetContext.caip,
|
|
4972
5056
|
sellAmount: inputAmount.toPrecision(8),
|
|
4973
|
-
buyAsset: this.outboundAssetContext,
|
|
5057
|
+
buyAsset: this.outboundAssetContext.caip,
|
|
4974
5058
|
recipientAddress,
|
|
4975
5059
|
senderAddress,
|
|
4976
|
-
slippage:
|
|
5060
|
+
slippage: swapPayload.slippagePercentage || 3
|
|
4977
5061
|
};
|
|
4978
5062
|
let result;
|
|
4979
5063
|
try {
|
|
4980
5064
|
result = await this.pioneer.Quote(quote);
|
|
4981
5065
|
result = result.data;
|
|
4982
5066
|
} catch (e) {
|
|
4983
|
-
console.error(
|
|
5067
|
+
console.error(tag6, "Failed to get quote: ", e);
|
|
5068
|
+
throw Error("Quote API failed for path: " + quote.sellAsset + " -> " + quote.buyAsset + ". Error: " + e);
|
|
4984
5069
|
}
|
|
4985
|
-
if (result.length === 0)
|
|
4986
|
-
throw Error("No quotes available! path: " + quote.sellAsset
|
|
5070
|
+
if (!result || result.length === 0)
|
|
5071
|
+
throw Error("No quotes available! path: " + quote.sellAsset + " -> " + quote.buyAsset);
|
|
4987
5072
|
let selected = result[0];
|
|
4988
5073
|
let txs = selected.quote.txs;
|
|
4989
5074
|
if (!txs)
|
|
@@ -4996,6 +5081,7 @@ class SDK {
|
|
|
4996
5081
|
balances: this.balances,
|
|
4997
5082
|
pioneer: this.pioneer,
|
|
4998
5083
|
pubkeys: this.pubkeys,
|
|
5084
|
+
pubkeyContext: this.pubkeyContext,
|
|
4999
5085
|
nodes: this.nodes,
|
|
5000
5086
|
keepKeySdk: this.keepKeySdk
|
|
5001
5087
|
};
|
|
@@ -5003,7 +5089,10 @@ class SDK {
|
|
|
5003
5089
|
let caip = swapPayload.caipIn;
|
|
5004
5090
|
let unsignedTx;
|
|
5005
5091
|
if (tx.type === "deposit") {
|
|
5006
|
-
unsignedTx = await createUnsignedTendermintTx(caip, tx.type, tx.txParams.amount, tx.txParams.memo, this.pubkeys, this.pioneer, this.
|
|
5092
|
+
unsignedTx = await createUnsignedTendermintTx(caip, tx.type, tx.txParams.amount, tx.txParams.memo, this.pubkeys, this.pioneer, this.pubkeyContext, false, undefined);
|
|
5093
|
+
} else if (tx.type === "EVM" || tx.type === "evm") {
|
|
5094
|
+
console.log(tag6, "Using pre-built EVM transaction from integration");
|
|
5095
|
+
unsignedTx = tx.txParams;
|
|
5007
5096
|
} else {
|
|
5008
5097
|
if (!tx.txParams.memo)
|
|
5009
5098
|
throw Error("memo required on swaps!");
|
|
@@ -5021,12 +5110,12 @@ class SDK {
|
|
|
5021
5110
|
return unsignedTx;
|
|
5022
5111
|
}
|
|
5023
5112
|
} catch (e) {
|
|
5024
|
-
console.error(
|
|
5113
|
+
console.error(tag6, "Error: ", e);
|
|
5025
5114
|
throw e;
|
|
5026
5115
|
}
|
|
5027
5116
|
};
|
|
5028
5117
|
this.transfer = async function(sendPayload) {
|
|
5029
|
-
let
|
|
5118
|
+
let tag6 = `${TAG9} | transfer | `;
|
|
5030
5119
|
try {
|
|
5031
5120
|
if (!sendPayload)
|
|
5032
5121
|
throw Error("sendPayload required!");
|
|
@@ -5060,15 +5149,15 @@ class SDK {
|
|
|
5060
5149
|
return { txid, events: this.events };
|
|
5061
5150
|
} catch (error) {
|
|
5062
5151
|
if (error instanceof Error) {
|
|
5063
|
-
console.error(
|
|
5152
|
+
console.error(tag6, "An error occurred during the transfer process:", error.message);
|
|
5064
5153
|
} else {
|
|
5065
|
-
console.error(
|
|
5154
|
+
console.error(tag6, "An unknown error occurred during the transfer process");
|
|
5066
5155
|
}
|
|
5067
5156
|
throw error;
|
|
5068
5157
|
}
|
|
5069
5158
|
};
|
|
5070
5159
|
this.followTransaction = async function(caip, txid) {
|
|
5071
|
-
let
|
|
5160
|
+
let tag6 = " | followTransaction | ";
|
|
5072
5161
|
try {
|
|
5073
5162
|
const finalConfirmationBlocksByCaip = {
|
|
5074
5163
|
dogecoin: 3,
|
|
@@ -5098,7 +5187,7 @@ class SDK {
|
|
|
5098
5187
|
}
|
|
5099
5188
|
}
|
|
5100
5189
|
} catch (error) {
|
|
5101
|
-
console.error(
|
|
5190
|
+
console.error(tag6, "Error:", error);
|
|
5102
5191
|
}
|
|
5103
5192
|
if (!isConfirmed) {
|
|
5104
5193
|
await new Promise((resolve) => setTimeout(resolve, 8000));
|
|
@@ -5116,18 +5205,18 @@ class SDK {
|
|
|
5116
5205
|
requiredConfirmations
|
|
5117
5206
|
};
|
|
5118
5207
|
} catch (error) {
|
|
5119
|
-
console.error(
|
|
5208
|
+
console.error(tag6, "Error:", error);
|
|
5120
5209
|
throw new Error("Failed to follow transaction");
|
|
5121
5210
|
}
|
|
5122
5211
|
};
|
|
5123
5212
|
this.setBlockchains = async function(blockchains) {
|
|
5124
|
-
const
|
|
5213
|
+
const tag6 = `${TAG9} | setBlockchains | `;
|
|
5125
5214
|
try {
|
|
5126
5215
|
if (!blockchains)
|
|
5127
5216
|
throw Error("blockchains required!");
|
|
5128
5217
|
const uniqueBlockchains = [...new Set(blockchains)];
|
|
5129
5218
|
if (blockchains.length !== uniqueBlockchains.length) {
|
|
5130
|
-
console.warn(
|
|
5219
|
+
console.warn(tag6, `Removed ${blockchains.length - uniqueBlockchains.length} duplicate blockchains`);
|
|
5131
5220
|
}
|
|
5132
5221
|
this.blockchains = uniqueBlockchains;
|
|
5133
5222
|
this.events.emit("SET_BLOCKCHAINS", this.blockchains);
|
|
@@ -5137,7 +5226,7 @@ class SDK {
|
|
|
5137
5226
|
}
|
|
5138
5227
|
};
|
|
5139
5228
|
this.addAsset = async function(caip, data) {
|
|
5140
|
-
let
|
|
5229
|
+
let tag6 = TAG9 + " | addAsset | ";
|
|
5141
5230
|
try {
|
|
5142
5231
|
let success = false;
|
|
5143
5232
|
if (!caip)
|
|
@@ -5175,22 +5264,22 @@ class SDK {
|
|
|
5175
5264
|
}
|
|
5176
5265
|
};
|
|
5177
5266
|
this.clearWalletState = async function() {
|
|
5178
|
-
const
|
|
5267
|
+
const tag6 = `${TAG9} | clearWalletState | `;
|
|
5179
5268
|
try {
|
|
5180
5269
|
this.context = null;
|
|
5181
5270
|
this.paths = [];
|
|
5182
5271
|
this.blockchains = [];
|
|
5183
5272
|
this.pubkeys = [];
|
|
5184
5273
|
this.pubkeySet.clear();
|
|
5185
|
-
console.log(
|
|
5274
|
+
console.log(tag6, "Cleared wallet state including pubkeys and tracking set");
|
|
5186
5275
|
return true;
|
|
5187
5276
|
} catch (e) {
|
|
5188
|
-
console.error(
|
|
5277
|
+
console.error(tag6, "e: ", e);
|
|
5189
5278
|
throw e;
|
|
5190
5279
|
}
|
|
5191
5280
|
};
|
|
5192
5281
|
this.addPath = async function(path) {
|
|
5193
|
-
const
|
|
5282
|
+
const tag6 = `${TAG9} | addPath | `;
|
|
5194
5283
|
try {
|
|
5195
5284
|
this.paths.push(path);
|
|
5196
5285
|
const pubkey = await getPubkey(path.networks[0], path, this.keepKeySdk, this.context);
|
|
@@ -5199,14 +5288,14 @@ class SDK {
|
|
|
5199
5288
|
this.buildDashboardFromBalances();
|
|
5200
5289
|
return { success: true, pubkey };
|
|
5201
5290
|
} catch (e) {
|
|
5202
|
-
console.error(
|
|
5291
|
+
console.error(tag6, "Failed:", e);
|
|
5203
5292
|
throw e;
|
|
5204
5293
|
}
|
|
5205
5294
|
};
|
|
5206
5295
|
this.addPaths = async function(paths) {
|
|
5207
|
-
const
|
|
5296
|
+
const tag6 = `${TAG9} | addPaths | `;
|
|
5208
5297
|
try {
|
|
5209
|
-
console.log(
|
|
5298
|
+
console.log(tag6, `Adding ${paths.length} paths in batch mode...`);
|
|
5210
5299
|
this.paths.push(...paths);
|
|
5211
5300
|
const newPubkeys = [];
|
|
5212
5301
|
for (const path of paths) {
|
|
@@ -5215,10 +5304,10 @@ class SDK {
|
|
|
5215
5304
|
this.addPubkey(pubkey);
|
|
5216
5305
|
newPubkeys.push(pubkey);
|
|
5217
5306
|
} catch (error) {
|
|
5218
|
-
console.warn(
|
|
5307
|
+
console.warn(tag6, `Failed to get pubkey for path ${path.note}:`, error.message);
|
|
5219
5308
|
}
|
|
5220
5309
|
}
|
|
5221
|
-
console.log(
|
|
5310
|
+
console.log(tag6, `Successfully added ${newPubkeys.length} pubkeys`);
|
|
5222
5311
|
const networkSet = new Set;
|
|
5223
5312
|
for (const path of paths) {
|
|
5224
5313
|
if (path.networks && Array.isArray(path.networks)) {
|
|
@@ -5226,13 +5315,13 @@ class SDK {
|
|
|
5226
5315
|
}
|
|
5227
5316
|
}
|
|
5228
5317
|
const uniqueNetworks = [...networkSet];
|
|
5229
|
-
console.log(
|
|
5318
|
+
console.log(tag6, `Fetching balances for ${uniqueNetworks.length} unique networks in single API call...`);
|
|
5230
5319
|
await this.getBalancesForNetworks(uniqueNetworks);
|
|
5231
5320
|
this.buildDashboardFromBalances();
|
|
5232
|
-
console.log(
|
|
5321
|
+
console.log(tag6, `Batch add complete: ${paths.length} paths, ${newPubkeys.length} pubkeys, ${this.balances?.length || 0} balances`);
|
|
5233
5322
|
return { success: true, pubkeys: newPubkeys };
|
|
5234
5323
|
} catch (e) {
|
|
5235
|
-
console.error(
|
|
5324
|
+
console.error(tag6, "Failed:", e);
|
|
5236
5325
|
throw e;
|
|
5237
5326
|
}
|
|
5238
5327
|
};
|
|
@@ -5240,7 +5329,7 @@ class SDK {
|
|
|
5240
5329
|
return this.getGasAssets();
|
|
5241
5330
|
};
|
|
5242
5331
|
this.getGasAssets = async function() {
|
|
5243
|
-
const
|
|
5332
|
+
const tag6 = `${TAG9} | getGasAssets | `;
|
|
5244
5333
|
try {
|
|
5245
5334
|
for (let i = 0;i < this.blockchains.length; i++) {
|
|
5246
5335
|
let networkId = this.blockchains[i];
|
|
@@ -5274,7 +5363,7 @@ class SDK {
|
|
|
5274
5363
|
denom: "maya"
|
|
5275
5364
|
};
|
|
5276
5365
|
this.assetsMap.set(mayaTokenCaip, mayaToken);
|
|
5277
|
-
console.log(
|
|
5366
|
+
console.log(tag6, "Added MAYA token to assetsMap");
|
|
5278
5367
|
}
|
|
5279
5368
|
return this.assetsMap;
|
|
5280
5369
|
} catch (e) {
|
|
@@ -5283,7 +5372,7 @@ class SDK {
|
|
|
5283
5372
|
}
|
|
5284
5373
|
};
|
|
5285
5374
|
this.getPubkeys = async function() {
|
|
5286
|
-
const
|
|
5375
|
+
const tag6 = `${TAG9} | getPubkeys | `;
|
|
5287
5376
|
try {
|
|
5288
5377
|
if (this.paths.length === 0)
|
|
5289
5378
|
throw new Error("No paths found!");
|
|
@@ -5299,15 +5388,15 @@ class SDK {
|
|
|
5299
5388
|
return pubkeys;
|
|
5300
5389
|
} catch (error) {
|
|
5301
5390
|
console.error("Error in getPubkeys:", error);
|
|
5302
|
-
console.error(
|
|
5391
|
+
console.error(tag6, "Error in getPubkeys:", error);
|
|
5303
5392
|
throw error;
|
|
5304
5393
|
}
|
|
5305
5394
|
};
|
|
5306
5395
|
this.getBalancesForNetworks = async function(networkIds) {
|
|
5307
|
-
const
|
|
5396
|
+
const tag6 = `${TAG9} | getBalancesForNetworks | `;
|
|
5308
5397
|
try {
|
|
5309
5398
|
if (!this.pioneer) {
|
|
5310
|
-
console.error(
|
|
5399
|
+
console.error(tag6, "ERROR: Pioneer client not initialized! this.pioneer is:", this.pioneer);
|
|
5311
5400
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
5312
5401
|
}
|
|
5313
5402
|
const assetQuery = [];
|
|
@@ -5345,48 +5434,48 @@ class SDK {
|
|
|
5345
5434
|
identifier: `${balance.caip}:${balance.pubkey}`
|
|
5346
5435
|
});
|
|
5347
5436
|
}
|
|
5348
|
-
console.log(
|
|
5437
|
+
console.log(tag6, "balances: ", balances);
|
|
5349
5438
|
this.balances = balances;
|
|
5350
5439
|
this.events.emit("SET_BALANCES", this.balances);
|
|
5351
5440
|
return this.balances;
|
|
5352
5441
|
} catch (apiError) {
|
|
5353
|
-
console.error(
|
|
5442
|
+
console.error(tag6, "GetPortfolioBalances API call failed:", apiError);
|
|
5354
5443
|
throw new Error(`GetPortfolioBalances API call failed: ${apiError?.message || "Unknown error"}`);
|
|
5355
5444
|
}
|
|
5356
5445
|
} catch (e) {
|
|
5357
|
-
console.error(
|
|
5446
|
+
console.error(tag6, "Error: ", e);
|
|
5358
5447
|
throw e;
|
|
5359
5448
|
}
|
|
5360
5449
|
};
|
|
5361
5450
|
this.getBalances = async function() {
|
|
5362
|
-
const
|
|
5451
|
+
const tag6 = `${TAG9} | getBalances | `;
|
|
5363
5452
|
try {
|
|
5364
5453
|
return await this.getBalancesForNetworks(this.blockchains);
|
|
5365
5454
|
} catch (e) {
|
|
5366
|
-
console.error(
|
|
5455
|
+
console.error(tag6, "Error in getBalances: ", e);
|
|
5367
5456
|
throw e;
|
|
5368
5457
|
}
|
|
5369
5458
|
};
|
|
5370
5459
|
this.getBalance = async function(networkId) {
|
|
5371
|
-
const
|
|
5460
|
+
const tag6 = `${TAG9} | getBalance | `;
|
|
5372
5461
|
try {
|
|
5373
5462
|
const results = await this.getBalancesForNetworks([networkId]);
|
|
5374
5463
|
const filtered = results.filter(async (b3) => b3.networkId === await networkIdToCaip2(networkId));
|
|
5375
5464
|
return filtered;
|
|
5376
5465
|
} catch (e) {
|
|
5377
|
-
console.error(
|
|
5466
|
+
console.error(tag6, "Error: ", e);
|
|
5378
5467
|
throw e;
|
|
5379
5468
|
}
|
|
5380
5469
|
};
|
|
5381
5470
|
this.getFees = async function(networkId) {
|
|
5382
|
-
const
|
|
5471
|
+
const tag6 = `${TAG9} | getFees | `;
|
|
5383
5472
|
try {
|
|
5384
5473
|
if (!this.pioneer) {
|
|
5385
5474
|
throw new Error("Pioneer client not initialized. Call init() first.");
|
|
5386
5475
|
}
|
|
5387
5476
|
return await getFees(this.pioneer, networkId);
|
|
5388
5477
|
} catch (e) {
|
|
5389
|
-
console.error(
|
|
5478
|
+
console.error(tag6, "Error getting fees: ", e);
|
|
5390
5479
|
throw e;
|
|
5391
5480
|
}
|
|
5392
5481
|
};
|
|
@@ -5394,11 +5483,11 @@ class SDK {
|
|
|
5394
5483
|
return estimateTransactionFee(feeRate, unit, networkType, txSize);
|
|
5395
5484
|
};
|
|
5396
5485
|
this.getCharts = async function() {
|
|
5397
|
-
const
|
|
5486
|
+
const tag6 = `${TAG9} | getCharts | `;
|
|
5398
5487
|
try {
|
|
5399
|
-
console.log(
|
|
5488
|
+
console.log(tag6, "Fetching charts");
|
|
5400
5489
|
const newBalances = await getCharts(this.blockchains, this.pioneer, this.pubkeys, this.context);
|
|
5401
|
-
console.log(
|
|
5490
|
+
console.log(tag6, "newBalances: ", newBalances);
|
|
5402
5491
|
const uniqueBalances = new Map([...this.balances, ...newBalances].map((balance) => [
|
|
5403
5492
|
balance.identifier,
|
|
5404
5493
|
{
|
|
@@ -5406,17 +5495,17 @@ class SDK {
|
|
|
5406
5495
|
type: balance.type || "balance"
|
|
5407
5496
|
}
|
|
5408
5497
|
]));
|
|
5409
|
-
console.log(
|
|
5498
|
+
console.log(tag6, "uniqueBalances: ", uniqueBalances);
|
|
5410
5499
|
this.balances = Array.from(uniqueBalances.values());
|
|
5411
|
-
console.log(
|
|
5500
|
+
console.log(tag6, "Updated this.balances: ", this.balances);
|
|
5412
5501
|
return this.balances;
|
|
5413
5502
|
} catch (e) {
|
|
5414
|
-
console.error(
|
|
5503
|
+
console.error(tag6, "Error in getCharts:", e);
|
|
5415
5504
|
throw e;
|
|
5416
5505
|
}
|
|
5417
5506
|
};
|
|
5418
5507
|
this.setContext = async (context) => {
|
|
5419
|
-
const
|
|
5508
|
+
const tag6 = `${TAG9} | setContext | `;
|
|
5420
5509
|
try {
|
|
5421
5510
|
if (!context)
|
|
5422
5511
|
throw Error("context required!");
|
|
@@ -5424,12 +5513,12 @@ class SDK {
|
|
|
5424
5513
|
this.events.emit("SET_CONTEXT", context);
|
|
5425
5514
|
return { success: true };
|
|
5426
5515
|
} catch (e) {
|
|
5427
|
-
console.error(
|
|
5516
|
+
console.error(tag6, "e: ", e);
|
|
5428
5517
|
return { success: false };
|
|
5429
5518
|
}
|
|
5430
5519
|
};
|
|
5431
5520
|
this.setContextType = async (contextType) => {
|
|
5432
|
-
const
|
|
5521
|
+
const tag6 = `${TAG9} | setContextType | `;
|
|
5433
5522
|
try {
|
|
5434
5523
|
if (!contextType)
|
|
5435
5524
|
throw Error("contextType required!");
|
|
@@ -5437,22 +5526,22 @@ class SDK {
|
|
|
5437
5526
|
this.events.emit("SET_CONTEXT_TYPE", contextType);
|
|
5438
5527
|
return { success: true };
|
|
5439
5528
|
} catch (e) {
|
|
5440
|
-
console.error(
|
|
5529
|
+
console.error(tag6, "e: ", e);
|
|
5441
5530
|
return { success: false };
|
|
5442
5531
|
}
|
|
5443
5532
|
};
|
|
5444
5533
|
this.refresh = async () => {
|
|
5445
|
-
const
|
|
5534
|
+
const tag6 = `${TAG9} | refresh | `;
|
|
5446
5535
|
try {
|
|
5447
5536
|
await this.sync();
|
|
5448
5537
|
return this.balances;
|
|
5449
5538
|
} catch (e) {
|
|
5450
|
-
console.error(
|
|
5539
|
+
console.error(tag6, "e: ", e);
|
|
5451
5540
|
throw e;
|
|
5452
5541
|
}
|
|
5453
5542
|
};
|
|
5454
5543
|
this.setAssetContext = async function(asset) {
|
|
5455
|
-
const
|
|
5544
|
+
const tag6 = `${TAG9} | setAssetContext | `;
|
|
5456
5545
|
try {
|
|
5457
5546
|
if (!asset) {
|
|
5458
5547
|
this.assetContext = null;
|
|
@@ -5464,7 +5553,7 @@ class SDK {
|
|
|
5464
5553
|
asset.networkId = caipToNetworkId7(asset.caip);
|
|
5465
5554
|
if (!this.pubkeys || this.pubkeys.length === 0) {
|
|
5466
5555
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no pubkeys loaded. Please initialize wallet first.`;
|
|
5467
|
-
console.error(
|
|
5556
|
+
console.error(tag6, errorMsg);
|
|
5468
5557
|
throw new Error(errorMsg);
|
|
5469
5558
|
}
|
|
5470
5559
|
const pubkeysForNetwork = this.pubkeys.filter((e) => {
|
|
@@ -5479,8 +5568,8 @@ class SDK {
|
|
|
5479
5568
|
});
|
|
5480
5569
|
if (pubkeysForNetwork.length === 0) {
|
|
5481
5570
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no address/xpub found for network ${asset.networkId}`;
|
|
5482
|
-
console.error(
|
|
5483
|
-
console.error(
|
|
5571
|
+
console.error(tag6, errorMsg);
|
|
5572
|
+
console.error(tag6, "Available networks in pubkeys:", [
|
|
5484
5573
|
...new Set(this.pubkeys.flatMap((p2) => p2.networks || []))
|
|
5485
5574
|
]);
|
|
5486
5575
|
throw new Error(errorMsg);
|
|
@@ -5490,43 +5579,43 @@ class SDK {
|
|
|
5490
5579
|
const xpubFound = pubkeysForNetwork.some((p2) => p2.type === "xpub" && p2.pubkey);
|
|
5491
5580
|
if (!xpubFound) {
|
|
5492
5581
|
const errorMsg = `Cannot set asset context for UTXO chain ${asset.caip} - xpub required but not found`;
|
|
5493
|
-
console.error(
|
|
5582
|
+
console.error(tag6, errorMsg);
|
|
5494
5583
|
throw new Error(errorMsg);
|
|
5495
5584
|
}
|
|
5496
5585
|
}
|
|
5497
5586
|
const hasValidAddress = pubkeysForNetwork.some((p2) => p2.address || p2.master || p2.pubkey);
|
|
5498
5587
|
if (!hasValidAddress) {
|
|
5499
5588
|
const errorMsg = `Cannot set asset context for ${asset.caip} - no valid address found in pubkeys`;
|
|
5500
|
-
console.error(
|
|
5589
|
+
console.error(tag6, errorMsg);
|
|
5501
5590
|
throw new Error(errorMsg);
|
|
5502
5591
|
}
|
|
5503
|
-
console.log(
|
|
5592
|
+
console.log(tag6, `✅ Validated: Found ${pubkeysForNetwork.length} addresses for ${asset.networkId}`);
|
|
5504
5593
|
let freshPriceUsd = 0;
|
|
5505
5594
|
try {
|
|
5506
5595
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
5507
|
-
console.warn(
|
|
5596
|
+
console.warn(tag6, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
5508
5597
|
} else {
|
|
5509
|
-
console.log(
|
|
5598
|
+
console.log(tag6, "Fetching fresh market price for:", asset.caip);
|
|
5510
5599
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
5511
|
-
console.log(
|
|
5600
|
+
console.log(tag6, "Market data response:", marketData);
|
|
5512
5601
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
5513
5602
|
freshPriceUsd = marketData.data[0];
|
|
5514
|
-
console.log(
|
|
5603
|
+
console.log(tag6, "✅ Fresh market price:", freshPriceUsd);
|
|
5515
5604
|
} else {
|
|
5516
|
-
console.warn(
|
|
5605
|
+
console.warn(tag6, "No market data returned for:", asset.caip);
|
|
5517
5606
|
}
|
|
5518
5607
|
}
|
|
5519
5608
|
} catch (marketError) {
|
|
5520
|
-
console.error(
|
|
5609
|
+
console.error(tag6, "Error fetching market price:", marketError);
|
|
5521
5610
|
}
|
|
5522
5611
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
5523
|
-
console.log(
|
|
5612
|
+
console.log(tag6, "assetInfo: ", assetInfo);
|
|
5524
5613
|
let assetInfoDiscovery = assetData2[asset.caip];
|
|
5525
|
-
console.log(
|
|
5614
|
+
console.log(tag6, "assetInfoDiscovery: ", assetInfoDiscovery);
|
|
5526
5615
|
if (assetInfoDiscovery)
|
|
5527
5616
|
assetInfo = assetInfoDiscovery;
|
|
5528
5617
|
if (!assetInfo) {
|
|
5529
|
-
console.log(
|
|
5618
|
+
console.log(tag6, "Building placeholder asset!");
|
|
5530
5619
|
assetInfo = {
|
|
5531
5620
|
caip: asset.caip.toLowerCase(),
|
|
5532
5621
|
networkId: asset.networkId,
|
|
@@ -5543,30 +5632,30 @@ class SDK {
|
|
|
5543
5632
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
5544
5633
|
if (balance > 0 && valueUsd > 0) {
|
|
5545
5634
|
priceValue = valueUsd / balance;
|
|
5546
|
-
console.log(
|
|
5635
|
+
console.log(tag6, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
5547
5636
|
}
|
|
5548
5637
|
}
|
|
5549
5638
|
if (priceValue && priceValue > 0) {
|
|
5550
|
-
console.log(
|
|
5639
|
+
console.log(tag6, "detected priceUsd from balance:", priceValue);
|
|
5551
5640
|
assetInfo.priceUsd = priceValue;
|
|
5552
5641
|
}
|
|
5553
5642
|
}
|
|
5554
5643
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
5555
5644
|
assetInfo.priceUsd = freshPriceUsd;
|
|
5556
|
-
console.log(
|
|
5645
|
+
console.log(tag6, "✅ Using fresh market price:", freshPriceUsd);
|
|
5557
5646
|
let totalBalance = 0;
|
|
5558
5647
|
let totalValueUsd = 0;
|
|
5559
|
-
console.log(
|
|
5648
|
+
console.log(tag6, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
5560
5649
|
for (const balanceEntry of matchingBalances) {
|
|
5561
5650
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5562
5651
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5563
5652
|
totalBalance += balance;
|
|
5564
5653
|
totalValueUsd += valueUsd;
|
|
5565
|
-
console.log(
|
|
5654
|
+
console.log(tag6, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5566
5655
|
}
|
|
5567
5656
|
assetInfo.balance = totalBalance.toString();
|
|
5568
5657
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5569
|
-
console.log(
|
|
5658
|
+
console.log(tag6, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5570
5659
|
}
|
|
5571
5660
|
const assetBalances = this.balances.filter((b3) => b3.caip === asset.caip);
|
|
5572
5661
|
const assetPubkeys = this.pubkeys.filter((p2) => p2.networks && Array.isArray(p2.networks) && p2.networks.includes(caipToNetworkId7(asset.caip)) || caipToNetworkId7(asset.caip).includes("eip155") && p2.networks && Array.isArray(p2.networks) && p2.networks.some((n2) => n2.startsWith("eip155")));
|
|
@@ -5586,7 +5675,7 @@ class SDK {
|
|
|
5586
5675
|
const balanceAmount = parseFloat(balance.balance || 0);
|
|
5587
5676
|
balance.valueUsd = (balanceAmount * freshPriceUsd).toString();
|
|
5588
5677
|
}
|
|
5589
|
-
console.log(
|
|
5678
|
+
console.log(tag6, "Updated all balances with fresh price data");
|
|
5590
5679
|
}
|
|
5591
5680
|
this.assetContext = finalAssetContext;
|
|
5592
5681
|
if (asset.isToken || asset.type === "token" || assetInfo.isToken || assetInfo.type === "token") {
|
|
@@ -5638,20 +5727,20 @@ class SDK {
|
|
|
5638
5727
|
const currentContextValid = this.pubkeyContext?.networks?.includes(networkId);
|
|
5639
5728
|
if (!this.pubkeyContext || !currentContextValid) {
|
|
5640
5729
|
this.pubkeyContext = assetPubkeys[0];
|
|
5641
|
-
console.log(
|
|
5730
|
+
console.log(tag6, "Auto-set pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey);
|
|
5642
5731
|
} else {
|
|
5643
|
-
console.log(
|
|
5732
|
+
console.log(tag6, "Preserving existing pubkey context for network:", this.pubkeyContext.address || this.pubkeyContext.pubkey, `(addressNList: [${(this.pubkeyContext.addressNList || this.pubkeyContext.addressNListMaster).join(", ")}])`);
|
|
5644
5733
|
}
|
|
5645
5734
|
}
|
|
5646
5735
|
this.events.emit("SET_ASSET_CONTEXT", this.assetContext);
|
|
5647
5736
|
return this.assetContext;
|
|
5648
5737
|
} catch (e) {
|
|
5649
|
-
console.error(
|
|
5738
|
+
console.error(tag6, "e: ", e);
|
|
5650
5739
|
throw e;
|
|
5651
5740
|
}
|
|
5652
5741
|
};
|
|
5653
5742
|
this.setPubkeyContext = async function(pubkey) {
|
|
5654
|
-
let
|
|
5743
|
+
let tag6 = `${TAG9} | setPubkeyContext | `;
|
|
5655
5744
|
try {
|
|
5656
5745
|
if (!pubkey)
|
|
5657
5746
|
throw Error("pubkey is required");
|
|
@@ -5659,31 +5748,31 @@ class SDK {
|
|
|
5659
5748
|
throw Error("invalid pubkey: missing pubkey or address");
|
|
5660
5749
|
const exists = this.pubkeys.some((pk) => pk.pubkey === pubkey.pubkey || pk.address === pubkey.address || pk.pubkey === pubkey.address);
|
|
5661
5750
|
if (!exists) {
|
|
5662
|
-
console.warn(
|
|
5751
|
+
console.warn(tag6, "Pubkey not found in current pubkeys array");
|
|
5663
5752
|
}
|
|
5664
5753
|
this.pubkeyContext = pubkey;
|
|
5665
|
-
console.log(
|
|
5754
|
+
console.log(tag6, "Pubkey context set to:", pubkey.address || pubkey.pubkey, "note:", pubkey.note, "addressNList:", pubkey.addressNList || pubkey.addressNListMaster);
|
|
5666
5755
|
return true;
|
|
5667
5756
|
} catch (e) {
|
|
5668
|
-
console.error(
|
|
5757
|
+
console.error(tag6, "e: ", e);
|
|
5669
5758
|
throw e;
|
|
5670
5759
|
}
|
|
5671
5760
|
};
|
|
5672
5761
|
this.setOutboundAssetContext = async function(asset) {
|
|
5673
|
-
const
|
|
5762
|
+
const tag6 = `${TAG9} | setOutputAssetContext | `;
|
|
5674
5763
|
try {
|
|
5675
|
-
console.log(
|
|
5764
|
+
console.log(tag6, "0. asset: ", asset);
|
|
5676
5765
|
if (!asset) {
|
|
5677
5766
|
this.outboundAssetContext = null;
|
|
5678
5767
|
return;
|
|
5679
5768
|
}
|
|
5680
|
-
console.log(
|
|
5769
|
+
console.log(tag6, "1 asset: ", asset);
|
|
5681
5770
|
if (!asset.caip)
|
|
5682
5771
|
throw Error("Invalid Asset! missing caip!");
|
|
5683
5772
|
if (!asset.networkId)
|
|
5684
5773
|
asset.networkId = caipToNetworkId7(asset.caip);
|
|
5685
|
-
console.log(
|
|
5686
|
-
console.log(
|
|
5774
|
+
console.log(tag6, "networkId: ", asset.networkId);
|
|
5775
|
+
console.log(tag6, "this.pubkeys: ", this.pubkeys);
|
|
5687
5776
|
const pubkey = this.pubkeys.find((p2) => {
|
|
5688
5777
|
if (!p2.networks || !Array.isArray(p2.networks))
|
|
5689
5778
|
return false;
|
|
@@ -5698,23 +5787,23 @@ class SDK {
|
|
|
5698
5787
|
let freshPriceUsd = 0;
|
|
5699
5788
|
try {
|
|
5700
5789
|
if (!asset.caip || typeof asset.caip !== "string" || !asset.caip.includes(":")) {
|
|
5701
|
-
console.warn(
|
|
5790
|
+
console.warn(tag6, "Invalid or missing CAIP, skipping market price fetch:", asset.caip);
|
|
5702
5791
|
} else {
|
|
5703
|
-
console.log(
|
|
5792
|
+
console.log(tag6, "Fetching fresh market price for:", asset.caip);
|
|
5704
5793
|
const marketData = await this.pioneer.GetMarketInfo([asset.caip]);
|
|
5705
|
-
console.log(
|
|
5794
|
+
console.log(tag6, "Market data response:", marketData);
|
|
5706
5795
|
if (marketData && marketData.data && marketData.data.length > 0) {
|
|
5707
5796
|
freshPriceUsd = marketData.data[0];
|
|
5708
|
-
console.log(
|
|
5797
|
+
console.log(tag6, "✅ Fresh market price:", freshPriceUsd);
|
|
5709
5798
|
} else {
|
|
5710
|
-
console.warn(
|
|
5799
|
+
console.warn(tag6, "No market data returned for:", asset.caip);
|
|
5711
5800
|
}
|
|
5712
5801
|
}
|
|
5713
5802
|
} catch (marketError) {
|
|
5714
|
-
console.error(
|
|
5803
|
+
console.error(tag6, "Error fetching market price:", marketError);
|
|
5715
5804
|
}
|
|
5716
5805
|
let assetInfo = this.assetsMap.get(asset.caip.toLowerCase());
|
|
5717
|
-
console.log(
|
|
5806
|
+
console.log(tag6, "assetInfo: ", assetInfo);
|
|
5718
5807
|
if (!assetInfo) {
|
|
5719
5808
|
assetInfo = {
|
|
5720
5809
|
caip: asset.caip.toLowerCase(),
|
|
@@ -5732,45 +5821,45 @@ class SDK {
|
|
|
5732
5821
|
const valueUsd = parseFloat(matchingBalances[0].valueUsd);
|
|
5733
5822
|
if (balance > 0 && valueUsd > 0) {
|
|
5734
5823
|
priceValue = valueUsd / balance;
|
|
5735
|
-
console.log(
|
|
5824
|
+
console.log(tag6, "calculated priceUsd from valueUsd/balance:", priceValue);
|
|
5736
5825
|
}
|
|
5737
5826
|
}
|
|
5738
5827
|
if (priceValue && priceValue > 0) {
|
|
5739
|
-
console.log(
|
|
5828
|
+
console.log(tag6, "detected priceUsd from balance:", priceValue);
|
|
5740
5829
|
assetInfo.priceUsd = priceValue;
|
|
5741
5830
|
}
|
|
5742
5831
|
}
|
|
5743
5832
|
if (freshPriceUsd && freshPriceUsd > 0) {
|
|
5744
5833
|
assetInfo.priceUsd = freshPriceUsd;
|
|
5745
|
-
console.log(
|
|
5834
|
+
console.log(tag6, "✅ Using fresh market price:", freshPriceUsd);
|
|
5746
5835
|
let totalBalance = 0;
|
|
5747
5836
|
let totalValueUsd = 0;
|
|
5748
|
-
console.log(
|
|
5837
|
+
console.log(tag6, `Found ${matchingBalances.length} balance entries for ${asset.caip}`);
|
|
5749
5838
|
for (const balanceEntry of matchingBalances) {
|
|
5750
5839
|
const balance = parseFloat(balanceEntry.balance) || 0;
|
|
5751
5840
|
const valueUsd = parseFloat(balanceEntry.valueUsd) || 0;
|
|
5752
5841
|
totalBalance += balance;
|
|
5753
5842
|
totalValueUsd += valueUsd;
|
|
5754
|
-
console.log(
|
|
5843
|
+
console.log(tag6, ` Balance entry: ${balance} (${valueUsd} USD)`);
|
|
5755
5844
|
}
|
|
5756
5845
|
assetInfo.balance = totalBalance.toString();
|
|
5757
5846
|
assetInfo.valueUsd = totalValueUsd.toFixed(2);
|
|
5758
|
-
console.log(
|
|
5847
|
+
console.log(tag6, `Aggregated balance: ${totalBalance} (${totalValueUsd.toFixed(2)} USD)`);
|
|
5759
5848
|
}
|
|
5760
|
-
console.log(
|
|
5849
|
+
console.log(tag6, "CHECKPOINT 1");
|
|
5761
5850
|
this.outboundAssetContext = { ...assetInfo, ...asset, ...pubkey };
|
|
5762
|
-
console.log(
|
|
5763
|
-
console.log(
|
|
5851
|
+
console.log(tag6, "CHECKPOINT 3");
|
|
5852
|
+
console.log(tag6, "outboundAssetContext: assetInfo: ", assetInfo);
|
|
5764
5853
|
if (asset.caip) {
|
|
5765
5854
|
this.outboundBlockchainContext = caipToNetworkId7(asset.caip);
|
|
5766
5855
|
} else if (asset.networkId) {
|
|
5767
5856
|
this.outboundBlockchainContext = asset.networkId;
|
|
5768
5857
|
}
|
|
5769
|
-
console.log(
|
|
5858
|
+
console.log(tag6, "CHECKPOINT 4");
|
|
5770
5859
|
this.events.emit("SET_OUTBOUND_ASSET_CONTEXT", this.outboundAssetContext);
|
|
5771
5860
|
return this.outboundAssetContext;
|
|
5772
5861
|
} catch (e) {
|
|
5773
|
-
console.error(
|
|
5862
|
+
console.error(tag6, "e: ", e);
|
|
5774
5863
|
throw e;
|
|
5775
5864
|
}
|
|
5776
5865
|
};
|