This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
/
synthetix.mintr.snx-holders.cache.ts
66 lines (56 loc) · 1.93 KB
/
synthetix.mintr.snx-holders.cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Inject, Injectable } from '@nestjs/common';
import { gql } from 'graphql-request';
import moment from 'moment';
import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
import { gqlFetch } from '~app-toolkit/helpers/the-graph.helper';
import { Cache } from '~cache/cache.decorator';
import { Network } from '~types/network.interface';
type Holder = {
id: string;
collateral: string;
transferable: string;
initialDebtOwnership: string;
};
type HoldersResponse = {
snxholders: Holder[];
};
const HOLDERS_QUERY = gql`
query getHolders($lastId: String!) {
snxholders(
first: 1000
skip: $skip
orderBy: collateral
orderDirection: desc
where: { initialDebtOwnership_not: "0", id_gt: $lastId }
) {
id
collateral
transferable
initialDebtOwnership
}
}
`;
@Injectable()
export class SynthetixMintrSnxHoldersCache {
constructor(@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit) {}
private graphs = {
[Network.ETHEREUM_MAINNET]: 'https://api.thegraph.com/subgraphs/name/synthetixio-team/mainnet-main?source=zapper',
[Network.OPTIMISM_MAINNET]: 'https://api.thegraph.com/subgraphs/name/synthetixio-team/optimism-main?source=zapper',
};
@Cache({
key: (network: Network) => `studio:synthetix:${network}:all-snx-holders`,
ttl: moment.duration('15', 'minutes').asSeconds(),
})
async getSynthetixHolders(network: Network) {
const endpoint = this.graphs[network];
const holders = new Map<string, Holder>();
let lastResult: HoldersResponse;
let lastId = '';
do {
lastResult = await gqlFetch<HoldersResponse>({ endpoint, query: HOLDERS_QUERY, variables: { lastId } });
lastId = lastResult.snxholders[lastResult.snxholders.length - 1].id;
lastResult.snxholders.forEach(v => holders.set(v.id, v));
} while (lastResult.snxholders.length === 1000);
return Array.from(holders.values());
}
}