@pioneer-platform/pioneer-sdk 4.20.5 → 4.20.6
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/package.json +5 -5
- package/src/utils/sync-portfolio.ts +100 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "highlander",
|
|
3
3
|
"name": "@pioneer-platform/pioneer-sdk",
|
|
4
|
-
"version": "4.20.
|
|
4
|
+
"version": "4.20.6",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@keepkey/keepkey-sdk": "^0.2.62",
|
|
7
7
|
"@pioneer-platform/loggerdog": "^8.11.0",
|
|
@@ -48,6 +48,8 @@
|
|
|
48
48
|
},
|
|
49
49
|
"react-native": "./src/index.ts",
|
|
50
50
|
"repository": "https://github.com/thorswap/SwapKit.git",
|
|
51
|
+
"type": "module",
|
|
52
|
+
"types": "./dist/index.d.ts",
|
|
51
53
|
"scripts": {
|
|
52
54
|
"build": "bash scripts/build.sh",
|
|
53
55
|
"build:watch": "nodemon --watch src --exec 'bun run build'",
|
|
@@ -55,7 +57,5 @@
|
|
|
55
57
|
"lint": "eslint ./ --ext .ts,.tsx --fix; tsc --noEmit",
|
|
56
58
|
"test": "echo 'vitest --run'",
|
|
57
59
|
"test:coverage": "echo 'vitest run --coverage'"
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
"types": "./dist/index.d.ts"
|
|
61
|
-
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -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
|
+
}
|