Skip to content
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

stellaswap v3 support #1470

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
938 changes: 938 additions & 0 deletions src/adaptors/stellaswap-v3/abis/BeefyCLMStrategy.json

Large diffs are not rendered by default.

479 changes: 479 additions & 0 deletions src/adaptors/stellaswap-v3/abis/BeefyCLMVault.json

Large diffs are not rendered by default.

537 changes: 537 additions & 0 deletions src/adaptors/stellaswap-v3/abis/OffchainRewarder.json

Large diffs are not rendered by default.

661 changes: 661 additions & 0 deletions src/adaptors/stellaswap-v3/abis/OffchainRewarderRegistry.json

Large diffs are not rendered by default.

418 changes: 418 additions & 0 deletions src/adaptors/stellaswap-v3/abis/eternal-virtual-pool.json

Large diffs are not rendered by default.

1,092 changes: 1,092 additions & 0 deletions src/adaptors/stellaswap-v3/abis/farming-center.json

Large diffs are not rendered by default.

1,236 changes: 1,236 additions & 0 deletions src/adaptors/stellaswap-v3/abis/nftPositionManager.json

Large diffs are not rendered by default.

1,041 changes: 1,041 additions & 0 deletions src/adaptors/stellaswap-v3/abis/pool.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/adaptors/stellaswap-v3/clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const KEY = "89ee4bac17c0a4b5c8f359d9b50b3abe"
const API = `https://gateway.thegraph.com/api/${KEY}`
exports.pulsarBlocks = `${API}/subgraphs/id/HaSkCZY1wcFLLaQE4xHksnHH9VPiLm6i63xAc1kze4Jp`;
exports.pulsar = `${API}/subgraphs/id/G7GRSbr917k92izqozMjNspBqRWp2U91xsj8q6bq8oyH`;
exports.pulsarFarming = `${API}/subgraphs/id/8ym6MbzW8UgYWoyCRkZrWV44YHmgS4ZxUmhv8kyNNSsn`;
126 changes: 126 additions & 0 deletions src/adaptors/stellaswap-v3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const { request, gql } = require('graphql-request');
const BigNumber = require('bignumber.js');
const sdk = require('@defillama/sdk');
const utils = require('../utils');
const { pulsar } = require('./clients');
const { queryPools, queryPrior } = require('./queries');
const { updateFarmsRewardsApr, updatePoolsApr } = require('./offchain-service');
const {
getPreviousBlockNumber,
getPositionsOfPool,
getAmounts,
} = require('./utils');

const topLvl = async (chainString, timestamp, url) => {
const balanceCalls = [];
const prevBlockNumber = await getPreviousBlockNumber()

let data = (await request(url, queryPools)).pools
const dataPrior = (await request(url, queryPrior.replace('<PREV_BLOCK_NUMBER>', prevBlockNumber))).pools;

for (const pool of data) {
balanceCalls.push({
target: pool.token0.id,
params: pool.id,
});
balanceCalls.push({
target: pool.token1.id,
params: pool.id,
});
}

const tokenBalances = await sdk.api.abi.multiCall({
abi: 'erc20:balanceOf',
calls: balanceCalls,
chain: chainString,
permitFailure: true,
});

data = data.map((p) => {
const x = tokenBalances.output.filter((i) => i.input.params[0] === p.id);
return {
...p,
reserve0: (parseFloat(x.find((i) => i.input.target === p.token0.id)?.output || 0) / Math.pow(10, p.token0.decimals)),
reserve1: (parseFloat(x.find((i) => i.input.target === p.token1.id)?.output || 0) / Math.pow(10, p.token1.decimals)),
};
});

data = await utils.tvl(data, chainString);

const poolsFees = {};
const poolsCurrentTvl = {};

for (const pool of data) {
const currentFeesInToken0 = new BigNumber(pool.feesToken0).plus(new BigNumber(pool.feesToken1).times(new BigNumber(pool.token0Price)));
const priorData = dataPrior.find(dp => dp.id === pool.id);
const priorFeesInToken0 = priorData ? new BigNumber(priorData.feesToken0).plus(new BigNumber(priorData.feesToken1).times(new BigNumber(priorData.token0Price))) : new BigNumber(0);
const feesIn24Hours = currentFeesInToken0.minus(priorFeesInToken0);

poolsFees[pool.id] = feesIn24Hours;
poolsCurrentTvl[pool.id] = new BigNumber(0);
const positionsJson = await getPositionsOfPool(pool.id);
for (const position of positionsJson) {
const currentTick = new BigNumber(pool.tick);
const { amount0, amount1 } = getAmounts(
new BigNumber(position.liquidity),
new BigNumber(position.tickLower.tickIdx),
new BigNumber(position.tickUpper.tickIdx),
currentTick,
);
const adjustedAmount0 = amount0 / Math.pow(10, position.token0.decimals);
const adjustedAmount1 = amount1 / Math.pow(10, position.token1.decimals);
poolsCurrentTvl[pool.id] += adjustedAmount0 + (adjustedAmount1 * parseFloat(pool.token0Price));
}
}

const poolsFarmApr = await updateFarmsRewardsApr();
const poolsAPRObj = await updatePoolsApr();

const poolsAPR = {};
const poolsRewardTokens = {}; // Add this to store reward tokens for each pool

const poolsBaseAPR = poolsAPRObj;

for (const pool of data) {
const apr = poolsBaseAPR[pool.id] ? new BigNumber(poolsBaseAPR[pool.id]) : new BigNumber(0);
poolsAPR[pool.id] = apr;

const rewardTokens = apr.isNaN() || apr.eq(0) ? [] : [pool.token0.id, pool.token1.id];
poolsRewardTokens[pool.id] = rewardTokens; // Store reward tokens for each pool
}

data = data.map((p) => {
const tvl = p.poolDayData[0]?.tvlUSD || 0;
if (tvl > 30000) {
const baseAPR = poolsAPR[p.id] ? poolsAPR[p.id].toNumber() : 0;
const rewardsAPR = poolsFarmApr.pools[p.id]?.apr.toNumber() || 0;
const rewardTokens = poolsRewardTokens[p.id];

return {
pool: p.id,
chain: utils.formatChain(chainString),
project: 'stellaswap-v3',
symbol: `${p.token0.symbol}-${p.token1.symbol}`,
tvlUsd: parseFloat(tvl),
apyBase: baseAPR,
apyReward: rewardsAPR,
rewardTokens: rewardTokens,
underlyingTokens: [p.token0.id, p.token1.id],
url: `https://app.stellaswap.com/pulsar/add/${p.token0.id}/${p.token1.id}`,
};
}
}).filter(p => p);

return data;
};

const main = async (timestamp = null) => {
const data = await Promise.all([topLvl('moonbeam', timestamp, pulsar)]);
return data.flat().filter((p) => utils.keepFinite(p));
};

module.exports = {
timetravel: false,
apy: main,
url: 'https://stellaswap.com/',
};
Loading
Loading