-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsend-many.ts
221 lines (199 loc) · 6.78 KB
/
send-many.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {Args, Command, Flags} from '@oclif/core'
import {
networkFrom,
STACKS_MAINNET,
STACKS_MOCKNET,
STACKS_TESTNET,
StacksNetwork,
} from '@stacks/network';
import {
broadcastTransaction,
STXPostConditionWire,
validateStacksAddress,
} from '@stacks/transactions';
import {
getAddress,
isNormalInteger,
Recipient,
sendMany,
sendStxTransfer,
} from '../builder';
import { getExplorerUrlForTx } from '../util';
type NetworkString = 'mainnet' | 'mocknet' | 'testnet';
const DEFAULT_TESTNET_CONTRACT =
'ST3F1X4QGV2SM8XD96X45M6RTQXKA1PZJZZCQAB4B.send-many';
const DEFAULT_MAINNET_CONTRACT =
'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.send-many';
export default class SendMany extends Command {
static override args = {
recipients: Args.string({description: `
A set of recipients in the format of "address,amount_ustx"
Example: STADMRP577SC3MCNP7T3PRSTZBJ75FJ59JGABZTW,100 ST2WPFYAW85A0YK9ACJR8JGWPM19VWYF90J8P5ZTH,50`}),
}
static override description = `Execute a bulk STX transfer.
The bulk transfer is executed in a single transaction by invoking a \`contract-call\` on the "send-many" contract.
The default contracts can be found below:
Testnet: https://explorer.hiro.so/txid/${DEFAULT_TESTNET_CONTRACT}?chain=testnet
Mainnet: https://explorer.hiro.so/txid/${DEFAULT_MAINNET_CONTRACT}?chain=mainnet`;
static override examples = [
'<%= config.bin %> <%= command.id %> STADMRP577SC3MCNP7T3PRSTZBJ75FJ59JGABZTW,100 ST2WPFYAW85A0YK9ACJR8JGWPM19VWYF90J8P5ZTH,50 -k my_private_key -n testnet -b',
]
static override flags = {
allowSingleStxTransfer: Flags.boolean({
char: 'a',
default: false,
description: `
If enabled and only a single recipient is specified, a STX-transfer transaction type will be used rather than a contract-call transaction.
If omitted, a contract-call will always be used, which can be less efficient.
`,
required: false,
}),
broadcast: Flags.boolean({
char: 'b',
default: false,
description:
'Whether to broadcast this transaction. Omitting this flag will not broadcast the transaction.',
}),
contractAddress: Flags.string({
char: 'c',
description:
'Manually specify the contract address for send-many. If omitted, default contracts will be used.',
}),
feeMultiplier: Flags.integer({
char: 'm',
description: `
Optionally specify a fee multiplier. If passed, the tx fee will be (estimated fee + (estimated fee * multiplier)).
For example, a fee multiplier of 15 for a tx with an estimated fee of 200 would result in a tx with the fee of 230.
`,
required: false,
}),
network: Flags.string({
char: 'n',
default: 'testnet',
description: 'Which network to broadcast this to',
options: ['mocknet', 'testnet', 'mainnet'],
}),
nodeUrl: Flags.string({
char: 'u',
description:
'A default node URL will be used based on the `network` option. Use this flag to manually override.',
required: false,
}),
nonce: Flags.integer({
description: 'Optionally specify a nonce for this transaction',
}),
privateKey: Flags.string({
char: 'k',
description: 'Your private key',
required: true,
}),
quiet: Flags.boolean({
char: 'q',
default: false,
description: `
Reduce logging from this command. If this flag is passed with the broadcast (-b) flag,
only the transaction ID will be logged. If the quiet flagged is passed without broadcast,
only the raw transaction hex will be logged.
`,
}),
}
// allow infinite arguments
static strict = false;
getContract(network: StacksNetwork) {
return network.chainId === STACKS_TESTNET.chainId
? DEFAULT_TESTNET_CONTRACT
: DEFAULT_MAINNET_CONTRACT;
}
getNetwork(flags: { network: string }) {
const networks = {
mainnet: STACKS_MAINNET,
mocknet: STACKS_MOCKNET,
testnet: STACKS_TESTNET,
};
return networks[flags.network as NetworkString];
}
public async run(): Promise<void> {
const {argv, flags} = await this.parse(SendMany)
const recipients: Recipient[] = argv.map(entry => {
const arg = entry as string;
const [address, amount] = arg.split(',');
if (!validateStacksAddress(address)) {
throw new Error(`${address} is not a valid STX address`);
}
if (!isNormalInteger(amount)) {
throw new Error(`${amount} is not a valid integer.`);
}
return {
address,
amount,
};
});
const networkClass = this.getNetwork(flags);
if (!networkClass) {
throw new Error('Unable to get network');
}
const network = networkFrom(networkClass);
if (flags.nodeUrl) {
network.client.baseUrl = flags.nodeUrl;
}
if (
network.magicBytes === STACKS_MOCKNET.magicBytes &&
!flags.contractAddress
) {
throw new Error('Must manually specify contract address for mocknet');
}
const contractIdentifier =
flags.contractAddress || this.getContract(network);
const performStxTransferTx: boolean =
recipients.length === 1 && flags.allowSingleStxTransfer;
const tx = await (performStxTransferTx ? sendStxTransfer({
feeMultiplier: flags.feeMultiplier,
network,
nonce: flags.nonce,
recipient: recipients[0],
senderKey: flags.privateKey,
}) : sendMany({
contractIdentifier,
feeMultiplier: flags.feeMultiplier,
network,
nonce: flags.nonce,
recipients,
senderKey: flags.privateKey,
}));
const verbose = !flags.quiet;
if (verbose) {
this.log('Transaction hex:', tx.serialize());
this.log('Fee:', tx.auth.spendingCondition.fee.toString());
this.log('Nonce:', tx.auth.spendingCondition?.nonce.toString());
this.log('Contract:', contractIdentifier);
this.log('Sender:', getAddress(flags.privateKey, network));
const postCondition = tx.postConditions.values[0] as STXPostConditionWire;
this.log('Total amount:', postCondition.amount.toString());
if (flags.allowSingleStxTransfer) {
this.log('Is STX-transfer transaction type: ', performStxTransferTx);
}
}
if (flags.broadcast) {
try {
const { txid: result } = await broadcastTransaction({
network,
transaction: tx,
});
if (verbose) {
this.log('Transaction ID:', result);
const explorerLink = getExplorerUrlForTx(result, flags.network);
if (explorerLink) {
this.log('View in explorer:', explorerLink);
}
} else {
console.log(result.toString());
}
} catch (error) {
this.log('Transaction rejected:', error);
this.exit(1);
}
} else if (flags.quiet) {
console.log(tx.serialize());
}
}
}