@pioneer-platform/pioneer-sdk 4.20.5 → 4.20.7

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.js CHANGED
@@ -3707,8 +3707,62 @@ function buildDashboardFromBalances(balances, blockchains, assetsMap) {
3707
3707
  return dashboardData;
3708
3708
  }
3709
3709
 
3710
+ // src/utils/sync-market.ts
3711
+ var TAG8 = " | sync-market | ";
3712
+ async function syncMarket(balances, pioneer) {
3713
+ const tag5 = `${TAG8} | syncMarket | `;
3714
+ try {
3715
+ const invalidBalances = balances.filter((b3) => !b3 || !b3.caip || typeof b3.caip !== "string" || !b3.caip.includes(":"));
3716
+ if (invalidBalances.length > 0) {
3717
+ console.warn(tag5, `Found ${invalidBalances.length} balances with invalid CAIPs:`, invalidBalances.map((b3) => ({
3718
+ caip: b3?.caip,
3719
+ type: typeof b3?.caip,
3720
+ symbol: b3?.symbol,
3721
+ balance: b3?.balance
3722
+ })));
3723
+ }
3724
+ let allCaips = balances.filter((b3) => b3 && b3.caip && typeof b3.caip === "string" && b3.caip.trim().length > 0).map((b3) => b3.caip);
3725
+ allCaips = [...new Set(allCaips)];
3726
+ allCaips = allCaips.filter((caip) => caip && typeof caip === "string" && caip.trim().length > 0 && caip.includes(":"));
3727
+ console.log("GetMarketInfo: payload: ", allCaips);
3728
+ console.log("GetMarketInfo: payload type: ", typeof allCaips);
3729
+ console.log("GetMarketInfo: payload length: ", allCaips.length);
3730
+ const invalidEntries = allCaips.filter((caip) => typeof caip !== "string");
3731
+ if (invalidEntries.length > 0) {
3732
+ console.error(tag5, "CRITICAL: Invalid entries detected in allCaips:", invalidEntries);
3733
+ throw new Error("Invalid CAIP entries detected - aborting market sync");
3734
+ }
3735
+ if (allCaips && allCaips.length > 0) {
3736
+ try {
3737
+ let allPrices = await pioneer.GetMarketInfo(allCaips);
3738
+ console.log("GetMarketInfo: response: ", allPrices);
3739
+ const priceMap = {};
3740
+ if (allPrices && allPrices.data) {
3741
+ for (let i = 0;i < allCaips.length && i < allPrices.data.length; i++) {
3742
+ priceMap[allCaips[i]] = allPrices.data[i];
3743
+ }
3744
+ }
3745
+ for (let balance of balances) {
3746
+ if (balance && balance.caip && priceMap[balance.caip] !== undefined) {
3747
+ balance.price = priceMap[balance.caip];
3748
+ balance.priceUsd = priceMap[balance.caip];
3749
+ balance.valueUsd = balance.price * (balance.balance || 0);
3750
+ }
3751
+ }
3752
+ } catch (apiError) {
3753
+ console.error(tag5, "API error fetching market info:", apiError);
3754
+ console.warn(tag5, "Continuing without market prices");
3755
+ }
3756
+ }
3757
+ return true;
3758
+ } catch (e) {
3759
+ console.error(tag5, "e:", e);
3760
+ throw e;
3761
+ }
3762
+ }
3763
+
3710
3764
  // src/index.ts
3711
- var TAG8 = " | Pioneer-sdk | ";
3765
+ var TAG9 = " | Pioneer-sdk | ";
3712
3766
 
