-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathconstructSetCodeCall.ts
89 lines (78 loc) · 2.81 KB
/
constructSetCodeCall.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 { flags } from '@oclif/command'
import { TxMethod, OptionsWithMeta } from '@substrate/txwrapper-core'
import AdvancedTransactionsCommandBase from '../../base/AdvancedTransactionsCommandBase'
import { ensureOutputFileIsWriteable, saveOutputJsonToFile } from '../../helpers/InputOutput'
import { blake2AsHex } from '@polkadot/util-crypto'
import { registry } from '@joystream/types'
export default class CreateSetCodeCallCommand extends AdvancedTransactionsCommandBase {
static description = 'Construct a "system.setCode" call.'
static flags = {
wasmPath: flags.string({
required: true,
description: 'The address that is performing the final call.',
}),
address: flags.string({
required: true,
description: 'The address that is performing the final call.',
}),
output: flags.string({
char: 'o',
required: true,
description: 'Path to the file where the call should be saved',
}),
codeOutput: flags.string({
required: false,
description: 'Path to where the parsed wasm code shold 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 { wasmPath, address, output, codeOutput, nonceIncrement, lifetime, tip } =
this.parse(CreateSetCodeCallCommand).flags
ensureOutputFileIsWriteable(output)
const code = await this.parseWasm(wasmPath)
const outCode = {
code: `0x${code}`,
}
const unsignedMethod: TxMethod = {
args: {
code: `0x${code}`,
},
name: 'setCode',
pallet: 'system',
}
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)
const unsignedTxData = {
call,
}
saveOutputJsonToFile(output, unsignedTxData)
this.log(`Call has callhash ${callHash}`)
if (codeOutput) {
ensureOutputFileIsWriteable(codeOutput)
saveOutputJsonToFile(codeOutput, outCode)
}
}
}