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

Validator Api calls made with js sdk, Fixed config types and test #614

Merged
merged 1 commit into from
Apr 10, 2021
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"resolve-url-loader": "^3.1.2",
"semver": "7.3.2",
"terser-webpack-plugin": "^4.2.3",
"thenewboston": "^1.1.0-alpha.0",
"thenewboston": "^1.1.0-alpha.1",
"ts-pnp": "^1.2.0",
"tweetnacl": "^1.0.3",
"typeface-roboto": "^0.0.75",
Expand Down
6 changes: 2 additions & 4 deletions src/renderer/dispatchers/balances.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import axios from 'axios';
import {getPrimaryValidator, getManagedAccounts} from '@renderer/selectors';
import {setAccountBalance} from '@renderer/store/accountBalances';
import {setManagedAccountBalance} from '@renderer/store/managedAccountBalances';
import {AppDispatch, Balance, RootState} from '@renderer/types';
import {AppDispatch, RootState} from '@renderer/types';
import {formatAddressFromNode} from '@renderer/utils/address';
import {AXIOS_TIMEOUT_MS} from '@renderer/config';
import {Account, PrimaryValidator} from 'thenewboston';
import {PrimaryValidator} from 'thenewboston';

export const fetchAccountBalance = (accountNumber: string) => async (
dispatch: AppDispatch,
Expand Down
11 changes: 3 additions & 8 deletions src/renderer/dispatchers/banks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import {Bank} from 'thenewboston';

import {
Expand Down Expand Up @@ -47,11 +46,7 @@ import {
RawBankConfig,
ValidatorConfirmationService,
} from '@renderer/types';
import {
fetchPaginatedResults,
fetchBankPaginatedResults,
sanitizePortFieldFromRawBankConfig,
} from '@renderer/utils/api';
import {fetchBankPaginatedResults, sanitizePortFieldFromRawBankConfig} from '@renderer/utils/api';

export const fetchBankAccounts = (address: string, params: PaginatedQueryParams) => async (dispatch: AppDispatch) => {
return fetchBankPaginatedResults<BankAccount>(
Expand Down Expand Up @@ -89,8 +84,8 @@ export const fetchBankConfig = (address: string) => async (
dispatch: AppDispatch,
): Promise<{address: string; data?: BankConfig; error?: any}> => {
try {
const rawData = (await new Bank(address).getConfig()) as RawBankConfig;
const data = sanitizePortFieldFromRawBankConfig(rawData);
const rawData = await new Bank(address).getConfig();
const data = sanitizePortFieldFromRawBankConfig(rawData as RawBankConfig);

if (data.node_type !== NodeType.bank) {
const errorObject = {address, error: 'Node not a bank'};
Expand Down
23 changes: 12 additions & 11 deletions src/renderer/dispatchers/validators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import {ConfirmationValidator} from 'thenewboston';

import {AXIOS_TIMEOUT_MS, defaultPaginatedQueryParam} from '@renderer/config';
import {defaultPaginatedQueryParam} from '@renderer/config';
import {ACCOUNTS, BANKS, VALIDATORS} from '@renderer/constants/actions';
import {
setValidatorAccounts,
Expand All @@ -17,18 +17,18 @@ import {
BaseValidator,
NodeType,
PaginatedQueryParams,
RawPrimaryValidatorConfig,
RawValidatorConfig,
ValidatorAccount,
ValidatorBank,
ValidatorConfig,
} from '@renderer/types';
import {fetchPaginatedResults, sanitizePortFieldFromRawPrimaryValidatorConfig} from '@renderer/utils/api';
import {fetchValidatorPaginatedResults, sanitizePortFieldFromRawValidatorConfig} from '@renderer/utils/api';

export const fetchValidatorAccounts = (
address: string,
params: PaginatedQueryParams = defaultPaginatedQueryParam,
) => async (dispatch: AppDispatch) => {
return fetchPaginatedResults<ValidatorAccount>(
return fetchValidatorPaginatedResults<ValidatorAccount>(
address,
ACCOUNTS,
params,
Expand All @@ -42,7 +42,7 @@ export const fetchValidatorBanks = (
address: string,
params: PaginatedQueryParams = defaultPaginatedQueryParam,
) => async (dispatch: AppDispatch) => {
return fetchPaginatedResults<ValidatorBank>(
return fetchValidatorPaginatedResults<ValidatorBank>(
address,
BANKS,
params,
Expand All @@ -56,10 +56,11 @@ export const fetchValidatorConfig = (address: string) => async (
dispatch: AppDispatch,
): Promise<{address: string; data?: ValidatorConfig; error?: any}> => {
try {
const {data: rawData} = await axios.get<RawPrimaryValidatorConfig>(`${address}/config`, {
timeout: AXIOS_TIMEOUT_MS,
});
const data = sanitizePortFieldFromRawPrimaryValidatorConfig(rawData);
const validator = new ConfirmationValidator(address);

const rawData = await validator.getConfig();

const data = sanitizePortFieldFromRawValidatorConfig(rawData as RawValidatorConfig);

if (data.node_type !== NodeType.primaryValidator && data.node_type !== NodeType.confirmationValidator) {
const errorObject = {address, error: 'Node not a validator'};
Expand Down Expand Up @@ -88,7 +89,7 @@ export const fetchValidatorValidators = (
address: string,
params: PaginatedQueryParams = defaultPaginatedQueryParam,
) => async (dispatch: AppDispatch) => {
return fetchPaginatedResults<BaseValidator>(
return fetchValidatorPaginatedResults<BaseValidator>(
address,
VALIDATORS,
params,
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
ProtocolType,
RawBankConfig,
RawPrimaryValidatorConfig,
RawValidatorConfig,
Tx,
ValidatorAccount,
ValidatorBank,
Expand Down Expand Up @@ -137,6 +138,7 @@ export {
ProtocolType,
RawBankConfig,
RawPrimaryValidatorConfig,
RawValidatorConfig,
RootState,
SocketConnectionStatus,
SocketType,
Expand Down
18 changes: 14 additions & 4 deletions src/renderer/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface BankAccount extends AccountNumber, CreatedModified, Id {

export interface BankConfig extends Omit<Node, 'trust'> {
node_type: NodeType.bank;
primary_validator: Omit<PrimaryValidatorConfig, 'node_type'>;
primary_validator: PrimaryValidatorInNodeConfig;
}

export interface BankConfirmationBlock extends CreatedModified, Id {
Expand Down Expand Up @@ -120,21 +120,31 @@ export interface PaginatedResults<T> {

export type PaginatedResultsWithError<T> = PaginatedResults<T> & Error;

export interface PrimaryValidatorConfig extends BaseValidator {
export interface PrimaryValidatorConfig extends Omit<BaseValidator, 'trust'> {
node_type: NodeType.primaryValidator;
}

export type ProtocolType = 'http' | 'https';

export interface RawBankConfig extends Omit<BankConfig, 'port' | 'primary_validator'> {
port: number | null;
primary_validator: Omit<RawPrimaryValidatorConfig, 'node_type'>;
primary_validator: RawPrimaryValidatorInNodeConfig;
}

export type PrimaryValidatorInNodeConfig = Omit<BaseValidator, 'node_type'>;

export interface RawPrimaryValidatorInNodeConfig extends Omit<PrimaryValidatorInNodeConfig, 'port'> {
port: number | null;
}

export interface RawPrimaryValidatorConfig extends Omit<PrimaryValidatorConfig, 'port'> {
port: number | null;
}

export interface RawValidatorConfig extends Omit<ValidatorConfig, 'port'> {
port: number | null;
}

export interface Tx {
amount: number;
fee?: AcceptedFees;
Expand All @@ -153,7 +163,7 @@ export interface ValidatorBank extends Node {
confirmation_expiration: string | null;
}

export interface ValidatorConfig extends BaseValidator {
export interface ValidatorConfig extends Omit<BaseValidator, 'trust'> {
node_type: NodeType.primaryValidator | NodeType.confirmationValidator;
}

Expand Down
96 changes: 61 additions & 35 deletions src/renderer/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios from 'axios';
import {Bank} from 'thenewboston';
import {Bank, ConfirmationValidator} from 'thenewboston';

import {AXIOS_TIMEOUT_MS, defaultPaginatedQueryParam} from '@renderer/config';
import {defaultPaginatedQueryParam} from '@renderer/config';
import {
ACCOUNTS,
BANK_TRANSACTIONS,
Expand All @@ -18,28 +17,13 @@ import {
BankConfig,
PaginatedQueryParams,
PaginatedResults,
PrimaryValidatorConfig,
RawBankConfig,
RawPrimaryValidatorConfig,
RawValidatorConfig,
ValidatorConfig,
} from '@renderer/types';

import {formatQueryParams} from '@renderer/utils/address';
import {SetError, SetResults} from '@renderer/utils/store';

const formatPaginatedData = <T extends unknown>(rawData: PaginatedResults<T>) => {
return {
...rawData,
results: rawData.results.map((result: any) => {
if (!result.port) {
return {
...result,
port: replaceNullPortFieldWithDefaultValue(result.port),
};
}
return result;
}),
};
};
import {PrimaryValidatorInNodeConfig, RawPrimaryValidatorInNodeConfig} from '@renderer/types/network';

export async function fetchBankPaginatedResults<T>(
address: string,
Expand All @@ -49,26 +33,26 @@ export async function fetchBankPaginatedResults<T>(
setResults: SetResults<T>,
setError: SetError,
) {
const bank = new Bank(address, {defaultPagination: {limit: defaultPaginatedQueryParam.limit, offset: 0}});
const bank = new Bank(address, {defaultPagination: {...defaultPaginatedQueryParam, offset: 0}});

let rawData;

try {
switch (action) {
case ACCOUNTS:
rawData = await bank.getAccounts();
rawData = await bank.getAccounts(queryParams);
break;

case BANKS:
rawData = await bank.getBanks();
rawData = await bank.getBanks(queryParams);
break;

case BANK_TRANSACTIONS:
rawData = await bank.getTransactions();
rawData = await bank.getTransactions(queryParams);
break;

case BLOCKS:
rawData = await bank.getBlocks();
rawData = await bank.getBlocks(queryParams);
break;

case CONFIRMATION_BLOCKS:
Expand Down Expand Up @@ -103,21 +87,41 @@ export async function fetchBankPaginatedResults<T>(
}
}

export async function fetchPaginatedResults<T>(
export async function fetchValidatorPaginatedResults<T>(
address: string,
urlParam: string,
action: string,
queryParams: PaginatedQueryParams,
dispatch: AppDispatch,
setResults: SetResults<T>,
setError: SetError,
) {
const validator = new ConfirmationValidator(address, {
defaultPagination: {limit: defaultPaginatedQueryParam.limit, offset: 0},
});

let rawData;
let data: PaginatedResults<T>;

try {
const {data: rawData} = await axios.get<PaginatedResults<T>>(
`${address}/${urlParam}${formatQueryParams(queryParams)}`,
{timeout: AXIOS_TIMEOUT_MS},
);
switch (action) {
case ACCOUNTS:
rawData = await validator.getAccounts(queryParams);
data = formatPaginatedData(rawData);
break;

const data = formatPaginatedData(rawData);
case BANKS:
rawData = await validator.getBanks(queryParams);
data = formatPaginatedData(rawData);
break;

case VALIDATORS:
rawData = await validator.getValidators(queryParams);
data = formatPaginatedData(rawData);
break;

default:
throw new Error('Fetch Validator Pagination Action not Set');
}

dispatch(setResults({address, ...data}));
return data.results;
Expand All @@ -129,6 +133,21 @@ export async function fetchPaginatedResults<T>(
}
}

function formatPaginatedData<T>(rawData: PaginatedResults<T>) {
return {
...rawData,
results: rawData.results.map((result: any) => {
if (!result.port) {
return {
...result,
port: replaceNullPortFieldWithDefaultValue(result.port),
};
}
return result;
}),
};
}

const replaceNullPortFieldWithDefaultValue = (port: number | null): number => {
if (port === null) {
return 80;
Expand All @@ -144,9 +163,16 @@ export const sanitizePortFieldFromRawBankConfig = (data: RawBankConfig): BankCon
};
};

export const sanitizePortFieldFromRawValidatorConfig = (data: RawValidatorConfig): ValidatorConfig => {
return {
...data,
port: replaceNullPortFieldWithDefaultValue(data.port),
};
};

export const sanitizePortFieldFromRawPrimaryValidatorConfig = (
data: RawPrimaryValidatorConfig,
): PrimaryValidatorConfig => {
data: RawPrimaryValidatorInNodeConfig,
): PrimaryValidatorInNodeConfig => {
return {
...data,
port: replaceNullPortFieldWithDefaultValue(data.port),
Expand Down
Loading