script.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // eslint-disable-next-line @typescript-eslint/no-var-requires
  9. const scripts = require('../scripts')
  10. async function main() {
  11. const scriptArg = process.argv[2]
  12. const script = scripts[scriptArg]
  13. if (!scriptArg || !script) {
  14. console.error('Please specify valid script name.')
  15. console.error('Available scripts:', Object.keys(scripts))
  16. return
  17. }
  18. const provider = new WsProvider('ws://127.0.0.1:9944')
  19. const api = new ApiPromise({ provider, types: joyTypes })
  20. await api.isReady
  21. // We don't pass a custom signer to the api so we must use a keyPair
  22. // when calling signAndSend on transactions
  23. const keyring = new Keyring()
  24. // Optional last argument is a SURI for account to use for signing transactions
  25. const suri = process.argv[3]
  26. if (suri) {
  27. keyring.addFromUri(suri, undefined, 'sr25519')
  28. }
  29. // Add development well known keys to keyring
  30. if ((await api.rpc.system.chain()).toString() === 'Development') {
  31. keyring.addFromUri('//Alice', undefined, 'sr25519')
  32. keyring.addFromUri('//Bob', undefined, 'sr25519')
  33. }
  34. try {
  35. await script({ api, types, util, hashing, keyring, joy })
  36. } catch (err) {
  37. console.error(err)
  38. }
  39. api.disconnect()
  40. }
  41. main()