-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Chaitanya <[email protected]>
- Loading branch information
1 parent
e7939f1
commit bcaa0bd
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Sacra score power from Subgraph | ||
|
||
Calculates users power by hero score from Sacra subgraph. | ||
|
||
``` | ||
heroEntities( | ||
where:{ | ||
owner: $address | ||
dead: false | ||
} | ||
orderBy: score | ||
orderDirection: desc | ||
first: 1000 | ||
) { | ||
score | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[ | ||
{ | ||
"name": "Example query", | ||
"strategy": { | ||
"name": "sacra-subgraph", | ||
"params": {} | ||
}, | ||
"network": "250", | ||
"addresses": [ | ||
"0x0009d6f1ee24dce26b3d59f7687acf9ef186908d", | ||
"0x006028a11d9fff45112b4983f3dad6313784eee3", | ||
"0x00662c5a6d56616b22f6180b325501b796825571" | ||
], | ||
"snapshot": 95377862 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { getAddress } from '@ethersproject/address'; | ||
import { subgraphRequest } from '../../utils'; | ||
|
||
const SUBGRAPH_URL = { | ||
'250': 'https://graph.tetu.io/subgraphs/name/sacra-fantom', | ||
'111188': 'https://graph.tetu.io/subgraphs/name/sacra-real' | ||
}; | ||
|
||
export const author = 'alexandersazonof'; | ||
export const version = '0.0.1'; | ||
|
||
export async function strategy( | ||
_space, | ||
network, | ||
_provider, | ||
addresses, | ||
options, | ||
snapshot | ||
) { | ||
// initialize scores | ||
const scores = {}; | ||
|
||
// If graph doesn't exist return | ||
if (!SUBGRAPH_URL[network]) { | ||
return scores; | ||
} | ||
|
||
const params = { | ||
heroEntities: { | ||
__args: { | ||
where: { | ||
owner_in: addresses.map((address) => address.toLowerCase()), | ||
id_gt: '', | ||
dead: false | ||
}, | ||
orderBy: 'id', | ||
orderDirection: 'asc', | ||
first: 1000 | ||
}, | ||
id: true, | ||
score: true, | ||
owner: { | ||
id: true | ||
} | ||
} | ||
}; | ||
|
||
if (snapshot !== 'latest') { | ||
// @ts-ignore | ||
params.heroEntities.__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]; | ||
|
||
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 : ''; | ||
} | ||
|
||
return scores || {}; | ||
} |