Jelajahi Sumber

api-scripts: fix broken scripts add helper for testing

Mokhtar Naamani 4 tahun lalu
induk
melakukan
49883c074f

+ 1 - 0
utils/api-scripts/README.md

@@ -43,4 +43,5 @@ You can run scripts that are found in the [./scripts/](./scripts) folder:
 
 ```sh
 yarn script example
+yarn script test-transfer
 ```

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

@@ -1,7 +1,7 @@
 /* global api, hashing, keyring, types, util, joy */
 
 // run this script with:
-// yarn script example
+// yarn workspace api-scripts script example
 //
 // or copy and paste the code into the pioneer javascript toolbox at:
 // https://testnet.joystream.org/#/js

+ 0 - 49
utils/api-scripts/scripts/export-data-directory.js

@@ -1,49 +0,0 @@
-/* global api, hashing, keyring, types, util, joy */
-
-// run this script with:
-// yarn script exportDataDirectory
-//
-// or copy and paste the code into the pioneer javascript toolbox at:
-// https://testnet.joystream.org/#/js
-
-const script = async ({ api }) => {
-  const ids = await api.query.dataDirectory.knownContentIds()
-
-  // When a BTreeMap is constructed for injection the node will fail to decode
-  // it if its not sorted.
-  ids.sort()
-
-  const transformed = await Promise.all(
-    ids.map(async (id) => {
-      let obj = await api.query.dataDirectory.dataByContentId(id)
-      if (obj.isNone) {
-        return null
-      }
-      obj = obj.unwrap()
-
-      return [
-        id,
-        {
-          owner: obj.owner,
-          added_at: obj.added_at,
-          type_id: obj.type_id,
-          size: obj.size_in_bytes,
-          liaison: obj.liaison,
-          liaison_judgement: obj.liaison_judgement,
-          ipfs_content_id: obj.ipfs_content_id,
-        },
-      ]
-    })
-  )
-
-  console.log(JSON.stringify(transformed))
-  console.error(`Exported ${transformed.length} objects`)
-}
-
-if (typeof module === 'undefined') {
-  // Pioneer js-toolbox
-  script({ api, hashing, keyring, types, util, joy })
-} else {
-  // Node
-  module.exports = script
-}

+ 29 - 0
utils/api-scripts/scripts/get-first-content-id.js

@@ -0,0 +1,29 @@
+/* global api, hashing, keyring, types, util, joy */
+
+// run this script with:
+// yarn workspace api-scripts script get-first-content-id
+//
+// or copy and paste the code into the pioneer javascript toolbox at:
+// https://testnet.joystream.org/#/js
+// requires nicaea release+
+
+const script = async ({ api }) => {
+  const entries = await api.query.dataDirectory.dataByContentId.entries()
+  const acceptedEntries = entries.filter(([, dataObject]) => dataObject.liaison_judgement.type === 'Accepted')
+  if (acceptedEntries.length) {
+    const [
+      {
+        args: [id],
+      },
+    ] = acceptedEntries[0]
+    console.log(`${api.createType('ContentId', id).encode()}`)
+  }
+}
+
+if (typeof module === 'undefined') {
+  // Pioneer js-toolbox
+  script({ api, hashing, keyring, types, util, joy })
+} else {
+  // Node
+  module.exports = script
+}

+ 0 - 9
utils/api-scripts/scripts/index.js

@@ -1,9 +0,0 @@
-const exportedScripts = {}
-
-exportedScripts.example = require('./example.js')
-exportedScripts.exportDataDirectory = require('./export-data-directory.js')
-exportedScripts.injectDataObjects = require('./inject-data-objects.js')
-exportedScripts.listDataDirectory = require('./list-data-directory.js')
-exportedScripts.testTransfer = require('./transfer.js')
-
-module.exports = exportedScripts

File diff ditekan karena terlalu besar
+ 0 - 15
utils/api-scripts/scripts/inject-data-objects.js


+ 15 - 11
utils/api-scripts/scripts/list-data-directory.js

@@ -1,27 +1,31 @@
 /* global api, hashing, keyring, types, util, joy */
 
 // run this script with:
