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

Reward constants hook #4692

Merged
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
["@bookmark", "./src/modules/bookmark/"],
["@common", "./src/modules/common/"],
["@pos", "./src/modules/pos/"],
["@reward", "./src/modules/reward/"],
["@network", "./src/modules/network/"],
["@settings", "./src/modules/settings/"],
["@token", "./src/modules/token/"],
Expand Down
3 changes: 1 addition & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pipeline {
steps {
nvm(getNodejsVersion()) {
sh '''
rm package-lock.json
npm i --registry https://npm.lisk.com
npm ci --registry https://npm.lisk.com
'''
}
}
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@common/*": ["./src/modules/common/*"],
"@legacy/*": ["./src/modules/legacy/*"],
"@pos/*": ["./src/modules/pos/*"],
"@reward/*": ["./src/modules/reward/*"],
"@network/*": ["./src/modules/network/*"],
"@settings/*": ["./src/modules/settings/*"],
"@token/*": ["./src/modules/token/*"],
Expand Down
1 change: 1 addition & 0 deletions setup/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const config = {
'@auth': resolve('./src/modules/auth'),
'@wallet': resolve('./src/modules/wallet'),
'@pos': resolve('./src/modules/pos'),
'@reward': resolve('./src/modules/reward'),
'@network': resolve('./src/modules/network'),
'@settings': resolve('./src/modules/settings'),
'@token': resolve('./src/modules/token'),
Expand Down
1 change: 1 addition & 0 deletions setup/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
'^@legacy(.*)$': resolve(__dirname, '../src/modules/legacy/$1'),
'^@message(.*)$': resolve(__dirname, '../src/modules/message/$1'),
'^@pos(.*)$': resolve(__dirname, '../src/modules/pos/$1'),
'^@reward(.*)$': resolve(__dirname, '../src/modules/reward/$1'),
'^@network(.*)$': resolve(__dirname, '../src/modules/network/$1'),
'^@settings(.*)$': resolve(__dirname, '../src/modules/settings/$1'),
'^@token(.*)$': resolve(__dirname, '../src/modules/token/$1'),
Expand Down
3 changes: 2 additions & 1 deletion src/const/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ 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';
export const POS_REWARDS_INFLATION = 'GET_POS_REWARDS_INFLATION';
soroushm marked this conversation as resolved.
Show resolved Hide resolved
export const REWARD_INFLATION = 'GET_REWARD_INFLATION';
export const REWARD_CONSTANTS = 'GET_REWARD_CONSTANTS';
1 change: 0 additions & 1 deletion src/modules/pos/reward/__fixtures__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './mockRewardsLocked';
export * from './mockRewardsClaimable';
export * from './mockRewardsInflation';
1 change: 0 additions & 1 deletion src/modules/pos/reward/hooks/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './useRewardsClaimable';
export * from './useRewardsLocked';
export * from './useRewardsInflation';
4 changes: 1 addition & 3 deletions src/modules/pos/reward/mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { rest } from 'msw';
import { API_VERSION } from 'src/const/config';
import { mockRewardsClaimable, mockRewardsInflation, mockRewardsLocked } from '@pos/reward/__fixtures__';
import { mockRewardsClaimable, mockRewardsLocked } from '@pos/reward/__fixtures__';

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)));

export const posRewardsInflation = rest.get(`*/api/${API_VERSION}/pos/rewards/inflation`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardsInflation)));
1 change: 0 additions & 1 deletion src/modules/pos/validator/__fixtures__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export * from './mockForgers';
export * from './mockGenerator';
export * from './mockRewardsLocked';
export * from './mockRewardsClaimable';
export * from './mockRewardsInflation';

This file was deleted.

2 changes: 2 additions & 0 deletions src/modules/reward/__fixtures__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mockRewardConstants';
export * from './mockRewardInflation';
6 changes: 6 additions & 0 deletions src/modules/reward/__fixtures__/mockRewardConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const mockRewardConstants = {
"data": {
"rewardTokenID": "0000000000000000"
},
"meta": {}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const mockRewardsInflation = {
export const mockRewardInflation = {
"data": {
"rate": "10.32",
"tokenID": "0000000000000000"
Expand Down
2 changes: 2 additions & 0 deletions src/modules/reward/hooks/queries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useRewardConstants';
export * from './useRewardInflation';
34 changes: 34 additions & 0 deletions src/modules/reward/hooks/queries/useRewardConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { REWARD_CONSTANTS } from 'src/const/queries';
import {
API_VERSION,
} from 'src/const/config';
import { useCustomQuery } from '@common/hooks';

/**
* Creates a custom hook to fetch reward constants
*
* @param {object} [configuration] - the custom query configuration object
* @param {object} [configuration.config] - the query config
* @param {string} [configuration.options] - the query options
*
* @returns the query object
*/

export const useRewardConstants = ({ config: customConfig = {}, options } = { }) => {
const config = {
url: `/api/${API_VERSION}/reward/constants`,
method: 'get',
event: 'get.reward.constants',
...customConfig,
};

return useCustomQuery({
keys: [REWARD_CONSTANTS],
config,
options: {
cacheTime: Infinity,
staleTime: Infinity,
...options,
},
});
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { renderHook } from '@testing-library/react-hooks';
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper';
import { mockRewardsInflation } from '@pos/validator/__fixtures__';
import { useRewardsInflation } from './useRewardsInflation';
import { mockRewardConstants } from '@reward/__fixtures__';
import { useRewardConstants } from '@reward/hooks/queries';

jest.useRealTimers();

describe('useRewardsInflation hook', () => {
describe('useRewardConstants hook', () => {
it('fetching data correctly', async () => {
const { result, waitFor } = renderHook(() => useRewardsInflation(), { wrapper });
const { result, waitFor } = renderHook(() => useRewardConstants(), { wrapper });
expect(result.current.isLoading).toBeTruthy();
await waitFor(() => result.current.isFetched);
expect(result.current.isSuccess).toBeTruthy();
expect(result.current.data).toEqual(mockRewardsInflation);
expect(result.current.data).toEqual(mockRewardConstants);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { POS_REWARDS_INFLATION } from 'src/const/queries';
import { REWARD_INFLATION } from 'src/const/queries';
import {
API_VERSION,
} from 'src/const/config';
import { useCustomQuery } from 'src/modules/common/hooks';
import { useCustomQuery } from '@common/hooks';

/**
* Creates a custom hook to fetch the current inflation rate in the total supply because of the token being rewarded (block generation)/ burnt (transaction minFee & extraFees).
Expand All @@ -14,16 +14,16 @@ import { useCustomQuery } from 'src/modules/common/hooks';
* @returns the query object
*/

export const useRewardsInflation = ({ config: customConfig = {}, options } = {}) => {
export const useRewardInflation = ({ config: customConfig = {}, options } = {}) => {
const config = {
url: `/api/${API_VERSION}/pos/rewards/inflation`,
url: `/api/${API_VERSION}/reward/inflation`,
method: 'get',
event: 'get.pos.rewards.inflation',
event: 'get.reward.inflation',
...customConfig,
};

return useCustomQuery({
keys: [POS_REWARDS_INFLATION],
keys: [REWARD_INFLATION],
config,
options,
});
Expand Down
16 changes: 16 additions & 0 deletions src/modules/reward/hooks/queries/useRewardInflation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { renderHook } from '@testing-library/react-hooks';
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper';
import { mockRewardInflation } from '@reward/__fixtures__';
import { useRewardInflation } from './useRewardInflation';

jest.useRealTimers();

describe('useRewardInflation hook', () => {
it('fetching data correctly', async () => {
const { result, waitFor } = renderHook(() => useRewardInflation(), { wrapper });
expect(result.current.isLoading).toBeTruthy();
await waitFor(() => result.current.isFetched);
expect(result.current.isSuccess).toBeTruthy();
expect(result.current.data).toEqual(mockRewardInflation);
});
});
7 changes: 7 additions & 0 deletions src/modules/reward/mocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { rest } from 'msw';
import { API_VERSION } from 'src/const/config';
import { mockRewardConstants, mockRewardInflation } from 'src/modules/reward/__fixtures__';

export const rewardConstants = rest.get(`*/api/${API_VERSION}/reward/constants`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardConstants)));

soroushm marked this conversation as resolved.
Show resolved Hide resolved
export const rewardInflation = rest.get(`*/api/${API_VERSION}/reward/inflation`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardInflation)));
27 changes: 13 additions & 14 deletions src/modules/transaction/utils/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*
*/

import { codec } from '@liskhq/lisk-codec';
import { utils } from '@liskhq/lisk-cryptography';
import { codec as liskCodec, cryptography } from '@liskhq/lisk-client';
import { trimBigintString } from './helpers';
import { joinModuleAndCommand } from './moduleCommand';

Expand Down Expand Up @@ -69,12 +68,12 @@ export const getCommandParamsSchema = (module, command, schema) => {
};

export const decodeBaseTransaction = (encodedTransaction) =>
codec.decode(baseTransactionSchema, encodedTransaction);
liskCodec.codec.decode(baseTransactionSchema, encodedTransaction);

export const decodeTransaction = (encodedTransaction, paramsSchema) => {
const transaction = decodeBaseTransaction(encodedTransaction);
const params = paramsSchema ? codec.decode(paramsSchema, transaction.params) : {};
const id = utils.hash(encodedTransaction);
const params = paramsSchema ? liskCodec.codec.decode(paramsSchema, transaction.params) : {};
const id = cryptography.utils.hash(encodedTransaction);
return {
...transaction,
params,
Expand All @@ -85,12 +84,12 @@ export const decodeTransaction = (encodedTransaction, paramsSchema) => {
export const encodeTransaction = (transaction, paramsSchema) => {
let encodedParams;
if (!Buffer.isBuffer(transaction.params)) {
encodedParams = paramsSchema ? codec.encode(paramsSchema, transaction.params) : Buffer.alloc(0);
encodedParams = paramsSchema ? liskCodec.codec.encode(paramsSchema, transaction.params) : Buffer.alloc(0);
} else {
encodedParams = transaction.params;
}

const decodedTransaction = codec.encode(baseTransactionSchema, {
const decodedTransaction = liskCodec.codec.encode(baseTransactionSchema, {
...transaction,
params: encodedParams,
});
Expand All @@ -100,18 +99,18 @@ export const encodeTransaction = (transaction, paramsSchema) => {

export const fromTransactionJSON = (transactionJSON, paramsSchema) => {
transactionJSON = trimBigintString(transactionJSON);
const tx = codec.fromJSON(baseTransactionSchema, {
const tx = liskCodec.codec.fromJSON(baseTransactionSchema, {
...transactionJSON,
params: '',
});

let params;
if (typeof transactionJSON.params === 'string') {
params = paramsSchema
? codec.decode(paramsSchema, Buffer.from(transactionJSON.params, 'hex'))
? liskCodec.codec.decode(paramsSchema, Buffer.from(transactionJSON.params, 'hex'))
: {};
} else {
params = paramsSchema ? codec.fromJSON(paramsSchema, transactionJSON.params) : {};
params = paramsSchema ? liskCodec.codec.fromJSON(paramsSchema, transactionJSON.params) : {};
}

return {
Expand All @@ -124,17 +123,17 @@ export const fromTransactionJSON = (transactionJSON, paramsSchema) => {
export const toTransactionJSON = (transaction, paramsSchema) => {
if (Buffer.isBuffer(transaction.params)) {
return {
...codec.toJSON(baseTransactionSchema, transaction),
params: paramsSchema ? codec.decodeJSON(paramsSchema, transaction.params) : {},
...liskCodec.codec.toJSON(baseTransactionSchema, transaction),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have ticket for liskCodec changes? if not please create one and assign to the pr

params: paramsSchema ? liskCodec.codec.decodeJSON(paramsSchema, transaction.params) : {},
id: transaction.id.toString('hex'),
};
}
return {
...codec.toJSON(baseTransactionSchema, {
...liskCodec.codec.toJSON(baseTransactionSchema, {
...transaction,
params: Buffer.alloc(0),
}),
params: paramsSchema ? codec.toJSON(paramsSchema, transaction.params) : {},
params: paramsSchema ? liskCodec.codec.toJSON(paramsSchema, transaction.params) : {},
id: transaction.id && transaction.id.toString('hex'),
};
};
8 changes: 4 additions & 4 deletions src/modules/transaction/utils/encoding.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as keys from '@tests/constants/keys';
import { codec } from '@liskhq/lisk-codec';
import { mockCommandParametersSchemas } from 'src/modules/common/__fixtures__';
import { utils } from '@liskhq/lisk-cryptography';
import { cryptography } from '@liskhq/lisk-client';
import {
decodeTransaction,
encodeTransaction,
Expand All @@ -14,7 +14,7 @@ jest.spyOn(codec, 'decode');
jest.spyOn(codec, 'toJSON');
jest.spyOn(codec, 'decodeJSON');
jest.spyOn(codec, 'encode');
jest.spyOn(utils, 'hash');
jest.spyOn(cryptography.utils, 'hash');

describe('encoding', () => {
const moduleCommandSchemas = mockCommandParametersSchemas.data.reduce(
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('encoding', () => {
});
});

it('should create transaction whoose id is an empty buffer', () => {
it('should create transaction id with an empty buffer', () => {
codec.decode.mockReturnValue('params-decoded');

const transactionJSON = {
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('encoding', () => {
codec.decode.mockReturnValue({
nonce: '1n',
});
utils.hash.mockReturnValue(Buffer.from('test-id'));
cryptography.utils.hash.mockReturnValue(Buffer.from('test-id'));

expect(decodeTransaction(Buffer.alloc(0))).toEqual({
id: Buffer.from('test-id'),
Expand Down
7 changes: 3 additions & 4 deletions src/modules/transaction/utils/transaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable complexity */
/* eslint-disable max-lines */
import { transactions, cryptography, codec } from '@liskhq/lisk-client';
import { constants } from '@liskhq/lisk-cryptography';
import { to } from 'await-to-js';
import { MODULE_COMMANDS_NAME_MAP } from 'src/modules/transaction/configuration/moduleCommand';
import { DEFAULT_NUMBER_OF_SIGNATURES } from '@transaction/configuration/transactions';
Expand Down Expand Up @@ -29,8 +28,8 @@ const multisigRegMsgSchema = {
address: {
dataType: 'bytes',
fieldNumber: 1,
minLength: constants.BINARY_ADDRESS_LENGTH,
maxLength: constants.BINARY_ADDRESS_LENGTH,
minLength: cryptography.constants.BINARY_ADDRESS_LENGTH,
maxLength: cryptography.constants.BINARY_ADDRESS_LENGTH,
},
nonce: {
dataType: 'uint64',
Expand Down Expand Up @@ -306,7 +305,7 @@ const signUsingPrivateKey = (wallet, schema, chainID, transaction, privateKey) =
}
}

// Sign the tx only if the account is the initator of the tx
// Sign the tx only if the account is the initiator of the tx

const { mandatoryKeys, optionalKeys, numberOfSignatures } = wallet.keys;
const isSender = Buffer.compare(transaction.senderPublicKey, publicKeyBuffer) === 0;
Expand Down
9 changes: 4 additions & 5 deletions src/modules/transaction/utils/transactions.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { legacy } from '@liskhq/lisk-cryptography';
import { codec } from '@liskhq/lisk-codec';
import { codec as liskCodec, cryptography } from '@liskhq/lisk-client';
import { mockNetworkStatus } from '@network/__fixtures__';
import { mockAuth } from '@auth/__fixtures__';
import { mockCommandParametersSchemas } from '../../common/__fixtures__';
Expand All @@ -16,7 +15,7 @@ import {
describe.skip('Transaction', () => {
let tokenTransfer = new Transaction();
const recoveryPhrase = 'target cancel solution recipe vague faint bomb convince pink vendor fresh patrol';
const { privateKey, publicKey } = legacy.getKeys(recoveryPhrase);
const { privateKey, publicKey } = cryptography.legacy.getKeys(recoveryPhrase);
const pubkey = publicKey.toString('hex');
const module = 'token';
const command = 'transfer';
Expand Down Expand Up @@ -284,7 +283,7 @@ describe.skip('Transaction', () => {
commandParametersSchemas: mockCommandParametersSchemas.data,
encodedTransaction,
});
const encodedParams = codec.encode(
const encodedParams = liskCodec.codec.encode(
tokenTransfer._paramsSchema,
tokenTransfer.transaction.params,
);
Expand Down Expand Up @@ -325,7 +324,7 @@ describe.skip('Transaction', () => {
commandParametersSchemas: mockCommandParametersSchemas.data,
encodedTransaction,
});
const encodedParams = codec.encode(
const encodedParams = liskCodec.codec.encode(
tokenTransfer._paramsSchema,
tokenTransfer.transaction.params,
);
Expand Down
Loading