1
-
2
1
const { queryContract } = require ( '../helper/chain/cosmos' ) ;
2
+ const axios = require ( 'axios' ) ;
3
+ const BigNumber = require ( 'bignumber.js' ) ;
3
4
4
5
const contractAddresses = {
5
6
osmosis : {
@@ -10,10 +11,15 @@ const contractAddresses = {
10
11
params : 'neutron1x4rgd7ry23v2n49y7xdzje0743c5tgrnqrqsvwyya2h6m48tz4jqqex06x' ,
11
12
redBank : 'neutron1n97wnm7q6d2hrcna3rqlnyqw2we6k0l8uqvmyqq6gsml92epdu7quugyph' ,
12
13
} ,
13
- }
14
+ } ;
15
+
16
+ const poolsApis = {
17
+ osmosis : 'https://api.astroport.fi/api/pools?chainId=osmosis-1' ,
18
+ neutron : 'https://api.astroport.fi/api/pools?chainId=neutron-1' ,
19
+ } ;
14
20
15
21
async function tvl ( api ) {
16
- const chain = api . chain
22
+ const chain = api . chain ;
17
23
const { params, redBank } = contractAddresses [ chain ] ;
18
24
let startAfter = null ;
19
25
const pageLimit = 5 ;
@@ -22,72 +28,91 @@ async function tvl(api) {
22
28
const assetParams = await queryContract ( {
23
29
contract : params ,
24
30
chain,
25
- data : { ' all_asset_params' : { limit : pageLimit , ' start_after' : startAfter } }
31
+ data : { all_asset_params : { limit : pageLimit , start_after : startAfter } } ,
26
32
} ) ;
27
33
28
- if ( assetParams . length === pageLimit )
29
- startAfter = assetParams [ assetParams . length - 1 ] . denom ;
30
- else
31
- startAfter = null ;
32
-
34
+ if ( assetParams . length === pageLimit ) startAfter = assetParams [ assetParams . length - 1 ] . denom ;
35
+ else startAfter = null ;
33
36
34
37
await addCoinsFromAssetParams ( assetParams ) ;
35
- } while ( startAfter )
36
-
38
+ } while ( startAfter ) ;
37
39
38
40
do {
39
41
const markets = await queryContract ( {
40
42
contract : contractAddresses [ chain ] . redBank ,
41
43
chain,
42
- data : { 'markets' : { limit : pageLimit , 'start_after' : startAfter } }
44
+ data : { 'markets' : { ' limit' : pageLimit , 'start_after' : startAfter } } ,
43
45
} ) ;
44
46
45
- if ( markets . length === pageLimit )
46
- startAfter = markets [ markets . length - 1 ] . denom ;
47
- else
48
- startAfter = null ;
49
-
47
+ if ( markets . length === pageLimit ) startAfter = markets [ markets . length - 1 ] . denom ;
48
+ else startAfter = null ;
50
49
51
50
await deductCoinsFromMarkets ( markets ) ;
52
- } while ( startAfter )
51
+ } while ( startAfter ) ;
53
52
54
53
async function addCoinsFromAssetParams ( assetParams ) {
55
- const assetDenoms = assetParams . map ( asset => asset . denom ) ;
54
+ const assetDenoms = assetParams . map ( ( asset ) => asset . denom ) ;
55
+
56
+ // fetch pool infos from the poolsApi based on chain
57
+ const poolInfos = await axios . get ( poolsApis [ chain ] ) ;
56
58
57
59
// query the deposited amount for each asset and add it to the depositCoins array
58
- await Promise . all ( assetDenoms . map ( async denom => {
59
- let totalDepositInfo = await queryContract ( {
60
- contract : params , chain,
61
- data : { 'total_deposit' : { 'denom' : denom , } }
62
- } ) ;
63
- api . add ( denom , totalDepositInfo . amount ) ;
64
- } ) ) ;
60
+ await Promise . all (
61
+ assetDenoms . map ( async ( denom ) => {
62
+ const totalDepositInfo = await queryContract ( {
63
+ contract : params ,
64
+ chain,
65
+ data : { 'total_deposit' : { 'denom' : denom } } ,
66
+ } ) ;
67
+ // check if the token is a liquidity pool share (deposited via farm)
68
+ // and find it in the api data
69
+ const poolInfo = poolInfos . data . find ( ( pool ) => pool . lpAddress === denom ) ;
70
+
71
+ if ( poolInfo ) {
72
+ // check for the underlying asset and calculate how much underlying assets a pool share holds
73
+ const totalShares = poolInfo . poolTotalShare ;
74
+ const poolAssets = poolInfo . assets ;
75
+ poolAssets . forEach ( ( asset ) => {
76
+ const amount = new BigNumber ( asset . amount ) ;
77
+ const amountPerShare = amount . div ( totalShares ) ;
78
+
79
+ // add the underlying tokens to the api
80
+ api . add ( asset . denom , amountPerShare . times ( totalDepositInfo . amount ) . integerValue ( BigNumber . ROUND_DOWN ) . toString ( ) ) ;
81
+ } ) ;
82
+ } else {
83
+ // if the it's a token and not a liquidity pool share, add it to the api
84
+ api . add ( denom , totalDepositInfo . amount ) ;
85
+ }
86
+ } ) ,
87
+ ) ;
65
88
}
66
89
67
90
async function deductCoinsFromMarkets ( markets ) {
68
-
69
91
// query the underlying debt amount from the debt_total_scaled
70
- await Promise . all ( markets . map ( async market => {
71
- let totalDebt = await queryContract ( {
72
- contract : redBank , chain,
73
- data : {
74
- 'underlying_debt_amount' : {
75
- 'denom' : market . denom ,
76
- 'amount_scaled' : market [ 'debt_total_scaled' ]
77
- }
78
- }
79
- } ) ;
80
- api . add ( market . denom , totalDebt * - 1 )
81
- } ) ) ;
92
+ await Promise . all (
93
+ markets . map ( async ( market ) => {
94
+ const totalDebt = await queryContract ( {
95
+ contract : redBank ,
96
+ chain,
97
+ data : {
98
+ 'underlying_debt_amount' : {
99
+ 'denom' : market . denom ,
100
+ 'amount_scaled' : market [ 'debt_total_scaled' ] ,
101
+ } ,
102
+ } ,
103
+ } ) ;
104
+ api . add ( market . denom , totalDebt * - 1 ) ;
105
+ } ) ,
106
+ ) ;
82
107
}
83
108
}
84
109
85
-
86
110
module . exports = {
87
111
timetravel : false ,
88
- methodology : 'For each chain, sum token balances by querying the total deposit amount for each asset in the chain\'s params contract.' ,
89
- osmosis : { tvl, } ,
90
- neutron : { tvl, } ,
112
+ methodology :
113
+ "For each chain, sum token balances by querying the total deposit amount for each asset in the chain's params contract." ,
114
+ osmosis : { tvl } ,
115
+ neutron : { tvl } ,
91
116
terra : {
92
117
tvl : ( ) => 0 ,
93
118
} ,
@@ -96,6 +121,6 @@ module.exports = {
96
121
[ 1675774800 , 'Relaunch on Osmosis' ] ,
97
122
[ 1690945200 , 'Launch on Neutron' ] ,
98
123
[ 1696906800 , 'Mars v2 launch on Osmosis' ] ,
99
- [ 1724166000 , 'Mars v2 launch on Neutron' ]
100
- ]
124
+ [ 1724166000 , 'Mars v2 launch on Neutron' ] ,
125
+ ] ,
101
126
} ;
0 commit comments