-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathconstructUnsignedTxApproveMs.ts
165 lines (150 loc) · 5.71 KB
/
constructUnsignedTxApproveMs.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { flags } from '@oclif/command'
import { blake2AsHex } from '@polkadot/util-crypto'
import AdvancedTransactionsCommandBase from '../../base/AdvancedTransactionsCommandBase'
import { Call } from '@polkadot/types/interfaces'
import { registry } from '@joystream/types'
import { OptionsWithMeta } from '@substrate/txwrapper-core'
import { ensureOutputFileIsWriteable, IOFlags } from '../../helpers/InputOutput'
import { formatBalance } from '@polkadot/util'
export default class ConstructUnsignedTxApproveMsCommand extends AdvancedTransactionsCommandBase {
static description = 'Approve a transaction from a multisig account, as initiated by another signer.'
static flags = {
input: IOFlags.input,
addressSigner: flags.string({
required: true,
description: 'The address of the signer that is approving the multisig transaction.',
}),
output: flags.string({
char: 'o',
required: true,
description: 'Path to the file where the output JSON should be saved.',
}),
inputCall: flags.string({
required: false,
description: 'The hex-encoded call that is to be executed by the multisig if successfull.',
exactlyOne: ['inputCallFile', 'inputCall'],
}),
inputCallFile: flags.string({
required: false,
description:
'Path to a JSON file with the hex-encoded call that is to be executed by the multisig if successfull.',
exactlyOne: ['inputCallFile', 'inputCall'],
}),
addressMs: flags.string({
required: false,
description: 'The address of the multisig that is performing the transaction.',
}),
others: flags.string({
required: false,
description:
'Comma separated list of the accounts (other than the addressSigner) who can approve this call. Ignored if "input" is provided.',
}),
threshold: flags.integer({
description:
'How many (m) of the n signatories (signer+others), are required to sign/approve the transaction. Ignored if "input" is provided.',
}),
timepointHeight: flags.integer({
description:
'Reference to the blockheight of the transaction that initiated the multisig transaction. Ignored if "input" is provided.',
}),
timepointIndex: flags.integer({
description: 'Reference to the extrinsic index in the "timepointHeight block. Ignored if "input" is provided.',
}),
lifetime: flags.integer({
required: false,
description:
'Lifetime of the transaction, from constructed to included in a block, in blocks before it becomes invalid. Must be a power of two between 4 and 65536',
}),
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 {
input,
addressSigner,
output,
addressMs,
others,
inputCall,
inputCallFile,
threshold,
timepointHeight,
timepointIndex,
nonceIncrement,
lifetime,
tip,
} = this.parse(ConstructUnsignedTxApproveMsCommand).flags
ensureOutputFileIsWriteable(output)
const call = await this.getCallInput(inputCall, inputCallFile)
const decodedCall: Call = this.createType('Call', call)
const fetchedWeight = await this.getWeight(decodedCall)
const callHash: string = blake2AsHex(call)
const args = await this.getApproveMsInputs(
input,
threshold,
timepointHeight,
timepointIndex,
others,
callHash,
fetchedWeight
)
const txMethod = {
args,
name: 'approveAsMulti',
pallet: 'multisig',
}
const multiAddress = await this.getMsAddress(
parseInt(args.threshold.toString()),
args.otherSignatories as string[],
addressSigner
)
const accBalances = await this.getApi().getAccountsBalancesInfo([multiAddress])
if (addressMs) {
if (multiAddress !== addressMs) {
await this.requireConfirmation(
`The input sender account, addressMs: ${addressMs},` +
`does not match the account calculated from signer + others: ${multiAddress}` +
`Do you wish to continue?`
)
}
}
this.log(
`You are approving a multisig transaction from ${multiAddress}, with balances:\n` +
` - free: ${formatBalance(accBalances[0].freeBalance)}\n` +
` - available: ${formatBalance(accBalances[0].availableBalance)}\n` +
`If the multisig approves, the transaction will execute:\n` +
` - module:method - ${decodedCall.section}:${decodedCall.method}\n`
)
if (decodedCall.argsEntries.toString().length < 500) {
this.log(` - ${decodedCall.argsEntries.toString()}\n`)
} else {
this.log(`Decoded call is to too long for log. Check the output file.`)
}
const multisigTxData = {
call,
callHash,
}
const txInfo = await this.getTxInfo(addressSigner, txMethod, nonceIncrement, lifetime, tip)
const optionsWithMeta: OptionsWithMeta = {
metadataRpc: `0x${txInfo.metadataRpc.slice(2)}`,
registry,
}
const unsigned = await this.getDefinedMethod(txInfo, optionsWithMeta)
const unsignedCall = unsigned.method
const unsignedCallHash: string = blake2AsHex(unsignedCall)
const unsignedTxData = {
call: unsignedCall,
callHash: unsignedCallHash,
}
await this.createTransactionReadyForSigning(unsigned, output, unsignedTxData, multisigTxData)
}
}