Skip to content

Commit

Permalink
updated library to use PAPI (#11)
Browse files Browse the repository at this point in the history
Migrated from `@polkadot/api` to `@polkadot-api/api` after watching
@josepot's video.

All the code entry points are still the same. So are the tests.

This resolves #9

Thanks you @josepot and @kratico for your help implementing PAPI and smoldot.

This commit also has a dirty fix to the issue reported in polkadot-api/polkadot-api#327
  • Loading branch information
Bullrich committed Mar 15, 2024
1 parent fe7e0a7 commit 4aaf650
Show file tree
Hide file tree
Showing 12 changed files with 773 additions and 491 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
.git

src/codegen
1 change: 1 addition & 0 deletions .github/workflows/javascript-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
command: [lint, build, test]
runs-on: ubuntu-latest
name: running ${{ matrix.command }}
timeout-minutes: 5
steps:
- uses: actions/checkout@v4.1.1
- uses: actions/setup-node@v4.0.2
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
!.vscode/extensions.json
!.vscode/settings.json
.idea

# Papi
src/codegen
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ WORKDIR /action

COPY package.json yarn.lock ./

COPY collectives.scale relay.scale polkadot-api.json ./

RUN yarn install --frozen-lockfile

COPY . .
Expand Down
Binary file added collectives.scale
Binary file not shown.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"start": "node dist",
"build": "ncc build --license LICENSE",
"test": "jest",
"postinstall": "papi",
"test": "jest --forceExit",
"fix": "eslint --fix 'src/**/*'",
"lint": "eslint 'src/**/*'"
},
Expand All @@ -24,7 +25,12 @@
"@actions/core": "^1.10.1",
"@actions/github": "^5.1.1",
"@octokit/webhooks-types": "^7.3.1",
"@polkadot/api": "^10.9.1"
"@polkadot-api/cli": "^0.0.1-a88cc12e054d60b3c599baf69bf41b99204e053d.1.0",
"@polkadot-api/client": "^0.0.1-a88cc12e054d60b3c599baf69bf41b99204e053d.1.0",
"@polkadot-api/node-polkadot-provider": "^0.0.1-a88cc12e054d60b3c599baf69bf41b99204e053d.1.0",
"@polkadot-api/sm-provider": "^0.0.1-2ade456e6ac9a1106b115cb8f44eed4f897e6017.1.0",
"@substrate/connect-known-chains": "^1.1.2",
"smoldot": "^2.0.22"
},
"devDependencies": {
"@eng-automation/js-style": "^2.3.0",
Expand Down
12 changes: 12 additions & 0 deletions polkadot-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"collectives": {
"outputFolder": "src/codegen",
"wsUrl": "wss://polkadot-collectives-rpc.polkadot.io",
"metadata": "collectives.scale"
},
"relay": {
"outputFolder": "src/codegen",
"wsUrl": "wss://rpc.polkadot.io",
"metadata": "relay.scale"
}
}
Binary file added relay.scale
Binary file not shown.
115 changes: 76 additions & 39 deletions src/fellows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { ApiPromise, WsProvider } from "@polkadot/api";
import { createClient } from "@polkadot-api/client";
import { getChain } from "@polkadot-api/node-polkadot-provider";
import { getSmProvider } from "@polkadot-api/sm-provider";
import {
polkadot,
polkadot_collectives,
} from "@substrate/connect-known-chains";
import { start } from "smoldot";

import collectiveDescriptor from "./codegen/collectives";
import relayDescriptor from "./codegen/relay";
import { ActionLogger } from "./github/types";

