Skip to content

Commit

Permalink
[Strudy V2]: updated to fetch pools from subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
atozICT20 committed Mar 1, 2024
1 parent 8f7fa80 commit 466385d
Showing 1 changed file with 59 additions and 15 deletions.
74 changes: 59 additions & 15 deletions src/adaptors/sturdy-v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,81 @@
const { BigNumber } = require('bignumber.js');
const { gql, request } = require('graphql-request');
const utils = require('../utils');

type V2Aggregator = {
const BIG_10 = new BigNumber('10');

const sturdy_v2_subgraph =
'https://api.thegraph.com/subgraphs/name/sturdyfi/sturdy-v2-ethereum';

const query = gql`
query SturdyV2Aggregators {
aggregators(first: 1000) {
address
totalAssets
asset {
id
symbol
decimals
}
}
}
`;

type V2AggregatorSubgraphData = {
address: `0x${string}`;
totalAssets: string;
asset: {
id: `0x${string}`;
symbol: string;
address: `0x${string}`;
}
tvl: number;
decimals: number;
};
};

type V2AggregatorAPIData = {
address: `0x${string}`;
baseAPY: number;
rewardsAPY: number;
rewardTokens: `0x${string}`[];
};

const aggregators = async () => {
const data = await utils.getData(
const apiData = (await utils.getData(
`https://us-central1-stu-dashboard-a0ba2.cloudfunctions.net/v2Aggregators`
) as V2Aggregator[];
)) as V2AggregatorAPIData[];

const subgraphData = await request(sturdy_v2_subgraph, query);
const aggregators = subgraphData.aggregators as V2AggregatorSubgraphData[];

// fetch token prices
const assetContracts = aggregators.map((a) => a.asset.id);
const coins = [...assetContracts].map((addr) => `ethereum:${addr}`);
const prices = (await utils.getPrices(coins)).pricesByAddress;

return aggregators.map((a, index) => {
const { address, totalAssets, asset } = a;

const tvl = new BigNumber(totalAssets)
.dividedBy(BIG_10.pow(asset.decimals))
.times(prices[assetContracts[index].toLowerCase()]);

return data.map((p) => {
const apyReward = p.rewardsAPY * 100;
const apyBase = p.baseAPY * 100;
const apy = apiData.find(
(e) => e.address.toLowerCase() === address.toLowerCase()
);
const apyBase = apy.baseAPY * 100 || 0;
const apyReward = apy.rewardsAPY * 100 || 0;
const rewardTokens = apy.rewardTokens || [];

return {
pool: p.address,
pool: address,
chain: 'ethereum',
project: 'sturdy-v2',
symbol: utils.formatSymbol(p.asset.symbol),
tvlUsd: p.tvl,
symbol: utils.formatSymbol(a.asset.symbol),
tvlUsd: tvl.toNumber(),
apyBase,
apyReward,
rewardTokens: p.rewardTokens,
url: `https://v2.sturdy.finance/aggregators/ethereum/${p.address}`,
underlyingTokens: [p.asset.address],
rewardTokens,
url: `https://v2.sturdy.finance/aggregators/ethereum/${address}`,
underlyingTokens: [a.asset.id],
};
});
};
Expand Down

0 comments on commit 466385d

Please sign in to comment.