Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add specs for the EarningsInterface #223

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
213 changes: 213 additions & 0 deletions src/interfaces/earnings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import { getAddress } from "@ethersproject/address";
import BigNumber from "bignumber.js";

import { AssetHistoricEarnings, ChainId, EarningsInterface, SdkError, Usdc, VaultStatic } from "..";
import { Context } from "../context";
import { createMockAssetHistoricEarnings, createMockAssetStaticVaultV2 } from "../test-utils/factories";
import { Yearn } from "../yearn";

const getAddressMock = jest.fn();
const subgraphFetchQueryMock = jest.fn();
const oracleGetPriceUsdcMock: jest.Mock<Promise<Usdc>> = jest.fn();
const lensAdaptersVaultsV2AssetsStaticMock: jest.Mock<Promise<VaultStatic[]>> = jest.fn();
const getBlockNumberMock: jest.Mock<Promise<number>> = jest.fn();

jest.mock("../yearn", () => ({
Yearn: jest.fn().mockImplementation(() => ({
services: {
subgraph: {
fetchQuery: subgraphFetchQueryMock
},
vision: {},
lens: {
adapters: { vaults: { v2: { assetsStatic: lensAdaptersVaultsV2AssetsStaticMock } } }
},
oracle: {
getPriceUsdc: oracleGetPriceUsdcMock
}
}
}))
}));

jest.mock("@ethersproject/address", () => ({
getAddress: jest.fn().mockImplementation(() => getAddressMock)
}));

jest.mock("../context", () => ({
Context: jest.fn().mockImplementation(() => ({
provider: {
read: {
getBlockNumber: getBlockNumberMock
}
}
}))
}));

