script.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { types as joyTypes } from '@joystream/types'
  6. import * as joy from '@joystream/types'
  7. import * as hashing from '@polkadot/util-crypto'
  8. import { Keyring } from '@polkadot/keyring'
  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 provider = new WsProvider('wss://testnet-rpc-2-singapore.joystream.org')
  20. const api = await ApiPromise.create({ provider, types: joyTypes })
  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()