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

Commit

Permalink
fix(compound): Split compound lending balance helper (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich authored May 25, 2022
1 parent 7dd4568 commit e17f5ec
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ const getTotalsMeta = (balances: (TokenBalance | ContractPositionBalance)[]): Me
];
};

type Product = {
label: string;
assets: (AppTokenPositionBalance | ContractPositionBalance | NonFungibleTokenBalance)[];
meta?: MetadataItemWithLabel[];
};

export const presentBalanceFetcherResponse = (
products: {
label: string;
assets: (AppTokenPositionBalance | ContractPositionBalance | NonFungibleTokenBalance)[];
meta?: MetadataItemWithLabel[];
}[],
products: Product[],
meta: MetadataItemWithLabel[] = [],
): TokenBalanceResponse => {
// Exclude any asset groups that have negligible balances
const nonZeroBalanceProducts = products
Expand All @@ -67,8 +70,11 @@ export const presentBalanceFetcherResponse = (
meta: assetGroup.meta ?? [],
}));

const totalsMeta = getTotalsMeta(productsWithTotals.flatMap(assetGroup => assetGroup.assets));
const metaWithTotals = [...meta, ...totalsMeta];

return {
products: productsWithTotals,
meta: getTotalsMeta(productsWithTotals.flatMap(assetGroup => assetGroup.assets)),
meta: metaWithTotals,
};
};
41 changes: 26 additions & 15 deletions src/apps/b-protocol/adapters/compound.b-protocol-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Inject } from '@nestjs/common';
import { ZERO_ADDRESS } from '~app-toolkit/constants/address';
import { COMPOUND_DEFINITION } from '~apps/compound/compound.definition';
import { CompoundContractFactory } from '~apps/compound/contracts';
import { CompoundLendingBalanceHelper } from '~apps/compound/helper/compound.lending.balance-helper';
import { CompoundBorrowBalanceHelper } from '~apps/compound/helper/compound.borrow.balance-helper';
import { CompoundLendingMetaHelper } from '~apps/compound/helper/compound.lending.meta-helper';
import { CompoundSupplyBalanceHelper } from '~apps/compound/helper/compound.supply.balance-helper';
import { ProductItem } from '~balance/balance-fetcher.interface';
import { Network } from '~types/network.interface';

Expand All @@ -14,7 +15,8 @@ const network = Network.ETHEREUM_MAINNET;

