Skip to content

Instantly share code, notes, and snippets.

@mempirate
Created October 31, 2019 16:31
Show Gist options
  • Save mempirate/e5de8af0e4436843a4ef59c4e9830b49 to your computer and use it in GitHub Desktop.
Save mempirate/e5de8af0e4436843a4ef59c4e9830b49 to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
class TransactionChecker {
web3;
account;
constructor(projectId, account) {
this.web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/' + projectId));
this.account = account.toLowerCase();
}
async checkBlock() {
let block = await this.web3.eth.getBlock('latest');
let number = block.number;
console.log('Searching block ' + number);
if (block != null && block.transactions != null) {
for (let txHash of block.transactions) {
let tx = await this.web3.eth.getTransaction(txHash);
if (this.account == tx.to.toLowerCase()) {
console.log('Transaction found on block: ' + number);
console.log({address: tx.from, value: this.web3.utils.fromWei(tx.value, 'ether'), timestamp: new Date()});
}
}
}
}
}
let txChecker = new TransactionChecker(process.env.INFURA_ID, '0xe1Dd30fecAb8a63105F2C035B084BfC6Ca5B1493');
setInterval(() => {
txChecker.checkBlock();
}, 15 * 1000);
@BuggerBag
Copy link

thank's a lot.

@lakshmankashyap
Copy link

How can be watched from a single block predefined block to latest (predefined when i deployed my contract) for our offchain users so when i try to do with setInterval with loop my server get stuck after a time.

@BuggerBag
Copy link

@BuggerBag
Copy link

using for loop or setInterval function has stuck problem, you could use api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment