createChannelWithoutTransaction.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import { FlattenRelations } from '@joystream/cd-schemas/types/utility'
  8. import { EntityId } from '@joystream/types/content-directory'
  9. // Alternative way of creating a channel using separate extrinsics (instead of contentDirectory.transaction)
  10. async function main() {
  11. // Initialize the api
  12. const provider = new WsProvider('ws://127.0.0.1:9944')
  13. const api = await ApiPromise.create({ provider, types: joyTypes })
  14. // Get Alice keypair
  15. const keyring = new Keyring()
  16. keyring.addFromUri('//Alice', undefined, 'sr25519')
  17. const [ALICE] = keyring.getPairs()
  18. const parser = InputParser.createWithKnownSchemas(api)
  19. // In this case we need to fetch some data first (like classId and language entity id)
  20. const classId = await parser.getClassIdByName('Channel')
  21. const languageEntityId = await parser.findEntityIdByUniqueQuery({ code: 'EN' }, 'Language')
  22. // We use FlattenRelations to exlude { new } and { existing } (which are not allowed if we want to parse only a single entity)
  23. const channel: FlattenRelations<ChannelEntity> = {
  24. handle: 'Example channel 2',
  25. description: 'This is an example channel',
  26. language: languageEntityId,
  27. coverPhotoUrl: '',
  28. avatarPhotoUrl: '',
  29. isPublic: true,
  30. }
  31. // In this case we use some basic callback to retrieve entityId from the extrinsc event
  32. const entityId = await new Promise<EntityId>((resolve, reject) => {
  33. api.tx.contentDirectory.createEntity(classId, { Member: 0 }).signAndSend(ALICE, {}, (res) => {
  34. if (res.isError) {
  35. reject(new Error(res.status.type))
  36. }
  37. res.events.forEach(({ event: e }) => {
  38. if (e.method === 'EntityCreated') {
  39. resolve(e.data[1] as EntityId)
  40. }
  41. if (e.method === 'ExtrinsicFailed') {
  42. reject(new Error('Extrinsic failed'))
  43. }
  44. })
  45. })
  46. })
  47. const inputPropertyValuesMap = await parser.parseToInputEntityValuesMap({ ...channel }, 'Channel')
  48. // Having entityId we can create and send addSchemaSupport tx
  49. await api.tx.contentDirectory
  50. .addSchemaSupportToEntity(
  51. { Member: 0 }, // Context (in this case we assume it's Alice's member id)
  52. entityId,
  53. 0, // Schema (currently we have one schema per class, so it can be just 0)
  54. inputPropertyValuesMap
  55. )
  56. .signAndSend(ALICE)
  57. }
  58. main()
  59. .then(() => process.exit())
  60. .catch(console.error)