Skip to content

Commit

Permalink
feat: allow getDelegatesBySpace to fetch delegates for all spaces (#1050
Browse files Browse the repository at this point in the history
)

* feat: allow getDelegatesBySpace to fetch delegates for all spaces

With this change space can be passed as null and this will result in all
delegates to be returned instead of per-space results only.

This will be used on new delegate-registry-api.

* v0.12.7

---------

Co-authored-by: Chaitanya <yourchaitu@gmail.com>
  • Loading branch information
Sekhmet and ChaituVR authored Aug 23, 2024
1 parent 57630e8 commit 08d5289
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.12.6",
"version": "0.12.7",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
11 changes: 7 additions & 4 deletions src/utils/delegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Delegation = {

export default async function getDelegatesBySpace(
network: string,
space: string,
space: string | null,
snapshot: string | number = 'latest',
options: any = {}
) {
Expand All @@ -26,7 +26,7 @@ export default async function getDelegatesBySpace(

let pivot = 0;
const result = new Map<string, Delegation>();
const spaceIn = buildSpaceIn(space);
const spaceIn = space ? buildSpaceIn(space) : null;

while (true) {
const newResults = await fetchData({
Expand Down Expand Up @@ -86,15 +86,14 @@ async function fetchData({
snapshot
}: {
url: string;
spaces: string[];
spaces: string[] | null;
pivot: number;
snapshot: string | number;
}): Promise<Delegation[]> {
const params: any = {
delegations: {
__args: {
where: {
space_in: spaces,
timestamp_gte: pivot
},
first: PAGE_SIZE,
Expand All @@ -113,5 +112,9 @@ async function fetchData({
params.delegations.__args.block = { number: snapshot };
}

if (spaces !== null) {
params.delegations.__args.where.space_in = spaces;
}

return (await subgraphRequest(url, params)).delegations || [];
}

0 comments on commit 08d5289

Please sign in to comment.