From ee2ffbf2e15abe36b56a473ae470df50b751bf3e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 10 Feb 2024 22:56:05 +0100 Subject: [PATCH] Fix getMultisig implementation --- db-schema.graphql | 7 ++-- lib/graphqlHelpers.ts | 33 +++++++++++++++---- .../multisig/[multisigAddress]/index.ts | 8 ++--- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/db-schema.graphql b/db-schema.graphql index c73ed2e..a376c75 100644 --- a/db-schema.graphql +++ b/db-schema.graphql @@ -1,7 +1,10 @@ type Multisig { id: ID! - chainId: String! - address: String! + # The @search annotation allows us to query the list of multisigs and filter + # by (chainId, address) pairs. We use "hash" since we only need exact matches. + # See https://dgraph.io/docs/graphql/schema/directives/search/#string + chainId: String! @search(by: [hash]) + address: String! @search(by: [hash]) pubkeyJSON: String! } diff --git a/lib/graphqlHelpers.ts b/lib/graphqlHelpers.ts index 2218cd6..836f153 100644 --- a/lib/graphqlHelpers.ts +++ b/lib/graphqlHelpers.ts @@ -32,27 +32,46 @@ const createMultisig = async (multisig: DbAccount) => { }; /** - * Gets multisig pubkey from faundb + * This is the format returned by the graphQL API. + * + * Keep the format in sync with `GetMultisigAccountResponse` because + * we return the full object in the API. Right now address and chainId + * are somewhat unnecessary to query but still nice for debgging. + */ +interface MultisigFromQuery { + address: string; + chainId: string; + pubkeyJSON: string; +} + +/** + * Gets multisig pubkey from DB * * @param {string} address A multisig address. * @param {string} chainId The chainId the multisig belongs to. * @return Returns async function that makes a request to the dgraph graphql endpoint */ -const getMultisig = async (address: string, chainId: string) => { - return requestGraphQlJson({ +async function getMultisig( + address: string, + chainId: string, +): Promise { + const result = await requestGraphQlJson({ body: { query: ` - query GetMultisig { - getMultisig(chainId: "${chainId}", address: "${address}") { - chainId + query MultisigsByAddressAndChainId { + queryMultisig(filter: {address: {eq: "${address}"}, chainId: {eq: "${chainId}"}}) { address + chainId pubkeyJSON } } `, }, }); -}; + const elements: [MultisigFromQuery] = result.data.queryMultisig; + const first = elements.find(() => true); + return first; +} /** * Creates transaction record in dgraph diff --git a/pages/api/chain/[chainId]/multisig/[multisigAddress]/index.ts b/pages/api/chain/[chainId]/multisig/[multisigAddress]/index.ts index fb1bb76..0c782b7 100644 --- a/pages/api/chain/[chainId]/multisig/[multisigAddress]/index.ts +++ b/pages/api/chain/[chainId]/multisig/[multisigAddress]/index.ts @@ -8,13 +8,13 @@ export default async function multisigAddressApi(req: NextApiRequest, res: NextA const multisigAddress = req.query.multisigAddress?.toString() || ""; const chainId = req.query.chainId?.toString() || ""; console.log("Function `getMultisig` invoked", multisigAddress, chainId); - const getRes = await getMultisig(multisigAddress, chainId); - if (!getRes.data.getMultisig) { + const multisig = await getMultisig(multisigAddress, chainId); + if (!multisig) { res.status(404).send("Multisig not found"); return; } - console.log("success", getRes.data.getMultisig); - res.status(200).send(getRes.data.getMultisig); + console.log("success", multisig); + res.status(200).send(multisig); return; // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (err: any) {