Skip to content

Commit 6716a33

Browse files
committed
tweaking so tvl is just the available liquiditya
1 parent ab51389 commit 6716a33

File tree

1 file changed

+48
-55
lines changed

1 file changed

+48
-55
lines changed

projects/loan-protocol/index.js

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { post } = require('../helper/http');
2-
const sdk = require('@defillama/sdk');
1+
const { post } = require('../helper/http')
2+
const sdk = require('@defillama/sdk')
33

44
const tokenMapping = {
55
'xtokens:XBTC': 'bitcoin',
@@ -8,20 +8,20 @@ const tokenMapping = {
88
'xtokens:XXRP': 'ripple',
99
'eosio.token:XPR': 'proton',
1010
'xtokens:XMT': 'metal',
11-
'xtokens:XUST': 'terrausd-wormhole', // optional/legacy
12-
'xtokens:XLUNA': 'terra-luna-2', // optional/legacy
1311
'xtokens:XUSDC': 'usd-coin',
1412
'xtokens:XDOGE': 'dogecoin',
1513
'xtokens:XUSDT': 'tether',
16-
};
14+
'xtokens:XUST': 'terrausd-wormhole',
15+
'xtokens:XLUNA': 'terra-luna-2',
16+
}
1717

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'
2020

2121
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 }
2525
}
2626

2727
async function fetchMarkets() {
@@ -31,81 +31,74 @@ async function fetchMarkets() {
3131
table: 'markets',
3232
limit: 100,
3333
json: true,
34-
});
35-
return res.rows || [];
34+
})
35+
return res.rows || []
3636
}
3737

3838
async function fetchLiquidity(tokenContract, symbol) {
39+
// available liquidity (cash) held by lending.loan for a given token
3940
const res = await post(`${API_ENDPOINT}/v1/chain/get_table_rows`, {
4041
code: tokenContract,
4142
scope: LENDING_CONTRACT,
4243
table: 'accounts',
4344
limit: 100,
4445
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
4950
}
5051

5152
// ----------------------------
52-
// TVL = borrows + cash reserves
53+
// TVL = only available liquidity (cash)
5354
// ----------------------------
5455
async function tvl() {
55-
const balances = {};
56-
const markets = await fetchMarkets();
56+
const balances = {}
57+
const markets = await fetchMarkets()
5758

5859
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
7972
}
8073

8174
// ----------------------------
82-
// Borrowed = borrows only
75+
// Borrowed = total variable + stable borrows
8376
// ----------------------------
8477
async function borrowed() {
85-
const balances = {};
86-
const markets = await fetchMarkets();
78+
const balances = {}
79+
const markets = await fetchMarkets()
8780

8881
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
9285

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
9891

99-
sdk.util.sumSingleBalance(balances, `coingecko:${cgkId}`, totalBorrows);
100-
});
92+
sdk.util.sumSingleBalance(balances, `coingecko:${cgkId}`, totalBorrows)
93+
})
10194

102-
return balances;
95+
return balances
10396
}
10497

10598
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.',
107100
proton: {
108101
tvl,
109102
borrowed,
110103
}
111-
};
104+
}

0 commit comments

Comments
 (0)