-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Liqwid Finance V2 API integration (#1471)
* add DAI, AGIX, WMT, MIN to Liqwid yields * fix: move to v2 API * fix: protect APY values against future API changes --------- Co-authored-by: Kylix Afonso <kylix@liqwid.finance>
- Loading branch information
1 parent
0ea7dc6
commit 123fdee
Showing
1 changed file
with
60 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,82 @@ | ||
const { request, gql } = require('graphql-request'); | ||
|
||
const apy = async () => { | ||
const endpoint = 'https://api.liqwid.finance/graphql' | ||
const endpoint = 'https://v2.api.liqwid.finance/graphql'; | ||
const registryEndpoint = 'https://public.liqwid.finance/registry.json'; | ||
|
||
const query = gql`query ($page: Int) { | ||
Page (page: $page) { | ||
market { | ||
asset { | ||
name | ||
} | ||
state { | ||
totalSupply | ||
supplyApy | ||
utilization | ||
borrowApy | ||
borrowLqDistributionApy | ||
supplyMarketParticipationAPR | ||
maxLoanToValue | ||
} | ||
info { | ||
scripts { | ||
batchFinal { | ||
hash | ||
const query = gql` | ||
query { | ||
liqwid { | ||
data { | ||
markets { | ||
page | ||
results { | ||
id | ||
supplyAPY | ||
supply | ||
lqSupplyAPY | ||
borrowAPY | ||
borrow | ||
utilization | ||
asset { | ||
price | ||
symbol | ||
} | ||
} | ||
} | ||
} | ||
price { | ||
price | ||
} | ||
decimals | ||
} | ||
} | ||
adaStakingAPY | ||
} | ||
` | ||
const data = await request(endpoint, query) | ||
const markets = data.Page.market; | ||
const adaStakingAPY = data.adaStakingAPY; // ada liquid staking | ||
`; | ||
|
||
const data = await request(endpoint, query); | ||
|
||
//NOTE: Action validator will be added to the API in the near future | ||
const registryData = await fetch(registryEndpoint) | ||
.then((res) => res.json()) | ||
.then((data) => | ||
data.scriptInfos.filter( | ||
(script) => | ||
script.type === 'Validator' && script.tag === 'liqwidMainnet' | ||
) | ||
); | ||
|
||
const getPool = (tokenName) => { | ||
const market = markets.filter((market) => market.asset.name === tokenName)[0]; | ||
const markets = data.liqwid.data.markets.results; | ||
|
||
const getPool = (market) => { | ||
return { | ||
// FIXME(Kylix, 20th Sep 2023): Construct Shelley address from hex scripthash | ||
pool: market.info.scripts.batchFinal.hash.value0, // market batch final hash | ||
chain: "Cardano", | ||
project: "liqwid", | ||
symbol: market.asset.name, | ||
tvlUsd: market.state.totalSupply / Math.pow(10, market.decimals) * market.price.price, | ||
apyBase: (tokenName == "ADA" ? adaStakingAPY : 0 + Number(market.state.supplyApy)) * 100, | ||
apyReward: Number(market.state.supplyMarketParticipationAPR) * 100, | ||
rewardTokens: [tokenName, "LQ"], | ||
underlyingTokens: [tokenName], | ||
pool: registryData.find( | ||
(script) => script.name === `Liqwid${market.id}Action` | ||
).scriptHash, | ||
chain: 'Cardano', | ||
project: 'liqwid', | ||
symbol: market.asset.symbol, | ||
tvlUsd: market.supply * market.asset.price, | ||
apyReward: | ||
market.lqSupplyAPY * 100 > 100 | ||
? market.lqSupplyAPY | ||
: market.lqSupplyAPY * 100, | ||
apyReward: | ||
market.lqSupplyAPY * 100 > 100 | ||
? market.lqSupplyAPY | ||
: market.lqSupplyAPY * 100, | ||
rewardTokens: [market.asset.symbol, 'LQ'], | ||
underlyingTokens: [market.asset.symbol], | ||
// lending protocol fields | ||
apyBaseBorrow: Number(market.state.borrowApy) * 100, | ||
apyRewardBorrow: Number(market.state.borrowLqDistributionApy) * 100, | ||
totalSupplyUsd: market.state.totalSupply / (1 - market.state.utilization) / Math.pow(10, market.decimals) * market.price.price, | ||
totalBorrowUsd: market.state.totalSupply / (1 - market.state.utilization) / Math.pow(10, market.decimals) * market.state.utilization * market.price.price, | ||
ltv: Number(market.state.maxLoanToValue) | ||
apyBaseBorrow: | ||
market.borrowAPY * 100 > 100 | ||
? market.borrowAPY | ||
: market.borrowAPY * 100, | ||
totalSupplyUsd: market.supply * market.asset.price, | ||
totalBorrowUsd: market.borrow * market.asset.price, | ||
}; | ||
}; | ||
|
||
return [ | ||
getPool("ADA"), | ||
getPool("DJED"), | ||
getPool("SHEN"), | ||
getPool("iUSD"), | ||
getPool("USDC"), | ||
getPool("USDT"), | ||
getPool("DAI"), | ||
getPool("AGIX"), | ||
getPool("WMT"), | ||
getPool("MIN") | ||
]; | ||
return markets.map(getPool); | ||
}; | ||
|
||
module.exports = { | ||
timetravel: false, | ||
apy: apy, | ||
url: 'https://app.liqwid.finance/', | ||
url: 'https://v2.liqwid.finance/', | ||
}; |