describe("EarningsInterface", () => {
let earningsInterface: EarningsInterface<1>;

let mockedYearn: Yearn<ChainId>;

beforeEach(() => {
mockedYearn = new (Yearn as jest.Mock<Yearn<ChainId>>)();
earningsInterface = new EarningsInterface(mockedYearn, 1, new Context({}));
});

afterEach(() => {
jest.clearAllMocks();
});

describe("protocolEarnings", () => {
describe("when there is no data", () => {
beforeEach(() => {
subgraphFetchQueryMock.mockResolvedValue(undefined);
});

it("should return a string representing the value of BigNumber(0)", async () => {
const actualProtocolEarnings = await earningsInterface.protocolEarnings();

expect(actualProtocolEarnings).toEqual("0");
});
});

describe("when there is data", () => {
beforeEach(() => {
oracleGetPriceUsdcMock.mockResolvedValueOnce("2.5");
subgraphFetchQueryMock.mockResolvedValue({
data: {
vaults: [
{
latestUpdate: {
returnsGenerated: new BigNumber(2).multipliedBy(10 ** 18)
},
token: {
decimals: 18,
id: "tokenId"
}
},
{
token: {
decimals: 18,
id: "tokenIdWithoutLatestUpdate"
jstashh marked this conversation as resolved.
Show resolved Hide resolved
}
}
]
}
});
});

it("should return the protocol earnings", async () => {
const actualProtocolEarnings = await earningsInterface.protocolEarnings();

expect(actualProtocolEarnings).toEqual(new BigNumber(5).toFixed(0));
});
});
});

describe("assetEarnings", () => {
describe("when there is no data", () => {
beforeEach(() => {
subgraphFetchQueryMock.mockResolvedValue(undefined);
});

it("should throw", async () => {
try {
await earningsInterface.assetEarnings("0x001");
} catch (error) {
expect(error).toStrictEqual(new SdkError("No asset with address 0x001"));
}
});
});

describe("when there is data", () => {
beforeEach(() => {
getAddressMock.mockReturnValue("0x001");
oracleGetPriceUsdcMock.mockResolvedValueOnce("3.5");
subgraphFetchQueryMock.mockResolvedValue({
data: {
vault: {
token: {
id: "vaultTokenId",
decimals: 18
},
latestUpdate: {
returnsGenerated: new BigNumber(2).multipliedBy(10 ** 18)
}
}
}
});
});

it("should return the asset earnings", async () => {
const actualAssetEarnings = await earningsInterface.assetEarnings("0x001");
jstashh marked this conversation as resolved.
Show resolved Hide resolved

expect(actualAssetEarnings).toEqual({
amount: "2000000000000000000",
amountUsdc: "7",
assetAddress: getAddressMock,
tokenAddress: getAddressMock
});
expect(oracleGetPriceUsdcMock).toHaveBeenCalledTimes(1);
expect(oracleGetPriceUsdcMock).toHaveBeenCalledWith("vaultTokenId");

expect(getAddress).toHaveBeenCalledTimes(2);
expect(getAddress).toHaveBeenNthCalledWith(1, "0x001");
expect(getAddress).toHaveBeenNthCalledWith(2, "vaultTokenId");
});
});
});

describe("accountAssetsData", () => {
it.todo("todo");
});
karelianpie marked this conversation as resolved.
Show resolved Hide resolved

describe("assetsHistoricEarnings", () => {
const assetHistoricEarnings = createMockAssetHistoricEarnings();
let assetHistoricEarningsCacheFetchMock: jest.Mock<Promise<AssetHistoricEarnings[] | undefined>> = jest.fn();

beforeEach(() => {
(earningsInterface as any).assetHistoricEarningsCache.fetch = assetHistoricEarningsCacheFetchMock;
});

describe("when there is cached data", () => {
beforeEach(() => {
assetHistoricEarningsCacheFetchMock.mockResolvedValue([assetHistoricEarnings]);
});

it("return the cached data", async () => {
const actualAssetsHistoricEarnings = await earningsInterface.assetsHistoricEarnings();

expect(actualAssetsHistoricEarnings).toEqual([assetHistoricEarnings]);
expect(assetHistoricEarningsCacheFetchMock).toHaveBeenCalledTimes(1);
});
});

describe("when there is no cached data", () => {
let assetHistoricEarningsMock: jest.Mock<Promise<AssetHistoricEarnings>> = jest.fn();
const assetsStatic = [createMockAssetStaticVaultV2()];
const assetHistoricEarnings = createMockAssetHistoricEarnings();

beforeEach(() => {
assetHistoricEarningsCacheFetchMock.mockResolvedValue(undefined);
lensAdaptersVaultsV2AssetsStaticMock.mockResolvedValue(assetsStatic);
getBlockNumberMock.mockResolvedValue(42000);
(earningsInterface as any).assetHistoricEarnings = assetHistoricEarningsMock;
assetHistoricEarningsMock.mockResolvedValue(assetHistoricEarnings);
});

it("should not call `assetHistoricEarningsCache.fetch`", async () => {
const actualAssetsHistoricEarnings = await earningsInterface.assetsHistoricEarnings();

expect(actualAssetsHistoricEarnings).toEqual([assetHistoricEarnings]);
expect(lensAdaptersVaultsV2AssetsStaticMock).toHaveBeenCalledTimes(1);
expect(getBlockNumberMock).toHaveBeenCalledTimes(1);
expect(assetHistoricEarningsMock).toHaveBeenCalledTimes(1);
expect(assetHistoricEarningsMock).toHaveBeenCalledWith("0x001", 30, 42000);
});
});
});

describe("assetHistoricEarnings", () => {});

describe("accountHistoricEarnings", () => {});
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
});
48 changes: 28 additions & 20 deletions src/interfaces/earnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ const BigZero = new BigNumber(0);

