Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated library to use PAPI #11

Merged
merged 20 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]
- uses: actions/[email protected]
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 ./
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies for the postinstall script


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.
113 changes: 74 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,73 @@ 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);
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({
Bullrich marked this conversation as resolved.
Show resolved Hide resolved
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 +98,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");
Comment on lines +106 to 107
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't trust that this will always not be null, so I'll keep the check from before

continue;
Expand All @@ -76,27 +111,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();
}
};
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
Loading