forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestimateTransaction.js
80 lines (68 loc) · 3 KB
/
estimateTransaction.js
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
//
// Get detailed transaction information prior to sending
//
// This example can be used to retrieve information regarding a transaction for a
// particular amount prior to sending. It can be helpful to know the estimated fee,
// or number of inputs needed to send a particular amount. This example can be used
// to retrieve such information.
//
// Copyright 2016, BitGo, Inc. All Rights Reserved.
//
const BitGoJS = require('../src/index.js');
if (process.argv.length < 8) {
console.log('usage:\n\t' + process.argv[0] + ' ' + process.argv[1] +
' <user> <pass> <otp> <walletId> <destinationAddress> <amountSatoshis> <feeRate>');
console.log('user: user email (on test.bitgo.com)');
console.log('pass: password');
console.log('otp: one-time password, 0000000 on test');
console.log('walletId: wallet ID (first address on the wallet)');
console.log('destinationAddress: the bitcoin address to send coins to');
console.log('amountSatoshis: number of satoshis to send');
console.log('feeRate: The fee rate to use in satoshis / kb [optional]');
process.exit(-1);
}
const user = process.argv[2];
const password = process.argv[3];
const otp = process.argv[4];
const walletId = process.argv[5];
const destinationAddress = process.argv[6];
const amountSatoshis = parseInt(process.argv[7], 10);
let feeRate = undefined;
if (!!process.argv[8]) {
feeRate = parseInt(process.argv[8], 10);
}
const bitgo = new BitGoJS.BitGo({ env: 'test' });
const getTransactionInfo = function() {
return bitgo.authenticate({ username: user, password: password, otp: otp })
.then(function() {
return bitgo.unlock({ otp: otp });
})
.then(function() {
// Fetch the specified wallet
return bitgo.wallets().get({ id: walletId });
})
.then(function(wallet) {
// Set recipients
const recipients = {};
recipients[destinationAddress] = amountSatoshis;
// Create the transaction
return wallet.createTransaction({ recipients: recipients, feeRate: feeRate });
}).then(function(transaction) {
console.log('\nEstimated Transaction Info:\n');
console.log('\tSending from: ' + walletId);
console.log('\tSending to: ' + destinationAddress);
console.log('\tAmount: ' + amountSatoshis + ' satoshis');
console.log('\n\tTotal number of inputs: ' + transaction.unspents.length);
console.log('\tTotal number of outputs: ' + transaction.txInfo.nOutputs);
console.log('\tBitcoin network fee rate: ' + transaction.feeRate + ' (satoshis / kb)');
console.log('\tEstimated transaction size: ' + transaction.estimatedSize + ' bytes');
console.log('\tEstimated Bitcoin network fee: ' + transaction.fee + ' satoshis');
console.log('\tBitGo fee: ' + (transaction.bitgoFee && transaction.bitgoFee.amount || 0) + ' satoshis');
process.exit(-1);
})
.catch(function(err) {
console.log(err);
process.exit(-1);
});
};
getTransactionInfo();