export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
async protocolEarnings(): Promise<String> {
const response = (await this.yearn.services.subgraph.fetchQuery(PROTOCOL_EARNINGS)) as ProtocolEarningsResponse;
const response = await this.yearn.services.subgraph.fetchQuery<ProtocolEarningsResponse>(PROTOCOL_EARNINGS);

let result = BigZero;

if (!response?.data || !response.data?.vaults) {
return result.toFixed(0);
}
karelianpie marked this conversation as resolved.
Show resolved Hide resolved

for (const vault of response.data.vaults) {
if (!vault.latestUpdate) {
continue;
Expand All @@ -47,16 +52,16 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
}

async assetEarnings(assetAddress: Address): Promise<AssetEarnings> {
const response = (await this.yearn.services.subgraph.fetchQuery(VAULT_EARNINGS, {
const response = await this.yearn.services.subgraph.fetchQuery<VaultEarningsResponse>(VAULT_EARNINGS, {
vault: assetAddress
})) as VaultEarningsResponse;

const vault = response.data.vault;
});

if (!vault) {
if (!response?.data || !response.data?.vault) {
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
throw new SdkError(`No asset with address ${assetAddress}`);
}

const { vault } = response.data;

const returnsGenerated = new BigNumber(vault.latestUpdate?.returnsGenerated || 0);
const earningsUsdc = await this.tokensValueInUsdc(returnsGenerated, vault.token.id, vault.token.decimals);
return {
Expand All @@ -68,10 +73,10 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
}

async accountAssetsData(accountAddress: Address): Promise<EarningsUserData> {
const response = (await this.yearn.services.subgraph.fetchQuery(
const response = await this.yearn.services.subgraph.fetchQuery<AccountEarningsResponse>(
ACCOUNT_EARNINGS,
buildAccountEarningsVariables(accountAddress)
)) as AccountEarningsResponse;
);

const account = response.data.account;

Expand Down Expand Up @@ -216,9 +221,9 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
.reverse()
.map(day => blockNumber - day * this.blocksPerDay());

const response = (await this.yearn.services.subgraph.fetchQuery(ASSET_HISTORIC_EARNINGS(blocks), {
const response = await this.yearn.services.subgraph.fetchQuery<any>(ASSET_HISTORIC_EARNINGS(blocks), {
id: vault
})) as any;
});

const data = response.data;

Expand Down Expand Up @@ -273,16 +278,19 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
throw new SdkError("fromDaysAgo must be greater than toDaysAgo");
}

const response = (await this.yearn.services.subgraph.fetchQuery(ACCOUNT_HISTORIC_EARNINGS, {
id: accountAddress,
shareToken: shareTokenAddress,
fromDate: this.getDate(fromDaysAgo)
.getTime()
.toString(),
toDate: this.getDate(toDaysAgo || 0)
.getTime()
.toString()
})) as AccountHistoricEarningsResponse;
const response = await this.yearn.services.subgraph.fetchQuery<AccountHistoricEarningsResponse>(
ACCOUNT_HISTORIC_EARNINGS,
{
id: accountAddress,
shareToken: shareTokenAddress,
fromDate: this.getDate(fromDaysAgo)
.getTime()
.toString(),
toDate: this.getDate(toDaysAgo || 0)
.getTime()
.toString()
}
);

const vaultPositions = response.data.account?.vaultPositions;

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Usdc } from "../types";

export class FeesInterface<C extends ChainId> extends ServiceInterface<C> {
async protocolFees(since: Date): Promise<Usdc> {
const response = (await this.yearn.services.subgraph.fetchQuery(PROTOCOL_FEES, {
const response = await this.yearn.services.subgraph.fetchQuery<FeesResponse>(PROTOCOL_FEES, {
sinceDate: since.getTime().toString()
})) as FeesResponse;
});

const transfers = response.data.transfers;

Expand Down
2 changes: 1 addition & 1 deletion src/services/subgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class SubgraphService extends Service {
this.yearnSubgraphEndpoint = `https://api.thegraph.com/subgraphs/name/${subgraphName}`;
}

async fetchQuery(query: string, variables: { [key: string]: any } = {}): Promise<unknown> {
async fetchQuery<T>(query: string, variables: { [key: string]: any } = {}): Promise<T> {
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
// the subgraph only works with lowercased addresses
Object.keys(variables).forEach(key => {
const variable = variables[key];
Expand Down
12 changes: 12 additions & 0 deletions src/test-utils/factories/assetHistoricEarnings.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AssetHistoricEarnings } from "../..";

export const DEFAULT_ASSET_HISTORIC_EARNINGS: AssetHistoricEarnings = {
assetAddress: "0x001",
decimals: 18,
dayData: [{ earnings: { amount: "1", amountUsdc: "1" }, date: "15-02-2022" }]
};

export const createMockAssetHistoricEarnings = (overwrites: Partial<AssetHistoricEarnings> = {}) => ({
...DEFAULT_ASSET_HISTORIC_EARNINGS,
...overwrites
});
1 change: 1 addition & 0 deletions src/test-utils/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./asset.factory";
export * from "./assetHistoricEarnings.factory";
export * from "./balance.factory";
export * from "./token.factory";