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

Commit

Permalink
Merge branch 'main' into remove-overrides-of-get-balance-per-token
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich authored Oct 18, 2023
2 parents 6b93954 + 5c5ff57 commit a973eac
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 212 deletions.
60 changes: 0 additions & 60 deletions src/apps/euler/common/euler.e-token.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<RawTokenBalance[]> {
const multicall = this.appToolkit.getMulticall(this.network);
const appTokens = await this.appToolkit.getAppTokenPositions<DefaultAppTokenDataProps>({
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<AppTokenPositionBalance<DefaultAppTokenDataProps>[]> {
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<typeof token, DefaultAppTokenDataProps>(token, tokenBalance.balance, {
isDebt: this.isDebt,
});

const underlyingToken = drillBalance<typeof token, DefaultAppTokenDataProps>(
appTokens[0],
underlyingTokenBalance.balance,
);

return { ...result, tokens: [underlyingToken] };
});

return _.compact(appTokenBalances);
}
}
57 changes: 6 additions & 51 deletions src/apps/meshswap/polygon/meshswap.supply.token-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshswapSinglePool> {
groupLabel = 'Supply';

constructor(
Expand Down Expand Up @@ -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<MeshswapSinglePool>): Promise<number[]> {
const exchangeRateRaw = await contract.exchangeRateStored();
const exchangeRate = Number(exchangeRateRaw) / 10 ** 18;
return [exchangeRate];
}

async getLabel({ contract }: GetDisplayPropsParams<MeshswapSinglePool>): Promise<string> {
Expand All @@ -76,41 +68,4 @@ export class PolygonMeshswapSupplyTokenFetcher extends AppTokenTemplatePositionF

return borrowAmount + cash;
}

async getDataProps(
params: GetDataPropsParams<MeshswapSinglePool, MeshswapContractPositionDataProps>,
): Promise<MeshswapContractPositionDataProps> {
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<MeshswapSinglePool>): Promise<StatsItem[] | undefined> {
const { liquidity } = appToken.dataProps;

return [{ label: 'Liquidity', value: buildDollarDisplayItem(liquidity) }];
}

async getRawBalances(address: string): Promise<RawTokenBalance[]> {
const multicall = this.appToolkit.getMulticall(this.network);

const appTokens = await this.appToolkit.getAppTokenPositions<MeshswapContractPositionDataProps>({
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(),
};
}),
);
}
}
27 changes: 0 additions & 27 deletions src/apps/rari-fuse/common/rari-fuse.supply.token-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -144,30 +143,4 @@ export abstract class RariFuseSupplyTokenFetcher<
async getBalanceDisplayMode() {
return BalanceDisplayMode.UNDERLYING;
}

async getRawBalances(address: string): Promise<RawTokenBalance[]> {
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<RariFuseSupplyTokenDataProps>({
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(),
};
}),
);
}
}
74 changes: 0 additions & 74 deletions src/apps/uniswap-v2/ethereum/uniswap-v2.pool.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -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?: {
Expand Down Expand Up @@ -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<UniswapV2TokenDataProps>({
appId: this.appId,
network: this.network,
groupIds: [this.groupId],
});
}

async getBalances(_address: string): Promise<AppTokenPositionBalance<UniswapV2TokenDataProps>[]> {
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<UniswapV2BalancesData>({
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<UniswapV2TokenDataProps>[];
}

async getRawBalances(_address: string): Promise<RawAppTokenBalance[]> {
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<UniswapV2BalancesData>({
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;
}
}

0 comments on commit a973eac

Please sign in to comment.