script.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // @ts-check
  2. import { ApiPromise, WsProvider } from '@polkadot/api'
  3. import * as types from '@polkadot/types'
  4. import * as util from '@polkadot/util'
  5. import joy, { types as joyTypes } from '@joystream/types'
  6. import * as hashing from '@polkadot/util-crypto'
  7. import { Keyring } from '@polkadot/keyring'
  8. import fs from 'fs'
  9. import path from 'path'
  10. async function main() {
  11. const scriptArg = process.argv[2]
  12. if (!scriptArg) {
  13. console.error('Please specify script name.')
  14. console.error('Available scripts:', fs.readdirSync(path.join(__dirname, '../scripts')))
  15. return
  16. }
  17. // eslint-disable-next-line @typescript-eslint/no-var-requires
  18. const script = require(`../scripts/${scriptArg}`)
  19. const provider = new WsProvider('ws://127.0.0.1:9944')
  20. const api = await ApiPromise.create({ provider, types: joyTypes })
  21. await api.isReady
  22. // We don't pass a custom signer to the api so we must use a keyPair
  23. // when calling signAndSend on transactions
  24. const keyring = new Keyring()
  25. // Optional last argument is a SURI for account to use for signing transactions
  26. const suri = process.argv[3]
  27. let userAddress
  28. if (suri) {
  29. userAddress = keyring.addFromUri(suri, undefined, 'sr25519').address
  30. console.error('userAddress:', userAddress)
  31. }
  32. // Add development well known keys to keyring
  33. if ((await api.rpc.system.chain()).toString() === 'Development') {
  34. keyring.addFromUri('//Alice', undefined, 'sr25519')
  35. keyring.addFromUri('//Bob', undefined, 'sr25519')
  36. }
  37. try {
  38. await script({ api, types, util, hashing, keyring, joy, userAddress })
  39. } catch (err) {
  40. console.error(err)
  41. }
  42. api.disconnect()
  43. }
  44. main()