-
Notifications
You must be signed in to change notification settings - Fork 115
/
transferTokens.ts
44 lines (37 loc) · 1.46 KB
/
transferTokens.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
import { flags } from '@oclif/command'
import ExitCodes from '../../ExitCodes'
import AccountsCommandBase from '../../base/AccountsCommandBase'
import { validateAddress } from '../../helpers/validation'
export default class AccountTransferTokens extends AccountsCommandBase {
static description = 'Transfer tokens from any of the available accounts'
static flags = {
from: flags.string({
required: false,
description: 'Address of the sender (can also be provided interactively)',
}),
to: flags.string({
required: false,
description: 'Address of the recipient (can also be provided interactively)',
}),
amount: flags.string({
required: true,
description: 'Amount of tokens to transfer',
}),
}
async run(): Promise<void> {
let { from, to, amount } = this.parse(AccountTransferTokens).flags
// Initial validation
if (!from) {
from = await this.promptForAccount('Select sender account')
} else if (!this.isKeyAvailable(from)) {
this.error('Sender key not available', { exit: ExitCodes.InvalidInput })
}
if (!to) {
to = await this.promptForAnyAddress('Select recipient')
} else if (validateAddress(to) !== true) {
this.error('Invalid recipient address', { exit: ExitCodes.InvalidInput })
}
await this.ensureJoyTransferIsPossible(from, to, amount)
await this.sendAndFollowNamedTx(await this.getDecodedPair(from), 'balances', 'transferKeepAlive', [to, amount])
}
}