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

feat(iron-bank): added iron bank to Zapper #617

Merged
merged 5 commits into from
Jun 16, 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
62 changes: 62 additions & 0 deletions src/apps/iron-bank/avalanche/iron-bank.balance-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Inject } from '@nestjs/common';

import { Register } from '~app-toolkit/decorators';
import { presentBalanceFetcherResponse } from '~app-toolkit/helpers/presentation/balance-fetcher-response.present';
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 { BalanceFetcher } from '~balance/balance-fetcher.interface';
import { Network } from '~types/network.interface';

import { IronBankContractFactory } from '../contracts';
import { IRON_BANK_DEFINITION } from '../iron-bank.definition';

const appId = IRON_BANK_DEFINITION.id;
const network = Network.AVALANCHE_MAINNET;

@Register.BalanceFetcher(appId, network)
export class AvalancheIronBankBalanceFetcher implements BalanceFetcher {
constructor(
@Inject(CompoundBorrowBalanceHelper)
private readonly compoundBorrowBalanceHelper: CompoundBorrowBalanceHelper,
@Inject(CompoundLendingMetaHelper)
private readonly compoundLendingMetaHelper: CompoundLendingMetaHelper,
@Inject(CompoundSupplyBalanceHelper)
private readonly compoundSupplyBalanceHelper: CompoundSupplyBalanceHelper,
@Inject(IronBankContractFactory)
private readonly ironBankContractFactory: IronBankContractFactory,
) {}
async getSupplyBalances(address: string) {
return this.compoundSupplyBalanceHelper.getBalances({
address,
appId,
groupId: IRON_BANK_DEFINITION.groups.supply.id,
network,
getTokenContract: ({ address, network }) => this.ironBankContractFactory.ironBankCToken({ address, network }),
getBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
});
}

async getBorrowBalances(address: string) {
return this.compoundBorrowBalanceHelper.getBalances({
address,
appId,
groupId: IRON_BANK_DEFINITION.groups.borrow.id,
network,
getTokenContract: ({ address, network }) => this.ironBankContractFactory.ironBankCToken({ address, network }),
getBorrowBalanceRaw: ({ contract, address, multicall }) => multicall.wrap(contract).borrowBalanceCurrent(address),
});
}

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

const meta = this.compoundLendingMetaHelper.getMeta({ balances: [...supplyBalances, ...borrowBalances] });
const lendingProduct = { label: 'Lending', assets: [...supplyBalances, ...borrowBalances], meta };

return presentBalanceFetcherResponse([lendingProduct]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Inject } from '@nestjs/common';

import { Register } from '~app-toolkit/decorators';
import { CompoundBorrowContractPositionHelper } from '~apps/compound/helper/compound.borrow.contract-position-helper';
import { PositionFetcher } from '~position/position-fetcher.interface';
import { ContractPosition } from '~position/position.interface';
import { Network } from '~types/network.interface';

import { IRON_BANK_DEFINITION } from '../iron-bank.definition';

const appId = IRON_BANK_DEFINITION.id;
const groupId = IRON_BANK_DEFINITION.groups.borrow.id;
const network = Network.AVALANCHE_MAINNET;

@Register.ContractPositionFetcher({ appId, groupId, network })
export class AvalancheIronBankBorrowContractPositionFetcher implements PositionFetcher<ContractPosition> {
constructor(
@Inject(CompoundBorrowContractPositionHelper)
private readonly compoundBorrowContractPositionHelper: CompoundBorrowContractPositionHelper,
) {}

async getPositions() {
return this.compoundBorrowContractPositionHelper.getPositions({
network,
appId,
groupId,
supplyGroupId: IRON_BANK_DEFINITION.groups.supply.id,
});
}
}
40 changes: 40 additions & 0 deletions src/apps/iron-bank/avalanche/iron-bank.supply.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Inject } from '@nestjs/common';

import { Register } from '~app-toolkit/decorators';
import { CompoundSupplyTokenHelper } from '~apps/compound/helper/compound.supply.token-helper';
import { PositionFetcher } from '~position/position-fetcher.interface';
import { AppTokenPosition } from '~position/position.interface';
import { Network } from '~types/network.interface';

import { IronBankContractFactory } from '../contracts';
import { IRON_BANK_DEFINITION } from '../iron-bank.definition';

const appId = IRON_BANK_DEFINITION.id;
const groupId = IRON_BANK_DEFINITION.groups.supply.id;
const network = Network.AVALANCHE_MAINNET;

@Register.TokenPositionFetcher({ appId, groupId, network })
export class AvalancheIronBankSupplyTokenFetcher implements PositionFetcher<AppTokenPosition> {
constructor(
@Inject(IronBankContractFactory) private readonly ironBankContractFactory: IronBankContractFactory,
@Inject(CompoundSupplyTokenHelper) private readonly compoundSupplyTokenHelper: CompoundSupplyTokenHelper,
) {}

async getPositions() {
return this.compoundSupplyTokenHelper.getTokens({
network,
appId,
groupId,
comptrollerAddress: '0x2ee80614ccbc5e28654324a66a396458fa5cd7cc',
getComptrollerContract: ({ address, network }) =>
this.ironBankContractFactory.ironBankComptroller({ address, network }),
getTokenContract: ({ address, network }) => this.ironBankContractFactory.ironBankCToken({ address, network }),
getAllMarkets: ({ contract, multicall }) => multicall.wrap(contract).getAllMarkets(),
getExchangeRate: ({ contract, multicall }) => multicall.wrap(contract).exchangeRateCurrent(),
getSupplyRate: ({ contract, multicall }) => multicall.wrap(contract).supplyRatePerBlock(),
getBorrowRate: ({ contract, multicall }) => multicall.wrap(contract).borrowRatePerBlock(),
getUnderlyingAddress: ({ contract, multicall }) => multicall.wrap(contract).underlying(),
getExchangeRateMantissa: ({ underlyingTokenDecimals }) => underlyingTokenDecimals + 10,
});
}
}
30 changes: 30 additions & 0 deletions src/apps/iron-bank/avalanche/iron-bank.tvl-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Inject } from '@nestjs/common';

import { Register } from '~app-toolkit/decorators';
import { CompoundTvlHelper } from '~apps/compound/helper/compound.tvl-helper';
import { TvlFetcher } from '~stats/tvl/tvl-fetcher.interface';
import { Network } from '~types/network.interface';

import { IRON_BANK_DEFINITION } from '../iron-bank.definition';

const appId = IRON_BANK_DEFINITION.id;
const network = Network.AVALANCHE_MAINNET;

@Register.TvlFetcher({ appId, network })
export class AvalancheIronBankTvlFetcher implements TvlFetcher {
constructor(@Inject(CompoundTvlHelper) private readonly compoundTvlHelper: CompoundTvlHelper) {}

async getTvl() {
const totalSupplyBasedOnBorrowedPositions = await this.compoundTvlHelper.getTotalSupplyBasedOnBorrowedPositions({
appId,
groupIds: [IRON_BANK_DEFINITION.groups.borrow.id],
network,
});
const liquidityBasedOnBorrowedPositions = await this.compoundTvlHelper.getLiquidityBasedOnBorrowedPositions({
appId,
groupIds: [IRON_BANK_DEFINITION.groups.borrow.id],
network,
});
return totalSupplyBasedOnBorrowedPositions + liquidityBasedOnBorrowedPositions;
}
}
Loading