Skip to content

Commit

Permalink
feat: add controller to space filters (#857)
Browse files Browse the repository at this point in the history
* feat: add `controller` to space filters

* fix: add default stamp url
  • Loading branch information
wa0x6e authored Jun 4, 2024
1 parent c74890a commit 960c18a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ NETWORK=testnet
HUB_DATABASE_URL=mysql://...
SEQ_DATABASE_URL=mysql://
SEQUENCER_URL=https://testnet.seq.snapshot.org
STAMP_URL=https://stamp.fyi
KEYCARD_URL=https://keycard.snapshot.org
KEYCARD_SECRET=
SCORE_API_URL=https://score.snapshot.org
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"graphql-query-count-limit": "^1.0.0",
"lodash": "^4.17.21",
"mysql": "^2.18.1",
"node-fetch": "2",
"rate-limit-redis": "^3.1.0",
"redis": "^4.6.8",
"ts-node": "^10.9.1",
Expand All @@ -55,7 +56,6 @@
"eslint": "^8.28.0",
"jest": "^29.6.1",
"jest-environment-node-single-context": "^29.1.0",
"node-fetch": "^2.7",
"nodemon": "^2.0.19",
"prettier": "^3.0.3",
"start-server-and-test": "^2.0.0",
Expand Down
40 changes: 40 additions & 0 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import graphqlFields from 'graphql-fields';
import fetch from 'node-fetch';
import { jsonParse } from '../helpers/utils';
import { spacesMetadata } from '../helpers/spaces';
import db from '../helpers/mysql';
Expand Down Expand Up @@ -178,6 +179,13 @@ export async function fetchSpaces(args) {
const { first = 20, skip = 0, where = {} } = args;

const fields = { id: 'string', created: 'number' };

if (where.controller) {
where.id_in = await getControllerDomains(where.controller);

delete where.controller;
}

const whereQuery = buildWhereQuery(fields, 's', where);
let queryStr = whereQuery.query;
const params: any[] = whereQuery.params;
Expand Down Expand Up @@ -375,3 +383,35 @@ export function formatSubscription(subscription) {
});
return subscription;
}

async function getControllerDomains(address: string): Promise<string[]> {
type JsonRpcResponse = {
result: string[];
error?: {
code: number;
message: string;
data: any;
};
};

try {
const response = await fetch(process.env.STAMP_URL ?? 'https://stamp.fyi', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
method: 'lookup_domains',
params: address
})
});
const { result, error } = (await response.json()) as JsonRpcResponse;

if (error) throw new PublicError("Failed to resolve controller's domains");

return result;
} catch (e) {
throw new PublicError("Failed to resolve controller's domains");
}
}
1 change: 1 addition & 0 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ input SpaceWhere {
created_lte: Int
strategy: String
plugin: String
controller: String
}

input RankingWhere {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4413,7 +4413,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-fetch@^2.6.11, node-fetch@^2.7, node-fetch@^2.7.0:
node-fetch@2, node-fetch@^2.6.11, node-fetch@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down

0 comments on commit 960c18a

Please sign in to comment.