> For the complete documentation index, see [llms.txt](https://docs.xrpl-commons.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xrpl-commons.org/xrpl-basics/reading-and-writing-memos.md).

# Reading and subscribing to Transactions

The XRPL is a public ledger, meaning that when you write on-chain, anyone can read it. In this session, we will demonstrate how to subscribe to events as they happen on-chain.

First, we will create a pair of accounts:

{% code title="index.ts" %}

```javascript
import xrpl from "xrpl";

const serverURL = "wss://s.altnet.rippletest.net:51233"; // For testnet

const main = async () => {
  const client = new xrpl.Client(serverURL)
  await client.connect()

  // do something useful
  console.log("lets fund 2 accounts...")
  const { wallet: wallet1, balance: balance1 } = await client.fundWallet()
  const { wallet: wallet2, balance: balance2 } = await client.fundWallet()

  console.log({ wallet1, balance1 })
  console.log({ wallet2, balance2 })

  // end
  client.disconnect()
};

main()
```

{% endcode %}

Check the logs to find the seed for both accounts. We will use these later to reuse the accounts deterministically.

### Creating a payment transaction

Here is how to generate a payment transaction between the two accounts.

{% hint style="success" %}
Insert the seeds from the previous section in the top section.
{% endhint %}

{% code title="index.ts" %}

```javascript
import xrpl from "xrpl";

const serverURL = "wss://s.altnet.rippletest.net:51233"; // For testnet

const wallet1 = xrpl.Wallet.fromSeed("s...");
const wallet2 = xrpl.Wallet.fromSeed("s...");

const main = async () => {
  const client = new xrpl.Client(serverURL);
  await client.connect();

  const tx = {
    TransactionType: "Payment",
    Account: wallet1.classicAddress,
    Destination: wallet2.classicAddress,
    Amount: "1234" // drops
  }

  const result = await client.submitAndWait(tx, {
    autofill: true,
    wallet: wallet1,
  });
  console.log(result)

  // end
  client.disconnect()
};

main()
```

{% endcode %}

### Reading transactions

One way to retrieve transactions for a given account is to create a request for transactions. Here is an example

```javascript
// ... define wallet1 before this point
const request = {
  command: 'account_tx',
  account: wallet1.classicAddress,
  ledger_index_min: -1, // To get transactions from all ledgers
  ledger_index_max: -1, // To get transactions up to the most recent ledger
  limit: 10, // Limit the number of transactions (optional)
}
const response = await client.request(request)
console.log('Account Transactions:', response.result.transactions)
```

### Listening for transactions

Finally, we will demonstrate how to listen for transactions affecting a given account to catch all transactions.

{% hint style="warning" %}
**You could use this to build a notification bot that alerts you when any activity occurs in your account.**
{% endhint %}

You can subscribe to an account, to trigger on each transaction for instance. This is how you would listen to a specific account.

{% code title="index.ts" %}

```javascript
import xrpl from 'xrpl';

const serverURL = 'wss://s.altnet.rippletest.net:51233'; // For testnet
const walletAddress = 'r...' // address to watch

const main = async () => {
  const client = new xrpl.Client(serverURL)
  await client.connect()

  // do something useful
    const subscriptionRequest = {
      command: 'subscribe',
      accounts: [walletAddress]
    };

    await client.request(subscriptionRequest)
    console.log(`Subscribed to transactions for account: ${walletAddress}`)

    // Event listener for incoming transactions
    client.on('transaction', (transaction) => {
      console.log('Transaction:', transaction);
    })

    // Event listener for errors
    client.on('error', (error) => {
      console.error('Error:', error);
    })

  // end
  // keep open
  console.log('all done')
}

main()
setInterval(() => {
  console.log('One second has passed!');
}, 1000)
```

{% endcode %}

###