export class CompoundBProtocolAdapter {
constructor(
@Inject(CompoundLendingBalanceHelper) private readonly compoundLendingBalanceHelper: CompoundLendingBalanceHelper,
@Inject(CompoundBorrowBalanceHelper) private readonly compoundBorrowBalanceHelper: CompoundBorrowBalanceHelper,
@Inject(CompoundSupplyBalanceHelper) private readonly compoundSupplyBalanceHelper: CompoundSupplyBalanceHelper,
@Inject(CompoundContractFactory) private readonly compoundContractFactory: CompoundContractFactory,
@Inject(CompoundLendingMetaHelper) private readonly compoundLendingMetaHelper: CompoundLendingMetaHelper,
@Inject(BProtocolContractFactory) private readonly bProtocolContractFactory: BProtocolContractFactory,
Expand All @@ -29,22 +31,31 @@ export class CompoundBProtocolAdapter {
const avatarAddress = await registry.avatarOf(address);
if (avatarAddress === ZERO_ADDRESS) return null;

const lendingBalances = await this.compoundLendingBalanceHelper.getBalances({
address: avatarAddress,
appId: COMPOUND_DEFINITION.id,
supplyGroupId: COMPOUND_DEFINITION.groups.supply.id,
borrowGroupId: COMPOUND_DEFINITION.groups.borrow.id,
network,
getTokenContract: ({ address, network }) => this.compoundContractFactory.compoundCToken({ address, network }),
getBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
getBorrowBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).borrowBalanceCurrent(address),
});

const meta = this.compoundLendingMetaHelper.getMeta({ balances: lendingBalances });
const [borrowBalances, supplyBalances] = await Promise.all([
this.compoundBorrowBalanceHelper.getBalances({
address: avatarAddress,
appId: COMPOUND_DEFINITION.id,
groupId: COMPOUND_DEFINITION.groups.borrow.id,
network,
getTokenContract: ({ address, network }) => this.compoundContractFactory.compoundCToken({ address, network }),
getBorrowBalanceRaw: ({ contract, address, multicall }) =>
multicall.wrap(contract).borrowBalanceCurrent(address),
}),
this.compoundSupplyBalanceHelper.getBalances({
address: avatarAddress,
appId: COMPOUND_DEFINITION.id,
groupId: COMPOUND_DEFINITION.groups.supply.id,
network,
getTokenContract: ({ address, network }) => this.compoundContractFactory.compoundCToken({ address, network }),
getBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
}),
]);

const meta = this.compoundLendingMetaHelper.getMeta({ balances: [...supplyBalances, ...borrowBalances] });

return {
label: 'Compound',
assets: lendingBalances,
assets: [...supplyBalances, ...borrowBalances],
meta,
};
}
Expand Down
9 changes: 6 additions & 3 deletions src/apps/compound/compound.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { EthereumCompoundBalanceFetcher } from './ethereum/compound.balance-fetc
import { EthereumCompoundBorrowContractPositionFetcher } from './ethereum/compound.borrow.contract-position-fetcher';
import { EthereumCompoundSupplyTokenFetcher } from './ethereum/compound.supply.token-fetcher';
import { EthereumCompoundTvlFetcher } from './ethereum/coumpound.tvl-fetcher';
import { CompoundBorrowBalanceHelper } from './helper/compound.borrow.balance-helper';
import { CompoundBorrowContractPositionHelper } from './helper/compound.borrow.contract-position-helper';
import { CompoundClaimableBalanceHelper } from './helper/compound.claimable.balance-helper';
import { CompoundLendingBalanceHelper } from './helper/compound.lending.balance-helper';
import { CompoundLendingMetaHelper } from './helper/compound.lending.meta-helper';
import { CompoundSupplyBalanceHelper } from './helper/compound.supply.balance-helper';
import { CompoundSupplyTokenHelper } from './helper/compound.supply.token-helper';
import { CompoundTvlHelper } from './helper/compound.tvl-helper';

Expand All @@ -25,19 +26,21 @@ import { CompoundTvlHelper } from './helper/compound.tvl-helper';
EthereumCompoundTvlFetcher,
// Helpers
CompoundClaimableBalanceHelper,
CompoundLendingBalanceHelper,
CompoundLendingMetaHelper,
CompoundSupplyTokenHelper,
CompoundSupplyBalanceHelper,
CompoundBorrowContractPositionHelper,
CompoundBorrowBalanceHelper,
CompoundContractFactory,
CompoundTvlHelper,
],
exports: [
CompoundClaimableBalanceHelper,
CompoundLendingBalanceHelper,
CompoundLendingMetaHelper,
CompoundSupplyTokenHelper,
CompoundSupplyBalanceHelper,
CompoundBorrowContractPositionHelper,
CompoundBorrowBalanceHelper,
CompoundContractFactory,
CompoundTvlHelper,
],
Expand Down
48 changes: 27 additions & 21 deletions src/apps/compound/ethereum/compound.balance-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import { Network } from '~types/network.interface';

import { COMPOUND_DEFINITION } from '../compound.definition';
import { CompoundContractFactory } from '../contracts';
import { CompoundBorrowBalanceHelper } from '../helper/compound.borrow.balance-helper';
import { CompoundClaimableBalanceHelper } from '../helper/compound.claimable.balance-helper';
import { CompoundLendingBalanceHelper } from '../helper/compound.lending.balance-helper';
import { CompoundLendingMetaHelper } from '../helper/compound.lending.meta-helper';
import { CompoundSupplyBalanceHelper } from '../helper/compound.supply.balance-helper';

const appId = COMPOUND_DEFINITION.id;
const network = Network.ETHEREUM_MAINNET;

