Skip to content

Commit

Permalink
bug fix - parseInt returns 0 evaluates to false
Browse files Browse the repository at this point in the history
  • Loading branch information
jac18281828 committed Dec 7, 2022
1 parent a4cdbfa commit 38dab6e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 38 deletions.
32 changes: 7 additions & 25 deletions governance/collectivegovernance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { Wallet } from './wallet';
import { loadAbi, pathWithSlash } from '../system/abi';
import { LoggerFactory } from '../system/logging';
import { Governance } from './governance';
import { parseIntOrThrow } from './version';

export class CollectiveGovernance implements Governance {
private readonly logger = LoggerFactory.getLogger(module.filename);
Expand Down Expand Up @@ -79,11 +80,7 @@ export class CollectiveGovernance implements Governance {

async version(): Promise<number> {
const version = await this.contract.methods.version().call();
const v = parseInt(version);
if (v) {
return v;
}
throw new Error('Version is not a number');
return parseIntOrThrow(version);
}

async propose(): Promise<number> {
Expand All @@ -94,11 +91,8 @@ export class CollectiveGovernance implements Governance {
});
this.logger.info(proposeTx);
const event: EventData = proposeTx.events['ProposalCreated'];
const proposalId = parseInt(event.returnValues['proposalId']);
if (proposalId) {
return proposalId;
}
throw new Error('Unknown proposal created');
return parseIntOrThrow(event.returnValues['proposalId']);

}

async choiceVote(choiceCount: number): Promise<number> {
Expand All @@ -109,11 +103,7 @@ export class CollectiveGovernance implements Governance {
});
this.logger.info(proposeTx);
const event: EventData = proposeTx.events['ProposalCreated'];
const proposalId = parseInt(event.returnValues['proposalId']);
if (proposalId) {
return proposalId;
}
throw new Error('Unknown proposal created');
return parseIntOrThrow(event.returnValues['proposalId']);
}

async setChoice(proposalId: number, choiceId: number, name: string, description: string, transactionId: number): Promise<void> {
Expand Down Expand Up @@ -144,11 +134,7 @@ export class CollectiveGovernance implements Governance {
});
this.logger.info(tx);
const event: EventData = tx.events['ProposalMeta'];
const metaId = parseInt(event.returnValues['metaId']);
if (metaId) {
return metaId;
}
throw new Error('Metadata Id is not a number');
return parseIntOrThrow(event.returnValues['metaId']);
}

async attachTransaction(
Expand All @@ -168,11 +154,7 @@ export class CollectiveGovernance implements Governance {
});
this.logger.info(attachTx);
const event: EventData = attachTx.events['ProposalTransactionAttached'];
const transactionId = parseInt(event.returnValues['transactionId']);
if (transactionId) {
return transactionId;
}
throw new Error('Transaction id is not a number');
return parseIntOrThrow(event.returnValues['transactionId']);
}

async configure(proposalId: number, quorum: number): Promise<void> {
Expand Down
7 changes: 2 additions & 5 deletions governance/metastorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import Web3 from 'web3';
import { ContractAbi } from './contractabi';
import { Meta } from './meta';
import { parseIntOrThrow } from './version';

export class MetaStorage extends ContractAbi implements Meta {
static ABI_NAME = 'MetaStorage.json';
Expand All @@ -49,11 +50,7 @@ export class MetaStorage extends ContractAbi implements Meta {

async version(): Promise<number> {
const version = await this.contract.methods.version().call();
const v = parseInt(version);
if (v) {
return v;
}
throw new Error('Version is not a number');
return parseIntOrThrow(version);
}

async community(): Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions governance/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

export function parseIntOrThrow(num: string): number {
const n = parseInt(num);
if (n) {
return n;
if (isNaN(n)) {
throw new Error(`${num} is not a number`);
}
throw new Error(`${num} is not a number`);
return n;
}
7 changes: 2 additions & 5 deletions system/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/

import Web3 from 'web3';
import { parseIntOrThrow } from '../governance/version';

export function timeNow(): number {
return Math.floor(Date.now() / 1000);
Expand All @@ -47,11 +48,7 @@ export async function blocktimeNow(web3: Web3): Promise<number> {
const blockNumber = await web3.eth.getBlockNumber();
const block = await web3.eth.getBlock(blockNumber);
if (typeof block.timestamp === 'string') {
const ts = parseInt(block.timestamp);
if (ts) {
return ts;
}
throw new Error('Timestamp is not a number');
return parseIntOrThrow(block.timestamp);
}
return block.timestamp;
}

0 comments on commit 38dab6e

Please sign in to comment.