-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
142 lines (135 loc) · 4.1 KB
/
index.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
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
const axios = require('axios'),
time = require("time"),
querystring = require('querystring'),
readline = require('readline'),
fs = require('fs'),
config = require('./config.json'),
bchaddr = require('bchaddrjs'),
path = require('path'),
env = process.env.NODE_ENV || 'development';
time.tzset("Asia/Shanghai");
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let addresses = []
const rl = readline.createInterface({
input: fs.createReadStream(path.normalize('./node_modules/addresses.txt')),
})
function collectAddressFromLine(line) {
if (line) {
let address = line.trim()
address = bchaddr.toLegacyAddress(address) // btc.com api 只支持旧版地址
addresses.push(address)
}
}
rl.on('line', (line) => {
collectAddressFromLine(line)
})
rl.on('close', (line) => {
collectAddressFromLine(line)
console.log({addresses})
app.init()
})
let { address_type: addressType } = config
addressType = addressType.toLowerCase()
const apiPrefixs = {
'btc': '',
'bch': 'bch-',
'bsv': 'bsv-',
}
const mobilePrefixs = {
'btc': 'm',
'bch': 'mbch',
'bsv': 'mbsv',
}
const API = `https://${apiPrefixs[addressType]}chain.api.btc.com/v3`
let lastTxId = null // 缓存最新交易ID
const app = {
init() {
this.checkAddress(addresses[0]) // 立刻执行一次
let addrStr
if (addresses.length > 1) {
addrStr = addresses.join(',')
} else {
addrStr = addresses
}
setInterval(() => this.checkAddress(addrStr), 10000)
},
checkAddress(addrStr) {
console.log('check')
axios.get(`${API}/address/${addrStr}`).then(async res => {
let data = res.data.data
if (!data) return console.error('地址无效')
if (data.length) {
for (let item of data) {
this.checkBalance(item)
}
} else {
this.checkBalance(data)
}
}).catch(error => console.log(error))
},
async checkBalance(data) {
if (data.unconfirmed_received && data.last_tx !== lastTxId) { // debug 时,第一个条件改为相反
await sleep(3000) // 请求间隔太短,会被拒绝
lastTxId = data.last_tx
const title = `收到 ${data.unconfirmed_received * 0.00000001} ${addressType.toUpperCase()} `
const desp = `
地址:${addressType.indexOf('bch') > -1 ? (bchaddr.toCashAddress(data.address)).substr(12) : data.address}
[查看交易](https://${mobilePrefixs[addressType]}.btc.com/${data.last_tx})
`
console.log(desp)
// return // debug 时,取消注释
if (config.server_chan_key) {
this.serverChan(title, desp)
}
if (config.telegram_bot_key && config.telegram_chat_id) {
this.sendToTelegram(title + desp)
}
}
},
async getTxDetail(id) {
let url = `${API}/tx/${id}`
return axios.get(url).then(res => {
const data = res.data.data
const myOutput = data.outputs.filter(output => output.addresses[0] === config.observed_address)
return {
'time': data.created_at,
'value': myOutput[0].value
}
}).catch(error => {
console.log(error)
return url
})
},
serverChan(title, desp) {
return axios.post(`http://sc.ftqq.com/${config.serverchan_key}.send`,
querystring.stringify({
text: title,
desp: desp
}))
.then((response) => {
if (response.status === 200) return console.log('serverChan: send success')
console.warn(response.status)
})
.catch((error) => {
console.error(error);
});
},
sendToTelegram(text) {
const url = `https://api.telegram.org/bot${config.telegram_bot_key}/sendMessage?chat_id=${config.telegram_chat_id}&disable_web_page_preview=true&parse_mode=markdown`
axios.post(url,
querystring.stringify({
text: text.replace(/(?=[*_`\[])/g, '\\')
}))
.then(res => {
if (res.data.ok) {
return console.log('telegram send success')
}
})
},
formatDate(t) {
return (new Date(t * 1000)).toLocaleString('zh-CN')
}
}
function sleep(ms = 0) {
return new Promise((resolve, reject) => setTimeout(resolve, ms));
}