updateChannelTitleWithoutTransaction.ts 2.0 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'
  7. import { FlattenRelations } from '@joystream/cd-schemas/types/utility'
  8. // Alternative way of update a channel using updateEntityPropertyValues extrinsic
  9. async function main() {
  10. // Initialize the api
  11. const provider = new WsProvider('ws://127.0.0.1:9944')
  12. const api = await ApiPromise.create({ provider, types: joyTypes })
  13. // Get Alice keypair
  14. const keyring = new Keyring()
  15. keyring.addFromUri('//Alice', undefined, 'sr25519')
  16. const [ALICE] = keyring.getPairs()
  17. // Create partial channel entity, only containing the fields we wish to update
  18. const channelUpdateInput: Partial<FlattenRelations<ChannelEntity>> = {
  19. handle: 'Updated channel handle 2',
  20. }
  21. // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
  22. const parser = InputParser.createWithKnownSchemas(api)
  23. // We can reuse InputParser's `findEntityIdByUniqueQuery` method to find entityId of the channel we
  24. // created in ./createChannelWithoutTransaction.ts example
  25. // (normally we would probably use some other way to do it, ie.: query node)
  26. const CHANNEL_ID = await parser.findEntityIdByUniqueQuery({ handle: 'Example channel 2' }, 'Channel')
  27. // We use parser to create input property values map
  28. const newPropertyValues = await parser.parseToInputEntityValuesMap(channelUpdateInput, 'Channel')
  29. await api.tx.contentDirectory
  30. .updateEntityPropertyValues(
  31. { Member: 0 }, // We use member with id 0 as actor (in this case we assume this is Alice)
  32. CHANNEL_ID,
  33. newPropertyValues
  34. )
  35. .signAndSend(ALICE)
  36. }
  37. main()
  38. .then(() => process.exit())
  39. .catch(console.error)