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

[export]: add export logic #59

Merged
merged 5 commits into from
Jan 22, 2024
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
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,24 @@ Daily rewards for the user who has made a deposit in the vault. With the help of

#### Arguments:

| Name | Type | Type | Description |
|------|----------|-------------|---------|
| dateFrom | `number` | **Require** | Time to start |
| Name | Type | Type | Description |
|------|----------|-------------|---|
| vaultAddress | `string` | **Require** | - |
| userAddress | `string` | **Require** | - |
| dateFrom | `number` | **Require** | Time to start |
| dateTo | `number` | **Optional** | Time to end |
| fillGaps | `boolean` | **Optional** | Fill in the empty days with zeros |

#### Returns:

```ts
type UserReward = {
sumRewards: number
dailyRewards: number
date: number
sumRewards: string
dailyRewards: string
dailyRewardsEur: string
dailyRewardsGbp: string
dailyRewardsUsd: string
}

type Output = {
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, $fillGaps: Boolean = true) {
userRewards(user: $user, vaultAddress: $vaultAddress, dateFrom: $dateFrom, dateTo: $dateTo, fillGaps: $fillGaps) {
date
sumRewards
dailyRewards
dailyRewardsEur
dailyRewardsGbp
dailyRewardsUsd
}
}
14 changes: 11 additions & 3 deletions src/methods/vault/requests/getUserRewards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ type GetUserRewardsInput = {
userAddress: UserRewardsQueryVariables['user']
vaultAddress: UserRewardsQueryVariables['vaultAddress']
dateFrom: number
dateTo?: number
fillGaps?: boolean
}

const getUserRewards = async (input: GetUserRewardsInput) => {
const { options, vaultAddress, userAddress, dateFrom } = input
const { options, vaultAddress, userAddress, dateFrom, dateTo, fillGaps } = 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(),
fillGaps,
dateFrom: String(dateFrom),
user: userAddress.toLowerCase(),
dateTo: dateTo ? String(dateTo) : undefined,
vaultAddress: vaultAddress.toLowerCase(),
} 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
1 change: 1 addition & 0 deletions src/utils/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum AllocatorActionType {
Redeemed = 'Redeemed',
Migrated = 'Migrated',
Deposited = 'Deposited',
Transferred = 'Transferred',
VaultCreated = 'VaultCreated',
OsTokenMinted = 'OsTokenMinted',
OsTokenBurned = 'OsTokenBurned',
Expand Down
Loading