-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathavalon-watcher.js
95 lines (88 loc) · 3.73 KB
/
avalon-watcher.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
var {spawn} = require('child_process');
var javalon = require('javalon')
javalon.init({api: 'https://avalon.d.tube'})
let delay = 2800
let txQueue = []
let isTransactioning = false
setInterval(function() {
if (!isTransactioning && txQueue && txQueue.length > 0) {
try {
isTransactioning = true
updateGasPrice(function() {
console.log('=== BEGIN SEND-TX ===')
let gasPrice = Math.round(ethGasPrice*(1+config.gasBoost))
console.log('Gas Price Used: '+gasPrice+' wei')
let cmd = txQueue[0] + " --price "+gasPrice
txQueue.splice(0,1)
let mintWdtc = spawn('npx', cmd.split(' '));
mintWdtc.stderr.on('data', (data) => {
console.error(data.toString());
});
mintWdtc.on('close', (code) => {
isTransactioning = false
console.log(`=== END SEND-TX: ${code} ===`);
});
})
} catch (error) {
throw err
}
}
}, 5000)
class AvalonWatcher {
constructor(address) {
console.log('Watching '+address+'@avalon')
this.address = address
}
checkBlock(number) {
javalon.getBlock(number, function(err, block) {
// if (err) console.log(err)
if (err || !block) {
setTimeout(function() {DTCWatcher.checkBlock(number)}, delay)
return
}
headblocks.avalon = number
fs.writeFileSync('./headblocks.js', "module.exports={eth:"+headblocks.eth+",avalon:"+headblocks.avalon+"}")
let secondsAgo = Math.round((new Date().getTime() - block.timestamp)/1000)
let transactions = block.txs;
if (number%config.blocksDisplay === 0)
console.log('Avalon Block #'+number+' '+secondsAgo+'s ago');
if (block.txs != null && block.txs.length > 0) {
for (let i = 0; i < block.txs.length; i++) {
let tx = block.txs[i]
if (tx.type === 3 && tx.data.receiver === DTCWatcher.address) {
let amount = tx.data.amount
let memo = tx.data.memo
console.log('transfer', tx.sender, amount, memo)
if (!memo) {
console.log('Error, tx without memo')
continue
}
let memoParsed = memo.split('@')
if (memoParsed.length !== 2) {
console.log('Error, memo invalid')
continue
}
let destinationNetwork = memoParsed[1].toLowerCase()
let destinationAddress = memoParsed[0]
if (destinationNetwork !== 'eth') {
console.log('Error, network is not ethereum')
continue
}
// take fees
if (amount <= txFeeDtc) {
console.log('Error, amount is <= fee')
continue
}
amount -= txFeeDtc
var cmd = "oz send-tx --to "+config.ethContractAddress
cmd += " --method transfer --args "+destinationAddress+","+amount
cmd += " -n mainnet --no-interactive"
txQueue.push(cmd)
}
}
}
setTimeout(function() {DTCWatcher.checkBlock(1+number)}, 1)
})
}
}
module.exports = AvalonWatcher