I am using Web3j and I want confirmation for deposits in user wallet. Is it possible if yes how can I do this using Web3j
1 Answer
If you are using web3 ^1.0.0, you can receive updates on any transaction confirmation you create. From the docs:
myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.on('transactionHash', function(hash){
...
})
.on('confirmation', function(confirmationNumber, receipt){
//you will be updated here everytime your transaction is confirmed
//(up until 24 confirmations by default)
console.log(confirmationNumber)
})
If you wish to receive updates whenever someone else sends a transaction to your address, the only way I know of to accomplish this requires the ABI and contract address of the token you expect to receive (I'm also assuming it will be an ERC20 token). Once you create the contract instance within web3, filter on the Transfer()
event like this:
myContract.events.Transfer({
filter: {to: 'yourWalletAddress'},
fromBlock: 0
})
.on('data', (event) => {
console.log("your wallet was just sent xyz tokens", event); //notification in navbar or something similar
})
This will log all transactions of that particular token sent to your wallet address.
If you only care about watching for changes in a wallets Ether balance, check out this post Set watch for ethereum address transaction using web3js