1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { ApiPromise, WsProvider } from '@polkadot/api'
- import { types as joyTypes } from '@joystream/types'
- import { Keyring } from '@polkadot/keyring'
- import { InputParser } from 'cd-schemas'
- import { ChannelEntity } from 'cd-schemas/types/entities'
- import { FlattenRelations } from 'cd-schemas/types/utility'
- import { EntityId } from '@joystream/types/content-directory'
- async function main() {
-
- const provider = new WsProvider('ws://127.0.0.1:9944')
- const api = await ApiPromise.create({ provider, types: joyTypes })
-
- const keyring = new Keyring()
- keyring.addFromUri('//Alice', undefined, 'sr25519')
- const [ALICE] = keyring.getPairs()
- const parser = InputParser.createWithKnownSchemas(api)
-
- const classId = await parser.getClassIdByName('Channel')
- const languageEntityId = await parser.findEntityIdByUniqueQuery({ code: 'EN' }, 'Language')
-
- const channel: FlattenRelations<ChannelEntity> = {
- title: 'Example channel 2',
- description: 'This is an example channel',
- language: languageEntityId,
- coverPhotoUrl: '',
- avatarPhotoURL: '',
- isPublic: true,
- }
-
- const entityId = await new Promise<EntityId>((resolve, reject) => {
- api.tx.contentDirectory.createEntity(classId, { Member: 0 }).signAndSend(ALICE, {}, (res) => {
- if (res.isError) {
- reject(new Error(res.status.type))
- }
- res.events.forEach(({ event: e }) => {
- if (e.method === 'EntityCreated') {
- resolve(e.data[1] as EntityId)
- }
- if (e.method === 'ExtrinsicFailed') {
- reject(new Error('Extrinsic failed'))
- }
- })
- })
- })
- const inputPropertyValuesMap = await parser.parseToInputEntityValuesMap({ ...channel }, 'Channel')
- // Having entityId we can create and send addSchemaSupport tx
- await api.tx.contentDirectory
- .addSchemaSupportToEntity(
- { Member: 0 },
- entityId,
- 0,
- inputPropertyValuesMap
- )
- .signAndSend(ALICE)
- }
- main()
- .then(() => process.exit())
- .catch(console.error)
|