@@ -8,23 +8,42 @@ program
8
8
. option ( '--name <name>' , 'REQUIRED - Wallet Name' )
9
9
. option (
10
10
'--recipients <recipients>' ,
11
- 'REQUIRED - Address of receiver, or json array \'[{"address": "abc1234", "amount": xxxxxx}]\''
11
+ 'REQUIRED - JSON array \'[{"address": "abc1234", "amount": xxxxxx}]\''
12
12
)
13
- . option ( '--fee <fee>' , 'REQUIRED - The transaction fee to pay' )
14
13
. option ( '--utxos <utxos>' , 'REQUIRED - Unspent transactions that can be spent' )
15
14
. option ( '--change <change>' , 'REQUIRED - Change addresses' )
15
+ . option ( '--fee [fee]' , 'optional - custom transaction fee to pay' )
16
+ . option ( '--target [target]' , 'optional - custom target block for confirmation' )
16
17
. option ( '--path [path]' , 'optional - Custom wallet storage path' )
17
18
. parse ( process . argv ) ;
18
19
19
20
const main = async ( ) => {
20
21
const { name, path } = program ;
21
22
let wallet ;
22
23
try {
23
- const { recipients, utxos, change, fee } = program ;
24
+ const { recipients, utxos, change, fee, target } = program ;
24
25
wallet = await Wallet . loadWallet ( { name, path } ) ;
26
+
27
+ const parsedUtxos = utxos . includes ( '[' ) ? JSON . parse ( utxos ) : [ ] ;
28
+ const parsedRecipients = recipients . includes ( '[' ) ? JSON . parse ( recipients ) : [ ] ;
29
+
30
+ if ( ! parsedRecipients . length || ! parsedUtxos . length ) {
31
+ throw new Error ( 'invalid arguments' ) ;
32
+ }
33
+
34
+ const utxoBytes = 148 ;
35
+ const outputBytes = 34 ;
36
+ const transactionHeader = 10 ;
37
+ const calculatedNetworkFeeKb = await wallet . getNetworkFee ( { target } ) ;
38
+ const netWorkFeeSat = calculatedNetworkFeeKb . feerate * 1E8 / 1000 ;
39
+
40
+ const transactionSize = ( parsedUtxos . length * utxoBytes + parsedRecipients . length * outputBytes + transactionHeader + parsedUtxos . length ) ;
41
+ const totalTransactionFee = transactionSize * netWorkFeeSat ;
42
+
25
43
let params = { recipients, utxos, change, fee } ;
26
- params . recipients = recipients . includes ( '[' ) ? JSON . parse ( recipients ) : recipients ;
27
- params . utxos = params . utxos ? JSON . parse ( params . utxos ) : [ ] ;
44
+ params . fee = Number ( fee ) || totalTransactionFee ;
45
+ params . recipients = parsedRecipients ;
46
+ params . utxos = parsedUtxos ;
28
47
const tx = await wallet . newTx ( params ) ;
29
48
console . log ( tx . uncheckedSerialize ( ) ) ;
30
49
} catch ( e ) {
0 commit comments