Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

fix(curve): Fix missing Curve farm balances on Arbitrum #533

Merged
merged 2 commits into from
May 28, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ArbitrumCurveFarmContractPositionFetcher implements PositionFetcher
appId,
groupId,
dependencies: [{ appId: CURVE_DEFINITION.id, groupIds: [CURVE_DEFINITION.groups.pool.id], network }],
resolveImplementation: () => 'rewards-only-gauge',
resolveFarmAddresses: () => definitions.map(v => v.gaugeAddress ?? null),
resolveFarmContract: ({ address, network }) =>
this.curveContractFactory.curveRewardsOnlyGauge({ address, network }),
Expand Down
25 changes: 11 additions & 14 deletions src/apps/curve/helpers/curve.pool.token-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@nestjs/common';
import { BigNumber, BigNumberish } from 'ethers';
import { compact, isUndefined } from 'lodash';
import { compact } from 'lodash';

import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
import { ETH_ADDR_ALIAS, ZERO_ADDRESS } from '~app-toolkit/constants/address';
Expand Down Expand Up @@ -101,19 +101,18 @@ export class CurvePoolTokenHelper {
const tokenAddresses = rawTokenAddresses.map(v => (v === ETH_ADDR_ALIAS ? ZERO_ADDRESS : v.toLowerCase()));
const reservesRaw = await resolvePoolReserves({ multicall, poolContract });

const tokens = tokenAddresses.map(tokenAddress => {
const maybeTokens = tokenAddresses.map(tokenAddress => {
const baseToken = baseTokens.find(price => price.address === tokenAddress);
const appToken = appTokens.find(p => p.address === tokenAddress);
const curveToken = baseCurveTokens.find(p => p.address === tokenAddress);
return curveToken ?? appToken ?? baseToken;
});

// If any underlying token is missing, do not display this pool
const isMissingUnderlyingTokens = tokens.some(isUndefined);
const tokens = compact(maybeTokens);
const isMissingUnderlyingTokens = tokens.length !== maybeTokens.length;
if (isMissingUnderlyingTokens) return null;

const sanitizedTokens = tokens as NonNullable<typeof tokens[number]>[];

const [symbol, supplyRaw, feeRaw] = await Promise.all([
resolvePoolTokenSymbol({ multicall, poolTokenContract }),
resolvePoolTokenSupply({ multicall, poolTokenContract }),
Expand All @@ -125,26 +124,24 @@ export class CurvePoolTokenHelper {
const fee = Number(feeRaw) / 10 ** 10;
if (supply === 0) return null;

const reserves = reservesRaw.map((r, i) => Number(r) / 10 ** sanitizedTokens[i].decimals);
const underlyingTokens = sanitizedTokens.flatMap(v => (v.type === ContractType.BASE_TOKEN ? v : v.tokens));
const reserves = reservesRaw.map((r, i) => Number(r) / 10 ** tokens[i].decimals);
const underlyingTokens = tokens.flatMap(v => (v.type === ContractType.BASE_TOKEN ? v : v.tokens));
const price = await resolvePoolTokenPrice({
tokens: sanitizedTokens,
tokens,
reserves,
poolContract,
multicall,
supply,
});
const volume = resolvePoolVolume
? await resolvePoolVolume({ definition, poolContract, tokens: sanitizedTokens, price })
: 0;
const volume = resolvePoolVolume ? await resolvePoolVolume({ definition, poolContract, tokens, price }) : 0;

const pricePerShare = reserves.map(r => r / supply);
const reservesUSD = sanitizedTokens.map((t, i) => reserves[i] * t.price);
const reservesUSD = tokens.map((t, i) => reserves[i] * t.price);
const liquidity = reservesUSD.reduce((total, r) => total + r, 0);
const reservePercentages = reservesUSD.map(reserveUSD => reserveUSD / liquidity);

// Display Properties
const underlyingLabels = sanitizedTokens.map(v => (isMetaPool(v) ? getLabelFromToken(v) : v.symbol)); // Flatten metapool label
const underlyingLabels = tokens.map(v => (isMetaPool(v) ? getLabelFromToken(v) : v.symbol)); // Flatten metapool label
const label = definition.label ?? underlyingLabels.join(' / ');
const secondaryLabel = reservePercentages.map(p => `${Math.floor(p * 100)}%`).join(' / ');
const images = underlyingTokens.map(t => getImagesFromToken(t)).flat();
Expand All @@ -160,7 +157,7 @@ export class CurvePoolTokenHelper {
supply,
price,
pricePerShare,
tokens: sanitizedTokens,
tokens,

dataProps: {
swapAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TokenService {

@Cache({ key: (network: Network) => `token-prices:${network}`, ttl: 60 })
async getTokenPrices(network: Network) {
const { data: tokenPrices } = await this.axios.get<BaseToken[]>('/v1/prices', { params: { network } });
const { data: tokenPrices } = await this.axios.get<BaseToken[]>('/v2/prices', { params: { network } });
return tokenPrices;
}

Expand Down