@osn/polkadot-api-container 1.0.1 → 1.0.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/package.json +8 -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.2",
|
|
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
|
+
"lodash.isnil": "^4.0.0",
|
|
12
|
+
"@polkadot/util": "9.5.1"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@polkadot/api": "*",
|
|
16
|
+
"@polkadot/util": "*"
|
|
11
17
|
}
|
|
12
18
|
}
|
|
@@ -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