From 07b9c1e5f82729e0ffa9b2c7a695498527d6013d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Thu, 6 May 2021 15:13:22 -0300 Subject: [PATCH] refactor: using strict, refactor types to pass tslint --- src/machine.ts | 2 +- src/types.ts | 49 ++++++++++++++++++++++++++++++++++++++++--------- src/utils.ts | 26 ++++++++++++++++++-------- tsconfig.json | 4 +++- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/machine.ts b/src/machine.ts index b97e58aa..c5e475dc 100644 --- a/src/machine.ts +++ b/src/machine.ts @@ -20,7 +20,7 @@ import { } from './types'; import logger from './logger'; -export const syncHandler = (_context, _event) => (callback, onReceive) => { +export const syncHandler = () => (callback, onReceive) => { logger.debug('Sync handler instantiated'); const iterator = syncToLatestBlock(); const asyncCall: () => void = async () => { diff --git a/src/types.ts b/src/types.ts index 187fc8c7..145b96c0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,9 +13,9 @@ export interface Block { export interface DecodedScript { type: string; address: string; - timelock?: number; - value?: number; - tokenData?: number; + timelock?: number | undefined | null; + value?: number | undefined | null; + tokenData?: number | undefined | null; } export interface Input { @@ -25,7 +25,7 @@ export interface Input { script: string; decoded: DecodedScript; index: number; - token?: string; + token?: string | undefined | null; } export interface Output { @@ -33,8 +33,8 @@ export interface Output { tokenData: number; script: string; decoded: DecodedScript; - token?: string; - spentBy?: string; + token?: string | undefined | null; + spentBy?: string | undefined | null; } export interface Token { @@ -103,7 +103,33 @@ export interface HandlerEvent { type: string; } -export interface StatusEvent { +export type StatusEvent = { + type: 'finished'; + success: boolean; + message?: string; +} | { + type: 'block_success'; + success: boolean; + height?: number; + blockId: string; + message?: string; + transactions: string[]; +} | { + type: 'transaction_failure'; + success: boolean; + message?: string; +} | { + type: 'reorg'; + success: boolean; + message?: string; +} | { + type: 'error'; + success: boolean; + message?: string; + error?: string; +} + +/* export interface StatusEvent { type: string; success: boolean; blockId?: string; @@ -111,7 +137,7 @@ export interface StatusEvent { transactions?: string[]; message?: string; error?: string; -}; +}; */ export interface GeneratorYieldResult { done?: boolean; @@ -121,7 +147,7 @@ export interface GeneratorYieldResult { export interface PreparedDecodedScript { type: string; address: string; - timelock?: number; + timelock?: number | undefined | null; value?: number; token_data?: number; } @@ -165,3 +191,8 @@ export interface PreparedTx { token_symbol?: string; raw?: string; } + +export interface RawTxResponse { + tx: any; + meta: any; +}; diff --git a/src/utils.ts b/src/utils.ts index f0bd4f73..0ee550a0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -18,6 +18,8 @@ import { PreparedTx, PreparedInput, PreparedOutput, + PreparedDecodedScript, + RawTxResponse, } from './types'; import { downloadTx, @@ -35,7 +37,7 @@ import logger from './logger'; dotenv.config(); -const TX_CACHE_SIZE: number = parseInt(process.env.TX_CACHE_SIZE) || 200; +const TX_CACHE_SIZE: number = parseInt(process.env.TX_CACHE_SIZE as string) || 200; /** * Recursively downloads all transactions that were confirmed by a given block @@ -49,8 +51,8 @@ export const recursivelyDownloadTx = async (blockId: string, txIds: string[] = [ return data; } - const txId = txIds.pop(); - const txData = await downloadTx(txId); + const txId: string = txIds.pop() as string; + const txData: RawTxResponse = await downloadTx(txId); const { tx, meta } = txData; const parsedTx: FullTx = parseTx(tx); @@ -89,8 +91,8 @@ export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => { value: input.value, token_data: input.tokenData, script: input.script, - token: input.token, - decoded: input.decoded, + token: input.token as string, + decoded: input.decoded as PreparedDecodedScript, index: input.index, }; @@ -101,6 +103,10 @@ export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => { }; } + if (!tx.tokens || tx.tokens.length <= 0) { + throw new Error('Input is a token but there are no tokens in the tokens list.'); + } + const { uid } = tx.tokens[wallet.getTokenIndex(input.tokenData) - 1]; return { @@ -113,9 +119,9 @@ export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => { value: output.value, token_data: output.tokenData, script: output.script, - token: output.token, - spent_by: output.spentBy, - decoded: output.decoded, + token: output.token as string, + spent_by: output.spentBy as string, + decoded: output.decoded as PreparedDecodedScript, }; if (output.tokenData === 0) { @@ -125,6 +131,10 @@ export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => { }; } + if (!tx.tokens || tx.tokens.length <= 0) { + throw new Error('Output is a token but there are no tokens in the tokens list.'); + } + const { uid } = tx.tokens[wallet.getTokenIndex(output.tokenData) - 1]; return { diff --git a/tsconfig.json b/tsconfig.json index 622e48f2..6aaa36df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,9 @@ "declaration": true, "sourceMap": true, "rootDir": "./src", - "strict": false, + "strict": true, + "strictFunctionTypes": false, + "noImplicitAny": false, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "noUnusedLocals": true,