3713
3767
  class SDK {
3714
3768
  status;
@@ -3872,7 +3926,7 @@ class SDK {
3872
3926
  return true;
3873
3927
  };
3874
3928
  this.setPubkeys = (newPubkeys) => {
3875
- const tag5 = `${TAG8} | setPubkeys | `;
3929
+ const tag5 = `${TAG9} | setPubkeys | `;
3876
3930
  this.pubkeys = [];
3877
3931
  this.pubkeySet.clear();
3878
3932
  let added = 0;
@@ -3883,7 +3937,7 @@ class SDK {
3883
3937
  }
3884
3938
  };
3885
3939
  this.getUnifiedPortfolio = async function() {
3886
- const tag5 = `${TAG8} | getUnifiedPortfolio | `;
3940
+ const tag5 = `${TAG9} | getUnifiedPortfolio | `;
3887
3941
  try {
3888
3942
  const startTime = performance.now();
3889
3943
  try {
@@ -3991,7 +4045,7 @@ class SDK {
3991
4045
  }
3992
4046
  };
3993
4047
  this.init = async function(walletsVerbose, setup) {
3994
- const tag5 = `${TAG8} | init | `;
4048
+ const tag5 = `${TAG9} | init | `;
3995
4049
  try {
3996
4050
  if (!this.username)
3997
4051
  throw Error("username required!");
@@ -4082,58 +4136,10 @@ class SDK {
4082
4136
  return buildDashboardFromBalances(this.balances, this.blockchains, this.assetsMap);
4083
4137
  };
4084
4138
  this.syncMarket = async function() {
4085
- const tag5 = `${TAG8} | syncMarket | `;
4086
- try {
4087
- const invalidBalances = this.balances.filter((b3) => !b3 || !b3.caip || typeof b3.caip !== "string" || !b3.caip.includes(":"));
4088
- if (invalidBalances.length > 0) {
4089
- console.warn(tag5, `Found ${invalidBalances.length} balances with invalid CAIPs:`, invalidBalances.map((b3) => ({
4090
- caip: b3?.caip,
4091
- type: typeof b3?.caip,
4092
- symbol: b3?.symbol,
4093
- balance: b3?.balance
4094
- })));
4095
- }
4096
- let allCaips = this.balances.filter((b3) => b3 && b3.caip && typeof b3.caip === "string" && b3.caip.trim().length > 0).map((b3) => b3.caip);
4097
- allCaips = [...new Set(allCaips)];
4098
- allCaips = allCaips.filter((caip) => caip && typeof caip === "string" && caip.trim().length > 0 && caip.includes(":"));
4099
- console.log("GetMarketInfo: payload: ", allCaips);
4100
- console.log("GetMarketInfo: payload type: ", typeof allCaips);
4101
- console.log("GetMarketInfo: payload length: ", allCaips.length);
4102
- const invalidEntries = allCaips.filter((caip) => typeof caip !== "string");
4103
- if (invalidEntries.length > 0) {
4104
- console.error(tag5, "CRITICAL: Invalid entries detected in allCaips:", invalidEntries);
4105
- throw new Error("Invalid CAIP entries detected - aborting market sync");
4106
- }
4107
- if (allCaips && allCaips.length > 0) {
4108
- try {
4109
- let allPrices = await this.pioneer.GetMarketInfo(allCaips);
4110
- console.log("GetMarketInfo: response: ", allPrices);
4111
- const priceMap = {};
4112
- if (allPrices && allPrices.data) {
4113
- for (let i = 0;i < allCaips.length && i < allPrices.data.length; i++) {
4114
- priceMap[allCaips[i]] = allPrices.data[i];
4115
- }
4116
- }
4117
- for (let balance of this.balances) {
4118
- if (balance && balance.caip && priceMap[balance.caip] !== undefined) {
4119
- balance.price = priceMap[balance.caip];
4120
- balance.priceUsd = priceMap[balance.caip];
4121
- balance.valueUsd = balance.price * (balance.balance || 0);
4122
- }
4123
- }
4124
- } catch (apiError) {
4125
- console.error(tag5, "API error fetching market info:", apiError);
4126
- console.warn(tag5, "Continuing without market prices");
4127
- }
4128
- }
4129
- return true;
4130
- } catch (e) {
4131
- console.error(tag5, "e:", e);
4132
- throw e;
4133
- }
4139
+ return syncMarket(this.balances, this.pioneer);
4134
4140
  };
4135
4141
  this.sync = async function() {
4136
- const tag5 = `${TAG8} | sync | `;
4142
+ const tag5 = `${TAG9} | sync | `;
4137
4143
  try {
4138
4144
  const matchesNetwork = (item, networkId) => {
4139
4145
  if (!item.networks || !Array.isArray(item.networks))
@@ -4286,7 +4292,7 @@ class SDK {
4286
4292
  }
4287
4293
  };
4288
4294
  this.buildTx = async function(sendPayload) {
4289
- let tag5 = TAG8 + " | buildTx | ";
4295
+ let tag5 = TAG9 + " | buildTx | ";
4290
4296
  try {
4291
4297
  const transactionDependencies = {
4292
4298
  context: this.context,
@@ -4308,7 +4314,7 @@ class SDK {
4308
4314
  }
4309
4315
  };
4310
4316
  this.buildDelegateTx = async function(caip, params) {
4311
- let tag5 = TAG8 + " | buildDelegateTx | ";
4317
+ let tag5 = TAG9 + " | buildDelegateTx | ";
4312
4318
  try {
4313
4319
  const delegateParams = {
4314
4320
  ...params,
@@ -4323,7 +4329,7 @@ class SDK {
4323
4329
  }
4324
4330
  };
4325
4331
  this.buildUndelegateTx = async function(caip, params) {
4326
- let tag5 = TAG8 + " | buildUndelegateTx | ";
4332
+ let tag5 = TAG9 + " | buildUndelegateTx | ";
4327
4333
  try {
4328
4334
  const undelegateParams = {
4329
4335
  ...params,
@@ -4338,7 +4344,7 @@ class SDK {
4338
4344
  }
4339
4345
  };
4340
4346
  this.buildClaimRewardsTx = async function(caip, params) {
4341
- let tag5 = TAG8 + " | buildClaimRewardsTx | ";
4347
+ let tag5 = TAG9 + " | buildClaimRewardsTx | ";
4342
4348
  try {
4343
4349
  const claimParams = {
4344
4350
  ...params,
@@ -4353,7 +4359,7 @@ class SDK {
4353
4359
  }
4354
4360
  };
4355
4361
  this.buildClaimAllRewardsTx = async function(caip, params) {
4356
- let tag5 = TAG8 + " | buildClaimAllRewardsTx | ";
4362
+ let tag5 = TAG9 + " | buildClaimAllRewardsTx | ";
4357
4363
  try {
4358
4364
  const claimAllParams = {
4359
4365
  ...params,
@@ -4367,7 +4373,7 @@ class SDK {
4367
4373
  }
4368
4374
  };
4369
4375
  this.signTx = async function(unsignedTx) {
4370
- let tag5 = TAG8 + " | signTx | ";
4376
+ let tag5 = TAG9 + " | signTx | ";
4371
4377
  try {
4372
4378
  const transactionDependencies = {
4373
4379
  context: this.context,
@@ -4388,7 +4394,7 @@ class SDK {
4388
4394
  }
4389
4395
  };
4390
4396
  this.broadcastTx = async function(caip, signedTx) {
4391
- let tag5 = TAG8 + " | broadcastTx | ";
4397
+ let tag5 = TAG9 + " | broadcastTx | ";
4392
4398
  try {
4393
4399
  const transactionDependencies = {
4394
4400
  context: this.context,
@@ -4412,7 +4418,7 @@ class SDK {
4412
4418
  }
4413
4419
  };
4414
4420
  this.swap = async function(swapPayload) {
4415
- let tag5 = `${TAG8} | swap | `;
4421
+ let tag5 = `${TAG9} | swap | `;
4416
4422
  try {
4417
4423
  if (!swapPayload)
4418
4424
  throw Error("swapPayload required!");
@@ -4547,7 +4553,7 @@ class SDK {
4547
4553
  }
4548
4554
  };
4549
4555
  this.transfer = async function(sendPayload) {
4550
- let tag5 = `${TAG8} | transfer | `;
4556
+ let tag5 = `${TAG9} | transfer | `;
4551
4557
  try {
4552
4558
  if (!sendPayload)
4553
4559
  throw Error("sendPayload required!");
@@ -4642,7 +4648,7 @@ class SDK {
4642
4648
  }
4643
4649
  };
4644
4650
  this.setBlockchains = async function(blockchains) {
4645
- const tag5 = `${TAG8} | setBlockchains | `;
4651
+ const tag5 = `${TAG9} | setBlockchains | `;
4646
4652
  try {
4647
4653
  if (!blockchains)
4648
4654
  throw Error("blockchains required!");
@@ -4658,7 +4664,7 @@ class SDK {
4658
4664
  }
4659
4665
  };
4660
4666
  this.addAsset = async function(caip, data) {
4661
- let tag5 = TAG8 + " | addAsset | ";
4667
+ let tag5 = TAG9 + " | addAsset | ";
4662
4668
  try {
4663
4669
  let success = false;
4664
4670
  if (!caip)
@@ -4696,7 +4702,7 @@ class SDK {
4696
4702
  }
4697
4703
  };
4698
4704
  this.clearWalletState = async function() {
4699
- const tag5 = `${TAG8} | clearWalletState | `;
4705
+ const tag5 = `${TAG9} | clearWalletState | `;
4700
4706
  try {
4701
4707
  this.context = null;
4702
4708
  this.paths = [];
@@ -4711,7 +4717,7 @@ class SDK {
4711
4717
  }
4712
4718
  };
4713
4719
  this.addPath = async function(path) {
4714
- const tag5 = `${TAG8} | addPath | `;
4720
+ const tag5 = `${TAG9} | addPath | `;
4715
4721
  try {
4716
4722
  this.paths.push(path);
4717
4723
  const pubkey = await getPubkey(path.networks[0], path, this.keepKeySdk, this.context);
@@ -4725,7 +4731,7 @@ class SDK {
4725
4731
  }
4726
4732
  };
4727
4733
  this.addPaths = async function(paths) {
4728
- const tag5 = `${TAG8} | addPaths | `;
4734
+ const tag5 = `${TAG9} | addPaths | `;
4729
4735
  try {
4730
4736
  console.log(tag5, `Adding ${paths.length} paths in batch mode...`);
4731
4737
  this.paths.push(...paths);
@@ -4761,7 +4767,7 @@ class SDK {
4761
4767
  return this.getGasAssets();
4762
4768
  };
4763
4769
  this.getGasAssets = async function() {
4764
- const tag5 = `${TAG8} | getGasAssets | `;
4770
+ const tag5 = `${TAG9} | getGasAssets | `;
4765
4771
  try {
4766
4772
  for (let i = 0;i < this.blockchains.length; i++) {
4767
4773
  let networkId = this.blockchains[i];
@@ -4804,7 +4810,7 @@ class SDK {
4804
4810
  }
4805
4811
  };
4806
4812
  this.getPubkeys = async function() {
4807
- const tag5 = `${TAG8} | getPubkeys | `;
4813
+ const tag5 = `${TAG9} | getPubkeys | `;
4808
4814
  try {
4809
4815
  if (this.paths.length === 0)
4810
4816
  throw new Error("No paths found!");
@@ -4825,7 +4831,7 @@ class SDK {
4825
4831
  }
4826
4832
  };
4827
4833
  this.getBalancesForNetworks = async function(networkIds) {
4828
- const tag5 = `${TAG8} | getBalancesForNetworks | `;
4834
+ const tag5 = `${TAG9} | getBalancesForNetworks | `;
4829
4835
  try {
4830
4836
  if (!this.pioneer) {
4831
4837
  console.error(tag5, "ERROR: Pioneer client not initialized! this.pioneer is:", this.pioneer);
@@ -4880,7 +4886,7 @@ class SDK {
4880
4886
  }
4881
4887
  };
4882
4888
  this.getBalances = async function() {
4883
- const tag5 = `${TAG8} | getBalances | `;
4889
+ const tag5 = `${TAG9} | getBalances | `;
4884
4890
  try {
4885
4891
  return await this.getBalancesForNetworks(this.blockchains);
4886
4892
  } catch (e) {
@@ -4889,7 +4895,7 @@ class SDK {
4889
4895
  }
4890
4896
  };
4891
4897
  this.getBalance = async function(networkId) {
4892
- const tag5 = `${TAG8} | getBalance | `;
4898
+ const tag5 = `${TAG9} | getBalance | `;
4893
4899
  try {
4894
4900
  const results = await this.getBalancesForNetworks([networkId]);
4895
4901
  const filtered = results.filter(async (b3) => b3.networkId === await networkIdToCaip2(networkId));
@@ -4900,7 +4906,7 @@ class SDK {
4900
4906
  }
4901
4907
  };
4902
4908
  this.getFees = async function(networkId) {
4903
- const tag5 = `${TAG8} | getFees | `;
4909
+ const tag5 = `${TAG9} | getFees | `;
4904
4910
  try {
4905
4911
  if (!this.pioneer) {
4906
4912
  throw new Error("Pioneer client not initialized. Call init() first.");
@@ -4915,7 +4921,7 @@ class SDK {
4915
4921
  return estimateTransactionFee(feeRate, unit, networkType, txSize);
4916
4922
  };
4917
4923
  this.getCharts = async function() {
4918
- const tag5 = `${TAG8} | getCharts | `;
4924
+ const tag5 = `${TAG9} | getCharts | `;
4919
4925
  try {
4920
4926
  console.log(tag5, "Fetching charts");
4921
4927
  const newBalances = await getCharts(this.blockchains, this.pioneer, this.pubkeys, this.context);
@@ -4937,7 +4943,7 @@ class SDK {
4937
4943
  }
4938
4944
  };
4939
4945
  this.setContext = async (context) => {
4940
- const tag5 = `${TAG8} | setContext | `;
4946
+ const tag5 = `${TAG9} | setContext | `;
4941
4947
  try {
4942
4948
  if (!context)
4943
4949
  throw Error("context required!");
@@ -4950,7 +4956,7 @@ class SDK {
4950
4956
  }
4951
4957
  };
4952
4958
  this.setContextType = async (contextType) => {
4953
- const tag5 = `${TAG8} | setContextType | `;
4959
+ const tag5 = `${TAG9} | setContextType | `;
4954
4960
  try {
4955
4961
  if (!contextType)
4956
4962
  throw Error("contextType required!");
@@ -4963,7 +4969,7 @@ class SDK {
4963
4969
  }
4964
4970
  };
4965
4971
  this.refresh = async () => {
4966
- const tag5 = `${TAG8} | refresh | `;
4972
+ const tag5 = `${TAG9} | refresh | `;
4967
4973
  try {
4968
4974
  await this.sync();
4969
4975
  return this.balances;
@@ -4973,7 +4979,7 @@ class SDK {
4973
4979
  }
4974
4980
  };
4975
4981
  this.setAssetContext = async function(asset) {
4976
- const tag5 = `${TAG8} | setAssetContext | `;
4982
+ const tag5 = `${TAG9} | setAssetContext | `;
4977
4983
  try {
4978
4984
  if (!asset) {
4979
4985
  this.assetContext = null;
@@ -5172,7 +5178,7 @@ class SDK {
5172
5178
  }
5173
5179
  };
5174
5180
  this.setPubkeyContext = async function(pubkey) {
5175
- let tag5 = `${TAG8} | setPubkeyContext | `;
5181
+ let tag5 = `${TAG9} | setPubkeyContext | `;
5176
5182
  try {
5177
5183
  if (!pubkey)
5178
5184
  throw Error("pubkey is required");
@@ -5191,7 +5197,7 @@ class SDK {
5191
5197
  }
5192
5198
  };
5193
5199
  this.setOutboundAssetContext = async function(asset) {
5194
- const tag5 = `${TAG8} | setOutputAssetContext | `;
5200
+ const tag5 = `${TAG9} | setOutputAssetContext | `;
5195
5201
  try {
5196
5202
  console.log(tag5, "0. asset: ", asset);
5197
5203
  if (!asset) {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "author": "highlander",
3
3
  "name": "@pioneer-platform/pioneer-sdk",
4
- "version": "4.20.5",
4
+ "version": "4.20.7",
5
5
  "dependencies": {
6
6
  "@keepkey/keepkey-sdk": "^0.2.62",
7
7
  "@pioneer-platform/loggerdog": "^8.11.0",
8
8
  "@pioneer-platform/pioneer-caip": "^9.10.0",
9
- "@pioneer-platform/pioneer-client": "^9.10.2",
9
+ "@pioneer-platform/pioneer-client": "^9.10.3",
10
10
  "@pioneer-platform/pioneer-coins": "^9.11.0",
11
11
  "@pioneer-platform/pioneer-discovery": "^0.8.0",
12
12
  "@pioneer-platform/pioneer-events": "^8.11.0",
package/src/index.ts CHANGED
@@ -19,6 +19,7 @@ import { getFees, estimateTransactionFee, type NormalizedFeeRates, type FeeEstim
19
19
  import { detectKkApiAvailability } from './utils/kkapi-detection.js';
20
20
  import { formatTime } from './utils/format-time.js';
21
21
  import { buildDashboardFromBalances } from './utils/build-dashboard.js';
22
+ import { syncMarket } from './utils/sync-market.js';
22
23
 
23
24
  const TAG = ' | Pioneer-sdk | ';
24
25
 
@@ -572,86 +573,8 @@ export class SDK {
572
573
  this.buildDashboardFromBalances = function () {
573
574
  return buildDashboardFromBalances(this.balances, this.blockchains, this.assetsMap);
574
575
  };
575
-
576
576
  this.syncMarket = async function () {
577
- const tag = `${TAG} | syncMarket | `;
578
- try {
579
- // Log balances with invalid CAIPs for debugging
580
- const invalidBalances = this.balances.filter(b =>
581
- !b || !b.caip || typeof b.caip !== 'string' || !b.caip.includes(':')
582
- );
583
- if (invalidBalances.length > 0) {
584
- console.warn(tag, `Found ${invalidBalances.length} balances with invalid CAIPs:`,
585
- invalidBalances.map(b => ({
586
- caip: b?.caip,
587
- type: typeof b?.caip,
588
- symbol: b?.symbol,
589
- balance: b?.balance
590
- }))
591
- );
592
- }
593
-
594
- // Extract all CAIP identifiers from balances, filtering out invalid entries
595
- let allCaips = this.balances
596
- .filter(b => b && b.caip && typeof b.caip === 'string' && b.caip.trim().length > 0)
597
- .map((b) => b.caip);
598
-
599
- // Remove duplicates
600
- allCaips = [...new Set(allCaips)];
601
-
602
- // CRITICAL: Double-check all elements are valid strings after Set deduplication
603
- // Filter out any non-string or empty values that might have slipped through
604
- allCaips = allCaips.filter(caip =>
605
- caip &&
606
- typeof caip === 'string' &&
607
- caip.trim().length > 0 &&
608
- caip.includes(':') // CAIP format always has a colon
609
- );
610
-
611
- // Fetch market prices for all CAIPs
612
- console.log('GetMarketInfo: payload: ', allCaips);
613
- console.log('GetMarketInfo: payload type: ', typeof allCaips);
614
- console.log('GetMarketInfo: payload length: ', allCaips.length);
615
-
616
- // Additional validation log to catch issues
617
- const invalidEntries = allCaips.filter(caip => typeof caip !== 'string');
618
- if (invalidEntries.length > 0) {
619
- console.error(tag, 'CRITICAL: Invalid entries detected in allCaips:', invalidEntries);
620
- throw new Error('Invalid CAIP entries detected - aborting market sync');
621
- }
622
-
623
- if (allCaips && allCaips.length > 0) {
624
- try {
625
- let allPrices = await this.pioneer.GetMarketInfo(allCaips);
626
- console.log('GetMarketInfo: response: ', allPrices);
627
-
628
- // Create a map of CAIP to price for easier lookup
629
- const priceMap = {};
630
- if (allPrices && allPrices.data) {
631
- for (let i = 0; i < allCaips.length && i < allPrices.data.length; i++) {
632
- priceMap[allCaips[i]] = allPrices.data[i];
633
- }
634
- }
635
-
636
- // Update each balance with the corresponding price and value
637
- for (let balance of this.balances) {
638
- if (balance && balance.caip && priceMap[balance.caip] !== undefined) {
639
- balance.price = priceMap[balance.caip];
640
- balance.priceUsd = priceMap[balance.caip]; // Also set priceUsd for compatibility
641
- balance.valueUsd = balance.price * (balance.balance || 0);
642
- }
643
- }
644
- } catch (apiError) {
645
- console.error(tag, 'API error fetching market info:', apiError);
646
- // Don't throw - just log and continue without prices
647
- console.warn(tag, 'Continuing without market prices');
648
- }
649
- }
650
- return true;
651
- } catch (e) {
652
- console.error(tag, 'e:', e);
653
- throw e;
654
- }
577
+ return syncMarket(this.balances, this.pioneer);
655
578
  };
656
579
  this.sync = async function () {
657
580
  const tag = `${TAG} | sync | `;
@@ -0,0 +1,86 @@
1
+ // Market data synchronization utility
2
+ const TAG = ' | sync-market | ';
3
+
4
+ export async function syncMarket(balances: any[], pioneer: any): Promise<boolean> {
5
+ const tag = `${TAG} | syncMarket | `;
6
+ try {
7
+ // Log balances with invalid CAIPs for debugging
8
+ const invalidBalances = balances.filter(
9
+ (b) => !b || !b.caip || typeof b.caip !== 'string' || !b.caip.includes(':'),
10
+ );
11
+ if (invalidBalances.length > 0) {
12
+ console.warn(
13
+ tag,
14
+ `Found ${invalidBalances.length} balances with invalid CAIPs:`,
15
+ invalidBalances.map((b) => ({
16
+ caip: b?.caip,
17
+ type: typeof b?.caip,
18
+ symbol: b?.symbol,
19
+ balance: b?.balance,
20
+ })),
21
+ );
22
+ }
23
+
24
+ // Extract all CAIP identifiers from balances, filtering out invalid entries
25
+ let allCaips = balances
26
+ .filter((b) => b && b.caip && typeof b.caip === 'string' && b.caip.trim().length > 0)
27
+ .map((b) => b.caip);
28
+
29
+ // Remove duplicates
30
+ allCaips = [...new Set(allCaips)];
31
+
32
+ // CRITICAL: Double-check all elements are valid strings after Set deduplication
33
+ // Filter out any non-string or empty values that might have slipped through
34
+ allCaips = allCaips.filter(
35
+ (caip) =>
36
+ caip &&
37
+ typeof caip === 'string' &&
38
+ caip.trim().length > 0 &&
39
+ caip.includes(':'), // CAIP format always has a colon
40
+ );
41
+
42
+ // Fetch market prices for all CAIPs
43
+ console.log('GetMarketInfo: payload: ', allCaips);
44
+ console.log('GetMarketInfo: payload type: ', typeof allCaips);
45
+ console.log('GetMarketInfo: payload length: ', allCaips.length);
46
+
47
+ // Additional validation log to catch issues
48
+ const invalidEntries = allCaips.filter((caip) => typeof caip !== 'string');
49
+ if (invalidEntries.length > 0) {
50
+ console.error(tag, 'CRITICAL: Invalid entries detected in allCaips:', invalidEntries);
51
+ throw new Error('Invalid CAIP entries detected - aborting market sync');
52
+ }
53
+
54
+ if (allCaips && allCaips.length > 0) {
55
+ try {
56
+ let allPrices = await pioneer.GetMarketInfo(allCaips);
57
+ console.log('GetMarketInfo: response: ', allPrices);
58
+
59
+ // Create a map of CAIP to price for easier lookup
60
+ const priceMap: Record<string, number> = {};
61
+ if (allPrices && allPrices.data) {
62
+ for (let i = 0; i < allCaips.length && i < allPrices.data.length; i++) {
63
+ priceMap[allCaips[i]] = allPrices.data[i];
64
+ }
65
+ }
66
+
67
+ // Update each balance with the corresponding price and value
68
+ for (let balance of balances) {
69
+ if (balance && balance.caip && priceMap[balance.caip] !== undefined) {
70
+ balance.price = priceMap[balance.caip];
71
+ balance.priceUsd = priceMap[balance.caip]; // Also set priceUsd for compatibility
72
+ balance.valueUsd = balance.price * (balance.balance || 0);
73
+ }
74
+ }
75
+ } catch (apiError) {
76
+ console.error(tag, 'API error fetching market info:', apiError);
77
+ // Don't throw - just log and continue without prices
78
+ console.warn(tag, 'Continuing without market prices');
79
+ }
80
+ }
81
+ return true;
82
+ } catch (e) {
83
+ console.error(tag, 'e:', e);
84
+ throw e;
85
+ }
86
+ }
@@ -0,0 +1,100 @@
1
+ // Portfolio synchronization logic
2
+ import { getPaths } from '@pioneer-platform/pioneer-coins';
3
+
4
+ const TAG = ' | sync-portfolio | ';
5
+
6
+ export async function syncPortfolio(context: {
7
+ blockchains: string[];
8
+ paths: any[];
9
+ pubkeys: any[];
10
+ balances: any[];
11
+ pioneer: any;
12
+ getPubkeys: () => Promise<any[]>;
13
+ getBalances: () => Promise<any[]>;
14
+ events: any;
15
+ }) {
16
+ const tag = `${TAG} | syncPortfolio | `;
17
+ try {
18
+ // Helper to check network match with EVM wildcard support (works for both paths and pubkeys)
19
+ const matchesNetwork = (item: any, networkId: string) => {
20
+ if (!item.networks || !Array.isArray(item.networks)) return false;
21
+ if (item.networks.includes(networkId)) return true;
22
+ if (networkId.startsWith('eip155:') && item.networks.includes('eip155:*')) return true;
23
+ return false;
24
+ };
25
+
26
+ //at least 1 path per chain
27
+ await context.getPubkeys();
28
+ for (let i = 0; i < context.blockchains.length; i++) {
29
+ let networkId = context.blockchains[i];
30
+ if (networkId.indexOf('eip155:') >= 0) networkId = 'eip155:*';
31
+
32
+ let paths = context.paths.filter((path) => matchesNetwork(path, networkId));
33
+ if (paths.length === 0) {
34
+ //get paths for chain
35
+ const pathsForChain = getPaths([networkId]);
36
+ console.log(tag, 'pathsForChain: ', pathsForChain);
37
+ for (let j = 0; j < pathsForChain.length; j++) {
38
+ const path = pathsForChain[j];
39
+ if (!context.paths.find((p) => p.note === path.note)) {
40
+ context.paths.push(path);
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ //get balances for paths
47
+ await context.getPubkeys();
48
+ console.log(tag, 'context.pubkeys: ', context.pubkeys);
49
+ await context.getBalances();
50
+
51
+ // Get market data with cache support
52
+ let allCaips = context.balances
53
+ .filter((b) => b && b.caip && typeof b.caip === 'string' && b.caip.trim().length > 0)
54
+ .map((b) => b.caip);
55
+
56
+ // Remove duplicates
57
+ allCaips = [...new Set(allCaips)];
58
+
59
+ // CRITICAL: Double-check all elements are valid strings after Set deduplication
60
+ allCaips = allCaips.filter(
61
+ (caip) =>
62
+ caip &&
63
+ typeof caip === 'string' &&
64
+ caip.trim().length > 0 &&
65
+ caip.includes(':'), // CAIP format always has a colon
66
+ );
67
+
68
+ if (allCaips && allCaips.length > 0) {
69
+ try {
70
+ let allPrices = await context.pioneer.GetMarketInfo(allCaips);
71
+
72
+ // Create a map of CAIP to price for easier lookup
73
+ const priceMap = {};
74
+ if (allPrices && allPrices.data) {
75
+ for (let i = 0; i < allCaips.length && i < allPrices.data.length; i++) {
76
+ priceMap[allCaips[i]] = allPrices.data[i];
77
+ }
78
+ }
79
+
80
+ // Update each balance with the corresponding price and value
81
+ for (let balance of context.balances) {
82
+ if (balance && balance.caip && priceMap[balance.caip] !== undefined) {
83
+ balance.price = priceMap[balance.caip];
84
+ balance.priceUsd = priceMap[balance.caip];
85
+ balance.valueUsd = balance.price * (balance.balance || 0);
86
+ }
87
+ }
88
+ } catch (apiError) {
89
+ console.error(tag, 'API error fetching market info:', apiError);
90
+ console.warn(tag, 'Continuing without market prices');
91
+ }
92
+ }
93
+
94
+ context.events.emit('BALANCES_UPDATED', context.balances);
95
+ return true;
96
+ } catch (e) {
97
+ console.error(tag, 'e: ', e);
98
+ throw e;
99
+ }
100
+ }