Browse Source

Types - comments and minor adjustments

Leszek Wiesner 4 years ago
parent
commit
f0232ca1b1
2 changed files with 46 additions and 33 deletions
  1. 20 20
      cli/src/Api.ts
  2. 26 13
      cli/src/Types.ts

+ 20 - 20
cli/src/Api.ts

@@ -1,11 +1,12 @@
 import BN from 'bn.js';
 import { registerJoystreamTypes } from '@joystream/types';
 import { ApiPromise, WsProvider } from '@polkadot/api';
+import { QueryableStorageMultiArg } from '@polkadot/api/types';
 import { formatBalance } from '@polkadot/util';
 import { Balance, Hash } from '@polkadot/types/interfaces';
 import { KeyringPair } from '@polkadot/keyring/types';
 import { Codec } from '@polkadot/types/types';
-import { AccountBalances, CouncilInfoObj, CouncilInfoTuple, AccountSummary, createCouncilInfoObj } from './Types';
+import { AccountBalances, AccountSummary, CouncilInfoObj, CouncilInfoTuple, createCouncilInfoObj } from './Types';
 import { DerivedFees, DerivedBalances } from '@polkadot/api-derive/types';
 import { CLIError } from '@oclif/errors';
 import ExitCodes from './ExitCodes';
@@ -83,25 +84,24 @@ export default class Api {
     }
 
     async getCouncilInfo(): Promise<CouncilInfoObj> {
-        const results = await this.queryMultiOnce([
-            this._api.query.council.activeCouncil,
-            this._api.query.council.termEndsAt,
-            this._api.query.councilElection.autoStart,
-            this._api.query.councilElection.newTermDuration,
-            this._api.query.councilElection.candidacyLimit,
-            this._api.query.councilElection.councilSize,
-            this._api.query.councilElection.minCouncilStake,
-            this._api.query.councilElection.minVotingStake,
-            this._api.query.councilElection.announcingPeriod,
-            this._api.query.councilElection.votingPeriod,
-            this._api.query.councilElection.revealingPeriod,
-            this._api.query.councilElection.round,
-            this._api.query.councilElection.stage
-        ]);
-
-        let infoObj: CouncilInfoObj = createCouncilInfoObj(...(<CouncilInfoTuple> results));
-
-        return infoObj;
+        const queries: { [P in keyof CouncilInfoObj]: QueryableStorageMultiArg<"promise"> } = {
+            activeCouncil:    this._api.query.council.activeCouncil,
+            termEndsAt:       this._api.query.council.termEndsAt,
+            autoStart:        this._api.query.councilElection.autoStart,
+            newTermDuration:  this._api.query.councilElection.newTermDuration,
+            candidacyLimit:   this._api.query.councilElection.candidacyLimit,
+            councilSize:      this._api.query.councilElection.councilSize,
+            minCouncilStake:  this._api.query.councilElection.minCouncilStake,
+            minVotingStake:   this._api.query.councilElection.minVotingStake,
+            announcingPeriod: this._api.query.councilElection.announcingPeriod,
+            votingPeriod:     this._api.query.councilElection.votingPeriod,
+            revealingPeriod:  this._api.query.councilElection.revealingPeriod,
+            round:            this._api.query.councilElection.round,
+            stage:            this._api.query.councilElection.stage
+        }
+        const results: CouncilInfoTuple = <CouncilInfoTuple> await this.queryMultiOnce(Object.values(queries));
+
+        return createCouncilInfoObj(...results);
     }
 
     // TODO: This formula is probably not too good, so some better implementation will be required in the future

+ 26 - 13
cli/src/Types.ts

@@ -5,38 +5,48 @@ import { BlockNumber, Balance } from '@polkadot/types/interfaces';
 import { DerivedBalances } from '@polkadot/api-derive/types';
 import { KeyringPair } from '@polkadot/keyring/types';
 
+// KeyringPair type extended with mandatory "meta.name"
+// It's used for accounts/keys management within CLI.
+// If not provided in the account json file, the meta.name value is set to "Unnamed Account"
 export type NamedKeyringPair = KeyringPair & {
     meta: {
         name: string
     }
 }
 
+// Simplified account balances (used when listing multiple accounts etc.)
+// Combines results returned by api.query.balances.freeBalance and api.query.balances.reservedBalance
 export type AccountBalances = {
     free: Balance,
     reserved: Balance,
     total: Balance
 };
 
+// Summary of the account information fetched from the api for "account:current" purposes (currently just balances)
 export type AccountSummary = {
     balances: DerivedBalances
 }
 
+// Object/Tuple containing council/councilElection information (council:info).
+// The tuple is useful, because that's how api.queryMulti returns the results.
 export type CouncilInfoTuple = Parameters<typeof createCouncilInfoObj>;
 export type CouncilInfoObj = ReturnType<typeof createCouncilInfoObj>;
+// This function allows us to easily transform the tuple into the object
+// and simplifies the creation of consitent Object and Tuple types (seen above).
 export function createCouncilInfoObj(
-    activeCouncil: Seat[] | undefined,
-    termEndsAt: BlockNumber | undefined,
-    autoStart: Boolean | undefined,
-    newTermDuration: BN | undefined,
-    candidacyLimit: BN | undefined,
-    councilSize: BN | undefined,
-    minCouncilStake: Balance | undefined,
-    minVotingStake: Balance | undefined,
-    announcingPeriod: BlockNumber | undefined,
-    votingPeriod: BlockNumber | undefined,
-    revealingPeriod: BlockNumber | undefined,
-    round: BN | undefined,
-    stage: Option<ElectionStage> | undefined
+    activeCouncil: Seat[],
+    termEndsAt: BlockNumber,
+    autoStart: Boolean,
+    newTermDuration: BN,
+    candidacyLimit: BN,
+    councilSize: BN,
+    minCouncilStake: Balance,
+    minVotingStake: Balance,
+    announcingPeriod: BlockNumber,
+    votingPeriod: BlockNumber,
+    revealingPeriod: BlockNumber,
+    round: BN,
+    stage: Option<ElectionStage>
 ) {
     return {
         activeCouncil,
@@ -55,4 +65,7 @@ export function createCouncilInfoObj(
     };
 }
 
+// Object with "name" and "value" properties, used for rendering simple CLI tables like:
+// Total balance:   100 JOY
+// Free calance:     50 JOY
 export type NameValueObj = { name: string, value: string };