Skip to content

Commit

Permalink
fix(cactus-example-electricity-trade): enable tsconfig strict flag an…
Browse files Browse the repository at this point in the history
…d fix all the warnings

Fixed all warnings when sctrict flag is enabled

Closes: #2144

Signed-off-by: Tomasz Awramski <tomasz.awramski@fujitsu.com>
  • Loading branch information
rwat17 authored and izuru0 committed Oct 4, 2022
1 parent 12e972e commit f7e726c
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 160 deletions.
16 changes: 7 additions & 9 deletions examples/cactus-example-electricity-trade/BalanceManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ const moduleName = "BalanceManagement";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export interface BalanceResponse {
status: string | number;
amount: number;
}

export class BalanceManagement {
private connectInfo: LPInfoHolder = null; // connection information
private connectInfo: LPInfoHolder | null = null; // connection information
private readonly verifierFactory: VerifierFactory;

constructor() {
Expand All @@ -32,19 +37,12 @@ export class BalanceManagement {
);
}

getBalance(account: string): Promise<any> {
getBalance(account: string): Promise<unknown> {
return new Promise((resolve, reject) => {
// for LedgerOperation
// const execData = {"referedAddress": account};
// const ledgerOperation: LedgerOperation = new LedgerOperation("getNumericBalance", "", execData);

// for Neo
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "getBalance" };
const template = "default";
const args = { args: [account] };
// const method = "default";
// const args = {"method": {type: "web3Eth", command: "getBalance"}, "args": {"args": [account]}};

this.verifierFactory
.getVerifier("84jUisrs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import {
} from "@hyperledger/cactus-cmd-socketio-server";
import { makeRawTransaction } from "./TransactionEthereum";

const fs = require("fs");
const yaml = require("js-yaml");
//const config: any = JSON.parse(fs.readFileSync("/etc/cactus/default.json", 'utf8'));
const config: any = ConfigUtil.getConfig();
const config: any = ConfigUtil.getConfig() as any;
import { getLogger } from "log4js";
import {
VerifierFactory,
Expand All @@ -39,6 +37,27 @@ const routesVerifierFactory = new VerifierFactory(
config.logLevel,
);

interface SawtoothEventData {
status: number | string;
blockData: [];
}

interface EthData {
hash: string;
}

interface EthEvent {
blockData: { transactions: { [key: string]: EthData } };
hash: string;
status: number;
}

interface SawtoothBlockDataData {
header_signature: string;
hash: string;
payload_decoded: { Verb: string; Name: string; Value: string }[];
}

export class BusinessLogicElectricityTrade extends BusinessLogicBase {
businessLogicID: string;
meterManagement: MeterManagement;
Expand All @@ -49,7 +68,11 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
this.meterManagement = new MeterManagement();
}

startTransaction(req: Request, businessLogicID: string, tradeID: string) {
startTransaction(
req: Request,
businessLogicID: string,
tradeID: string,
): void {
logger.debug("called startTransaction()");

// set RequestInfo
Expand All @@ -68,7 +91,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
this.startMonitor(tradeInfo);
}

startMonitor(tradeInfo: TradeInfo) {
startMonitor(tradeInfo: TradeInfo): void {
// Get Verifier Instance
logger.debug(
`##startMonitor(): businessLogicID: ${tradeInfo.businessLogicID}`,
Expand All @@ -94,7 +117,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
logger.debug("getVerifierSawtooth");
}

remittanceTransaction(transactionSubset: object) {
remittanceTransaction(transactionSubset: Record<string, string>): void {
logger.debug(
`called remittanceTransaction(), accountInfo = ${json2str(
transactionSubset,
Expand Down Expand Up @@ -140,17 +163,21 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
);
logger.debug("getVerifierEthereum");

interface RawTransationResultData {
args: unknown;
}
// Generate parameters for// sendRawTransaction
logger.debug(`####exec makeRawTransaction!!`);
makeRawTransaction(txParam)
.then((result) => {
logger.info("remittanceTransaction txId : " + result.txId);

// Set Parameter
logger.debug("remittanceTransaction data : " + json2str(result.data));
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const args = { args: [result.data["serializedTx"]] };
const args: RawTransationResultData = {
args: [result.data["serializedTx"]],
};

// Run Verifier (Ethereum)
verifierEthereum
Expand Down Expand Up @@ -179,6 +206,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
break;
case config.electricityTradeInfo.ethereum.validatorID:
this.onEventEthereum(ledgerEvent.data, targetIndex);

break;
default:
logger.error(
Expand All @@ -188,7 +216,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
}
}

onEventSawtooth(event: object, targetIndex: number): void {
onEventSawtooth(event: SawtoothEventData, targetIndex: number): void {
logger.debug(`##in onEventSawtooth()`);
const tx = this.getTransactionFromSawtoothEvent(event, targetIndex);
if (tx == null) {
Expand Down Expand Up @@ -216,23 +244,26 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
}

getTransactionFromSawtoothEvent(
event: object,
targetIndex: number,
): object | null {
event: SawtoothEventData,
targetIndex: number | string,
): SawtoothBlockDataData | undefined {
try {
const retTransaction = event["blockData"][targetIndex];
logger.debug(
`##getTransactionFromSawtoothEvent(), retTransaction: ${retTransaction}`,
);
return retTransaction;
if (typeof targetIndex === "number") {
const retTransaction = event["blockData"][targetIndex];

logger.debug(
`##getTransactionFromSawtoothEvent(), retTransaction: ${retTransaction}`,
);
return retTransaction;
}
} catch (err) {
logger.error(
`##getTransactionFromSawtoothEvent(): invalid even, err:${err}, event:${event}`,
);
}
}

onEventEthereum(event: object, targetIndex: number): void {
onEventEthereum(event: EthEvent, targetIndex: number): void {
logger.debug(`##in onEventEthereum()`);
const tx = this.getTransactionFromEthereumEvent(event, targetIndex);
if (tx == null) {
Expand All @@ -243,6 +274,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
try {
const txId = tx["hash"];
const status = event["status"];

logger.debug(`##txId = ${txId}`);
logger.debug(`##status =${status}`);

Expand All @@ -260,23 +292,24 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
}

getTransactionFromEthereumEvent(
event: object,
event: EthEvent,
targetIndex: number,
): object | null {
): EthData | undefined {
try {
const retTransaction = event["blockData"]["transactions"][targetIndex];
logger.debug(
`##getTransactionFromEthereumEvent(), retTransaction: ${retTransaction}`,
);
return retTransaction;
// return (retTransaction as unknown) as EthEvent;
} catch (err) {
logger.error(
`##getTransactionFromEthereumEvent(): invalid even, err:${err}, event:${event}`,
);
}
}

getOperationStatus(tradeID: string): object {
getOperationStatus(): Record<string, unknown> {
logger.debug(`##in getOperationStatus()`);
return {};
}
Expand All @@ -301,7 +334,10 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
return null;
}

getTxIDFromEventSawtooth(event: object, targetIndex: number): string | null {
getTxIDFromEventSawtooth(
event: SawtoothEventData,
targetIndex: number | string,
): string | null {
logger.debug(`##in getTxIDFromEventSawtooth()`);
const tx = this.getTransactionFromSawtoothEvent(event, targetIndex);
if (tx == null) {
Expand Down Expand Up @@ -331,7 +367,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
}
}

getTxIDFromEventEtherem(event: object, targetIndex: number): string | null {
getTxIDFromEventEtherem(event: EthEvent, targetIndex: number): string | null {
logger.debug(`##in getTxIDFromEventEtherem()`);
const tx = this.getTransactionFromEthereumEvent(event, targetIndex);
if (tx == null) {
Expand All @@ -348,10 +384,12 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
event,
)}`,
);

return null;
}

logger.debug(`###getTxIDFromEventEtherem(): txId: ${txId}`);

return txId;
} catch (err) {
logger.error(
Expand All @@ -369,6 +407,8 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
let retEventNum = 0;

try {
logger.error(ledgerEvent.data);

switch (ledgerEvent.verifierId) {
case config.electricityTradeInfo.sawtooth.validatorID:
retEventNum = event["blockData"].length;
Expand All @@ -394,8 +434,10 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
}
}

getAccountInfo(transactionSubset: object): object {
const transactionInfo = {};
getAccountInfo(
transactionSubset: Record<string, string>,
): Record<string, string> {
const transactionInfo: Record<string, string> = {};

// Get Meter Information.
const meterInfo: MeterInfo | null = this.meterManagement.getMeterInfo(
Expand All @@ -417,12 +459,12 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
return transactionInfo;
}

setConfig(meterParams: string[]): object {
setConfig(meterParams: string[]): Record<string, string> {
logger.debug("called setConfig()");

// add MeterInfo
const meterInfo = new MeterInfo(meterParams);
const result: {} = this.meterManagement.addMeterInfo(meterInfo);
const result = this.meterManagement.addMeterInfo(meterInfo);
return result;
}
}
17 changes: 9 additions & 8 deletions examples/cactus-example-electricity-trade/MeterManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { MeterInfo } from "./MeterInfo";
import { ConfigUtil } from "@hyperledger/cactus-cmd-socketio-server";
const fs = require("fs");
const yaml = require("js-yaml");
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = "MeterManagement";
Expand All @@ -19,16 +18,18 @@ logger.level = config.logLevel;
export class MeterManagement {
fileName = "MeterInfo.json";

constructor() {}
constructor() {
// do nothing.
}

// For debugging
fileDump() {
fileDump(): void {
const confirmData: string = fs.readFileSync(this.fileName, "utf8");
const arrayData: MeterInfo[] = JSON.parse(confirmData).table as MeterInfo[];
logger.debug(arrayData);
}

addMeterInfo(addMeterInfo: MeterInfo): object {
addMeterInfo(addMeterInfo: MeterInfo): Record<string, string> {
// Existence check of table file
try {
fs.statSync(this.fileName);
Expand All @@ -47,12 +48,12 @@ export class MeterManagement {
.table as string[];

// Search target records / replace data
const meterInfoTableNew = {
const meterInfoTableNew: Record<string, string[]> = {
table: [],
};
let existFlag = false;
let action = "";
meterInfoTable.forEach((meterInfoJSON, index) => {
meterInfoTable.forEach((meterInfoJSON) => {
const meterInfo: MeterInfo = JSON.parse(meterInfoJSON) as MeterInfo;

// Determine if it is a target record
Expand Down Expand Up @@ -90,7 +91,7 @@ export class MeterManagement {
return result;
}

getMeterInfo(meterID: string): MeterInfo {
getMeterInfo(meterID: string): MeterInfo | null {
// Existence check of table file
try {
fs.statSync(this.fileName);
Expand All @@ -105,7 +106,7 @@ export class MeterManagement {

// Search target records
let retMeterInfo: MeterInfo | null = null;
meterInfoTable.forEach((meterInfoJSON, index) => {
meterInfoTable.forEach((meterInfoJSON) => {
const meterInfo: MeterInfo = JSON.parse(meterInfoJSON) as MeterInfo;

// Determine if it is a target record
Expand Down

0 comments on commit f7e726c

Please sign in to comment.