Created
October 31, 2019 16:31
-
-
Save mempirate/e5de8af0e4436843a4ef59c4e9830b49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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.
Hi, you can use
https://docs.alchemy.com/alchemy/enhanced-apis/transaction-receipts-api
or
https://www.blockcypher.com/dev/ethereum/#creating-transactions
please tell me more, So I can help you. thank's in advance
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
thank's a lot.