Skip to content

Commit

Permalink
[export]: add export logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dfkadyr committed Jan 16, 2024
1 parent 0aa66f0 commit 38d632c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions src/graphql/backend/vault/userRewardsQuery.graphql
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 7 additions & 1 deletion src/methods/vault/requests/getUserRewards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModifyUserReward>({
url: apiUrls.getBackendUrl(options),
variables: {
vaultAddress: vaultAddress.toLowerCase(),
user: userAddress.toLowerCase(),
dateFrom: String(dateFrom),
dateTo: dateTo ? String(dateTo) : null,
} as UserRewardsQueryVariables,
modifyResult: modifyUserRewards,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
}
Expand All @@ -24,21 +30,33 @@ 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,
})
})

it('should correctly modify multiple rewards', () => {
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,
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import { ModifyUserReward } from './types'
import type { UserRewardsQueryPayload } from '../../../../graphql/backend/vault'


export const modifyUserReward = (reward: Omit<UserRewardsQueryPayload['userRewards'][number], 'date'>) => {
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'])
Expand Down
4 changes: 4 additions & 0 deletions src/methods/vault/requests/getUserRewards/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
type UserReward = {
date: number
sumRewards: number
dailyRewards: number
dailyRewardsEur: number
dailyRewardsGbp: number
dailyRewardsUsd: number
}

export type ModifyUserReward = {
Expand Down

0 comments on commit 38d632c

Please sign in to comment.