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 test for EarningsInterface#assetHistoricEarnings #271

Merged
merged 7 commits into from
Apr 13, 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
65 changes: 63 additions & 2 deletions src/interfaces/earnings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,69 @@ describe("EarningsInterface", () => {
});
});

// eslint-disable-next-line @typescript-eslint/no-empty-function
describe.skip("assetHistoricEarnings", () => {});
describe("assetHistoricEarnings", () => {
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => {
subgraphFetchQueryMock.mockResolvedValue({
data: {
block_30380: {
strategies: [
{
latestReport: {
totalGain: "303000000000000000000",
totalLoss: "80000000000000000000",
},
},
],
vaultDayData: [{ timestamp: "1648887421" }],
},
block_36140: {
strategies: [
{
latestReport: {
totalGain: "361000000000000000000",
totalLoss: "40000000000000000000",
},
},
],
vaultDayData: [{ timestamp: "1648801021" }],
},
block_41900: {
strategies: [
{
latestReport: {
totalGain: "419000000000000000000",
totalLoss: "0",
},
},
],
vaultDayData: [{ timestamp: "1648714621" }],
},
vault: {
token: {
id: "0xVaultTokenId",
decimals: "18",
},
},
},
});
oracleGetPriceUsdcMock.mockResolvedValue("6000000");
});

it("should return the asset historic earnings", async () => {
const actual = await earningsInterface.assetHistoricEarnings("0x00", 3, 42000);

expect(actual).toEqual({
assetAddress: "0x00",
dayData: [
{ date: "2022-04-02T08:17:01.000Z", earnings: { amount: "223000000000000000000", amountUsdc: "1338000000" } },
{ date: "2022-04-01T08:17:01.000Z", earnings: { amount: "321000000000000000000", amountUsdc: "1926000000" } },
{ date: "2022-03-31T08:17:01.000Z", earnings: { amount: "419000000000000000000", amountUsdc: "2514000000" } },
],
decimals: "18",
});
expect(actual.dayData.length).toEqual(3);
});
});

// eslint-disable-next-line @typescript-eslint/no-empty-function
describe.skip("accountHistoricEarnings", () => {});
Expand Down
52 changes: 23 additions & 29 deletions src/interfaces/earnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ import {
EarningsDayData,
EarningsUserData,
} from "../types/custom/earnings";
import { toBN } from "../utils";

const BigZero = new BigNumber(0);

interface StrategiesResponse {
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
latestReport?: {
totalGain: string;
totalLoss: string;
};
}

export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
/**
* @deprecated
Expand Down Expand Up @@ -210,19 +218,7 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
fromDaysAgo: number,
latestBlockNumber?: number
): Promise<AssetHistoricEarnings> {
interface StrategiesResponse {
latestReport?: {
totalGain: string;
totalLoss: string;
};
}

let blockNumber: number;
if (latestBlockNumber) {
blockNumber = latestBlockNumber;
} else {
blockNumber = await this.ctx.provider.read.getBlockNumber();
}
let blockNumber = latestBlockNumber ?? (await this.ctx.provider.read.getBlockNumber());

blockNumber -= this.blockOffset(); // subgraph might be slightly behind latest block

Expand All @@ -231,32 +227,32 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
.map((day) => blockNumber - day * this.blocksPerDay());

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response = await this.yearn.services.subgraph.fetchQuery<any>(ASSET_HISTORIC_EARNINGS(blocks), {
const { data } = await this.yearn.services.subgraph.fetchQuery<any>(ASSET_HISTORIC_EARNINGS(blocks), {
id: vault,
});

const data = response.data;

const labels = blocks.map((block) => `block_${block}`);

const token = data.vault.token.id as string;
const token = data.vault.token.id as Address;
const priceUsdc = await this.yearn.services.oracle.getPriceUsdc(token).then((price) => new BigNumber(price));

const earningsDayData = labels.map((label) => {
const strategies: StrategiesResponse[] = data[label].strategies;
const totalGain = strategies
.map((strategy) => (strategy.latestReport ? new BigNumber(strategy.latestReport.totalGain) : new BigNumber(0)))
.reduce((sum, value) => sum.plus(value));

const totalLoss = strategies
.map((strategy) => (strategy.latestReport ? new BigNumber(strategy.latestReport.totalLoss) : new BigNumber(0)))
.reduce((sum, value) => sum.plus(value));
const { totalGain, totalLoss } = strategies.reduce(
({ totalGain, totalLoss }, { latestReport }) => {
if (latestReport) {
totalGain = totalGain.plus(toBN(latestReport.totalGain));
totalLoss = totalLoss.plus(toBN(latestReport.totalLoss));
}
return { totalGain, totalLoss };
},
{ totalGain: toBN(0), totalLoss: toBN(0) }
);
karelianpie marked this conversation as resolved.
Show resolved Hide resolved

const amountEarnt = totalGain.minus(totalLoss);

const amountUsdc = priceUsdc
.multipliedBy(amountEarnt)
.dividedBy(new BigNumber(10).pow(new BigNumber(data.vault.token.decimals)));
const amountUsdc = priceUsdc.multipliedBy(amountEarnt).dividedBy(toBN(10).pow(toBN(data.vault.token.decimals)));

const dayData: EarningsDayData = {
earnings: {
Expand All @@ -269,13 +265,11 @@ export class EarningsInterface<C extends ChainId> extends ServiceInterface<C> {
return dayData;
});

const result: AssetHistoricEarnings = {
karelianpie marked this conversation as resolved.
Show resolved Hide resolved
return {
assetAddress: vault,
dayData: earningsDayData,
decimals: data.vault.token.decimals,
};

return result;
}

async accountHistoricEarnings(
Expand Down