Skip to content

Instantly share code, notes, and snippets.

@dodikk
Forked from dsemenovsky/watchEtherTransfers.js
Created October 2, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodikk/e65977a694cca74f5bd00899377165ba to your computer and use it in GitHub Desktop.
Save dodikk/e65977a694cca74f5bd00899377165ba to your computer and use it in GitHub Desktop.
Ether transfers watcher
function watchEtherTransfers() {
// Instantiate web3 with WebSocket provider
const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws'))
// Instantiate subscription object
const subscription = web3.eth.subscribe('pendingTransactions')
// Subscribe to pending transactions
subscription.subscribe((error, result) => {
if (error) console.log(error)
})
.on('data', async (txHash) => {
try {
// Instantiate web3 with HttpProvider
const web3Http = new Web3('https://rinkeby.infura.io/')
// Get transaction details
const trx = await web3Http.eth.getTransaction(txHash)
const valid = validateTransaction(trx)
// If transaction is not valid, simply return
if (!valid) return
console.log('Found incoming Ether transaction from ' + process.env.WALLET_FROM + ' to ' + process.env.WALLET_TO);
console.log('Transaction value is: ' + process.env.AMOUNT)
console.log('Transaction hash is: ' + txHash + '\n')
// Initiate transaction confirmation
confirmEtherTransaction(txHash)
// Unsubscribe from pending transactions.
subscription.unsubscribe()
}
catch (error) {
console.log(error)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment