Get value from web3.eth.subscribe


I’m using infura to get all the tx of a smart contract, and I’m able to get: address,blockHash,blockNumber,data,logIndex,transactionHash and topics but how to get the _value of the tx (how many tokens were sent)?
and also what’s the first value inside topics? I’ve noticed its the same for every tx

Thanks for your help

Hello @damland, the first value inside topics is the function signature, more detail [here].(

Is this for erc20 tokens or Ether? For Ether, you can make an additional call with getTransaction to get the value transferred. For other tokens, it’s probably encoded in the the data field. See here

For Ether I need to make a getTransaction call for each tx?
Using web3.eth.getTransaction for each tx found, it will make too many infura calls, is there a workaround?

Can you copy paste the full output from your web3.eth.subscribe call? And the code snippet where you call it?

var subscription = web3house.eth.subscribe("logs", {
        fromBlock: '0x0',
        toBlock: 'latest',
        address: '0x6AC4C15dE98A3363E0113fd95cc4Ac4D8d39b563'
    .on("connected", function (subscriptionId) {
        console.log("subscriptionId:" + subscriptionId);
    .on("data", function (log) {
    .on("changed", function (log) {
    .on("error", function (error) {
        console.log("error:" + error);

and I get all the tx, like this:
This is using an erc20 token contract, I haven’t created the contract to use Ether yet, but I think the output will be very similar

Hey @damland, here’s the etherscan page with the Event Logs for that transaction.

You can see that the data is the hex encoded amount of the value transferred, so you’ll need to do a hex to decimal conversion and you’ll have the value.

how to re-subscribe to a previous subscriptionid (to save infura calls)?
On the documentation it says: subscription.arguments : The subscription arguments, used when re-subscribing. But there isn’t an example that shows how to do it… I’ve tried everything :slight_smile:

Hello @damland ,

Are you referring to the following web3.js library documentation?

Reconnecting to the same subscription after a disconnect is not supported by Infura.
I think this is a feature that is intended to make it easier for a user to reconnect to the same type of subscription (newheads, newpendingtransaction, syncing etc). This does not reconnect to the same subscription with the specified subscription id.

`subscription.arguments` : The subscription arguments, used when re-subscribing.

this is referring to the arguments you passed in, not the subscription id which is a return value.

Ah ok, what I was trying to do is to save the subscriptionid somewhere in my db and show the same subscriptionid to all the users of that page, to have only 1 request instead of 1 per user. Because right now if I have 1 page view I’m making 1 request and since all the users are making the same request I was hopping for a work around to don’t reach my infura daily requests limit… any advice? Thanks

ethereum nodes do not support sharing of subscriptions across multiple websocket connections. When you create a subscription, that subscription only exists for that particular websocket connection it was created on, and will cease to exist once that connection is gone. You do not have access to a subscription on a different websocket connection.

The subscription id is used for identifying the data that is coming back from your websocket connection.
for example if you have two subscriptions on the same websocket connection, logs and newheads, you can use the subscription id to identify the subscription data you are getting back. (IE, if it’s a logs subscription data or a newheads subscription data.

not sure if this is possible with your db, but the best way would be to save the subscription data to the db and have your users access that instead of creating their own websocket connection

I’ve just noticed that using web3.eth.subscribe I don’t get “from” and the value of the tx, and I need to do web3.eth.getTransaction(txhash) on each transaction to get them and input data… but if for example I have 10 users on that page and a new tx has found, using getTransaction(txhash) will it make only 1 infura call or 10 (one for each user on the page)?

That would depend on how you make that call, if you’re doing it client side it would generate a call per client but if you are doing it server side it could do a single subscription and a single call per transaction which you could then disseminate to the clients.