-
Notifications
You must be signed in to change notification settings - Fork 115
/
directChannelPayment.ts
81 lines (74 loc) · 2.97 KB
/
directChannelPayment.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
import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
import { metadataToString } from '../../helpers/serialization'
import { MakeChannelPayment, MemberRemarked } from '@joystream/metadata-protobuf'
import { flags } from '@oclif/command'
import UploadCommandBase from '../../base/UploadCommandBase'
import ExitCodes from '../../ExitCodes'
import Long from 'long'
import chalk from 'chalk'
export default class DirectChannelPaymentCommand extends ContentDirectoryCommandBase {
static description = `Make direct payment to channel's reward account by any member .`
static flags = {
rewardAccount: flags.string({
description: 'Reward account of the channel to be paid',
exclusive: ['channelId'],
}),
channelId: flags.string({
description: 'ID of the channel to be paid',
exclusive: ['rewardAccount'],
}),
amount: flags.string({
required: true,
description: 'JOY amount to be paid',
}),
rationale: flags.string({
char: 'r',
required: true,
description: 'Reason for the payment',
}),
videoId: flags.string({
char: 'v',
description:
'video ID for which payment is being made. If not provided, payment is supposed to be a channel wide tip',
}),
...UploadCommandBase.flags,
}
async run(): Promise<void> {
const { rewardAccount, channelId, amount, rationale, videoId } = this.parse(DirectChannelPaymentCommand).flags
if (!channelId && !rewardAccount) {
this._help()
return
}
const channel = channelId
? await this.getQNApi().getChannelById(channelId)
: await this.getQNApi().getChannelByRewardAccount(rewardAccount || '')
if (channelId && !channel) {
this.error(`Channel with ID ${channelId} does not exist`, { exit: ExitCodes.InvalidInput })
} else if (!channel) {
this.error(`Channel with reward account ${rewardAccount} does not exist`, { exit: ExitCodes.InvalidInput })
}
if (videoId) {
const video = await this.getApi().videoById(videoId)
if (video.inChannel.toString() !== channel.id) {
this.error(`Video with id ${videoId} does not exist in payee channel`, {
exit: ExitCodes.InvalidInput,
})
}
}
const meta = new MemberRemarked({
makeChannelPayment: new MakeChannelPayment({
videoId: videoId ? Long.fromString(videoId) : undefined,
rationale,
}),
})
const message = metadataToString(MemberRemarked, meta)
const [memberId, controllerAccount, payment] = await this.getValidatedMemberRemarkParams({
account: channel.rewardAccount,
amount,
})
const keypair = await this.getDecodedPair(controllerAccount)
const result = await this.sendAndFollowNamedTx(keypair, 'members', 'memberRemark', [memberId, message, payment])
const [id] = this.getEvent(result, 'members', 'MemberRemarked').data
this.log(chalk.green(`Member ${id} successfully paid channel ${channelId} with amount ${amount}!`))
}
}