Skip to content

Commit

Permalink
Finish eslint migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Oct 5, 2023
1 parent 3345b65 commit 33a306d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 67 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module.exports = {
extends: ['./node_modules/commons/dist/eslint/universal', './node_modules/commons/dist/eslint/jest'],
extends: ['./node_modules/@api3/commons/dist/eslint/universal', './node_modules/@api3/commons/dist/eslint/jest'],
parserOptions: {
project: ['./tsconfig.json', './packages/*/tsconfig.json'],
},
rules: {
'unicorn/consistent-function-scoping': 'off', // Disabling due to the rule's constraints conflicting with established patterns, especially in test suites where local helper or mocking functions are prevalent and do not necessitate exports.
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"keywords": [],
"license": "MIT",
"devDependencies": {
"@api3/commons": "^0.1.0",
"@types/jest": "^29.5.5",
"@types/node": "^20.8.0",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"commons": "^1.0.4",
"eslint": "^8.50.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.4.2",
Expand Down
48 changes: 24 additions & 24 deletions packages/api/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { generateErrorResponse, isBatchUnique } from './utils';
export const batchInsertData = async (requestBody: unknown): Promise<ApiResponse> => {
const goValidateSchema = await go(async () => batchSignedDataSchema.parseAsync(requestBody));
if (!goValidateSchema.success) {
return generateErrorResponse(
return generateErrorResponse(
400,
'Invalid request, body must fit schema for batch of signed data',
goValidateSchema.error.message
);
}
);
}

// Ensure there is at least one signed data to push
const batchSignedData = goValidateSchema.data;
Expand All @@ -31,8 +31,8 @@ export const batchInsertData = async (requestBody: unknown): Promise<ApiResponse
// Check whether the size of batch exceeds a maximum batch size
const { maxBatchSize, endpoints } = getConfig();
if (size(batchSignedData) > maxBatchSize) {
return generateErrorResponse(400, `Maximum batch size (${maxBatchSize}) exceeded`);
}
return generateErrorResponse(400, `Maximum batch size (${maxBatchSize}) exceeded`);
}

// Check whether any duplications exist
if (!isBatchUnique(batchSignedData)) return generateErrorResponse(400, 'No duplications are allowed');
Expand All @@ -41,26 +41,26 @@ export const batchInsertData = async (requestBody: unknown): Promise<ApiResponse
const signedDataValidationResults = batchSignedData.map((signedData) => {
const goRecoverSigner = goSync(() => recoverSignerAddress(signedData));
if (!goRecoverSigner.success) {
return generateErrorResponse(400, 'Unable to recover signer address', goRecoverSigner.error.message, signedData);
}
return generateErrorResponse(400, 'Unable to recover signer address', goRecoverSigner.error.message, signedData);
}

if (signedData.airnode !== goRecoverSigner.data) {
return generateErrorResponse(400, 'Signature is invalid', undefined, signedData);
}
return generateErrorResponse(400, 'Signature is invalid', undefined, signedData);
}

const goDeriveBeaconId = goSync(() => deriveBeaconId(signedData.airnode, signedData.templateId));
if (!goDeriveBeaconId.success) {
return generateErrorResponse(
return generateErrorResponse(
400,
'Unable to derive beaconId by given airnode and templateId',
goDeriveBeaconId.error.message,
signedData
);
}
);
}

if (signedData.beaconId !== goDeriveBeaconId.data) {
return generateErrorResponse(400, 'beaconId is invalid', undefined, signedData);
}
return generateErrorResponse(400, 'beaconId is invalid', undefined, signedData);
}

return null;
});
Expand All @@ -70,16 +70,16 @@ export const batchInsertData = async (requestBody: unknown): Promise<ApiResponse
// Write batch of validated data to the database
const goBatchWriteDb = await go(async () => putAll(batchSignedData));
if (!goBatchWriteDb.success) {
return generateErrorResponse(500, 'Unable to send batch of signed data to database', goBatchWriteDb.error.message);
}
return generateErrorResponse(500, 'Unable to send batch of signed data to database', goBatchWriteDb.error.message);
}

// Prune the cache with the data that is too old (no endpoint will ever return it)
const maxDelay = endpoints.reduce((acc, endpoint) => Math.max(acc, endpoint.delaySeconds), 0);
const maxIgnoreAfterTimestamp = Math.floor(Date.now() / 1000 - maxDelay);
const goPruneCache = await go(async () => prune(batchSignedData, maxIgnoreAfterTimestamp));
if (!goPruneCache.success) {
return generateErrorResponse(500, 'Unable to remove outdated cache data', goPruneCache.error.message);
}
return generateErrorResponse(500, 'Unable to remove outdated cache data', goPruneCache.error.message);
}

