-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
backend/packages/backend/src/services/node.service/getSocietyMembers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { NODE_API_ENDPOINT } = require("../../env"); | ||
const { fetchApi } = require("../../utils/fech.api"); | ||
|
||
async function getSocietyMembers(network, height) { | ||
const url = `${NODE_API_ENDPOINT}/${network}/society/members/height/${height}`; | ||
return await fetchApi(url); | ||
} | ||
|
||
module.exports = { | ||
getSocietyMembers, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
backend/packages/node-api/src/features/society/getSocietyMembers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { getApis, getBlockApi } = require("@osn/polkadot-api-container"); | ||
const { chains } = require("../../constants"); | ||
|
||
async function getSocietyMembersFromOneApi(api, blockHashOrHeight) { | ||
let blockApi = await getBlockApi(api, blockHashOrHeight); | ||
const societyMembers = await blockApi.query.society.members.entries(); | ||
|
||
return societyMembers.map(([key, value]) => { | ||
const { | ||
args: [id], | ||
} = key; | ||
const address = id.toJSON(); | ||
const data = value.toJSON(); | ||
return { address, ...data }; | ||
}); | ||
} | ||
|
||
async function getSocietyMembersFromApis(apis, blockHashOrHeight) { | ||
const promises = []; | ||
for (const api of apis) { | ||
promises.push(getSocietyMembersFromOneApi(api, blockHashOrHeight)); | ||
} | ||
|
||
return await Promise.any(promises); | ||
} | ||
|
||
async function getSocietyMembers(ctx) { | ||
const { chain } = ctx.params; | ||
const { block: blockHashOrHeight } = ctx.query; | ||
if (![chains.kusama].includes(chain)) { | ||
ctx.throw(400, `Not support chain ${chain}`); | ||
} | ||
|
||
const apis = getApis(chain); | ||
ctx.body = await getSocietyMembersFromApis(apis, blockHashOrHeight); | ||
} | ||
|
||
module.exports = { | ||
getSocietyMembers, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const Router = require("koa-router"); | ||
const { getSocietyMembers } = require("./getSocietyMembers"); | ||
|
||
const router = new Router(); | ||
router.get("/society/members/height/:blockHashOrHeight?", getSocietyMembers); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters