Skip to content

Commit

Permalink
feat: upgrade nftx amm subgraph to the new uniswap subgraph
Browse files Browse the repository at this point in the history
this new uniswap subgraph works much better for nftx.js's needs than the messari one
this commit updates the subgraph urls, schemas, and transformations to work with the new subgraph

all api-level types (liquidityPools/posittions/vaults) maintain the same structure as before. however, the underlying subgraph types and urls have all changed and are considered breaking.
this only directly affects internal methods (api/indexer/@nftx/core) it has not been marked as breaking, the public API remains the same
  • Loading branch information
jackmellis committed Apr 18, 2024
1 parent 7809ca5 commit a9912b3
Show file tree
Hide file tree
Showing 25 changed files with 4,903 additions and 6,599 deletions.
7 changes: 7 additions & 0 deletions packages/constants/src/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ export const NFTX_FEE_DISTRIBUTOR = {
[Network.Sepolia]: a('0x66EF5B4b6ee05639194844CE4867515665F14fED'),
[Network.Arbitrum]: a('0x0d50970C7848ebbE52661e70057D7D063B7de886'),
};

/** A list of old fee distributors that are no longer active (but useful for historical data) */
export const NFTX_FEE_DISTRIBUTOR_LEGACY = {
[Network.Mainnet]: [a('0x6845fF5f102bEF9D785468F0bEb535b4687406E7')],
[Network.Sepolia]: <Address[]>[],
[Network.Arbitrum]: <Address[]>[],
};
19 changes: 4 additions & 15 deletions packages/constants/src/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { Network } from './networks';

const SHARED_PATH = 'subgraphs/name';

const SHARED_HOST = 'https://api.thegraph.com';
const PROXY_HOST = 'https://graph-proxy.nftx.xyz';

const SHARED_PROXY_NAME = 'shared';

const SHARED_ROOT = `${SHARED_HOST}/${SHARED_PATH}`;

const SHARED_PROXY_ROOT = `${PROXY_HOST}/${SHARED_PROXY_NAME}/${SHARED_PATH}`;

export const NON_STANDARD_SUBGRAPH = {
[Network.Mainnet]: `${SHARED_PROXY_ROOT}/0xorg/non-standard-nfts`,
[Network.Mainnet]: `https://graph-proxy.nftx.xyz/shared/subgraphs/name/0xorg/non-standard-nfts`,
};

export const ERC721_SUBGRAPH = {
Expand Down Expand Up @@ -44,9 +33,9 @@ export const NFTX_SUBGRAPH = {

export const NFTX_UNISWAP_SUBGRAPH = {
[Network.Mainnet]:
'https://graph-proxy.nftx.xyz/nftx-indexer/subgraphs/id/QmTqJsUzqsmSzkvDeSGDmGYCcVu8v3Tr3j3MqNsTZvVCMm',
'https://query.graph.nftx.xyz/subgraphs/id/QmXavb61qVUrfYe7daW47n77LhinFgSoMhWVkMfgEdUspS',
[Network.Sepolia]:
'https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v3-amm-sepolia',
'https://api.thegraph.com/subgraphs/name/gundamdweeb/nftx-univ3-sepolia',
[Network.Arbitrum]:
'https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v3-amm-arbitrum',
'https://query.graph.nftx.xyz/subgraphs/id/QmaTrSbtkkCeJW5fhbsm1BoEZm31zEE9HtCP5BEjfeGLMT',
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ beforeEach(() => {
createdAt = oneMonthAgo;
dailySnapshots = [
{
dailyTotalRevenueETH: '0.01',
inputTokenBalances: [
parseEther('10').toString(),
parseEther('5').toString(),
],
timestamp: `${now - ONE_DAY * 2}`,
feesETH: '0.01',
totalValueLockedToken0: '10',
totalValueLockedToken1: '5',
date: now - ONE_DAY * 2,
},
{
dailyTotalRevenueETH: '0.02',
inputTokenBalances: [
parseEther('10').toString(),
parseEther('5').toString(),
],
timestamp: `${now - ONE_DAY * 3}`,
feesETH: '0.02',
totalValueLockedToken0: '10',
totalValueLockedToken1: '5',
date: now - ONE_DAY * 3,
},
];
vaultFeeReceipts = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,90 @@
import { WETH_TOKEN, WeiPerEther } from '@nftx/constants';
import { makeFetchPoolsSet } from '../fetchPoolsSet';
import { getChainConstant } from '@nftx/utils';
import { NftxV3Uniswap } from '@nftx/types';
import { formatEther } from 'viem';

let poolData: any;
let poolData: { pools: NftxV3Uniswap.Pool[] };
let queryPoolData: jest.Mock;
let fetchPoolsSet: ReturnType<typeof makeFetchPoolsSet>;
let args: Parameters<typeof fetchPoolsSet>[0];
let run: () => ReturnType<typeof fetchPoolsSet>;

beforeEach(() => {
const _pool = {} as NftxV3Uniswap.Pool;
const _token = {} as NftxV3Uniswap.Token;
const _poolHourData = {} as NftxV3Uniswap.PoolHourData;
const _poolDayData = {} as NftxV3Uniswap.PoolDayData;

poolData = {
liquidityPools: [
pools: [
{
..._pool,
id: '0x',
name: '0x',
tick: 0,
tick: '0',
totalLiquidity: `${WeiPerEther * 3n}`,
activeLiquidity: `${WeiPerEther * 2n}`,
inputTokenBalances: [
WeiPerEther.toString(),
(WeiPerEther * 2n).toString(),
],
liquidity: `${WeiPerEther * 2n}`,
totalValueLockedToken0: '1',
totalValueLockedToken1: '2',
totalValueLockedETH: '0',
openPositionCount: 0,
createdTimestamp: 0,
createdAtTimestamp: '0',
fees: [
{
id: '0x',
feePercentage: 0,
feeType: 'FIXED_LP_FEE',
feePercentage: '0',
feeType: 'FIXED_LP_FEE' as NftxV3Uniswap.PoolFeeType,
},
{
id: '0x',
feePercentage: 0,
feeType: 'FIXED_PROTOCOL_FEE',
feePercentage: '0',
feeType: 'FIXED_PROTOCOL_FEE' as NftxV3Uniswap.PoolFeeType,
},
{
id: '0x',
feePercentage: 0.3,
feeType: 'FIXED_TRADING_FEE',
feePercentage: '0.3',
feeType: 'FIXED_TRADING_FEE' as NftxV3Uniswap.PoolFeeType,
},
],
token0: { ..._token, id: '0x', symbol: '0x', name: '0x' },
token1: {
..._token,
id: getChainConstant(WETH_TOKEN, 1),
symbol: 'WETH',
name: 'Wrapped Ether',
},
inputTokens: [
{ ..._token, id: '0x', symbol: '0x', name: '0x' },
{
id: '0x',
symbol: '0x',
name: '0x',
},
{
..._token,
id: getChainConstant(WETH_TOKEN, 1),
symbol: 'WETH',
name: 'Wrapped Ether',
},
],
hourlySnapshots: [
poolHourData: [
{
hourlyVolumeByTokenAmount: ['0', '0'],
hourlyTotalRevenueETH: '0',
..._poolHourData,
volumeToken0: '0',
volumeToken1: '0',
totalValueLockedToken0: '0',
totalValueLockedToken1: '0',
feesETH: '0',
id: '0x',
inputTokenBalances: ['0', '0'],
timestamp: 0,
periodStartUnix: 0,
},
],
dailySnapshots: [
poolDayData: [
{
dailyVolumeByTokenAmount: ['0', '0'],
dailyTotalRevenueETH: '0.001',
..._poolDayData,
volumeToken0: '0',
volumeToken1: '1',
feesETH: '0.001',
id: '0x',
inputTokenBalances: ['0', '0'],
timestamp: 0,
totalValueLockedToken0: '0',
totalValueLockedToken1: '0',
date: 0,
},
],
},
Expand Down Expand Up @@ -172,20 +187,20 @@ it('calculates the APRs', async () => {

describe('when pool is a 0.3% tier', () => {
beforeEach(() => {
poolData.liquidityPools[0].fees[2].feePercentage = 0.3;
poolData.pools[0].fees[2].feePercentage = '0.3';
});

it('calculates the daily and weekly revenue from the vault fee receipts', async () => {
const [[pool]] = await run();

expect(formatEther(pool.dailyRevenue)).toEqual('0');
expect(formatEther(pool.weeklyRevenue)).toEqual('1.4');
expect(formatEther(pool.weeklyRevenue)).toEqual('1.401');
});
});

describe('when pool is a 1% tier', () => {
beforeEach(() => {
poolData.liquidityPools[0].fees[2].feePercentage = 1;
poolData.pools[0].fees[2].feePercentage = '1';
});

it('calculates the daily and weekly revenue from the AMM fees', async () => {
Expand All @@ -198,8 +213,8 @@ describe('when pool is a 1% tier', () => {

describe('when there are more than 1000 pools', () => {
beforeEach(() => {
poolData.liquidityPools = Array.from({ length: 1000 }).fill(
poolData.liquidityPools[0]
poolData.pools = Array.from<NftxV3Uniswap.Pool>({ length: 1000 }).fill(
poolData.pools[0]
);
});

Expand Down
48 changes: 31 additions & 17 deletions packages/core/src/pools/fetchLiquidityPools/calculateAprs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ const calculatePoolValue = ({
isWeth0: boolean;
vTokenToEth: bigint;
}) => {
const eth = BigInt(balances?.[isWeth0 ? 0 : 1] ?? '0');
const vToken = BigInt(balances?.[isWeth0 ? 1 : 0] ?? '0');
const eth = parseEther(balances?.[isWeth0 ? 0 : 1] ?? '0');
const vToken = parseEther(balances?.[isWeth0 ? 1 : 0] ?? '0');
const vTokenValue = (vToken * vTokenToEth) / WeiPerEther;
const poolValue = eth + vTokenValue;
return poolValue;
};

const snapshotToBalances = (
snapshot:
| Pick<
NftxV3Uniswap.PoolDayData,
'totalValueLockedToken0' | 'totalValueLockedToken1'
>
| undefined
) => {
if (!snapshot) {
return undefined;
}
return [snapshot.totalValueLockedToken0, snapshot.totalValueLockedToken1];
};

const calculateAMMApr = ({
createdAt,
dailySnapshots,
Expand All @@ -36,8 +50,8 @@ const calculateAMMApr = ({
vTokenToEth,
}: {
dailySnapshots: Pick<
NftxV3Uniswap.LiquidityPoolDailySnapshot,
'timestamp' | 'inputTokenBalances' | 'dailyTotalRevenueETH'
NftxV3Uniswap.PoolDayData,
'date' | 'totalValueLockedToken0' | 'totalValueLockedToken1' | 'feesETH'
>[];
isWeth0: boolean;
periodStart: number;
Expand All @@ -50,29 +64,29 @@ const calculateAMMApr = ({
let start = periodStart > createdAt ? periodStart : createdAt;
let end = start + 86400;
const initialSnapshot = dailySnapshots.findLast(
(s) => Number(s.timestamp) >= start && Number(s.timestamp) <= end
(s) => Number(s.date) >= start && Number(s.date) <= end
);
let lastPoolValue = calculatePoolValue({
isWeth0,
balances: initialSnapshot?.inputTokenBalances,
balances: snapshotToBalances(initialSnapshot),
vTokenToEth,
});

while (end <= periodEnd) {
const dailySnapshot = dailySnapshots.find(
(s) => Number(s.timestamp) >= start && Number(s.timestamp) <= end
(s) => Number(s.date) >= start && Number(s.date) <= end
);
let apr = 0n;

if (dailySnapshot) {
lastPoolValue = calculatePoolValue({
isWeth0,
vTokenToEth,
balances: dailySnapshot.inputTokenBalances,
balances: snapshotToBalances(dailySnapshot),
});

const poolValue = lastPoolValue;
const periodFees = parseEther(dailySnapshot.dailyTotalRevenueETH);
const periodFees = parseEther(dailySnapshot.feesETH);
if (periodFees && poolValue) {
apr = (periodFees * WeiPerEther * 365n) / poolValue;
if (apr < 0n) {
Expand Down Expand Up @@ -108,8 +122,8 @@ const calculateVaultFeeApr = ({
periodStart: number;
periodEnd: number;
dailySnapshots: Pick<
NftxV3Uniswap.LiquidityPoolDailySnapshot,
'timestamp' | 'inputTokenBalances'
NftxV3Uniswap.PoolDayData,
'date' | 'totalValueLockedToken0' | 'totalValueLockedToken1'
>[];
vaultFeeReceipts: Pick<VaultFeeReceipt, 'date' | 'amount'>[];
vTokenToEth: bigint;
Expand All @@ -121,17 +135,17 @@ const calculateVaultFeeApr = ({
let start = periodStart > createdAt ? periodStart : createdAt;
let end = start + 86400;
const initialSnapshot = dailySnapshots.findLast(
(s) => Number(s.timestamp) >= start && Number(s.timestamp) <= end
(s) => Number(s.date) >= start && Number(s.date) <= end
);
let lastPoolValue = calculatePoolValue({
isWeth0,
balances: initialSnapshot?.inputTokenBalances,
balances: snapshotToBalances(initialSnapshot),
vTokenToEth,
});

while (end <= periodEnd) {
const dailySnapshot = dailySnapshots.find(
(s) => Number(s.timestamp) >= start && Number(s.timestamp) <= end
(s) => Number(s.date) >= start && Number(s.date) <= end
);
const vaultFeeReceipt = vaultFeeReceipts.find(
(r) => r.date >= start && r.date <= end
Expand All @@ -142,7 +156,7 @@ const calculateVaultFeeApr = ({
lastPoolValue = calculatePoolValue({
isWeth0,
vTokenToEth,
balances: dailySnapshot.inputTokenBalances,
balances: snapshotToBalances(dailySnapshot),
});
}
const poolValue = lastPoolValue;
Expand Down Expand Up @@ -179,8 +193,8 @@ export default ({
}: {
createdAt: number;
dailySnapshots: Pick<
NftxV3Uniswap.LiquidityPoolDailySnapshot,
'timestamp' | 'dailyTotalRevenueETH' | 'inputTokenBalances'
NftxV3Uniswap.PoolDayData,
'date' | 'totalValueLockedToken0' | 'totalValueLockedToken1' | 'feesETH'
>[];
vaultFeeReceipts: VaultFeeReceipt[];
vTokenToEth: bigint;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/pools/fetchLiquidityPools/fetchPoolsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const makeFetchPoolsSet =
vaultIds,
});

const pools = data.liquidityPools.reduce((acc, pool) => {
const pools = data.pools.reduce((acc, pool) => {
const vault = getVaultByTokens({
inputTokens: pool.inputTokens,
vaults,
Expand All @@ -77,8 +77,8 @@ export const makeFetchPoolsSet =
}, [] as LiquidityPool[]);

let nextId: Address | undefined;
if (data.liquidityPools.length === 1000) {
nextId = data.liquidityPools.pop()?.id as Address;
if (data.pools.length === 1000) {
nextId = data.pools.pop()?.id as Address;
}

return [pools, nextId] as const;
Expand Down
Loading

0 comments on commit a9912b3

Please sign in to comment.