Skip to content

Add LiquidOps yield adapter #1803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/adaptors/liquidops/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const utils = require('../utils');
const axios = require('axios');

const endpoint = 'https://cu.ao-testnet.xyz'
const controllerId = 'SmmMv0rJwfIDVM3RvY2-P729JFYwhdGSeGo2deynbfY'
const redstoneOracleAddress = 'R5rRjBFS90qIGaohtzd1IoyPwZD0qJZ25QXkP7_p5a0'
const chain = 'AO'


const apy = async () => {

const supportedTokensRes = await DryRun(controllerId, "Get-Tokens")
const supportedTokens = JSON.parse(supportedTokensRes.Messages[0].Data)
const redstoneTickers = JSON.stringify(supportedTokens.map(token => convertTicker(token.ticker.toUpperCase())))

const redstonePriceFeedRes = await DryRun(redstoneOracleAddress, "v2.Request-Latest-Data", [["Tickers", redstoneTickers]]);
const redstonePrices = JSON.parse(redstonePriceFeedRes.Messages[0].Data)

const poolPromises = supportedTokens.map(async poolObject => {

const getAPRRes = await DryRun(poolObject.oToken, "Get-APR");
const APRTagsObject = Object.fromEntries(
getAPRRes.Messages[0].Tags.map((tag) => [tag.name, tag.value])
);
const borrowAPR = formatBorrowAPR(APRTagsObject)

const infoRes = await DryRun(poolObject.oToken, "Info");
const infoTagsObject = Object.fromEntries(
infoRes.Messages[0].Tags.map((tag) => [tag.name, tag.value])
);

const ticker = poolObject.ticker
const redstoneTicker = convertTicker(ticker.toUpperCase())
const tokenUSDPrice = (redstonePrices[redstoneTicker]).v

const totalLends = scaleBalance(infoTagsObject['Cash'], infoTagsObject['Denomination'])
const totalBorrows = scaleBalance(infoTagsObject['Total-Borrows'], infoTagsObject['Denomination'])
const totalSupply = scaleBalance(infoTagsObject['Total-Supply'], infoTagsObject['Denomination'])

const supplyAPY = formatSupplyAPR(borrowAPR, infoTagsObject, totalBorrows, totalSupply)

const ltv = Number(infoTagsObject['Collateral-Factor']) / 100

const tokenID = poolObject.id
const oTokenID = poolObject.oToken

return {
pool: `${oTokenID}-${chain}`.toLowerCase(),
chain,
project: 'liquidops',
symbol: utils.formatSymbol(`o${ticker}`),
tvlUsd: totalLends * tokenUSDPrice,
apyBase: supplyAPY,
underlyingTokens: [tokenID],
url: `https://liquidops.io/${ticker}`,
apyBaseBorrow: borrowAPR,
totalSupplyUsd: totalSupply * tokenUSDPrice,
totalBorrowUsd: totalBorrows * tokenUSDPrice,
ltv
};
});

const poolsArray = await Promise.all(poolPromises);

return poolsArray

}


// Access AO on chain data via the node endpoint
async function DryRun(target, action, extraTags = []) {
const response = await axios.post(`${endpoint}/dry-run?process-id=${target}`, {
Id: "1234",
Target: target,
Owner: "1234",
Anchor: "0",
Data: "1234",
Tags: [
["Target", target],
["Action", action],
["Data-Protocol", "ao"],
["Type", "Message"],
["Variant", "ao.TN.1"],
...extraTags
].map(([name, value]) => ({ name, value }))
}, {
headers: {
'Content-Type': 'application/json'
}
});
await new Promise((resolve) => setTimeout(resolve, 1000));
return response.data;
}


function scaleBalance(amount, denomination) {
const scaledDivider = BigInt(10) ** BigInt(denomination)
const balance = BigInt(amount) / scaledDivider
return Number(balance)
}

function formatBorrowAPR(aprResponse) {
const apr = parseFloat(aprResponse["Annual-Percentage-Rate"]);
const rateMultiplier = parseFloat(aprResponse["Rate-Multiplier"]);
return apr / rateMultiplier
}

function formatSupplyAPR(borrowAPR, infoTagsObject, totalBorrows, totalSupply) {
const utilizationRate = totalBorrows / totalSupply
const reserveFactorFract = Number(infoTagsObject['Reserve-Factor']) / 100;
return borrowAPR * utilizationRate * (1 - reserveFactorFract);
}

function convertTicker(ticker) {
if (ticker === "QAR") return "AR";
if (ticker === "WUSDC") return "USDC";
return ticker;
}


module.exports = {
apy,
};
// npm run test --adapter=liquidops





Loading