@osn/polkadot-api-container 1.0.0 → 1.0.3
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 +8 -2
- package/src/apis/index.js +20 -2
- package/src/apis/query/block.js +45 -0
- package/src/index.js +2 -1
package/package.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@osn/polkadot-api-container",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"repository": "git@github.com:opensquare-network/polkadot-api-container.git",
|
|
6
6
|
"author": "OpenSquare <https://www.opensquare.network/>",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@osn/provider-options": "^1.0.7",
|
|
10
|
-
"@polkadot/api": "8.9.1"
|
|
10
|
+
"@polkadot/api": "^8.9.1",
|
|
11
|
+
"@polkadot/util": "^9.6.1",
|
|
12
|
+
"lodash.isnil": "^4.0.0"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@polkadot/api": "*",
|
|
16
|
+
"@polkadot/util": "*"
|
|
11
17
|
}
|
|
12
18
|
}
|
package/src/apis/index.js
CHANGED
|
@@ -97,15 +97,32 @@ async function createApiInLimitTime(network, endpoint, logger = console) {
|
|
|
97
97
|
]);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
async function createApiForChain(chain, endpoints, logger = console) {
|
|
101
|
+
for (const endpoint of endpoints) {
|
|
102
|
+
if (!endpoint) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await createApiInLimitTime(chain, endpoint);
|
|
108
|
+
console.log(`${ chain }: ${ endpoint } created!`);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
logger.info(
|
|
111
|
+
`Can not connected to ${ endpoint } in ${ nodeTimeoutSeconds } seconds, just disconnect it`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
100
117
|
function getApis(chain) {
|
|
101
118
|
return (chainApis[chain] || []).map(({ api }) => api);
|
|
102
119
|
}
|
|
103
120
|
|
|
104
121
|
function logApiStatus(logger = console) {
|
|
105
122
|
Object.entries(chainApis).map(([chain, apis]) => {
|
|
106
|
-
logger.info(`chain: ${chain}`);
|
|
123
|
+
logger.info(`chain: ${ chain }`);
|
|
107
124
|
for (const { endpoint, api } of apis) {
|
|
108
|
-
logger.info(`\t ${endpoint} connected: ${api.isConnected}`);
|
|
125
|
+
logger.info(`\t ${ endpoint } connected: ${ api.isConnected }`);
|
|
109
126
|
}
|
|
110
127
|
});
|
|
111
128
|
}
|
|
@@ -113,6 +130,7 @@ function logApiStatus(logger = console) {
|
|
|
113
130
|
module.exports = {
|
|
114
131
|
createApi,
|
|
115
132
|
createApiInLimitTime,
|
|
133
|
+
createApiForChain,
|
|
116
134
|
getApis,
|
|
117
135
|
logApiStatus,
|
|
118
136
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const { isHex } = require("@polkadot/util")
|
|
2
|
+
const isNil = require("lodash.isnil");
|
|
3
|
+
|
|
4
|
+
async function getBlockApi(api, blockHashOrHeight) {
|
|
5
|
+
if (!blockHashOrHeight) {
|
|
6
|
+
return api;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (isHex(blockHashOrHeight)) {
|
|
10
|
+
return await api.at(blockHashOrHeight);
|
|
11
|
+
} else if (/^\d+$/.test(blockHashOrHeight)) {
|
|
12
|
+
const hash = await api.rpc.chain.getBlockHash(blockHashOrHeight);
|
|
13
|
+
return await api.at(hash);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
throw 'Invalid block hash or height'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function getBlockHashFromApi(api, blockHeight) {
|
|
20
|
+
if (isNil(blockHeight)) {
|
|
21
|
+
const hash = await api.rpc.chain.getBlockHash();
|
|
22
|
+
return hash.toString();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const hash = await api.rpc.chain.getBlockHash(blockHeight);
|
|
26
|
+
return hash.toString();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function getBlockHash(apis, blockHashOrHeight) {
|
|
30
|
+
if (isHex(blockHashOrHeight)) {
|
|
31
|
+
return blockHashOrHeight;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const promises = [];
|
|
35
|
+
for (const api of apis) {
|
|
36
|
+
promises.push(getBlockHashFromApi(api, blockHashOrHeight));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Promise.any(promises);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
getBlockApi,
|
|
44
|
+
getBlockHash,
|
|
45
|
+
}
|
package/src/index.js
CHANGED