Skip to content

Commit

Permalink
refactor: using strict, refactor types to pass tslint
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed May 6, 2021
1 parent 6d80493 commit 07b9c1e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
49 changes: 40 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,16 +25,16 @@ export interface Input {
script: string;
decoded: DecodedScript;
index: number;
token?: string;
token?: string | undefined | null;
}

export interface Output {
value: number;
tokenData: number;
script: string;
decoded: DecodedScript;
token?: string;
spentBy?: string;
token?: string | undefined | null;
spentBy?: string | undefined | null;
}

export interface Token {
Expand Down Expand Up @@ -103,15 +103,41 @@ 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;
height?: number;
transactions?: string[];
message?: string;
error?: string;
};
}; */

export interface GeneratorYieldResult<StatusEvent> {
done?: boolean;
Expand All @@ -121,7 +147,7 @@ export interface GeneratorYieldResult<StatusEvent> {
export interface PreparedDecodedScript {
type: string;
address: string;
timelock?: number;
timelock?: number | undefined | null;
value?: number;
token_data?: number;
}
Expand Down Expand Up @@ -165,3 +191,8 @@ export interface PreparedTx {
token_symbol?: string;
raw?: string;
}

export interface RawTxResponse {
tx: any;
meta: any;
};
26 changes: 18 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
PreparedTx,
PreparedInput,
PreparedOutput,
PreparedDecodedScript,
RawTxResponse,
} from './types';
import {
downloadTx,
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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,
};

Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"strict": false,
"strict": true,
"strictFunctionTypes": false,
"noImplicitAny": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
Expand Down

0 comments on commit 07b9c1e

Please sign in to comment.