Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions utils/api-examples/src/tohex.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { CuratorApplicationId } from '@joystream/types/content-working-group'
import { BTreeSet } from '@polkadot/types'
import { BTreeSet, createType, TypeRegistry } from '@polkadot/types'
import { types } from '@joystream/types'

async function main() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script only needs to create some types and doesn't need to read state from chain, and therefore shouldn't require creating an api instance or connecting to a node. I this type of scenario, what is the proper way to create a type registry and register our types so we can construct them safely.

cc: @Lezek123

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a function to do this in https://github.com/Joystream/joystream/pull/1176/files#diff-9e9b5c1632859d4c7bee5d17dd9f7c5c, it's not well-tested yet, but I think it should work fine. I can move this to another PR if needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const provider = new WsProvider('ws://127.0.0.1:9944')
const api = await ApiPromise.create({ provider, types })

const wgId = [1, 2]

// Is it not possible to create the registry without actually connecting to a node?
const set = new BTreeSet<CuratorApplicationId>(api.registry, CuratorApplicationId, [])
const registry = new TypeRegistry()
registry.register(types)

const set = new BTreeSet<CuratorApplicationId>(registry, CuratorApplicationId, [])

wgId.forEach((id) => {
set.add(new CuratorApplicationId(api.registry, id))
set.add(createType(registry, 'CuratorApplicationId', id))
Copy link
Contributor

@Lezek123 Lezek123 Aug 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found out we can actually use registry.createType() instead, which is probably a better approach (I'm going to change it in @joystream/types too)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that is neater, and one less import :)

})

/*
Expand All @@ -27,8 +25,6 @@ async function main() {

console.log('copy/paste the output below to hire curator applicant(s) with WG IDs:', wgId)
console.log(set.toHex())

api.disconnect()
}

main()