Skip to content

Commit

Permalink
Add sacra subgraph strategy (#1617)
Browse files Browse the repository at this point in the history
Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
alexandersazonof and ChaituVR authored Oct 23, 2024
1 parent e7939f1 commit bcaa0bd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ import * as snxMultichain from './snx-multichain';
import * as moxie from './moxie';
import * as stakingAmountDurationLinear from './staking-amount-duration-linear';
import * as stakingAmountDurationExponential from './staking-amount-duration-exponential';
import * as sacraSubgraph from './sacra-subgraph';

const strategies = {
'delegatexyz-erc721-balance-of': delegatexyzErc721BalanceOf,
Expand Down Expand Up @@ -932,7 +933,8 @@ const strategies = {
'snx-multichain': snxMultichain,
moxie: moxie,
'staking-amount-duration-linear': stakingAmountDurationLinear,
'staking-amount-duration-exponential': stakingAmountDurationExponential
'staking-amount-duration-exponential': stakingAmountDurationExponential,
'sacra-subgraph': sacraSubgraph
};

Object.keys(strategies).forEach(function (strategyName) {
Expand Down
17 changes: 17 additions & 0 deletions src/strategies/sacra-subgraph/README.md
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
}
```
16 changes: 16 additions & 0 deletions src/strategies/sacra-subgraph/examples.json
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
}
]
72 changes: 72 additions & 0 deletions src/strategies/sacra-subgraph/index.ts
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 || {};
}

0 comments on commit bcaa0bd

Please sign in to comment.