-
Notifications
You must be signed in to change notification settings - Fork 115
/
constructTxCall.ts
88 lines (75 loc) · 2.89 KB
/
constructTxCall.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
import { flags } from '@oclif/command'
import { blake2AsHex } from '@polkadot/util-crypto'
import AdvancedTransactionsCommandBase from '../../base/AdvancedTransactionsCommandBase'
import { registry } from '@joystream/types'
import { OptionsWithMeta } from '@substrate/txwrapper-core'
import { ensureOutputFileIsWriteable, saveOutputJsonToFile } from '../../helpers/InputOutput'
import chalk from 'chalk'
export default class ConstructTxCallCommand extends AdvancedTransactionsCommandBase {
static description = 'Construct a call that as argument for a transaction, or to wrap in another call.'
static flags = {
address: flags.string({
required: true,
description: 'The address that is performing the (final) transaction.',
}),
module: flags.string({
required: true,
description: 'The module (a.k.a. section) of the extrinsic',
}),
method: flags.string({
required: true,
description: 'The method of the extrinsic',
}),
output: flags.string({
char: 'o',
required: true,
description: 'Path to the file where the output JSON should be saved.',
}),
lifetime: flags.integer({
required: false,
description:
'Lifetime of the transaction, from creation to included on chain, in blocks before it becomes invalid.',
default: 64,
}),
tip: flags.integer({
required: false,
default: 0,
description: 'Optional "tip" (in base value) for faster block inclusion.',
}),
nonceIncrement: flags.integer({
required: false,
default: 0,
description:
'If you are preparing multiple transactions from the samme account, before broadcasting them, you need to increase the nonce by 1 for each. This value will be added to the nonce read from the chain.',
}),
}
async run(): Promise<void> {
const { address, module, method, output, lifetime, tip, nonceIncrement } = this.parse(ConstructTxCallCommand).flags
ensureOutputFileIsWriteable(output)
const unsignedMethod = await this.promptForTxMethod(module, method)
const txInfo = await this.getTxInfo(address, unsignedMethod, nonceIncrement, lifetime, tip)
const optionsWithMeta: OptionsWithMeta = {
metadataRpc: `0x${txInfo.metadataRpc.slice(2)}`,
registry,
}
const unsigned = await this.getDefinedMethod(txInfo, optionsWithMeta)
const call = unsigned.method
const callHash: string = blake2AsHex(call)
this.log(`The callhash is: ${callHash}`)
if (call.length > 500) {
this.log(`Call too long to log to console - see output file`)
} else {
this.log(`Call: ${call}`)
}
const outputJson = {
call,
callHash,
}
try {
saveOutputJsonToFile(output, outputJson)
this.log(chalk.green(`Output successfully saved in: ${chalk.magentaBright(output)}!`))
} catch (e) {
this.warn(`Could not save output to ${output}!`)
}
}
}