diff --git a/src/apps/euler/common/euler.e-token.token-fetcher.ts b/src/apps/euler/common/euler.e-token.token-fetcher.ts index d9d1e1953..a37c170b0 100644 --- a/src/apps/euler/common/euler.e-token.token-fetcher.ts +++ b/src/apps/euler/common/euler.e-token.token-fetcher.ts @@ -1,10 +1,7 @@ import { Inject } from '@nestjs/common'; import { ethers } from 'ethers'; -import _ from 'lodash'; import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface'; -import { drillBalance } from '~app-toolkit/helpers/drill-balance.helper'; -import { AppTokenPositionBalance, RawTokenBalance } from '~position/position-balance.interface'; import { AppTokenTemplatePositionFetcher } from '~position/template/app-token.template.position-fetcher'; import { GetUnderlyingTokensParams, @@ -77,61 +74,4 @@ export abstract class EulerETokenTokenFetcher extends AppTokenTemplatePositionFe const market = await this.tokenDefinitionsResolver.getMarket(appToken.address, this.tokenType); return (Number(market!.supplyAPY) * 100) / 1e27; } - - async getRawBalances(address: string): Promise { - const multicall = this.appToolkit.getMulticall(this.network); - const appTokens = await this.appToolkit.getAppTokenPositions({ - appId: this.appId, - network: this.network, - groupIds: [this.groupId], - }); - - return ( - await Promise.all( - appTokens.map(async appToken => { - const balanceRaw = await this.getBalancePerToken({ multicall, address, appToken }); - const underlyingBalance = await multicall - .wrap(this.getContract(appToken.address)) - .convertBalanceToUnderlying(balanceRaw); - - return [ - { - key: this.appToolkit.getPositionKey(appToken), - balance: (await this.getBalancePerToken({ multicall, address, appToken })).toString(), - }, - { - key: `${this.appToolkit.getPositionKey(appToken)}-underlying`, - balance: underlyingBalance.toString(), - }, - ]; - }), - ) - ).flat(); - } - - async drillRawBalances(balances: RawTokenBalance[]): Promise[]> { - const appTokens = await this.getPositionsForBalances(); - - const appTokenBalances = appTokens.map(token => { - const tokenBalance = balances.find(b => b.key === this.appToolkit.getPositionKey(token)); - const underlyingTokenBalance = balances.find( - b => b.key === `${this.appToolkit.getPositionKey(token)}-underlying`, - ); - - if (!tokenBalance || !underlyingTokenBalance) return null; - - const result = drillBalance(token, tokenBalance.balance, { - isDebt: this.isDebt, - }); - - const underlyingToken = drillBalance( - appTokens[0], - underlyingTokenBalance.balance, - ); - - return { ...result, tokens: [underlyingToken] }; - }); - - return _.compact(appTokenBalances); - } } diff --git a/src/apps/meshswap/polygon/meshswap.supply.token-fetcher.ts b/src/apps/meshswap/polygon/meshswap.supply.token-fetcher.ts index f420f009e..7471137b8 100644 --- a/src/apps/meshswap/polygon/meshswap.supply.token-fetcher.ts +++ b/src/apps/meshswap/polygon/meshswap.supply.token-fetcher.ts @@ -3,28 +3,18 @@ import _ from 'lodash'; import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface'; import { PositionTemplate } from '~app-toolkit/decorators/position-template.decorator'; -import { buildDollarDisplayItem } from '~app-toolkit/helpers/presentation/display-item.present'; -import { StatsItem } from '~position/display.interface'; -import { RawTokenBalance } from '~position/position-balance.interface'; import { AppTokenTemplatePositionFetcher } from '~position/template/app-token.template.position-fetcher'; import { - DefaultAppTokenDataProps, GetDataPropsParams, GetDisplayPropsParams, + GetPricePerShareParams, GetUnderlyingTokensParams, } from '~position/template/app-token.template.types'; import { MeshswapContractFactory, MeshswapSinglePool } from '../contracts'; -export type MeshswapContractPositionDataProps = DefaultAppTokenDataProps & { - exchangeRate: number; -}; - @PositionTemplate() -export class PolygonMeshswapSupplyTokenFetcher extends AppTokenTemplatePositionFetcher< - MeshswapSinglePool, - MeshswapContractPositionDataProps -> { +export class PolygonMeshswapSupplyTokenFetcher extends AppTokenTemplatePositionFetcher { groupLabel = 'Supply'; constructor( @@ -60,8 +50,10 @@ export class PolygonMeshswapSupplyTokenFetcher extends AppTokenTemplatePositionF return [{ address: await contract.token(), network: this.network }]; } - async getPricePerShare() { - return [1]; + async getPricePerShare({ contract }: GetPricePerShareParams): Promise { + const exchangeRateRaw = await contract.exchangeRateStored(); + const exchangeRate = Number(exchangeRateRaw) / 10 ** 18; + return [exchangeRate]; } async getLabel({ contract }: GetDisplayPropsParams): Promise { @@ -76,41 +68,4 @@ export class PolygonMeshswapSupplyTokenFetcher extends AppTokenTemplatePositionF return borrowAmount + cash; } - - async getDataProps( - params: GetDataPropsParams, - ): Promise { - const defaultDataProps = await super.getDataProps(params); - const exchangeRateRaw = await params.contract.exchangeRateStored(); - const exchangeRate = Number(exchangeRateRaw) / 10 ** 18; - return { ...defaultDataProps, exchangeRate }; - } - - async getStatsItems({ appToken }: GetDisplayPropsParams): Promise { - const { liquidity } = appToken.dataProps; - - return [{ label: 'Liquidity', value: buildDollarDisplayItem(liquidity) }]; - } - - async getRawBalances(address: string): Promise { - const multicall = this.appToolkit.getMulticall(this.network); - - const appTokens = await this.appToolkit.getAppTokenPositions({ - appId: this.appId, - network: this.network, - groupIds: [this.groupId], - }); - - return Promise.all( - appTokens.map(async appToken => { - const balanceRaw = await multicall.wrap(this.getContract(appToken.address)).balanceOf(address); - const balance = Number(balanceRaw) * appToken.dataProps.exchangeRate; - - return { - key: this.appToolkit.getPositionKey(appToken), - balance: balance.toString(), - }; - }), - ); - } } diff --git a/src/apps/rari-fuse/common/rari-fuse.supply.token-fetcher.ts b/src/apps/rari-fuse/common/rari-fuse.supply.token-fetcher.ts index 4c12c380d..e08afdb7e 100644 --- a/src/apps/rari-fuse/common/rari-fuse.supply.token-fetcher.ts +++ b/src/apps/rari-fuse/common/rari-fuse.supply.token-fetcher.ts @@ -8,7 +8,6 @@ import { getLabelFromToken } from '~app-toolkit/helpers/presentation/image.prese import { IMulticallWrapper } from '~multicall'; import { isMulticallUnderlyingError } from '~multicall/multicall.ethers'; import { BalanceDisplayMode } from '~position/display.interface'; -import { RawTokenBalance } from '~position/position-balance.interface'; import { AppTokenTemplatePositionFetcher } from '~position/template/app-token.template.position-fetcher'; import { GetUnderlyingTokensParams, @@ -144,30 +143,4 @@ export abstract class RariFuseSupplyTokenFetcher< async getBalanceDisplayMode() { return BalanceDisplayMode.UNDERLYING; } - - async getRawBalances(address: string): Promise { - const lens = this.getLensContract(this.lensAddress); - const poolsBySupplier = await this.getPoolsBySupplier(address, lens); - const participatedComptrollers = poolsBySupplier[1].map(t => t.comptroller.toLowerCase()); - - const multicall = this.appToolkit.getMulticall(this.network); - const appTokens = await this.appToolkit.getAppTokenPositions({ - appId: this.appId, - network: this.network, - groupIds: [this.groupId], - }); - - return Promise.all( - appTokens - .filter(v => participatedComptrollers.includes(v.dataProps.comptroller)) - .map(async appToken => { - const balanceRaw = await this.getBalancePerToken({ multicall, address, appToken }); - - return { - key: this.appToolkit.getPositionKey(appToken), - balance: balanceRaw.toString(), - }; - }), - ); - } } diff --git a/src/apps/uniswap-v2/ethereum/uniswap-v2.pool.token-fetcher.ts b/src/apps/uniswap-v2/ethereum/uniswap-v2.pool.token-fetcher.ts index fb99b6630..cd4c7ed9c 100644 --- a/src/apps/uniswap-v2/ethereum/uniswap-v2.pool.token-fetcher.ts +++ b/src/apps/uniswap-v2/ethereum/uniswap-v2.pool.token-fetcher.ts @@ -1,15 +1,8 @@ import { gql } from 'graphql-request'; -import { chunk, compact } from 'lodash'; -import { ZERO_ADDRESS } from '~app-toolkit/constants/address'; import { PositionTemplate } from '~app-toolkit/decorators/position-template.decorator'; -import { drillBalance } from '~app-toolkit/helpers/drill-balance.helper'; -import { gqlFetch } from '~app-toolkit/helpers/the-graph.helper'; -import { AppTokenPositionBalance, RawAppTokenBalance } from '~position/position-balance.interface'; -import { isAppToken } from '~position/position.interface'; import { UniswapV2DefaultPoolSubgraphTemplateTokenFetcher } from '../common/uniswap-v2.default.subgraph.template.token-fetcher'; -import { UniswapV2TokenDataProps } from '../common/uniswap-v2.pool.on-chain.template.token-fetcher'; type UniswapV2BalancesData = { user?: { @@ -43,71 +36,4 @@ export class EthereumUniswapV2PoolTokenFetcher extends UniswapV2DefaultPoolSubgr first = 5000; // Todo: remove when subgraph isn't throwing when calling volumeUSD skipVolume = true; - - async getPositionsForBalances() { - return this.appToolkit.getAppTokenPositionsFromDatabase({ - appId: this.appId, - network: this.network, - groupIds: [this.groupId], - }); - } - - async getBalances(_address: string): Promise[]> { - const multicall = this.appToolkit.getMulticall(this.network); - const tokenLoader = this.appToolkit.getTokenDependencySelector(); - const address = await this.getAccountAddress(_address); - if (address === ZERO_ADDRESS) return []; - - // Use the subgraph to determine holdings. Later, we'll optimize this to use our own holdings by default. - const data = await gqlFetch({ - endpoint: this.subgraphUrl, - query: UNISWAP_V2_BALANCES_QUERY, - variables: { address: address.toLowerCase() }, - }); - - const heldTokenAddresses = data.user?.liquidityPositions.map(v => v.pair.id.toLowerCase()) ?? []; - const heldTokens = await tokenLoader.getMany(heldTokenAddresses.map(t => ({ address: t, network: this.network }))); - const sanitized = compact(heldTokens).filter(isAppToken); - - const balances = await Promise.all( - sanitized.map(async appToken => { - const balanceRaw = await this.getBalancePerToken({ multicall, address, appToken }); - const tokenBalance = drillBalance(appToken, balanceRaw.toString(), { isDebt: this.isDebt }); - return tokenBalance; - }), - ); - - return balances as AppTokenPositionBalance[]; - } - - async getRawBalances(_address: string): Promise { - const multicall = this.appToolkit.getMulticall(this.network); - const tokenLoader = this.appToolkit.getTokenDependencySelector(); - const address = await this.getAccountAddress(_address); - if (address === ZERO_ADDRESS) return []; - - // Use the subgraph to determine holdings. Later, we'll optimize this to use our own holdings by default. - const data = await gqlFetch({ - endpoint: this.subgraphUrl, - query: UNISWAP_V2_BALANCES_QUERY, - variables: { address: address.toLowerCase() }, - }); - - const heldTokenAddresses = data.user?.liquidityPositions.map(v => v.pair.id.toLowerCase()) ?? []; - const heldTokens = await tokenLoader.getMany(heldTokenAddresses.map(t => ({ address: t, network: this.network }))); - const sanitized = compact(heldTokens).filter(isAppToken); - - let results: RawAppTokenBalance[] = []; - for (const batch of chunk(sanitized, 100).values()) { - results = results.concat( - await Promise.all( - batch.map(async appToken => ({ - key: this.appToolkit.getPositionKey(appToken), - balance: (await this.getBalancePerToken({ multicall, address, appToken })).toString(), - })), - ), - ); - } - return results; - } }