-// yarn script listDataDirectory
+// yarn workspace api-scripts script list-data-directory
 //
 // or copy and paste the code into the pioneer javascript toolbox at:
 // https://testnet.joystream.org/#/js
 // requires nicaea release+
 
 const script = async ({ api }) => {
-  const ids = await api.query.dataDirectory.knownContentIds()
+  const entries = await api.query.dataDirectory.dataByContentId.entries()
 
-  await Promise.all(
-    ids.map(async (id) => {
-      let obj = await api.query.dataDirectory.dataByContentId(id)
-      if (obj.isNone) {
-        return
-      }
-      obj = obj.unwrap()
+  console.error(`Data Directory contains ${entries.length} objects`)
+
+  const acceptedEntries = entries.filter(([, dataObject]) => dataObject.liaison_judgement.type === 'Accepted')
+
+  acceptedEntries.forEach(
+    ([
+      {
+        args: [id],
+      },
+      obj,
+    ]) => {
       console.log(`contentId: ${api.createType('ContentId', id).encode()}, ipfs: ${obj.ipfs_content_id}`)
-    })
+    }
   )
 
-  console.error(`Data Directory contains ${ids.length} objects`)
+  console.error(`Data Directory contains ${acceptedEntries.length} Accepted objects`)
 }
 
 if (typeof module === 'undefined') {

+ 10 - 3
utils/api-scripts/scripts/transfer.js → utils/api-scripts/scripts/stop-council-election.js

@@ -1,11 +1,12 @@
 /* global api, hashing, keyring, types, util, joy, window */
 
 // run this script with:
-// yarn script testTransfer
+// yarn workspace api-scripts script stop-council-election
 //
 // or copy and paste the code into the pioneer javascript toolbox at:
 // https://testnet.joystream.org/#/js
 //
+// requires nicaea release+
 
 const script = async ({ api, keyring }) => {
   const sudoAddress = (await api.query.sudo.key()).toString()
@@ -18,8 +19,14 @@ const script = async ({ api, keyring }) => {
     sudo = sudoAddress
   }
 
-  const transfer = api.tx.balances.transfer(sudoAddress, 100)
-  await transfer.signAndSend(sudo)
+  const tx = api.tx.councilElection.forceStopElection()
+
+  const nonce = (await api.query.system.account(sudoAddress)).nonce
+  const sudoTx = api.tx.sudo.sudo(tx)
+  const signed = sudoTx.sign(sudo, { nonce })
+  await signed.send()
+
+  console.log(`sent tx with nonce: ${nonce.toNumber()}, tx hash: ${signed.hash}`)
 }
 
 if (typeof module === 'undefined') {

+ 33 - 0
utils/api-scripts/scripts/test-transfer.js

@@ -0,0 +1,33 @@
+/* global api, hashing, keyring, types, util, joy, window */
+
+// run this script with:
+// yarn workspace api-scripts script test-transfer
+//
+// or copy and paste the code into the pioneer javascript toolbox at:
+// https://testnet.joystream.org/#/js
+//
+
+const script = async ({ api, keyring, userAddress }) => {
+  const sudoAddress = (await api.query.sudo.key()).toString()
+  const destination = userAddress || sudoAddress
+  let sender
+  if (typeof window === 'undefined') {
+    // In node, get the keyPair if the keyring was provided
+    sender = keyring.getPair(destination)
+  } else {
+    // Pioneer: let the UI Signer handle it
+    sender = destination
+  }
+
+  // Send test transfer to self
+  const transfer = api.tx.balances.transfer(destination, 10)
+  await transfer.signAndSend(sender)
+}
+
+if (typeof module === 'undefined') {
+  // Pioneer js-toolbox
+  script({ api, hashing, keyring, types, util, joy })
+} else {
+  // Node
+  module.exports = script
+}

+ 12 - 9
utils/api-scripts/src/script.ts

@@ -6,20 +6,21 @@ import * as util from '@polkadot/util'
 import joy, { types as joyTypes } from '@joystream/types'
 import * as hashing from '@polkadot/util-crypto'
 import { Keyring } from '@polkadot/keyring'
-
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-const scripts = require('../scripts')
+import fs from 'fs'
+import path from 'path'
 
 async function main() {
   const scriptArg = process.argv[2]
-  const script = scripts[scriptArg]
 
-  if (!scriptArg || !script) {
-    console.error('Please specify valid script name.')
-    console.error('Available scripts:', Object.keys(scripts))
+  if (!scriptArg) {
+    console.error('Please specify script name.')
+    console.error('Available scripts:', fs.readdirSync(path.join(__dirname, '../scripts')))
     return
   }
 
+  // eslint-disable-next-line @typescript-eslint/no-var-requires
+  const script = require(`../scripts/${scriptArg}`)
+
   const provider = new WsProvider('ws://127.0.0.1:9944')
 
   const api = await ApiPromise.create({ provider, types: joyTypes })
@@ -32,8 +33,10 @@ async function main() {
 
   // Optional last argument is a SURI for account to use for signing transactions
   const suri = process.argv[3]
+  let userAddress
   if (suri) {
-    keyring.addFromUri(suri, undefined, 'sr25519')
+    userAddress = keyring.addFromUri(suri, undefined, 'sr25519').address
+    console.error('userAddress:', userAddress)
   }
 
   // Add development well known keys to keyring
@@ -43,7 +46,7 @@ async function main() {
   }
 
   try {
-    await script({ api, types, util, hashing, keyring, joy })
+    await script({ api, types, util, hashing, keyring, joy, userAddress })
   } catch (err) {
     console.error(err)
   }

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini