Web3 Guard
Web3 Guard maintains a built-in confirmation checker for every Ethereum transactions, smart contract events and value transfers that may interest you. Web3 Guard provides a bunch of workflow utilities that you don't need to DRY adding confirmation checking of your dapps.
Installation
// The package is designed to work with web3js version 0.20.xnpm install web3@020npm install web3-guard
Add confirmation checking for your dapps
let Web3 = let Guard = // your contract ABIconst HotelCal = const web3 = 'http://127.0.0.1:8545'const HotelCalABI = HotelCalabi // It can work with any smart contract on Ethereum networkconst hotel = web3eth let guard = web3// set confirmation to 12 blockslet confirmations = 12 // listen every `Transfer` events that hotel smart contract broadcasts// replay it until it reaches the confirmationguard
Bind transactions
Some dapps often work with databases for keeping better user experience and high availabilities. A workflow could be like is: A cronjob query a result set from a centralized database. For every row of the result set, it should send several on-chain transactions to complete synchronization process. But a web3js filter can't directly distinguish which event is coming from which row of the result set so that it can't update the status of the row and brings it to the next status easily.
// result set from a RDBMSlet rows = await for let index = 0; index < rowslength; index++ let row = rowsindex let user_address token_id ticket_price = row // create a guard for each row let guard = web3 // set confirmations let confirmations = 12 guard = guard // bind every Guard instances based on separate indexes for different row guard = guard // transfer ERC721 to user_address let txHash = await hotel // bind transaction with a specific gaurd instance guard guard
Transfer detection
You could also listen any normal ETH transfers with the on
method
let guard = web3// set confirmation to 12 blockslet confirmations = 12 // listen ETH transfer from some addresslet opt = from: 'some_address' // listen ETH transfer to your addresslet opt = to: 'your_address' // listen ETH transfer from some address to your addresslet opt = from: 'some_address' to: 'your_address' guard
Processed block
You may wanna log the block height of a Guard instance has processed for initialization. Web3 Guard provides a done
callback for you.
let guard = web3// set confirmation to 12 blockslet confirmations = 12 guard