return { statusCode: 201, headers: COMMON_HEADERS, body: JSON.stringify({ count: batchSignedData.length }) };
};
Expand All @@ -92,14 +92,14 @@ export const getData = async (airnodeAddress: string, delaySeconds: number): Pro

const goValidateSchema = await go(async () => evmAddressSchema.parseAsync(airnodeAddress));
if (!goValidateSchema.success) {
return generateErrorResponse(400, 'Invalid request, airnode address must be an EVM address');
}
return generateErrorResponse(400, 'Invalid request, airnode address must be an EVM address');
}

const ignoreAfterTimestamp = Math.floor(Date.now() / 1000 - delaySeconds);
const goReadDb = await go(async () => getAll(airnodeAddress, ignoreAfterTimestamp));
if (!goReadDb.success) {
return generateErrorResponse(500, 'Unable to get signed data from database', goReadDb.error.message);
}
return generateErrorResponse(500, 'Unable to get signed data from database', goReadDb.error.message);
}

const data = goReadDb.data.reduce((acc, signedData) => {
return { ...acc, [signedData.beaconId]: omit(signedData, 'beaconId') };
Expand All @@ -117,8 +117,8 @@ export const getData = async (airnodeAddress: string, delaySeconds: number): Pro
export const listAirnodeAddresses = async (): Promise<ApiResponse> => {
const goAirnodeAddresses = await go(async () => getAllAirnodeAddresses());
if (!goAirnodeAddresses.success) {
return generateErrorResponse(500, 'Unable to scan database', goAirnodeAddresses.error.message);
}
return generateErrorResponse(500, 'Unable to scan database', goAirnodeAddresses.error.message);
}
const airnodeAddresses = goAirnodeAddresses.data;

return {
Expand Down
7 changes: 6 additions & 1 deletion packages/api/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export const generateRandomWallet = () => ethers.Wallet.createRandom();

export const generateRandomEvmAddress = () => generateRandomWallet().address;

export const generateDataSignature = async (wallet: ethers.Wallet, templateId: string, timestamp: string, data: string) => {
export const generateDataSignature = async (
wallet: ethers.Wallet,
templateId: string,
timestamp: string,
data: string
) => {
return wallet.signMessage(
ethers.utils.arrayify(
ethers.utils.keccak256(
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export interface Logger {
debug: (message: string, context?: LogContext) => void;
info: (message: string, context?: LogContext) => void;
warn: (message: string, context?: LogContext) => void;
error: ((message: string, context?: LogContext) => void) & ((message: string, error: Error, context?: LogContext) => void);
error: ((message: string, context?: LogContext) => void) &
((message: string, error: Error, context?: LogContext) => void);
child: (options: { name: string }) => Logger;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/e2e/src/data-provider-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ app.get('/', (_req, res) => {
logger.debug('Request GET /');

for (const asset of assets) {
asset.value = Number.parseFloat((asset.value * (1 + ((Math.random() - 0.5) * asset.deltaPercent) / 100)).toFixed(5));
asset.value = Number.parseFloat(
(asset.value * (1 + ((Math.random() - 0.5) * asset.deltaPercent) / 100)).toFixed(5)
);
}

const response = Object.fromEntries(assets.map((asset) => [asset.name, asset.value]));
Expand Down
2 changes: 1 addition & 1 deletion packages/pusher/src/api-requests/signed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const postSignedApiData = async (group: SignedApiNameUpdateDelayGroup) =>
return { success: false };
}

const {count} = parsedResponse.data;
const { count } = parsedResponse.data;
logger.info(`Pushed signed data updates to the signed API.`, { ...logContext, count });
return { success: true, count };
};
7 changes: 6 additions & 1 deletion packages/pusher/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export const getRandomId = () => ethers.utils.randomBytes(16).toString();
export const deriveEndpointId = (oisTitle: string, endpointName: string) =>
ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['string', 'string'], [oisTitle, endpointName]));

export const signWithTemplateId = async (wallet: ethers.Wallet, templateId: string, timestamp: string, data: string) => {
export const signWithTemplateId = async (
wallet: ethers.Wallet,
templateId: string,
timestamp: string,
data: string
) => {
return wallet.signMessage(
ethers.utils.arrayify(
ethers.utils.keccak256(
Expand Down
66 changes: 33 additions & 33 deletions pnpm-lock.yaml

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

0 comments on commit 33a306d

Please sign in to comment.