createChannel.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { ApiPromise, WsProvider } from '@polkadot/api'
  2. import { types as joyTypes } from '@joystream/types'
  3. import { Keyring } from '@polkadot/keyring'
  4. // Import input parser and channel entity from @joystream/cd-schemas (we use it as library here)
  5. import { InputParser } from '@joystream/cd-schemas'
  6. import { ChannelEntity } from '@joystream/cd-schemas/types/entities'
  7. async function main() {
  8. // Initialize the api
  9. const provider = new WsProvider('ws://127.0.0.1:9944')
  10. const api = await ApiPromise.create({ provider, types: joyTypes })
  11. // Get Alice keypair
  12. const keyring = new Keyring()
  13. keyring.addFromUri('//Alice', undefined, 'sr25519')
  14. const [ALICE] = keyring.getPairs()
  15. const channel: ChannelEntity = {
  16. handle: 'Example channel',
  17. description: 'This is an example channel',
  18. // We can use "existing" syntax to reference either an on-chain entity or other entity that's part of the same batch.
  19. // Here we reference language that we assume was added by initialization script (initialize:dev), as it is part of
  20. // input/entityBatches/LanguageBatch.json
  21. language: { existing: { code: 'EN' } },
  22. coverPhotoUrl: '',
  23. avatarPhotoUrl: '',
  24. isPublic: true,
  25. }
  26. // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
  27. const parser = InputParser.createWithKnownSchemas(
  28. api,
  29. // The second argument is an array of entity batches, following standard entity batch syntax ({ className, entries }):
  30. [
  31. {
  32. className: 'Channel',
  33. entries: [channel], // We could specify multiple entries here, but in this case we only need one
  34. },
  35. ]
  36. )
  37. // We parse the input into CreateEntity and AddSchemaSupportToEntity operations
  38. const operations = await parser.getEntityBatchOperations()
  39. await api.tx.contentDirectory
  40. .transaction(
  41. { Member: 0 }, // We use member with id 0 as actor (in this case we assume this is Alice)
  42. operations // We provide parsed operations as second argument
  43. )
  44. .signAndSend(ALICE)
  45. }
  46. main()
  47. .then(() => process.exit())
  48. .catch(console.error)