Forráskód Böngészése

fix api-examples, and re introduce `joy` global into pioneer javascript playground

Mokhtar Naamani 4 éve
szülő
commit
eab655a5e2

+ 4 - 1
pioneer/packages/page-js/src/Playground.tsx

@@ -16,6 +16,7 @@ import uiKeyring from '@polkadot/ui-keyring';
 import * as types from '@polkadot/types';
 import * as util from '@polkadot/util';
 import * as hashing from '@polkadot/util-crypto';
+import * as joy from '@joystream/types';
 
 import { STORE_EXAMPLES, STORE_SELECTED, CUSTOM_LABEL } from './constants';
 import makeWrapper from './snippets/wrapping';
@@ -36,6 +37,7 @@ interface Injected {
   setIsRunning: (isRunning: boolean) => void;
   types: typeof types;
   util: typeof util;
+  joy: typeof joy;
   [name: string]: any;
 }
 
@@ -69,7 +71,8 @@ function setupInjected ({ api, isDevelopment }: ApiProps, setIsRunning: (isRunni
     uiKeyring: isDevelopment
       ? uiKeyring
       : null,
-    util
+    util,
+    joy
   };
 }
 

+ 1 - 1
utils/api-examples/scripts/example.js

@@ -24,7 +24,7 @@ const script = async ({ api, hashing, keyring, types, util, joy }) => {
 
 if (typeof module === 'undefined') {
   // Pioneer js-toolbox
-  // Available globals [api, hashing, keyring, types, util]
+  // Available globals [api, hashing, keyring, types, util, joy]
   script({ api, hashing, keyring, types, util, joy })
 } else {
   // Node

+ 2 - 2
utils/api-examples/scripts/export-data-directory.js

@@ -1,4 +1,4 @@
-/* global api, hashing, keyring, types, util */
+/* global api, hashing, keyring, types, util, joy */
 
 // run this script with:
 // yarn script exportDataDirectory
@@ -42,7 +42,7 @@ const script = async ({ api }) => {
 
 if (typeof module === 'undefined') {
   // Pioneer js-toolbox
-  script({ api, hashing, keyring, types, util })
+  script({ api, hashing, keyring, types, util, joy })
 } else {
   // Node
   module.exports = script

+ 2 - 2
utils/api-examples/scripts/inject-data-objects.js

@@ -1,4 +1,4 @@
-/* global api, hashing, keyring, types, util, window */
+/* global api, hashing, keyring, types, util, joy, window */
 
 // run this script with:
 // yarn script injectDataObjects
@@ -53,7 +53,7 @@ const script = async ({ api, keyring }) => {
 
 if (typeof module === 'undefined') {
   // Pioneer js-toolbox
-  script({ api, hashing, keyring, types, util })
+  script({ api, hashing, keyring, types, util, joy })
 } else {
   // Node
   module.exports = script

+ 1 - 1
utils/api-examples/scripts/list-data-directory.js

@@ -7,7 +7,7 @@
 // https://testnet.joystream.org/#/js
 // requires nicaea release+
 
-const script = async ({ api, joy }) => {
+const script = async ({ api }) => {
   const ids = await api.query.dataDirectory.knownContentIds()
 
   await Promise.all(

+ 2 - 2
utils/api-examples/scripts/transfer.js

@@ -1,4 +1,4 @@
-/* global api, hashing, keyring, types, util, window */
+/* global api, hashing, keyring, types, util, joy, window */
 
 // run this script with:
 // yarn script testTransfer
@@ -24,7 +24,7 @@ const script = async ({ api, keyring }) => {
 
 if (typeof module === 'undefined') {
   // Pioneer js-toolbox
-  script({ api, hashing, keyring, types, util })
+  script({ api, hashing, keyring, types, util, joy })
 } else {
   // Node
   module.exports = script

+ 7 - 14
utils/api-examples/src/status.ts

@@ -3,7 +3,6 @@
 import { ApiPromise, WsProvider } from '@polkadot/api'
 import { types } from '@joystream/types'
 import { Seat } from '@joystream/types/council'
-import { ValidatorId } from '@polkadot/types/interfaces'
 
 import BN from 'bn.js'
 
@@ -24,30 +23,24 @@ async function main() {
   console.log(`Chain ${chain} using ${nodeName} v${nodeVersion}`)
 
   const council = ((await api.query.council.activeCouncil()) as unknown) as Seat[]
-  const validators = ((await api.query.session.validators()) as unknown) as ValidatorId[]
-  const version = (await api.rpc.state.getRuntimeVersion()) as any
+  const validators = await api.query.session.validators()
+  const version = await api.rpc.state.getRuntimeVersion()
 
   console.log(`Runtime Version: ${version.authoringVersion}.${version.specVersion}.${version.implVersion}`)
 
-  // let council: QueryableStorageFunction<Seat[], SubscriptionResult> = (await api.query.council.activeCouncil()) as unknown as Seat[]
-  // let council = (await api.query.council.activeCouncil()) as unknown as Seat[];
-
   // number of council members
   console.log('Council size:', council.length)
 
   console.log('Validator count:', validators.length)
 
   if (validators && validators.length > 0) {
-    // Retrieve the free balances for all validators
-    const validatorBalances = await Promise.all(
-      validators.map((authorityId) => api.query.balances.account(authorityId))
-    )
-
-    const totalValidatorBalances = validatorBalances.reduce((total, value) => total.add(value.free), new BN(0))
+    // Retrieve the balances of validators' stash accounts
+    // for more detailed staking information we can use api.derive.staking.*
+    const validatorBalances = await Promise.all(validators.map((authorityId) => api.derive.balances.all(authorityId)))
 
-    // TODO: to get the staked amounts we need to read the account lock information.
+    const totalValidatorBalances = validatorBalances.reduce((total, value) => total.add(value.lockedBalance), new BN(0))
 
-    console.log('Total Validator Free Balance:', totalValidatorBalances.toString())
+    console.log('Total Validator Locked Balances:', totalValidatorBalances.toString())
   }
 
   api.disconnect()