File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ const headers = {
2
+ origin : "https://subgraph.smardex.io" ,
3
+ referer : "https://subgraph.smardex.io" ,
4
+ "x-api-key" : process . env . SMARDEX_SUBGRAPH_API_KEY ,
5
+ } ;
6
+
7
+ const subgraphUrl = "https://subgraph.smardex.io/ethereum/spro" ;
8
+
9
+ const getTokenMetrics = async ( ) => {
10
+ const tokenMetricsQuery = `{
11
+ tokenMetrics_collection {
12
+ id
13
+ totalCollateralAmount
14
+ totalBorrowedAmount
15
+ }
16
+ }` ;
17
+
18
+ const result = await fetch ( subgraphUrl , {
19
+ method : "POST" ,
20
+ headers,
21
+ body : JSON . stringify ( {
22
+ query : tokenMetricsQuery ,
23
+ } ) ,
24
+ } ) . then ( ( res ) => res . json ( ) ) ;
25
+ return result ?. data ?. tokenMetrics_collection || [ ] ;
26
+ } ;
27
+
28
+ async function getData ( isBorrowed = false ) {
29
+ const tokenMetrics = await getTokenMetrics ( ) ;
30
+
31
+ return tokenMetrics . reduce ( ( acc , token ) => {
32
+ const totalBorrowedAmount = parseFloat ( token . totalBorrowedAmount ) ;
33
+
34
+ return {
35
+ ...acc ,
36
+ [ token . id ] :
37
+ // We only need to add the total collateral amount if it's not borrowed
38
+ totalBorrowedAmount + parseFloat ( isBorrowed ? 0 : token . totalCollateralAmount ) ,
39
+ } ;
40
+ } , { } ) ;
41
+ }
42
+
43
+ module . exports = {
44
+ ethereum : {
45
+ tvl : ( ) => getData ( ) ,
46
+ borrowed : ( ) => getData ( true ) ,
47
+ } ,
48
+ } ;
49
+ // node test.js projects/p2p-lending/index.js
You can’t perform that action at this time.
0 commit comments