updateChannelTitle.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/ChannelEntity'
  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. // Create partial channel entity, only containing the fields we wish to update
  16. const channelUpdateInput: Partial<ChannelEntity> = {
  17. handle: 'Updated channel handle',
  18. }
  19. // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
  20. const parser = InputParser.createWithKnownSchemas(api)
  21. // We can reuse InputParser's `findEntityIdByUniqueQuery` method to find entityId of the channel we
  22. // created in ./createChannel.ts example (normally we would probably use some other way to do it, ie.: query node)
  23. const CHANNEL_ID = await parser.findEntityIdByUniqueQuery({ handle: 'Example channel' }, 'Channel')
  24. // Use getEntityUpdateOperations to parse the update input
  25. const updateOperations = await parser.getEntityUpdateOperations(
  26. channelUpdateInput,
  27. 'Channel', // Class name
  28. CHANNEL_ID // Id of the entity we want to update
  29. )
  30. await api.tx.contentDirectory
  31. .transaction(
  32. { Member: 0 }, // We use member with id 0 as actor (in this case we assume this is Alice)
  33. updateOperations // In this case this will be just a single UpdateEntityPropertyValues operation
  34. )
  35. .signAndSend(ALICE)
  36. }
  37. main()
  38. .then(() => process.exit())
  39. .catch(console.error)