1
- const { post } = require ( '../helper/http' ) ;
2
- const sdk = require ( '@defillama/sdk' ) ;
1
+ const { post } = require ( '../helper/http' )
2
+ const sdk = require ( '@defillama/sdk' )
3
3
4
4
const tokenMapping = {
5
5
'xtokens:XBTC' : 'bitcoin' ,
@@ -8,20 +8,20 @@ const tokenMapping = {
8
8
'xtokens:XXRP' : 'ripple' ,
9
9
'eosio.token:XPR' : 'proton' ,
10
10
'xtokens:XMT' : 'metal' ,
11
- 'xtokens:XUST' : 'terrausd-wormhole' , // optional/legacy
12
- 'xtokens:XLUNA' : 'terra-luna-2' , // optional/legacy
13
11
'xtokens:XUSDC' : 'usd-coin' ,
14
12
'xtokens:XDOGE' : 'dogecoin' ,
15
13
'xtokens:XUSDT' : 'tether' ,
16
- } ;
14
+ 'xtokens:XUST' : 'terrausd-wormhole' ,
15
+ 'xtokens:XLUNA' : 'terra-luna-2' ,
16
+ }
17
17
18
- const API_ENDPOINT = 'https://proton.eosusa.io' ;
19
- const LENDING_CONTRACT = 'lending.loan' ;
18
+ const API_ENDPOINT = 'https://proton.eosusa.io'
19
+ const LENDING_CONTRACT = 'lending.loan'
20
20
21
21
function parseAsset ( assetString ) {
22
- if ( ! assetString ) return { amount : 0 , symbol : '' } ;
23
- const [ amount , symbol ] = assetString . split ( ' ' ) ;
24
- return { amount : parseFloat ( amount ) , symbol } ;
22
+ if ( ! assetString ) return { amount : 0 , symbol : '' }
23
+ const [ amount , symbol ] = assetString . split ( ' ' )
24
+ return { amount : parseFloat ( amount ) , symbol }
25
25
}
26
26
27
27
async function fetchMarkets ( ) {
@@ -31,81 +31,74 @@ async function fetchMarkets() {
31
31
table : 'markets' ,
32
32
limit : 100 ,
33
33
json : true ,
34
- } ) ;
35
- return res . rows || [ ] ;
34
+ } )
35
+ return res . rows || [ ]
36
36
}
37
37
38
38
async function fetchLiquidity ( tokenContract , symbol ) {
39
+ // available liquidity (cash) held by lending.loan for a given token
39
40
const res = await post ( `${ API_ENDPOINT } /v1/chain/get_table_rows` , {
40
41
code : tokenContract ,
41
42
scope : LENDING_CONTRACT ,
42
43
table : 'accounts' ,
43
44
limit : 100 ,
44
45
json : true ,
45
- } ) ;
46
- const rows = res . rows || [ ] ;
47
- const tokenBalance = rows . find ( b => parseAsset ( b . balance ) . symbol === symbol ) ;
48
- return tokenBalance ? parseAsset ( tokenBalance . balance ) . amount : 0 ;
46
+ } )
47
+ const rows = res . rows || [ ]
48
+ const tokenBalance = rows . find ( b => parseAsset ( b . balance ) . symbol === symbol )
49
+ return tokenBalance ? parseAsset ( tokenBalance . balance ) . amount : 0
49
50
}
50
51
51
52
// ----------------------------
52
- // TVL = borrows + cash reserves
53
+ // TVL = only available liquidity (cash)
53
54
// ----------------------------
54
55
async function tvl ( ) {
55
- const balances = { } ;
56
- const markets = await fetchMarkets ( ) ;
56
+ const balances = { }
57
+ const markets = await fetchMarkets ( )
57
58
58
59
const promises = markets . map ( async ( market ) => {
59
- const totalVar = parseAsset ( market . total_variable_borrows . quantity ) . amount ;
60
- const totalStable = parseAsset ( market . total_stable_borrows . quantity ) . amount ;
61
- const totalBorrows = totalVar + totalStable ;
62
-
63
- const [ , symbol ] = market . underlying_symbol . sym . split ( ',' ) ;
64
- const tokenContract = market . underlying_symbol . contract ;
65
-
66
- // liquidity available in lending contract
67
- const cashAvailable = await fetchLiquidity ( tokenContract , symbol ) ;
68
- const totalSupplied = totalBorrows + cashAvailable ;
69
-
70
- const internalId = `${ tokenContract } :${ symbol } ` ;
71
- const cgkId = tokenMapping [ internalId ] ;
72
- if ( ! cgkId ) return ;
73
-
74
- sdk . util . sumSingleBalance ( balances , `coingecko:${ cgkId } ` , totalSupplied ) ;
75
- } ) ;
76
-
77
- await Promise . all ( promises ) ;
78
- return balances ;
60
+ const [ , symbol ] = market . underlying_symbol . sym . split ( ',' )
61
+ const tokenContract = market . underlying_symbol . contract
62
+ const internalId = `${ tokenContract } :${ symbol } `
63
+ const cgkId = tokenMapping [ internalId ]
64
+ if ( ! cgkId ) return
65
+
66
+ const cashAvailable = await fetchLiquidity ( tokenContract , symbol )
67
+ sdk . util . sumSingleBalance ( balances , `coingecko:${ cgkId } ` , cashAvailable )
68
+ } )
69
+
70
+ await Promise . all ( promises )
71
+ return balances
79
72
}
80
73
81
74
// ----------------------------
82
- // Borrowed = borrows only
75
+ // Borrowed = total variable + stable borrows
83
76
// ----------------------------
84
77
async function borrowed ( ) {
85
- const balances = { } ;
86
- const markets = await fetchMarkets ( ) ;
78
+ const balances = { }
79
+ const markets = await fetchMarkets ( )
87
80
88
81
markets . forEach ( market => {
89
- const totalVar = parseAsset ( market . total_variable_borrows . quantity ) . amount ;
90
- const totalStable = parseAsset ( market . total_stable_borrows . quantity ) . amount ;
91
- const totalBorrows = totalVar + totalStable ;
82
+ const totalVar = parseAsset ( market . total_variable_borrows . quantity ) . amount
83
+ const totalStable = parseAsset ( market . total_stable_borrows . quantity ) . amount
84
+ const totalBorrows = totalVar + totalStable
92
85
93
- const [ , symbol ] = market . underlying_symbol . sym . split ( ',' ) ;
94
- const tokenContract = market . underlying_symbol . contract ;
95
- const internalId = `${ tokenContract } :${ symbol } ` ;
96
- const cgkId = tokenMapping [ internalId ] ;
97
- if ( ! cgkId ) return ;
86
+ const [ , symbol ] = market . underlying_symbol . sym . split ( ',' )
87
+ const tokenContract = market . underlying_symbol . contract
88
+ const internalId = `${ tokenContract } :${ symbol } `
89
+ const cgkId = tokenMapping [ internalId ]
90
+ if ( ! cgkId ) return
98
91
99
- sdk . util . sumSingleBalance ( balances , `coingecko:${ cgkId } ` , totalBorrows ) ;
100
- } ) ;
92
+ sdk . util . sumSingleBalance ( balances , `coingecko:${ cgkId } ` , totalBorrows )
93
+ } )
101
94
102
- return balances ;
95
+ return balances
103
96
}
104
97
105
98
module . exports = {
106
- methodology : 'TVL = variable borrows + stable borrows + available liquidity in lending.loan. Borrowed = total outstanding borrows ( variable + stable). Mapping is to CoinGecko IDs .' ,
99
+ methodology : 'TVL = only available liquidity (cash held by lending.loan) . Borrowed = total variable + stable borrows (outstanding debt ). Deposits = TVL + Borrowed, but we report liquidity as TVL per DefiLlama standards .' ,
107
100
proton : {
108
101
tvl,
109
102
borrowed,
110
103
}
111
- } ;
104
+ }
0 commit comments