From 38d632cf977190d89d6a3975a9316142e811c06d Mon Sep 17 00:00:00 2001 From: dfkadyr Date: Tue, 16 Jan 2024 17:44:36 +0300 Subject: [PATCH] [export]: add export logic --- README.md | 5 +++-- .../backend/vault/userRewardsQuery.graphql | 7 +++++-- .../vault/requests/getUserRewards/index.ts | 8 +++++++- .../getUserRewards/modifyUserRewards.spec.ts | 18 ++++++++++++++++++ .../getUserRewards/modifyUserRewards.ts | 8 ++++++-- .../vault/requests/getUserRewards/types.ts | 4 ++++ 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6ef9b2d4..8c52b3a4 100644 --- a/README.md +++ b/README.md @@ -251,11 +251,12 @@ Daily rewards for the user who has made a deposit in the vault. With the help of #### Arguments: -| Name | Type | Type | Description | +| Name | Type | Type | Description | |------|----------|-------------|---------| -| dateFrom | `number` | **Require** | Time to start | | vaultAddress | `string` | **Require** | - | | userAddress | `string` | **Require** | - | +| dateFrom | `number` | **Require** | Time to start | +| dateTo | `number` | Time to end | #### Returns: diff --git a/src/graphql/backend/vault/userRewardsQuery.graphql b/src/graphql/backend/vault/userRewardsQuery.graphql index c8dff0f0..e55c7db4 100644 --- a/src/graphql/backend/vault/userRewardsQuery.graphql +++ b/src/graphql/backend/vault/userRewardsQuery.graphql @@ -1,7 +1,10 @@ -query UserRewards($user: String!, $vaultAddress: String!, $dateFrom: DateAsTimestamp!) { - userRewards(user: $user, vaultAddress: $vaultAddress, dateFrom: $dateFrom) { +query UserRewards($user: String!, $vaultAddress: String!, $dateFrom: DateAsTimestamp!, $dateTo: DateAsTimestamp) { + userRewards(user: $user, vaultAddress: $vaultAddress, dateFrom: $dateFrom, dateTo: $dateTo) { date sumRewards dailyRewards + dailyRewardsEur + dailyRewardsGbp + dailyRewardsUsd } } diff --git a/src/methods/vault/requests/getUserRewards/index.ts b/src/methods/vault/requests/getUserRewards/index.ts index f6023b39..25aa8878 100644 --- a/src/methods/vault/requests/getUserRewards/index.ts +++ b/src/methods/vault/requests/getUserRewards/index.ts @@ -10,20 +10,26 @@ type GetUserRewardsInput = { userAddress: UserRewardsQueryVariables['user'] vaultAddress: UserRewardsQueryVariables['vaultAddress'] dateFrom: number + dateTo?: number } const getUserRewards = async (input: GetUserRewardsInput) => { - const { options, vaultAddress, userAddress, dateFrom } = input + const { options, vaultAddress, userAddress, dateFrom, dateTo } = input validateArgs.address({ vaultAddress, userAddress }) validateArgs.number({ dateFrom }) + if (dateTo) { + validateArgs.number({ dateTo }) + } + const data = await graphql.backend.vault.fetchUserRewardsQuery({ url: apiUrls.getBackendUrl(options), variables: { vaultAddress: vaultAddress.toLowerCase(), user: userAddress.toLowerCase(), dateFrom: String(dateFrom), + dateTo: dateTo ? String(dateTo) : null, } as UserRewardsQueryVariables, modifyResult: modifyUserRewards, }) diff --git a/src/methods/vault/requests/getUserRewards/modifyUserRewards.spec.ts b/src/methods/vault/requests/getUserRewards/modifyUserRewards.spec.ts index a13413ea..4423c885 100644 --- a/src/methods/vault/requests/getUserRewards/modifyUserRewards.spec.ts +++ b/src/methods/vault/requests/getUserRewards/modifyUserRewards.spec.ts @@ -9,11 +9,17 @@ describe('modifyUserReward and modifyUserRewards functions', () => { date: '1694908800', sumRewards: '344379922475148628745', dailyRewards: '344379922475148628745', + dailyRewardsEur: "0.10", + dailyRewardsGbp: "0.09", + dailyRewardsUsd: "0.11", }, { date: '1694995200', sumRewards: '344382187878289278175', dailyRewards: '0', + dailyRewardsEur: "0.10", + dailyRewardsGbp: "0.09", + dailyRewardsUsd: "0.11", }, ], } @@ -24,8 +30,12 @@ describe('modifyUserReward and modifyUserRewards functions', () => { const result = modifyUserReward(userReward) expect(result).toEqual({ + date: 1694908800, sumRewards: 344.37992247514865, dailyRewards: 344.37992247514865, + dailyRewardsEur: 0.10, + dailyRewardsGbp: 0.09, + dailyRewardsUsd: 0.11, }) }) @@ -33,12 +43,20 @@ describe('modifyUserReward and modifyUserRewards functions', () => { const expectedResult = { days: { 1694908800: { + date: 1694908800, sumRewards: 344.37992247514865, dailyRewards: 344.37992247514865, + dailyRewardsEur: 0.10, + dailyRewardsGbp: 0.09, + dailyRewardsUsd: 0.11, }, 1694995200: { + date: 1694995200, sumRewards: 344.3821878782893, dailyRewards: 0, + dailyRewardsEur: 0.10, + dailyRewardsGbp: 0.09, + dailyRewardsUsd: 0.11, }, }, } diff --git a/src/methods/vault/requests/getUserRewards/modifyUserRewards.ts b/src/methods/vault/requests/getUserRewards/modifyUserRewards.ts index 47fbd63a..209118c6 100644 --- a/src/methods/vault/requests/getUserRewards/modifyUserRewards.ts +++ b/src/methods/vault/requests/getUserRewards/modifyUserRewards.ts @@ -4,19 +4,23 @@ import { ModifyUserReward } from './types' import type { UserRewardsQueryPayload } from '../../../../graphql/backend/vault' -export const modifyUserReward = (reward: Omit) => { +export const modifyUserReward = (reward: UserRewardsQueryPayload['userRewards'][number]) => { const sumRewards = String(reward.sumRewards) || '0' const dailyRewards = String(reward.dailyRewards) || '0' return { + date: Number(reward.date), sumRewards: Number(formatEther(sumRewards)), dailyRewards: Number(formatEther(dailyRewards)), + dailyRewardsEur: Number(reward.dailyRewardsEur) || 0, + dailyRewardsGbp: Number(reward.dailyRewardsGbp) || 0, + dailyRewardsUsd: Number(reward.dailyRewardsUsd) || 0, } } const modifyUserRewards = (input: UserRewardsQueryPayload): ModifyUserReward => { const days = input.userRewards.reduce((acc, { date, ...rest }) => { - acc[Number(date)] = modifyUserReward(rest) + acc[Number(date)] = modifyUserReward({ date, ...rest }) return acc }, {} as ModifyUserReward['days']) diff --git a/src/methods/vault/requests/getUserRewards/types.ts b/src/methods/vault/requests/getUserRewards/types.ts index b0dff29b..b11cd4d8 100644 --- a/src/methods/vault/requests/getUserRewards/types.ts +++ b/src/methods/vault/requests/getUserRewards/types.ts @@ -1,6 +1,10 @@ type UserReward = { + date: number sumRewards: number dailyRewards: number + dailyRewardsEur: number + dailyRewardsGbp: number + dailyRewardsUsd: number } export type ModifyUserReward = {