From 90a83e64f09759739dd63db759b450b585df412e Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Wed, 18 Oct 2023 19:46:57 +0900 Subject: [PATCH 1/2] fix: get `network` data from DB directly --- src/graphql/operations/networks.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/graphql/operations/networks.ts b/src/graphql/operations/networks.ts index 872f5a6f..ef9bec97 100644 --- a/src/graphql/operations/networks.ts +++ b/src/graphql/operations/networks.ts @@ -1,12 +1,20 @@ -import { spaces } from '../../helpers/spaces'; +import { capture } from '@snapshot-labs/snapshot-sentry'; +import db from '../../helpers/mysql'; -export default function () { - const networks = {}; - Object.values(spaces).forEach((space: any) => { - networks[space.network] = networks[space.network] ? networks[space.network] + 1 : 1; - }); - return Object.entries(networks).map(network => ({ - id: network[0], - spacesCount: network[1] - })); +export default async function () { + const query = ` + SELECT + JSON_UNQUOTE(JSON_EXTRACT(settings, '$.network')) as network, + COUNT(name) AS spacesCount + FROM spaces + GROUP BY network + ORDER BY spacesCount DESC; + `; + + try { + return (await db.queryAsync(query)).map(v => ({ ...v, id: v.network })); + } catch (e: any) { + capture(e); + return Promise.reject(new Error('request failed')); + } } From 0ca5df02402fd916ec2e0a3688aa4ce67539cad5 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Thu, 19 Oct 2023 11:50:40 +0900 Subject: [PATCH 2/2] feat: return fields from networks.json in network operation --- src/graphql/operations/networks.ts | 16 +++++++++++++--- src/graphql/schema.gql | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/graphql/operations/networks.ts b/src/graphql/operations/networks.ts index ef9bec97..5655f6f3 100644 --- a/src/graphql/operations/networks.ts +++ b/src/graphql/operations/networks.ts @@ -1,18 +1,28 @@ import { capture } from '@snapshot-labs/snapshot-sentry'; +import networks from '@snapshot-labs/snapshot.js/src/networks.json'; import db from '../../helpers/mysql'; export default async function () { const query = ` SELECT JSON_UNQUOTE(JSON_EXTRACT(settings, '$.network')) as network, - COUNT(name) AS spacesCount + COUNT(name) AS count FROM spaces GROUP BY network - ORDER BY spacesCount DESC; + ORDER BY count DESC; `; try { - return (await db.queryAsync(query)).map(v => ({ ...v, id: v.network })); + const results = new Map(await db.queryAsync(query).map(r => [r.network, r.count])); + + return Object.values(networks) + .map((network: any) => { + network.id = network.key; + network.spacesCount = results.get(network.id) || 0; + network.testnet ||= false; + return network; + }) + .sort((a, b) => b.spacesCount - a.spacesCount); } catch (e: any) { capture(e); return Promise.reject(new Error('request failed')); diff --git a/src/graphql/schema.gql b/src/graphql/schema.gql index 73ee28c1..003d0a93 100644 --- a/src/graphql/schema.gql +++ b/src/graphql/schema.gql @@ -89,7 +89,7 @@ type Query { skins: [Item] - networks: [Item] + networks: [Network] validations: [Item] @@ -555,3 +555,20 @@ type Vp { vp_by_strategy: [Float] vp_state: String } + +type Network { + key: String + id: String + spacesCount: Int + name: String + chainId: String + network: String + explorer: NetworkExplorer + logo: String + testnet: Boolean +} + +type NetworkExplorer { + url: String + apiUrl: String +}