-
Notifications
You must be signed in to change notification settings - Fork 115
/
buy.ts
89 lines (82 loc) · 3.18 KB
/
buy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import AccountsCommandBase from '../../base/AccountsCommandBase'
import { flags } from '@oclif/command'
import { IMembershipMetadata, MembershipMetadata } from '@joystream/metadata-protobuf'
import { metadataToBytes } from '../../helpers/serialization'
import chalk from 'chalk'
import { formatBalance } from '@polkadot/util'
import ExitCodes from '../../ExitCodes'
export default class MembershipBuyCommand extends AccountsCommandBase {
static description = 'Buy / register a new membership on the Joystream platform.'
static aliases = ['membership:create', 'membership:register']
static flags = {
handle: flags.string({
required: true,
description: "Member's handle",
}),
name: flags.string({
required: false,
description: "Member's first name / full name",
}),
avatarUri: flags.string({
required: false,
description: "Member's avatar uri",
}),
about: flags.string({
required: false,
description: "Member's md-formatted about text (bio)",
}),
controllerKey: flags.string({
required: false,
description: "Member's controller key. Can also be provided interactively.",
}),
rootKey: flags.string({
required: false,
description: "Member's root key. Can also be provided interactively.",
}),
senderKey: flags.string({
required: false,
description: 'Tx sender key. If not provided, controllerKey will be used by default.',
}),
}
async run(): Promise<void> {
const api = this.getOriginalApi()
let { handle, name, avatarUri, about, controllerKey, rootKey, senderKey } = this.parse(MembershipBuyCommand).flags
if (await this.getApi().isHandleTaken(handle)) {
this.error(`Provided handle (${chalk.magentaBright(handle)}) is already taken!`, { exit: ExitCodes.InvalidInput })
}
if (!controllerKey) {
controllerKey = await this.promptForAnyAddress('Choose member controller key')
}
if (!rootKey) {
rootKey = await this.promptForAnyAddress('Choose member root key')
}
senderKey =
senderKey ??
(this.isKeyAvailable(controllerKey) ? controllerKey : await this.promptForAccount('Choose tx sender key'))
const metadata: IMembershipMetadata = {
name,
about,
avatarUri,
}
const membershipPrice = await api.query.members.membershipPrice()
this.warn(
`Buying membership will cost additional ${chalk.cyanBright(
formatBalance(membershipPrice)
)} on top of the regular transaction fee.`
)
this.jsonPrettyPrint(JSON.stringify({ rootKey, controllerKey, senderKey, handle, metadata }))
await this.requireConfirmation('Do you confirm the provided input?')
const result = await this.sendAndFollowTx(
await this.getDecodedPair(senderKey),
api.tx.members.buyMembership({
rootAccount: rootKey,
controllerAccount: controllerKey,
handle,
metadata: metadataToBytes(MembershipMetadata, metadata),
})
)
const memberId = this.getEvent(result, 'members', 'MembershipBought').data[0]
this.log(chalk.green(`Membership with id ${chalk.cyanBright(memberId.toString())} successfully created!`))
this.output(memberId.toString())
}
}