Skip to content

Commit

Permalink
feat(vault): add user metadata and user summary (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
nymmrx authored Jul 8, 2021
1 parent 4867553 commit a411099
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 62 deletions.
8 changes: 4 additions & 4 deletions examples/ironbank.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
50 changes: 7 additions & 43 deletions src/interfaces/earnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends ChainId> extends ServiceInterface<C> {
async protocolEarnings(): Promise<String> {
const response = await this.yearn.services.subgraph.client.query<ProtocolEarnings>({
Expand Down
13 changes: 5 additions & 8 deletions src/interfaces/ironbank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
IronBankMarket,
IronBankMarketDynamic,
IronBankMarketStatic,
IronBankPosition,
IronBankUserSummary,
Position,
SdkError,
Token
Expand Down Expand Up @@ -68,26 +68,23 @@ export class IronBankInterface<T extends ChainId> extends ServiceInterface<T> {
}

/**
* 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<IronBankPosition> {
async summaryOf(address: Address, overrides?: CallOverrides): Promise<IronBankUserSummary> {
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<CyTokenUserMetadata[]> {
async metadataOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise<CyTokenUserMetadata[]> {
return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address, addresses, overrides);
}

Expand Down
24 changes: 24 additions & 0 deletions src/interfaces/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Token,
VaultDynamic,
VaultStatic,
VaultsUserSummary,
VaultUserMetadata,
WithdrawOptions
} from "../types";
import { Position, Vault } from "../types";
Expand Down Expand Up @@ -96,6 +98,28 @@ export class VaultInterface<T extends ChainId> extends ServiceInterface<T> {
).then(arr => arr.flat());
}

/**
* Get the Vaults User Summary for a particular address.
* @param address
* @returns
*/
async summaryOf(address: Address): Promise<VaultsUserSummary> {
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<VaultUserMetadata[]> {
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
Expand Down
8 changes: 4 additions & 4 deletions src/services/adapters/ironbank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
CyTokenUserMetadata,
IronBankMarketDynamic,
IronBankMarketStatic,
IronBankPosition,
IronBankUserSummary,
Position
} from "../../types";

Expand All @@ -35,15 +35,15 @@ const CyTokenUserMetadataAbi = `tuple(
uint256 borrowLimitUsdc
)`;

const IronBankPositionAbi = `tuple(
const IronBankUserSummaryAbi = `tuple(
uint256 supplyBalanceUsdc,
uint256 borrowBalanceUsdc,
uint256 borrowLimitUsdc,
uint256 utilizationRatioBips
)`;

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)`
];

Expand Down Expand Up @@ -118,7 +118,7 @@ export class IronBankAdapter<T extends ChainId> extends ContractService<T> {
* @param overrides
* @returns
*/
async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise<IronBankPosition> {
async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise<IronBankUserSummary> {
return await this.contract.read.adapterPositionOf(address, overrides).then(struct);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions src/types/custom/earnings.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/types/custom/ironbank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface CyTokenUserMetadata {
borrowLimitUsdc: Integer;
}

export interface IronBankPosition {
export interface IronBankUserSummary {
supplyBalanceUsdc: Integer;
borrowBalanceUsdc: Integer;
borrowLimitUsdc: Integer;
Expand Down
13 changes: 12 additions & 1 deletion src/types/custom/vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, TypedMap } from "../common";
import { Address, Integer, TypedMap, Usdc } from "../common";
import { ZapOptions } from "./zapper";

/**
Expand All @@ -16,3 +16,14 @@ export type ApyMap<T extends Address> = TypedMap<T, Apy | undefined>;

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;
}

0 comments on commit a411099

Please sign in to comment.