Skip to content

Commit 8df5533

Browse files
authored
Liqwid Finance V2 integration (DefiLlama#11433)
1 parent a5dc6cc commit 8df5533

File tree

1 file changed

+111
-85
lines changed

1 file changed

+111
-85
lines changed

projects/liqwid/index.js

Lines changed: 111 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,99 +11,124 @@ module.exports = {
1111
}
1212
};
1313

14-
15-
const endpoint = 'https://api.liqwid.finance/graphql'
16-
17-
const queryAdaLoans = `query ($page: Int) {
18-
Page (page: $page) {
19-
pageInfo {
20-
currentPage
21-
hasNextPage
22-
}
23-
loan(marketId: "Ada") {
24-
collaterals {
25-
id
26-
amount
14+
const endpoint = 'https://v2.api.liqwid.finance/graphql'
15+
16+
const queryAdaLoans = `query($input: LoansInput){
17+
liqwid {
18+
data {
19+
loans(input: $input) {
20+
page
21+
pagesCount
22+
results {
23+
collaterals {
24+
qTokenAmount
25+
market {
26+
id
27+
}
28+
}
29+
}
2730
}
2831
}
2932
}
3033
}
3134
`
3235

33-
const query = `query ($page: Int) {
34-
Page (page: $page) {
35-
pageInfo {
36-
currentPage
37-
hasNextPage
38-
}
39-
market {
40-
asset {
41-
marketId
42-
name
43-
qTokenId
44-
qTokenPolicyId
45-
}
46-
state {
47-
totalSupply
48-
utilization
49-
}
50-
marketId
51-
decimals
52-
info {
53-
params {
54-
underlyingClass {
55-
value0 {
56-
symbol
57-
name
58-
}
59-
}
60-
}
61-
scripts {
62-
actionToken {
63-
script {
64-
value0 {
65-
value0
66-
}
67-
}
36+
const query = `query($input: MarketsInput) {
37+
liqwid {
38+
data {
39+
markets(input: $input) {
40+
page
41+
pagesCount
42+
results {
43+
asset {
44+
id
45+
symbol
46+
qTokenName
47+
qTokenCurrencySymbol
48+
currencySymbol
49+
name
50+
decimals
6851
}
52+
supply
53+
liquidity
54+
borrow
55+
utilization
6956
}
7057
}
7158
}
7259
}
7360
}
7461
`
62+
7563
const tokenMapping = {
7664
ADA: 'lovelace',
7765
DJED: '8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd61446a65644d6963726f555344',
7866
DAI: 'dai',
7967

8068
}
8169

82-
const getToken = market => tokenMapping[market.marketId.toUpperCase()] ?? market.info.params.underlyingClass.value0.symbol + toHex(market.info.params.underlyingClass.value0.name)
70+
const getToken = (market) => tokenMapping[market.asset.symbol] ?? market.asset.currencySymbol + toHex(market.asset.name)
8371

8472
const getOptimBondTVL = async () => {
85-
const getLoans = async (pageIndex) => {
86-
const { Page: { pageInfo, loan: loans } } = await graphQuery(endpoint, queryAdaLoans, { page: pageIndex })
87-
88-
if (!pageInfo.hasNextPage) {
89-
return loans
73+
const getLoans = async (pageIndex = 0, collectedLoans = []) => {
74+
const {
75+
liqwid: {
76+
data: { loans },
77+
},
78+
} = await graphQuery(endpoint, queryAdaLoans, {
79+
input: {
80+
marketIds: 'Ada',
81+
page: pageIndex,
82+
},
83+
})
84+
85+
const allLoans = [...collectedLoans, ...loans.results]
86+
87+
// Check if we've reached the last page
88+
if (pageIndex < loans.pagesCount - 1) {
89+
return await getLoans(pageIndex + 1, allLoans)
9090
}
91-
return [...loans, ...(await getLoans(pageIndex + 1))]
91+
92+
return allLoans
9293
}
93-
const loans = await getLoans(0)
94-
const relevantLoans =
95-
loans.filter(l => (l.collaterals.filter(c => c.id === "OptimBond1")).length != 0)
96-
const bonds =
97-
relevantLoans.map(l => l.collaterals[0].amount).reduce((acc, amount) =>
98-
acc + Number(amount), 0)
9994

95+
const loans = await getLoans()
96+
const relevantLoans = loans.filter((l) =>
97+
l.collaterals.some((c) => c.market.id === 'OptimBond1'),
98+
)
99+
const bonds = relevantLoans
100+
.flatMap((l) => l.collaterals)
101+
.filter((c) => c.market.id === 'OptimBond1')
102+
.reduce((acc, collateral) => acc + collateral.qTokenAmount, 0)
100103
return bonds
101104
}
102105

103106
async function tvl(api) {
104-
const { Page: { market: markets } } = await graphQuery(endpoint, query, { page: 0 })
107+
const getMarkets = async (pageIndex = 0, collectedMarkets = []) => {
108+
const {
109+
liqwid: {
110+
data: { markets },
111+
},
112+
} = await graphQuery(endpoint, query, {
113+
input: {
114+
page: pageIndex,
115+
},
116+
})
117+
118+
const allMarkets = [...collectedMarkets, ...markets.results]
119+
120+
// Check if we've reached the last page
121+
if (pageIndex < markets.pagesCount - 1) {
122+
return await getMarkets(pageIndex + 1, allMarkets)
123+
}
105124

106-
markets.forEach(market => add(api, market, market.state.totalSupply))
125+
return allMarkets
126+
}
127+
128+
const markets = await getMarkets()
129+
markets.forEach((market) =>
130+
add(api, market, market.liquidity * 10 ** market.asset.decimals),
131+
)
107132
add(api, "OptimBond1", await getOptimBondTVL())
108133
}
109134

@@ -117,30 +142,31 @@ function add(api, market, bal) {
117142
}
118143

119144
async function borrowed(api) {
120-
const { Page: { market: markets } } = await graphQuery(endpoint, query)
121-
122-
markets.forEach(market => {
123-
const utilization = market.state.utilization
124-
const availability = 1 - utilization
125-
const totalBorrowed = market.state.totalSupply * utilization / availability
126-
add(api, market, totalBorrowed)
127-
})
128-
}
129-
130-
function base64ToHex(base64) {
131-
return base64
132-
/* // Step 1: Decode the Base64 string to a byte array
133-
const binaryData = atob(base64);
145+
const getMarkets = async (pageIndex = 0, collectedMarkets = []) => {
146+
const {
147+
liqwid: {
148+
data: { markets },
149+
},
150+
} = await graphQuery(endpoint, query, {
151+
input: {
152+
page: pageIndex,
153+
},
154+
})
155+
156+
const allMarkets = [...collectedMarkets, ...markets.results]
157+
158+
// Check if we've reached the last page
159+
if (pageIndex < markets.pagesCount - 1) {
160+
return await getMarkets(pageIndex + 1, allMarkets)
161+
}
134162

135-
// Step 2: Convert each byte to its hexadecimal representation
136-
const hexArray = [];
137-
for (let i = 0; i < binaryData.length; i++) {
138-
const byte = binaryData.charCodeAt(i).toString(16).padStart(2, '0');
139-
hexArray.push(byte);
163+
return allMarkets
140164
}
141165

142-
// Step 3: Concatenate the hexadecimal values to form the final hexadecimal string
143-
return hexArray.join(''); */
166+
const markets = await getMarkets()
167+
markets.forEach((market) => {
168+
add(api, market, market.borrow * 10 ** market.asset.decimals)
169+
})
144170
}
145171

146172
function toHex(str) {
@@ -149,4 +175,4 @@ function toHex(str) {
149175
hex += str.charCodeAt(i).toString(16);
150176
}
151177
return hex
152-
}
178+
}

0 commit comments

Comments
 (0)