1

I'm building a webapp using nodejs and web3js. I have a simple use-case: I provide the user with an address to which he should deposit funds. I want to then listen on my nodejs server for:

  1. when the user deposits funds to this address
  2. the transaction which deposits these funds has gone through 12 confirmations.

I know I can check the balance of the account using:

var balanceWei = web3.eth.getBalance(walletAddress).toNumber();

however this is not enough in my use case. I need to know when a transaction occurs which deposits funds into this account, as well as when this transaction is confirmed so that I can make these funds available to the user within my app.

1

There are several solutions to this:

  1. You can search in every new block that arrives for transactions related to your account as described in this answer (an script is given).

  2. You can use etherscan API, which will give you all the transaction to and from a particular account starting at a block number given. This is described here

In ethersacn you will do a request, for instance:
http://api.etherscan.io/api?module=account&action=txlist&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken

where you specify the address, startblock and endblock interval in which to look for your transactions.

  1. Write a smart contract and give your users the address of the contract instead, then you can add events on it about the transactions. In this way, you can use filters in web3 to get the information of any new deposit to your contract account. An example of such contract is:

    contract MyContractAccount{
    
    address public owner;
    
    event received(address from, uint256 value, uint256 blocknumber);
    
    constructor(){
        owner = msg.sender;
    }
    
    function () payable public{
        emit received(msg.sender, msg.value, block.number)
    }
    
    function withdraw(){
        owner.transfer(address(this).balance);
    }
    

    }

The withdraw will allow you to get the funds whenever you want. The information about the block number will let you know when it happened.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.