forked from you21979/node-electrum-client
-
Notifications
You must be signed in to change notification settings - Fork 11
/
raii_client_transaction_send.js
46 lines (36 loc) · 1.14 KB
/
raii_client_transaction_send.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
'use strict'
const ElectrumClient = require('..')
const createRaiiClient = (port, host, protocol, options) => {
return (params, promise) => {
const name = params.join(':')
const client = new ElectrumClient(port, host, protocol, options)
console.time(name)
return client.connect()
.then(() => {
return promise(client)
}).catch((e) => {
client.close()
console.timeEnd(name)
throw e
}).then((res) => {
client.close()
console.timeEnd(name)
return res
})
}
}
const main = async (hex) => {
const hosts = ['electrum.bitaroo.net', 'electrumx.tamami-foundation.org']
const host = hosts[Math.floor(Math.random() * hosts.length)]
const connect = createRaiiClient(host, 50001, 'tcp')
await connect(['blockchain_transaction_broadcast', hex], async (client) => {
const ver = await client.server_version('2.7.11', '1.4')
console.log('Version:', ver)
const result = await client.blockchain_transaction_broadcast(hex)
console.log('Result:', result)
})
}
const getopt = () => {
return process.argv.slice(2)[0]
}
main(getopt()).catch(console.log)