type FellowData = { address: string; rank: number };
Expand All @@ -14,44 +22,75 @@ export type FellowObject = {
export const fetchAllFellows = async (
logger: ActionLogger,
): Promise<FellowObject[]> => {
let api: ApiPromise;
logger.debug("Connecting to collective parachain");
// we connect to the collective rpc node
const wsProvider = new WsProvider(
"wss://polkadot-collectives-rpc.polkadot.io",
);
api = await ApiPromise.create({ provider: wsProvider });
logger.info("Initializing smoldot");
const smoldot = start();
const SmProvider = getSmProvider(smoldot);

// TODO: Replace once https://github.com/paritytech/opstooling/discussions/373 is fixed
let polkadotClient: ReturnType<typeof createClient> | null = null;

try {
// We fetch all the members
const membersObj = await api.query.fellowshipCollective.members.entries();
const relayChain = await smoldot.addChain({
chainSpec: polkadot,
});
logger.debug("Connecting to collective parachain");
await smoldot.addChain({
chainSpec: polkadot_collectives,
potentialRelayChains: [relayChain],
});

logger.info("Initializing PAPI");
polkadotClient = createClient(
getChain({
provider: SmProvider({
potentialRelayChains: [relayChain],
chainSpec: polkadot_collectives,
}),
keyring: [],
}),
);

// We fetch all the members from the collective
const collectivesApi = polkadotClient.getTypedApi(collectiveDescriptor);
const memberEntries =
await collectivesApi.query.FellowshipCollective.Members.getEntries();

// We iterate over the fellow data and convert them into usable values
const fellows: FellowData[] = [];
for (const [key, rank] of membersObj) {
// @ts-ignore
const [address]: [string] = key.toHuman();
fellows.push({ address, ...(rank.toHuman() as object) } as FellowData);
for (const member of memberEntries) {
const [address] = member.keyArgs;
fellows.push({ address, rank: member.value });
}
logger.debug(JSON.stringify(fellows));

// Once we obtained this information, we disconnect this api.
await api.disconnect();
polkadotClient.destroy();

logger.debug("Connecting to relay parachain.");
// We connect to the relay chain
api = await ApiPromise.create({
provider: new WsProvider("wss://rpc.polkadot.io"),
});

// We iterate over the different members and extract their data
// We move into the relay chain
polkadotClient = createClient(
getChain({
provider: SmProvider({
potentialRelayChains: [relayChain],
chainSpec: polkadot,
}),
keyring: [],
}),
);
const relayApi = polkadotClient.getTypedApi(relayDescriptor);

const users: FellowObject[] = [];

// We iterate over the different members and extract their data
for (const fellow of fellows) {
logger.debug(
`Fetching identity of '${fellow.address}', rank: ${fellow.rank}`,
);
const fellowData = (
await api.query.identity.identityOf(fellow.address)
).toHuman() as Record<string, unknown> | undefined;

const fellowData = await relayApi.query.Identity.IdentityOf.getValue(
fellow.address,
);

let data: FellowObject = { address: fellow.address, rank: fellow.rank };

Expand All @@ -61,12 +100,10 @@ export const fetchAllFellows = async (
continue;
}

// @ts-ignore
const additional = fellowData.info.additional as
| [{ Raw: string }, { Raw: string }][]
| undefined;
const additional = fellowData.info.additional;

// If it does not have additional data (GitHub handle goes here) we ignore it
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!additional || additional.length < 1) {
logger.debug("Additional data is null. Skipping");
continue;
Expand All @@ -76,27 +113,27 @@ export const fetchAllFellows = async (
const [key, value] = additionalData;
// We verify that they have an additional data of the key "github"
// If it has a handle defined, we push it into the array
if (
key?.Raw &&
key?.Raw === "github" &&
value?.Raw &&
value?.Raw.length > 0
) {
logger.debug(`Found handles: '${value.Raw}'`);
const fieldName = key.value?.asText();
logger.debug(`Analyzing: ${fieldName ?? "unknown field"}`);
const fieldValue = value.value?.asText();
if (fieldName === "github" && fieldValue && fieldValue.length > 0) {
logger.debug(`Found handles: '${fieldValue}`);
// We add it to the array and remove the @ if they add it to the handle
data = { ...data, githubHandle: value.Raw.replace("@", "") };
data = { ...data, githubHandle: fieldValue.replace("@", "") };
}
users.push(data);
}
users.push(data);
}

logger.info(`Found users: ${JSON.stringify(Array.from(users.entries()))}`);

return users;
} catch (error) {
logger.error(error as Error);
throw error;
} finally {
await api.disconnect();
if (polkadotClient) {
polkadotClient.destroy();
}
await smoldot.terminate();
}
};
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ const mapFellows = (fellows: FellowObject[]) => {
setOutput("github-handles", githubHandles);
};

fetchAllFellows(logger).then(mapFellows).catch(setFailed);
fetchAllFellows(logger)
.then(mapFellows)
.catch(setFailed)
.finally(() => {
logger.info("Shutting down application");
// TODO: Remove this once https://github.com/polkadot-api/polkadot-api/issues/327 is fixed
process.exit(0);
});
2 changes: 1 addition & 1 deletion src/test/fellows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ describe("Fellows test", () => {
test("Should fetch fellows", async () => {
const members = await fetchAllFellows(logger);
expect(members.length).toBeGreaterThan(0);
});
}, 60_000);
});
Loading

0 comments on commit 4aaf650

Please sign in to comment.