@Register.BalanceFetcher(appId, network)
export class EthereumCompoundBalanceFetcher implements BalanceFetcher {
constructor(
@Inject(CompoundLendingBalanceHelper)
private readonly compoundLendingBalanceHelper: CompoundLendingBalanceHelper,
@Inject(CompoundBorrowBalanceHelper)
private readonly compoundBorrowBalanceHelper: CompoundBorrowBalanceHelper,
@Inject(CompoundSupplyBalanceHelper)
private readonly compoundSupplyBalanceHelper: CompoundSupplyBalanceHelper,
@Inject(CompoundClaimableBalanceHelper)
private readonly compoundClaimableBalanceHelper: CompoundClaimableBalanceHelper,
@Inject(CompoundLendingMetaHelper)
Expand All @@ -27,15 +30,24 @@ export class EthereumCompoundBalanceFetcher implements BalanceFetcher {
private readonly compoundContractFactory: CompoundContractFactory,
) {}

async getLendingBalances(address: string) {
return this.compoundLendingBalanceHelper.getBalances({
async getSupplyBalances(address: string) {
return this.compoundSupplyBalanceHelper.getBalances({
address,
appId,
supplyGroupId: COMPOUND_DEFINITION.groups.supply.id,
borrowGroupId: COMPOUND_DEFINITION.groups.borrow.id,
groupId: COMPOUND_DEFINITION.groups.supply.id,
network,
getTokenContract: ({ address, network }) => this.compoundContractFactory.compoundCToken({ address, network }),
getBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
});
}

async getBorrowBalances(address: string) {
return this.compoundBorrowBalanceHelper.getBalances({
address,
appId,
groupId: COMPOUND_DEFINITION.groups.borrow.id,
network,
getTokenContract: ({ address, network }) => this.compoundContractFactory.compoundCToken({ address, network }),
getBorrowBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).borrowBalanceCurrent(address),
});
}
Expand All @@ -53,23 +65,17 @@ export class EthereumCompoundBalanceFetcher implements BalanceFetcher {
}

async getBalances(address: string) {
const [lendingBalances, claimableBalances] = await Promise.all([
this.getLendingBalances(address),
const [supplyBalances, borrowBalances, claimableBalances] = await Promise.all([
this.getSupplyBalances(address),
this.getBorrowBalances(address),
this.getClaimableBalances(address),
]);

const meta = this.compoundLendingMetaHelper.getMeta({ balances: lendingBalances });
const supplyProduct = { label: 'Supply', assets: supplyBalances };
const borrowProduct = { label: 'Borrow', assets: borrowBalances };
const claimableProduct = { label: 'Claimable', assets: claimableBalances };
const meta = this.compoundLendingMetaHelper.getMeta({ balances: [...supplyBalances, ...borrowBalances] });

return presentBalanceFetcherResponse([
{
label: 'Lending',
assets: lendingBalances,
meta: meta,
},
{
label: 'Claimable',
assets: claimableBalances,
},
]);
return presentBalanceFetcherResponse([supplyProduct, borrowProduct, claimableProduct], meta);
}
}
53 changes: 53 additions & 0 deletions src/apps/compound/helper/compound.borrow.balance-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Inject, Injectable } from '@nestjs/common';
import { BigNumberish } from 'ethers';

import { drillBalance } from '~app-toolkit';
import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
import { EthersMulticall as Multicall } from '~multicall/multicall.ethers';
import { Network } from '~types/network.interface';

import { CompoundCToken } from '../contracts';

import { CompoundSupplyTokenDataProps } from './compound.supply.token-helper';

type CompoundBorrowBalanceHelperParams<T> = {
address: string;
network: Network;
appId: string;
groupId: string;
getTokenContract: (opts: { address: string; network: Network }) => T;
getBorrowBalanceRaw: (opts: { contract: T; multicall: Multicall; address: string }) => Promise<BigNumberish>;
};

@Injectable()
export class CompoundBorrowBalanceHelper {
constructor(@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit) {}

async getBalances<T = CompoundCToken>({
address,
network,
appId,
groupId,
getTokenContract,
getBorrowBalanceRaw,
}: CompoundBorrowBalanceHelperParams<T>) {
const multicall = this.appToolkit.getMulticall(network);

const borrowPositions = await this.appToolkit.getAppContractPositions<CompoundSupplyTokenDataProps>({
appId,
groupIds: [groupId],
network,
});

const borrowPositionBalances = await Promise.all(
borrowPositions.map(async borrowPosition => {
const borrowContract = getTokenContract({ address: borrowPosition.address, network });
const balanceRaw = await getBorrowBalanceRaw({ contract: borrowContract, multicall, address });
const tokens = [drillBalance(borrowPosition.tokens[0], balanceRaw.toString(), { isDebt: true })];
return { ...borrowPosition, tokens, balanceUSD: tokens[0].balanceUSD };
}),
);

return borrowPositionBalances;
}
}
73 changes: 0 additions & 73 deletions src/apps/compound/helper/compound.lending.balance-helper.ts

This file was deleted.

Loading

0 comments on commit e17f5ec

Please sign in to comment.