-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[graphics]: add rewards graphics (#25)
* [graphics]: add rewards graphics * [graphics]: add test, update README
- Loading branch information
1 parent
5dc84c7
commit 1b73246
Showing
8 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export { fetchUserRewardsQuery } from './userRewardsQuery.graphql' | ||
export type { UserRewardsQueryPayload, UserRewardsQueryVariables } from './userRewardsQuery.graphql' | ||
|
||
export { fetchVaultValidatorsQuery } from './vaultValidatorsQuery.graphql' | ||
export type { VaultValidatorsQueryPayload, VaultValidatorsQueryVariables } from './vaultValidatorsQuery.graphql' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
query UserRewards($user: String!, $vaultAddress: String!, $dateFrom: date_as_timestamp!) { | ||
userRewards(user: $user, vaultAddress: $vaultAddress, dateFrom: $dateFrom) { | ||
date, | ||
sumRewards, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { UserRewardsQueryVariables } from '../../../../graphql/backend/vault' | ||
import { apiUrls, validateArgs } from '../../../../utils' | ||
import modifyUserRewards from './modifyUserRewards' | ||
import { backend } from '../../../../graphql' | ||
import { ModifyUserReward } from './types' | ||
|
||
|
||
type GetUserRewardsInput = { | ||
options: StakeWise.Options | ||
userAddress: UserRewardsQueryVariables['user'] | ||
vaultAddress: UserRewardsQueryVariables['vaultAddress'] | ||
dateFrom: UserRewardsQueryVariables['dateFrom'] | ||
} | ||
|
||
const getUserRewards = async (input: GetUserRewardsInput) => { | ||
const { options, vaultAddress, userAddress, dateFrom } = input | ||
|
||
validateArgs.address({ vaultAddress, userAddress }) | ||
validateArgs.string({ dateFrom }) | ||
|
||
const data = await backend.vault.fetchUserRewardsQuery<ModifyUserReward>({ | ||
url: apiUrls.getBackendUrl(options), | ||
variables: { | ||
vaultAddress: vaultAddress.toLowerCase(), | ||
user: userAddress.toLowerCase(), | ||
dateFrom, | ||
} as UserRewardsQueryVariables, | ||
modifyResult: modifyUserRewards, | ||
}) | ||
|
||
return data | ||
} | ||
|
||
|
||
export default getUserRewards |
45 changes: 45 additions & 0 deletions
45
src/methods/vault/requests/getUserRewards/modifyUserRewards.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { UserRewardsQueryPayload } from '../../../../graphql/backend/vault' | ||
import modifyUserRewards, { modifyUserReward } from './modifyUserRewards' | ||
|
||
|
||
describe('modifyUserReward and modifyUserRewards functions', () => { | ||
const sampleInput: UserRewardsQueryPayload = { | ||
userRewards: [ | ||
{ | ||
date: '1694908800', | ||
sumRewards: '344379922475148628745', | ||
}, | ||
{ | ||
date: '1694995200', | ||
sumRewards: '344382187878289278175', | ||
}, | ||
], | ||
} | ||
|
||
it('should correctly modify a single reward', () => { | ||
const userReward = sampleInput.userRewards[0] | ||
|
||
const result = modifyUserReward(userReward) | ||
|
||
expect(result).toEqual({ | ||
rewards: 344.37992247514865, | ||
}) | ||
}) | ||
|
||
it('should correctly modify multiple rewards', () => { | ||
const expectedResult = { | ||
days: { | ||
1694908800: { | ||
rewards: 344.37992247514865, | ||
}, | ||
1694995200: { | ||
rewards: 344.3821878782893, | ||
}, | ||
}, | ||
} | ||
|
||
const result = modifyUserRewards(sampleInput) | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) |
26 changes: 26 additions & 0 deletions
26
src/methods/vault/requests/getUserRewards/modifyUserRewards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { formatEther } from 'ethers' | ||
|
||
import { ModifyUserReward } from './types' | ||
import { UserRewardsQueryPayload } from '../../../../graphql/backend/vault' | ||
|
||
|
||
export const modifyUserReward = (reward: Omit<UserRewardsQueryPayload['userRewards'][number], 'date'>) => { | ||
const totalRewards = reward.sumRewards || '0' | ||
|
||
return { | ||
rewards: Number(formatEther(totalRewards)), | ||
} | ||
} | ||
|
||
const modifyUserRewards = (input: UserRewardsQueryPayload): ModifyUserReward => { | ||
const days = input.userRewards.reduce((acc, { date, ...rest }) => { | ||
acc[Number(date)] = modifyUserReward(rest) | ||
|
||
return acc | ||
}, {} as ModifyUserReward['days']) | ||
|
||
return { days } | ||
} | ||
|
||
|
||
export default modifyUserRewards |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type UserReward = { | ||
rewards: number | ||
} | ||
|
||
export type ModifyUserReward = { | ||
days: Record<number, UserReward> | ||
} |