From ff745d39a53ab7926230b2a67a7d800976871740 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Fri, 2 Feb 2024 17:13:49 +0530 Subject: [PATCH 1/2] feat: Added chainspace method based on working-score --- docker-compose.yml | 2 +- package.json | 2 +- schema.graphql | 20 ++++++- src/handlers/call.ts | 118 +++++++++++++++++++++---------------- src/handlers/chainSpace.ts | 76 ++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 55 deletions(-) create mode 100644 src/handlers/chainSpace.ts diff --git a/docker-compose.yml b/docker-compose.yml index 5c3c44b..9962b50 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,7 +47,7 @@ services: graphql-engine: image: onfinality/subql-query:latest ports: - - 3000:5001 + - 3000:3000 depends_on: "postgres": condition: service_healthy diff --git a/package.json b/package.json index 0dc32d1..034cbdc 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,6 @@ "chaintypes": "src/chaintypes.ts" }, "dependencies": { - "@cord.network/sdk": "^0.9.1" + "@cord.network/sdk": "^0.9.3-1develop1" } } diff --git a/schema.graphql b/schema.graphql index 2aeaa8a..ea420ab 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2,7 +2,6 @@ # Add the `@index` or `@index(unique: true)` annotation after any non-key field # https://academy.subquery.network/build/graphql.html#indexing-by-non-primary-key-field - type KVData @jsonField { key: String value: String @@ -46,8 +45,8 @@ type Call @entity { id: ID! section: String - method: String - args:[KVData] + method: String @index + args: [KVData] timestamp: Date isSuccess: Boolean @@ -92,3 +91,18 @@ type Rating @entity { entityType: String ratingType: String } + +type ChainSpace @entity { + id: ID! + + callIndex: String + method: String + blockNumber: BigInt + submitter: String + signature: String + chainspace_id: String + authorization: String + space_code: String + space_id: String + delegate: String +} diff --git a/src/handlers/call.ts b/src/handlers/call.ts index 5b5826f..4d13be8 100644 --- a/src/handlers/call.ts +++ b/src/handlers/call.ts @@ -1,14 +1,18 @@ -import type { Vec } from '@polkadot/types' -import { Call, Extrinsic } from '../types' -import { AnyCall, DispatchedCallData } from './types' +import type { Vec } from "@polkadot/types"; +import { Call, Extrinsic } from "../types"; +import { AnyCall, DispatchedCallData } from "./types"; import { SubstrateExtrinsic } from "@subql/types"; -import { Dispatcher, getBatchInterruptedIndex, getKVData } from './utils'; -import { createScore } from './score'; +import { Dispatcher, getBatchInterruptedIndex, getKVData } from "./utils"; +import { createScore } from "./score"; +import { createChainSpace } from "./chainSpace"; -async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): Promise { - const list: any[] = [] - const batchInterruptedIndex = getBatchInterruptedIndex(raw) +async function traverExtrinsic( + extrinsic: Extrinsic, + raw: SubstrateExtrinsic +): Promise { + const list: any[] = []; + const batchInterruptedIndex = getBatchInterruptedIndex(raw); const inner = async ( data: AnyCall, @@ -17,35 +21,36 @@ async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): P isRoot: boolean, depth: number ) => { - const id = isRoot ? parentCallId : `${parentCallId}-${idx}` - const method = data.method - const section = data.section - const args = data.args - - if (method === 'submitDidCall' && section === 'did') { - /* there will be one call below */ - const temp = (args[0] as any).call as unknown as AnyCall; - logger.info(`DidSubmitCall: ${temp}`); - await inner(temp, id, 1, false, depth + 1); + const id = isRoot ? parentCallId : `${parentCallId}-${idx}`; + const method = data.method; + const section = data.section; + const args = data.args; + + if (method === "submitDidCall" && section === "did") { + /* there will be one call below */ + const temp = (args[0] as any).call as unknown as AnyCall; + logger.info(`DidSubmitCall: ${temp}`); + await inner(temp, id, 1, false, depth + 1); } - const call = new Call(id) + const call = new Call(id); - call.method = method - call.section = section - call.args = getKVData(data.args, data.argsDef) - call.signerId = extrinsic.signerId - call.isSuccess = depth === 0 ? extrinsic.isSuccess : batchInterruptedIndex > idx; - call.timestamp = extrinsic.timestamp + call.method = method; + call.section = section; + call.args = getKVData(data.args, data.argsDef); + call.signerId = extrinsic.signerId; + call.isSuccess = + depth === 0 ? extrinsic.isSuccess : batchInterruptedIndex > idx; + call.timestamp = extrinsic.timestamp; if (!isRoot) { - call.parentCallId = isRoot ? '' : parentCallId + call.parentCallId = isRoot ? "" : parentCallId; - call.extrinsicId = parentCallId.split('-')[0] + call.extrinsicId = parentCallId.split("-")[0]; } else { - call.extrinsicId = parentCallId + call.extrinsicId = parentCallId; } - list.push(call) + list.push(call); /* await dispatcher.dispatch( @@ -53,38 +58,51 @@ async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): P { call, extrinsic, rawCall: data, rawExtrinsic: raw } ) */ - if (call.section === 'networkScore') { - logger.info("Scoring call"); - await createScore(raw, id as string); + if (call.section === "networkScore") { + logger.info("Scoring call"); + await createScore(raw, id as string); } - - if (depth < 1 && section === 'utility' && (method === 'batch' || method === 'batchAll')) { - const temp = args[0] as unknown as Vec - await Promise.all(temp.map((item, idx) => inner(item, id, idx, false, depth + 1))) - } - } + if (call.section === "chainSpace") { + logger.info("ChainSpace call"); + await createChainSpace(raw, call, id as string, data.method); + } - await inner(raw.extrinsic.method, extrinsic.id, 0, true, 0) + if ( + depth < 1 && + section === "utility" && + (method === "batch" || method === "batchAll") + ) { + const temp = args[0] as unknown as Vec; - return list -} + await Promise.all( + temp.map((item, idx) => inner(item, id, idx, false, depth + 1)) + ); + } + }; + + await inner(raw.extrinsic.method, extrinsic.id, 0, true, 0); -export async function createCalls (extrinsic: Extrinsic, raw: SubstrateExtrinsic) { + return list; +} - const calls = await traverExtrinsic(extrinsic, raw) - logger.info(`Calls: ${calls}`) - await Promise.all(calls.map(async (item) => item.save())); +export async function createCalls( + extrinsic: Extrinsic, + raw: SubstrateExtrinsic +) { + const calls = await traverExtrinsic(extrinsic, raw); + logger.info(`Calls: ${calls}`); + await Promise.all(calls.map(async (item) => item.save())); } -export async function ensureCallExist (id: any) { - let data = await Call.get(id) +export async function ensureCallExist(id: any) { + let data = await Call.get(id); if (!data) { - data = new Call(id) + data = new Call(id); - await data.save() + await data.save(); } - return data + return data; } diff --git a/src/handlers/chainSpace.ts b/src/handlers/chainSpace.ts new file mode 100644 index 0000000..027e0f8 --- /dev/null +++ b/src/handlers/chainSpace.ts @@ -0,0 +1,76 @@ +import { SubstrateExtrinsic } from "@subql/types"; +import { ChainSpace, Call } from "../types"; + +export async function createChainSpace( + extrinsic: SubstrateExtrinsic, + call: Call, + id: string, + method: string +): Promise { + const data = extrinsic.extrinsic.method; + + let arrayed = JSON.parse(JSON.stringify(data)); + let chainSpaceId = JSON.parse(JSON.stringify(extrinsic.events))[0].event + .data[0]; + let chainspace = new ChainSpace(id); + + chainspace.method = method; + chainspace.callIndex = arrayed.args.did_call.call.callIndex; + chainspace.chainspace_id = chainSpaceId; + chainspace.blockNumber = extrinsic.block.block.header.number.toBigInt(); + chainspace.signature = JSON.stringify(arrayed.args.signature); + + if (method === "create") { + chainspace.space_code = arrayed.args.did_call.call.args.space_code; + } + + if (method === "addDelegate") { + chainspace.space_id = arrayed.args.did_call.call.args.space_id; + chainspace.delegate = arrayed.args.did_call.call.args.delegate; + chainspace.authorization = arrayed.args.did_call.call.args.authorization; + chainspace.submitter = arrayed.args.did_call.submitter; + } + + if (method === "approve") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + + if (method === "archive") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + + if (method === "restore") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + + if (method === "addAdminDelegate") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + + if (method === "addAuditDelegate") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + + if (method === "removeDelegate") { + // ToDo: + // Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which + // calls this method. Execute the script to capture the extrinsic information, analyze the obtained data, + // and complete the implementation. + } + await chainspace.save(); +} From 1a3c575b4812725f8a9cf0d20feb4f747a205888 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Fri, 2 Feb 2024 17:14:41 +0530 Subject: [PATCH 2/2] chore: removed github actions --- .github/scripts/publish-deploy.sh | 15 -------------- .github/workflows/cli-deploy.yml | 34 ------------------------------- .github/workflows/pr.yml | 24 ---------------------- 3 files changed, 73 deletions(-) delete mode 100644 .github/scripts/publish-deploy.sh delete mode 100644 .github/workflows/cli-deploy.yml delete mode 100644 .github/workflows/pr.yml diff --git a/.github/scripts/publish-deploy.sh b/.github/scripts/publish-deploy.sh deleted file mode 100644 index 3c9dc04..0000000 --- a/.github/scripts/publish-deploy.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -while getopts p:o:e: flag -do - case "${flag}" in - e) ENDPOINT=${OPTARG};; - p) PROJECTNAME=${OPTARG};; - o) ORG=${OPTARG};; - *) echo "Usage: $0 [-p projectname] [-o org] [-e endpoint]" && exit 1;; - esac -done - -IPFSCID=$(npx subql publish -o -f .) - -npx subql deployment:deploy -d --ipfsCID="$IPFSCID" --projectName="${PROJECTNAME}" --org="${ORG%/*}" --endpoint="${ENDPOINT}" diff --git a/.github/workflows/cli-deploy.yml b/.github/workflows/cli-deploy.yml deleted file mode 100644 index 658d2c6..0000000 --- a/.github/workflows/cli-deploy.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: "CLI deploy" - -on: - workflow_dispatch: - inputs: - projectName: - description: "Project name" - required: true - type: string -jobs: - deploy: - name: CLI Deploy - runs-on: ubuntu-latest - environment: - name: DEPLOYMENT - env: - SUBQL_ACCESS_TOKEN: ${{ secrets.SUBQL_ACCESS_TOKEN }} - ENDPOINT: ${{ secrets.ENDPOINT }} - steps: - - uses: actions/checkout@v2 - - name: Setup Node.js environment - uses: actions/setup-node@v2 - with: - node-version: 16 - - run: yarn - - name: Codegen - run: yarn codegen - - name: Version - run: npx subql --version - - name: repo - run: echo ${{github.repository}} - - name: Publish and Deploy - run: | - sh .github/workflows/scripts/publish-deploy.sh -o ${{github.repository}} -p ${{github.event.inputs.projectName}} -e ${{secrets.ENDPOINT}} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml deleted file mode 100644 index 9f83021..0000000 --- a/.github/workflows/pr.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: PR -on: - pull_request: - paths-ignore: - - ".github/workflows/**" -jobs: - pr: - name: pr - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Setup Node.js environment - uses: actions/setup-node@v2 - with: - node-version: 20 - - run: yarn - - name: Codegen - run: yarn codegen - - name: Build - run: yarn build - - name: Install subql-node - run: yarn global add @subql/node - - name: Run tests with Subquery Node - run: subql-node test -f ${{ github.workspace }}