Skip to content

Commit 23b53c5

Browse files
authored
query angstrom history tvl (#15670)
1 parent 705cfc7 commit 23b53c5

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

projects/angstrom/index.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11

22
const subgraphEndpoint = 'https://api.goldsky.com/api/public/project_cm97dvfxxyivn01xe2sda93ka/subgraphs/angstrom-mainnet/1.3.3/gn'
3-
const subgraphQuery = {
4-
query: `
5-
{
6-
pools(first: 1000) {
7-
token0 {
8-
id
9-
decimals
10-
}
11-
token1 {
12-
id
13-
decimals
14-
}
15-
totalValueLockedToken0
16-
totalValueLockedToken1
17-
}
18-
}
3+
const subgraphQuery = (blockNumber) => {
4+
return {
5+
query: `
6+
{
7+
pools(first: 1000, block: {number: ${blockNumber}}) {
8+
token0 {
9+
id
10+
decimals
11+
}
12+
token1 {
13+
id
14+
decimals
15+
}
16+
totalValueLockedToken0
17+
totalValueLockedToken1
18+
}
19+
}
1920
20-
`
21+
`
22+
}
2123
};
2224

25+
async function getGraphBlock() {
26+
const query = {
27+
query: `
28+
{
29+
_meta {
30+
block {
31+
number
32+
}
33+
}
34+
}
35+
`
36+
}
37+
const response = await fetch(subgraphEndpoint, {
38+
"body": JSON.stringify(query),
39+
"method": "POST"
40+
})
41+
const data = await response.json()
42+
43+
return Number(data.data._meta.block.number)
44+
}
45+
2346
module.exports.ethereum = {
2447
tvl: async (api) => {
2548
const block = await api.getBlock(api.timestamp)
49+
const graphBlock = await getGraphBlock()
2650

2751
const response = await fetch(subgraphEndpoint, {
28-
"body": JSON.stringify(subgraphQuery),
52+
"body": JSON.stringify(subgraphQuery(block > graphBlock ? graphBlock : block)),
2953
"method": "POST"
3054
})
3155
const data = await response.json()

0 commit comments

Comments
 (0)