From a411099f6d495abd7f967700c8d2340fe8f1dd0d Mon Sep 17 00:00:00 2001 From: nymmrx Date: Thu, 8 Jul 2021 17:09:21 +0200 Subject: [PATCH] feat(vault): add user metadata and user summary (#54) --- examples/ironbank.mjs | 8 ++--- src/interfaces/earnings.ts | 50 +++++-------------------------- src/interfaces/ironbank.ts | 13 ++++---- src/interfaces/vault.ts | 24 +++++++++++++++ src/services/adapters/ironbank.ts | 8 ++--- src/types/common.ts | 2 +- src/types/custom/earnings.ts | 42 ++++++++++++++++++++++++++ src/types/custom/ironbank.ts | 2 +- src/types/custom/vault.ts | 13 +++++++- 9 files changed, 100 insertions(+), 62 deletions(-) diff --git a/examples/ironbank.mjs b/examples/ironbank.mjs index 188b0bee..8053c9e3 100644 --- a/examples/ironbank.mjs +++ b/examples/ironbank.mjs @@ -28,12 +28,12 @@ async function main() { console.log("Yearn Multisig general IronBank position:"); console.log(ironBankGeneralPositionTable.toString()); - const ironBankUserMetadataTable = new Table(); - const ironBankUserMetadata = await yearn.ironBank.userMetadata(YearnGovernance); + const CyTokenUserMetadataTable = new Table(); + const CyTokenUserMetadata = await yearn.ironBank.userMetadata(YearnGovernance); - ironBankUserMetadataTable.push(...ironBankUserMetadata.map(market => Object.values(market))); + CyTokenUserMetadataTable.push(...CyTokenUserMetadata.map(market => Object.values(market))); console.log("Yearn Multisig IronBank user metadata:"); - console.log(ironBankUserMetadataTable.toString()); + console.log(CyTokenUserMetadataTable.toString()); } main(); diff --git a/src/interfaces/earnings.ts b/src/interfaces/earnings.ts index 4c911f75..1e0cdec0 100644 --- a/src/interfaces/earnings.ts +++ b/src/interfaces/earnings.ts @@ -24,52 +24,16 @@ import { PROTOCOL_EARNINGS, VAULT_EARNINGS } from "../services/subgraph/apollo/queries"; -import { Address, SdkError, TokenAmount, Usdc } from "../types"; -import { EarningsUserData } from "../types/custom/earnings"; +import { Address, SdkError } from "../types"; +import { + AccountHistoricEarnings, + AssetEarnings, + AssetHistoricEarnings, + EarningsUserData +} from "../types/custom/earnings"; const BigZero = new BigNumber(0); -export interface AccountSummary { - accountId: Address; - aggregatedApy: number; - totalDepositedUsdc: Usdc; - totalEarningsUsdc: Usdc; - projectedDailyEarningsUsdc: Usdc; -} - -export interface AccountAssetPosition { - accountAddress: Address; - assetAddress: Address; - tokenAddress: Address; - balance: TokenAmount; - earnings: TokenAmount; - roi: number; -} - -export interface AssetEarnings extends TokenAmount { - tokenAddress: Address; - assetAddress: Address; -} - -export interface AssetHistoricEarnings extends HistoricEarnings { - assetAddress: Address; -} - -export interface AccountHistoricEarnings extends HistoricEarnings { - accountAddress: Address; - shareTokenAddress: Address; -} - -export interface HistoricEarnings { - decimals: number; - dayData: EarningsDayData[]; -} - -export interface EarningsDayData { - earnings: TokenAmount; - date: Date; -} - export class EarningsInterface extends ServiceInterface { async protocolEarnings(): Promise { const response = await this.yearn.services.subgraph.client.query({ diff --git a/src/interfaces/ironbank.ts b/src/interfaces/ironbank.ts index 6d868d1e..2fe0e531 100644 --- a/src/interfaces/ironbank.ts +++ b/src/interfaces/ironbank.ts @@ -9,7 +9,7 @@ import { IronBankMarket, IronBankMarketDynamic, IronBankMarketStatic, - IronBankPosition, + IronBankUserSummary, Position, SdkError, Token @@ -68,26 +68,23 @@ export class IronBankInterface extends ServiceInterface { } /** - * Get the IronBank Position for a particular address. + * Get the IronBank User Summary for a particular address. * @param address * @param overrides * @returns */ - async generalPositionOf(address: Address, overrides?: CallOverrides): Promise { + async summaryOf(address: Address, overrides?: CallOverrides): Promise { return this.yearn.services.lens.adapters.ironBank.generalPositionOf(address, overrides); } /** * Get the IronBank User Metadata for a particular address. * @param address + * @param addresses * @param overrides * @returns */ - async userMetadata( - address: Address, - addresses?: Address[], - overrides?: CallOverrides - ): Promise { + async metadataOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise { return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address, addresses, overrides); } diff --git a/src/interfaces/vault.ts b/src/interfaces/vault.ts index abd4c0a0..636bc2d2 100644 --- a/src/interfaces/vault.ts +++ b/src/interfaces/vault.ts @@ -14,6 +14,8 @@ import { Token, VaultDynamic, VaultStatic, + VaultsUserSummary, + VaultUserMetadata, WithdrawOptions } from "../types"; import { Position, Vault } from "../types"; @@ -96,6 +98,28 @@ export class VaultInterface extends ServiceInterface { ).then(arr => arr.flat()); } + /** + * Get the Vaults User Summary for a particular address. + * @param address + * @returns + */ + async summaryOf(address: Address): Promise { + const { earnings, holdings, estimatedYearlyYield } = await this.yearn.earnings.accountAssetsData(address); + return { earnings, holdings, estimatedYearlyYield }; + } + + /** + * Get the Vault User Metadata for a particular address. + * @param address + * @param addresses + * @returns + */ + async metadataOf(address: Address, addresses?: Address[]): Promise { + const { earningsAssetData } = await this.yearn.earnings.accountAssetsData(address); + if (!addresses) return earningsAssetData; + return earningsAssetData.filter(asset => addresses.includes(asset.assetAddress)); + } + /** * Get all yearn vault's underlying token balances for a particular address. * @param address diff --git a/src/services/adapters/ironbank.ts b/src/services/adapters/ironbank.ts index 727c4a03..7ca9a2c9 100644 --- a/src/services/adapters/ironbank.ts +++ b/src/services/adapters/ironbank.ts @@ -10,7 +10,7 @@ import { CyTokenUserMetadata, IronBankMarketDynamic, IronBankMarketStatic, - IronBankPosition, + IronBankUserSummary, Position } from "../../types"; @@ -35,7 +35,7 @@ const CyTokenUserMetadataAbi = `tuple( uint256 borrowLimitUsdc )`; -const IronBankPositionAbi = `tuple( +const IronBankUserSummaryAbi = `tuple( uint256 supplyBalanceUsdc, uint256 borrowBalanceUsdc, uint256 borrowLimitUsdc, @@ -43,7 +43,7 @@ const IronBankPositionAbi = `tuple( )`; const CustomAbi = [ - `function adapterPositionOf(address) external view returns (${IronBankPositionAbi} memory)`, + `function adapterPositionOf(address) external view returns (${IronBankUserSummaryAbi} memory)`, `function assetsUserMetadata(address) public view returns (${CyTokenUserMetadataAbi}[] memory)` ]; @@ -118,7 +118,7 @@ export class IronBankAdapter extends ContractService { * @param overrides * @returns */ - async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise { + async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise { return await this.contract.read.adapterPositionOf(address, overrides).then(struct); } diff --git a/src/types/common.ts b/src/types/common.ts index 857a2690..925bec4f 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -18,7 +18,7 @@ export type Address = string; export type Integer = string; /** - * Utility type to help distinguish [[Integers]] that represent a USDC (6 dec) + * Utility type to help distinguish [[Integer]]s that represent a USDC (6 dec) * value. */ export type Usdc = string; diff --git a/src/types/custom/earnings.ts b/src/types/custom/earnings.ts index ad22c5ec..2bdb4c1e 100644 --- a/src/types/custom/earnings.ts +++ b/src/types/custom/earnings.ts @@ -1,5 +1,47 @@ +import { TokenAmount } from "../asset"; import { Address, Usdc } from "../common"; +export interface AccountSummary { + accountId: Address; + aggregatedApy: number; + totalDepositedUsdc: Usdc; + totalEarningsUsdc: Usdc; + projectedDailyEarningsUsdc: Usdc; +} + +export interface AccountAssetPosition { + accountAddress: Address; + assetAddress: Address; + tokenAddress: Address; + balance: TokenAmount; + earnings: TokenAmount; + roi: number; +} + +export interface AssetEarnings extends TokenAmount { + tokenAddress: Address; + assetAddress: Address; +} + +export interface AssetHistoricEarnings extends HistoricEarnings { + assetAddress: Address; +} + +export interface AccountHistoricEarnings extends HistoricEarnings { + accountAddress: Address; + shareTokenAddress: Address; +} + +export interface HistoricEarnings { + decimals: number; + dayData: EarningsDayData[]; +} + +export interface EarningsDayData { + earnings: TokenAmount; + date: Date; +} + export interface EarningsUserData { earnings: Usdc; holdings: Usdc; diff --git a/src/types/custom/ironbank.ts b/src/types/custom/ironbank.ts index 29133de9..e3128e3e 100644 --- a/src/types/custom/ironbank.ts +++ b/src/types/custom/ironbank.ts @@ -11,7 +11,7 @@ export interface CyTokenUserMetadata { borrowLimitUsdc: Integer; } -export interface IronBankPosition { +export interface IronBankUserSummary { supplyBalanceUsdc: Integer; borrowBalanceUsdc: Integer; borrowLimitUsdc: Integer; diff --git a/src/types/custom/vault.ts b/src/types/custom/vault.ts index 6078995e..a2c908c8 100644 --- a/src/types/custom/vault.ts +++ b/src/types/custom/vault.ts @@ -1,4 +1,4 @@ -import { Address, TypedMap } from "../common"; +import { Address, Integer, TypedMap, Usdc } from "../common"; import { ZapOptions } from "./zapper"; /** @@ -16,3 +16,14 @@ export type ApyMap = TypedMap; export interface DepositOptions extends ZapOptions {} export interface WithdrawOptions extends ZapOptions {} + +export interface VaultsUserSummary { + holdings: Usdc; + earnings: Usdc; + estimatedYearlyYield: Integer; +} + +export interface VaultUserMetadata { + assetAddress: Address; + earned: Usdc; +}