Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sacra-subgraph] Update sacra subgraph strategy #1620

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/strategies/sacra-subgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ Calculates users power by hero score from Sacra subgraph.
) {
score
}

itemEntities(
where:{
user: $address
dead: false
}
orderBy: score
orderDirection: desc
first: 1000
) {
score
}
```
60 changes: 37 additions & 23 deletions src/strategies/sacra-subgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const SUBGRAPH_URL = {
};

export const author = 'alexandersazonof';
export const version = '0.0.1';
export const version = '0.0.2';

export async function strategy(
_space,
Expand All @@ -20,52 +20,66 @@ export async function strategy(
// initialize scores
const scores = {};

// If graph doesn't exist return
if (!SUBGRAPH_URL[network]) {
return scores;
}

const params = {
const queryOne = {
heroEntities: {
__args: {
where: {
owner_in: addresses.map((address) => address.toLowerCase()),
id_gt: '',
dead: false
dead: false,
score_gt: 0
},
orderBy: 'id',
orderDirection: 'asc',
orderBy: 'score',
orderDirection: 'desc',
first: 1000
},
id: true,
score: true,
owner: {
id: true
}
},
itemEntities: {
__args: {
where: {
user_in: addresses.map((address) => address.toLowerCase()),
score_gt: 0
},
orderBy: 'score',
orderDirection: 'desc',
first: 1000
},
score: true,
user: {
id: true
}
}
};

if (snapshot !== 'latest') {
// @ts-ignore
params.heroEntities.__args.block = { number: snapshot };
queryOne.heroEntities.__args.block = { number: snapshot };
// @ts-ignore
queryOne.itemEntities.__args.block = { number: snapshot };
}

let hasNext = true;
while (hasNext) {
const result = await subgraphRequest(SUBGRAPH_URL[network], params);

const heroEntities =
result && result.heroEntities ? result.heroEntities : [];
const latest = heroEntities[heroEntities.length - 1];
const queryResultOne = await subgraphRequest(SUBGRAPH_URL[network], queryOne);

if (queryResultOne) {
const heroEntities = queryResultOne.heroEntities
? queryResultOne.heroEntities
: [];
for (const heroEntity of heroEntities) {
const userAddress = getAddress(heroEntity.owner.id);
const score = heroEntity.score;
scores[userAddress] = (scores[userAddress] ?? 0) + score;
}

hasNext = heroEntities.length === params.heroEntities.__args.first;
params.heroEntities.__args.where.id_gt = latest ? latest.id : '';
const itemEntities = queryResultOne.itemEntities
? queryResultOne.itemEntities
: [];
for (const itemEntity of itemEntities) {
const userAddress = getAddress(itemEntity.user.id);
const score = itemEntity.score;
scores[userAddress] = (scores[userAddress] ?? 0) + score;
}
}

return scores || {};
Expand Down
Loading