diff --git a/src/strategies/sacra-subgraph/README.md b/src/strategies/sacra-subgraph/README.md index dd63aaf38..52c35ced5 100644 --- a/src/strategies/sacra-subgraph/README.md +++ b/src/strategies/sacra-subgraph/README.md @@ -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 + } ``` \ No newline at end of file diff --git a/src/strategies/sacra-subgraph/index.ts b/src/strategies/sacra-subgraph/index.ts index b9d4d4d84..cbc2e4435 100644 --- a/src/strategies/sacra-subgraph/index.ts +++ b/src/strategies/sacra-subgraph/index.ts @@ -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, @@ -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 || {};