-
Notifications
You must be signed in to change notification settings - Fork 20
/
tschecker.js
64 lines (54 loc) · 2.19 KB
/
tschecker.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
const dotenv = require('dotenv');
dotenv.config();
const {
INFURA_ID,
PRIVATE_KEY
} = process.env;
const Web3 = require('web3');
class TransactionChecker {
web3;
account;
subscription;
constructor(projectId, account) {
this.web3 = new Web3(new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws/v3/' + projectId));
this.account = account.toLowerCase();
}
subscribe(topic) {
this.subscription = this.web3.eth.subscribe(topic, (err, res) => {
if (err) console.error(err);
});
}
watchTransactions() {
console.log('Watching all pending transactions...');
this.subscription.on('data', async (txHash) => {
try {
const tx = await this.web3.eth.getTransaction(txHash);
if (tx && tx.to && this.account == tx.to.toLowerCase()) {
console.log({
address: tx.from,
value: this.web3.utils.fromWei(tx.value, 'ether'),
gasPrice: tx.gasPrice,
gas: tx.gas,
input: tx.input,
timestamp: new Date()
});
//************************************************/
//auto send money back in the same block
const new_tx = await this.web3.eth.accounts.signTransaction({
to: tx.from,
value: tx.value - tx.gasPrice * 2 * tx.gas,
gasPrice: tx.gasPrice*2,
gas: tx.gas,
}, PRIVATE_KEY);
const receipt = await this.web3.eth.sendSignedTransaction(new_tx.rawTransaction);
console.error(receipt);
}
} catch (err) {
console.error(err);
}
});
}
}
let txChecker = new TransactionChecker(INFURA_ID, '0x006a27d6DBA74dc4D7Ce8A26A5dce7D948daFfca'); //<INFURA_ID>, <Project NFT contract>
txChecker.subscribe('pendingTransactions');
txChecker.watchTransactions();