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

Implement query hook rewards locked #4684

Merged
merged 8 commits into from
Dec 28, 2022
2 changes: 2 additions & 0 deletions src/const/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export const BLOCKCHAIN_APPS_STATICS = 'GET_BLOCKCHAIN_APPS_STATICS';
export const BLOCKCHAIN_APPS_META = 'GET_BLOCKCHAIN_APPS_META';
export const BLOCKCHAIN_APPS_META_TOKENS = 'GET_BLOCKCHAIN_APPS_META_TOKENS';
export const POS_CONSTANTS = 'GET_POS_CONSTANTS';
export const POS_REWARDS_LOCKED = 'GET_POS_REWARDS_LOCKED';
export const POS_REWARDS_CLAIMABLE = 'GET_POS_REWARDS_CLAIMABLE';
2 changes: 2 additions & 0 deletions src/modules/pos/validator/__fixtures__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './mockReceivedVotes';
export * from './mockUnlocks';
export * from './mockForgers';
export * from './mockGenerator';
export * from './mockRewardsLocked';
export * from './mockRewardsClaimable';
13 changes: 13 additions & 0 deletions src/modules/pos/validator/__fixtures__/mockRewardsClaimable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const mockRewardsClaimable = {
"data": [
{
"reward": "0",
"tokenID": "0000000000000000"
},
],
"meta": {
"count": 1,
"offset": 0,
"total": 1
}
};
21 changes: 21 additions & 0 deletions src/modules/pos/validator/__fixtures__/mockRewardsLocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const mockRewardsLocked = {
"data": [
{
"reward": "10",
"tokenID": "0000000000000000"
},
{
"reward": "20",
"tokenID": "0000000000000000"
},
{
"reward": "30",
"tokenID": "0000000000000000"
},
],
"meta": {
"count": 3,
"offset": 0,
"total": 3
}
};
2 changes: 2 additions & 0 deletions src/modules/pos/validator/hooks/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export * from './useReceivedStakes';
export * from './useSentStakes';
export * from './useUnlocks';
export * from './useForgersValidator';
export * from './useRewardsLocked';
export * from './usePosConstants';
export * from './useRewardsClaimable';
35 changes: 35 additions & 0 deletions src/modules/pos/validator/hooks/queries/useRewardsClaimable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { POS_REWARDS_CLAIMABLE } from 'src/const/queries';
import {
API_VERSION,
} from 'src/const/config';
import { useCustomQuery } from 'src/modules/common/hooks';

/**
* Creates a custom hook to fetch claimable rewards by address
*
* @param {object} configuration - the custom query configuration object
* @param {object} configuration.config - the query config
* @param {object} configuration.config.params - the query config params
* @param {string} [configuration.config.params.address] - account address
* @param {string} [configuration.config.params.publicKey] - publicKey
* @param {string} [configuration.config.params.name] - name
* @param {string} [configuration.config.params.limit] - limit
* @param {string} [configuration.config.params.offset] - offset
* @param {string} [configuration.options] - the query options
*
* @returns the query object
*/

export const useRewardsClaimable = ({ config: customConfig = {}, options } = { }) => {
const config = {
url: `/api/${API_VERSION}/pos/rewards/claimable`,
method: 'get',
event: 'get.pos.rewards.claimable',
...customConfig,
};
return useCustomQuery({
keys: [POS_REWARDS_CLAIMABLE],
config,
options,
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { renderHook } from '@testing-library/react-hooks';
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper';
import { mockRewardsClaimable } from '@pos/validator/__fixtures__';
import { useRewardsClaimable } from './useRewardsClaimable';

jest.useRealTimers();

describe('useRewardsClaimable hook', () => {
const config = { params: { address:'lsktzb4j7e3knk4mkxckdr3y69gtu2nwmsb3hjbkg' } };

it('fetching data correctly', async () => {
const { result, waitFor } = renderHook(() => useRewardsClaimable({ config }), { wrapper });
expect(result.current.isLoading).toBeTruthy();
await waitFor(() => result.current.isFetched);
expect(result.current.isSuccess).toBeTruthy();
expect(result.current.data).toEqual(mockRewardsClaimable);
});
});
35 changes: 35 additions & 0 deletions src/modules/pos/validator/hooks/queries/useRewardsLocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { POS_REWARDS_LOCKED } from 'src/const/queries';
import {
API_VERSION,
} from 'src/const/config';
import { useCustomQuery } from 'src/modules/common/hooks';

/**
* Creates a custom hook to fetch claimable rewards by address
*
* @param {object} configuration - the custom query configuration object
* @param {object} configuration.config - the query config
* @param {object} configuration.config.params - the query config params
* @param {string} configuration.config.params.address - account address
* @param {string} configuration.config.params.publicKey - publicKey
* @param {string} configuration.config.params.name - name
* @param {string} configuration.config.params.limit - limit
* @param {string} configuration.config.params.offset - offset
soroushm marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} configuration.options - the query options
*
* @returns the query object
*/

export const useRewardsLocked = ({ config: customConfig = {}, options } = { }) => {
const config = {
url: `/api/${API_VERSION}/pos/rewards/locked`,
method: 'get',
event: 'get.pos.rewards.locked',
...customConfig,
};
return useCustomQuery({
keys: [POS_REWARDS_LOCKED],
config,
options,
});
};
18 changes: 18 additions & 0 deletions src/modules/pos/validator/hooks/queries/useRewardsLocked.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { renderHook } from '@testing-library/react-hooks';
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper';
import { mockRewardsLocked } from '@pos/validator/__fixtures__';
import { useRewardsLocked } from './useRewardsLocked';

jest.useRealTimers();

describe('useRewardsLocked hook', () => {
const config = { params: { address:'lsktzb4j7e3knk4mkxckdr3y69gtu2nwmsb3hjbkg' } };

it('fetching data correctly', async () => {
const { result, waitFor } = renderHook(() => useRewardsLocked({ config }), { wrapper });
expect(result.current.isLoading).toBeTruthy();
await waitFor(() => result.current.isFetched);
expect(result.current.isSuccess).toBeTruthy();
expect(result.current.data).toEqual(mockRewardsLocked);
});
});
6 changes: 6 additions & 0 deletions src/modules/pos/validator/mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
mockUnlocks,
mockForgers,
mockGenerator,
mockRewardsLocked,
mockRewardsClaimable,
} from '@pos/validator/__fixtures__';
import composeMockList from 'src/modules/common/utils/composeMockList';
import { mockPosConstants } from '../__fixtures__/mockPosConstants';
Expand Down Expand Up @@ -95,3 +97,7 @@ export const generators = rest.get(`*/api/${API_VERSION}/generators`, async (req
export const posConstants = rest.get(`*/api/${API_VERSION}/dpos/constants`, async (_, res, ctx) =>
res(ctx.delay(20), ctx.json(mockPosConstants))
);

export const posRewardsLocked = rest.get(`*/api/${API_VERSION}/pos/rewards/locked`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardsLocked)));

export const posRewardsClaimable = rest.get(`*/api/${API_VERSION}/pos/rewards/claimable`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardsClaimable)));