XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.21k stars 512 forks source link

How to get notification via Websocket subscribe when receiving funds to account #889

Closed bazooka70 closed 6 years ago

bazooka70 commented 6 years ago

When I try to use subscribe api to track transactions for account(s) e.g.:

{
  "id": "Example watch wallet",
  "command": "subscribe",
  "accounts": ["rrpNnNLKrartuEqfJGpqyDwPj1AFPg9vn1", "rPQ13XiP5os716ij5339Vs4P9FdfSkLa93", ...]
}

I only get notified when sending tx from the accounts. How can I get notifications when receiving funds to the accounts?

intelliot commented 6 years ago

Hi @bazooka70 -

I think this subscription should provide notifications for both sending and receiving funds, along with other types of transactions affecting the account(s).

If not, can you share a specific example?

Note that you'll need to have a registered handler for transaction stream messages ("type": "transaction").

bazooka70 commented 6 years ago

Hi @intelliot

from my tests, the subscription only provide notifications for sending.

If not, can you share a specific example?

I'm not sure how can I share it. I'm using C# WebSocketSharp and using testnet:

        public static void TestWebSocket()
        {            
            using (var ws = new WebSocket("wss://s.altnet.rippletest.net:51233"))
            {
                ws.OnMessage += (sender, e) =>
                    Console.WriteLine("Response: " + e.Data);

                ws.Connect();                
                ws.Send("{ \"id\": 1, \"command\": \"subscribe\", \"accounts\": [\"rpdqRSmZt7mwWVjdjnR76BJk71BX8qmDsN\"], \"streams\": [ \"transaction\"] }");
                Console.ReadKey(true);
            }
        }

If I'm using stream type transactions (not transaction), I get notifications on ALL transactions that are included in a closed ledger for every account. not only the account I specified.

If I omit transactions I only get notified when the rpdqRSmZt7mwWVjdjnR76BJk71BX8qmDsN is sending funds, but not when I send funds to it from another account.

I was also assuming that I will be notified both ways. What should I do? Listen to ALL transactions in the network?

intelliot commented 6 years ago
  1. You created this issue in the ripple-lib project, so I thought you were using ripple-lib: https://github.com/ripple/ripple-lib

You may want to move this issue to rippled instead: https://github.com/ripple/rippled

  1. If you want to only get notifications when specific addresses are sending and receiving funds, do not subscribe to the transactions stream. According to the documentation, if you include transactions in the streams parameter, rippled "Sends a message whenever a transaction is included in a closed ledger".

  2. With ripple-lib, here is the code I'm using to subscribe to track transactions for your testnet account:

api.connection.on('transaction', ev => {
    console.log(JSON.stringify(ev, null, 2))
  })

  api.connection.request({
    command: 'subscribe',
    accounts: [address]
  })

I set address to your testnet address rpdqRSmZt7mwWVjdjnR76BJk71BX8qmDsN. I then used my own testnet account to send 1000 XRP to your address. I got the following notification:

{
  "engine_result": "tesSUCCESS",
  "engine_result_code": 0,
  "engine_result_message": "The transaction was applied. Only final in a validated ledger.",
  "ledger_hash": "CBF417FBC058189D76E743B541497FC10881AF68DD34244C7FF7DC1C1F9EA637",
  "ledger_index": 8870408,
  "meta": {
    "AffectedNodes": [
      {
        "ModifiedNode": {
          "FinalFields": {
            "Account": "rpdqRSmZt7mwWVjdjnR76BJk71BX8qmDsN",
            "Balance": "1309980843",
            "Flags": 0,
            "OwnerCount": 0,
            "Sequence": 7
          },
          "LedgerEntryType": "AccountRoot",
          "LedgerIndex": "B5A1B26DC3558D64D59A97EE25F7D0242E460CD9A5F30C81D6E64966BF319BEE",
          "PreviousFields": {
            "Balance": "309980843"
          },
          "PreviousTxnID": "D0CE143CBF8FDCD6B0A356CE8AC5EF2F45A91D475AA484136CE49A38C2B0AE11",
          "PreviousTxnLgrSeq": 8864095
        }
      },
      {
        "ModifiedNode": {
          "FinalFields": {
            "Account": "raEujmHmA8Zt44K5ibqntmAegTX7UvoCod",
            "Balance": "5978837594",
            "Flags": 0,
            "OwnerCount": 0,
            "Sequence": 11585
          },
          "LedgerEntryType": "AccountRoot",
          "LedgerIndex": "D819E24725CEE4D3D7BC38FE08FB6B02A88A575AFDA39FA2B82D86D7ACADD53C",
          "PreviousFields": {
            "Balance": "6978837606",
            "Sequence": 11584
          },
          "PreviousTxnID": "B39906AE23F05F9AE7B141B0CC28B686567B5F21C5438F291B51CA3CCF317A74",
          "PreviousTxnLgrSeq": 8870407
        }
      }
    ],
    "TransactionIndex": 0,
    "TransactionResult": "tesSUCCESS"
  },
  "status": "closed",
  "transaction": {
    "Account": "raEujmHmA8Zt44K5ibqntmAegTX7UvoCod",
    "Amount": "1000000000",
    "Destination": "rpdqRSmZt7mwWVjdjnR76BJk71BX8qmDsN",
    "Fee": "12",
    "Flags": 2147483648,
    "LastLedgerSequence": 8870409,
    "Sequence": 11584,
    "SigningPubKey": "034F8AF4FE695E3319F8514388817D92421E91E31BE4761CF6D6F946A6D4FE33E3",
    "TransactionType": "Payment",
    "TxnSignature": "304402202D91FB82E33FBE40935C976D634B87AD30CD55C2EC86F36F6D890B33DC7E735602205A698D5B963BCEA21CCE00BA41E6E7F33006DE3ADA661BB63040B86EEDB6285D",
    "date": 578759971,
    "hash": "EFF1BB18937B2B445D0D7030CEDF691A78D5CEEB16CE9557A1CEFDFCA6155962"
  },
  "type": "transaction",
  "validated": true
}

This shows that you also get notified when you send funds to the address from another account.

bazooka70 commented 6 years ago

@intelliot Thanks a million. I must have came to the wrong conclusions in my tests yesterday. Now, It works like you describe and expected.

You created this issue in the ripple-lib project, so I thought you were using ripple-lib...

I don't think it makes such a difference if I'm using Web sockets in c# instead of the ripple-lib js implementation. the results are the same for the api :)

You have been very helpful.

bazooka70 commented 6 years ago

@intelliot , Do you know by chance what is the limit of subscription accounts I could use in the accounts array?

intelliot commented 6 years ago

I do not know of a limit. If you find one, please feel free to tell us about it here.