Skip to content

Commit c50a473

Browse files
authored
add underlying pool assets to TVL of Mars Protocol (DefiLlama#11523)
1 parent 08c06fe commit c50a473

File tree

1 file changed

+70
-45
lines changed

1 file changed

+70
-45
lines changed

projects/mars/index.js

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
21
const { queryContract } = require('../helper/chain/cosmos');
2+
const axios = require('axios');
3+
const BigNumber = require('bignumber.js');
34

45
const contractAddresses = {
56
osmosis: {
@@ -10,10 +11,15 @@ const contractAddresses = {
1011
params: 'neutron1x4rgd7ry23v2n49y7xdzje0743c5tgrnqrqsvwyya2h6m48tz4jqqex06x',
1112
redBank: 'neutron1n97wnm7q6d2hrcna3rqlnyqw2we6k0l8uqvmyqq6gsml92epdu7quugyph',
1213
},
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+
};
1420

1521
async function tvl(api) {
16-
const chain = api.chain
22+
const chain = api.chain;
1723
const { params, redBank } = contractAddresses[chain];
1824
let startAfter = null;
1925
const pageLimit = 5;
@@ -22,72 +28,91 @@ async function tvl(api) {
2228
const assetParams = await queryContract({
2329
contract: params,
2430
chain,
25-
data: { 'all_asset_params': { limit: pageLimit, 'start_after': startAfter } }
31+
data: { all_asset_params: { limit: pageLimit, start_after: startAfter } },
2632
});
2733

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;
3336

3437
await addCoinsFromAssetParams(assetParams);
35-
} while (startAfter)
36-
38+
} while (startAfter);
3739

3840
do {
3941
const markets = await queryContract({
4042
contract: contractAddresses[chain].redBank,
4143
chain,
42-
data: { 'markets': { limit: pageLimit, 'start_after': startAfter } }
44+
data: { 'markets': { 'limit': pageLimit, 'start_after': startAfter } },
4345
});
4446

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;
5049

5150
await deductCoinsFromMarkets(markets);
52-
} while (startAfter)
51+
} while (startAfter);
5352

5453
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]);
5658

5759
// 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+
);
6588
}
6689

6790
async function deductCoinsFromMarkets(markets) {
68-
6991
// 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+
);
82107
}
83108
}
84109

85-
86110
module.exports = {
87111
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 },
91116
terra: {
92117
tvl: () => 0,
93118
},
@@ -96,6 +121,6 @@ module.exports = {
96121
[1675774800, 'Relaunch on Osmosis'],
97122
[1690945200, 'Launch on Neutron'],
98123
[1696906800, 'Mars v2 launch on Osmosis'],
99-
[1724166000, 'Mars v2 launch on Neutron']
100-
]
124+
[1724166000, 'Mars v2 launch on Neutron'],
125+
],
101126
};

0 commit comments

Comments
 (0)