Skip to content

Adding Funds to a Payment Channel | XRPL Development in JavaScript - Level 4

In this chapter, we will introduce how to add funds (deposit) to an existing payment channel.

Creating the Script

  1. Create a new file named fundPaymentChannel.js in your project directory.

  2. Paste the following code into fundPaymentChannel.js.

    import { xrpToDrops } from 'xrpl';
    import { client, bobWallet } from './config.js';
    const fundPaymentChannel = async (channelId) => {
    try {
    const tx = {
    TransactionType: 'PaymentChannelFund', // Transaction type: Adding funds to the payment channel
    Account: bobWallet.address, // Sender (Bob)'s account address
    Channel: channelId, // Payment channel ID
    Amount: xrpToDrops('5'), // Amount of XRP to add to the channel (in this case, 5 XRP)
    };
    console.log('Submitting a PaymentChannelFund transaction...');
    // Submit the transaction
    const fundChannelResponse = await client.submitAndWait(tx, {
    wallet: bobWallet,
    });
    console.log(
    'PaymentChannelFund transaction response:',
    fundChannelResponse
    );
    } catch (error) {
    console.error('Error funding Payment Channel:', error);
    }
    };
    const main = async () => {
    try {
    await client.connect();
    const channelId = 'CHANNEL_ID_HERE'; // Enter the previously created channel ID
    await fundPaymentChannel(channelId);
    } catch (error) {
    console.error('Error connecting to XRPL:', error);
    } finally {
    await client.disconnect();
    }
    };
    main();

Running the Script

  1. Run the script by executing the following command in your command line:

    Terminal window
    node fundPaymentChannel.js
  2. If successful, the console will display the following:

    Submitting a PaymentChannelFund transaction...
    PaymentChannelFund transaction response: {
    id: 12,
    result: {
    Account: 'rDW8W3rzDFUyU4pw5Ei8QL1J9nQ947h68f',
    Amount: '5000000',
    Channel: '25C67138FB51F65A7015632C07E00AD0AE1C8A21F0282FD0401BAEDFDFD3423E',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1743984,
    Sequence: 1742741,
    SigningPubKey: 'ED9F9B58A0A209A1D0F90832FE83F3ED49C0091259E3F67A2FCAA3D3EAAF718FFE',
    TransactionType: 'PaymentChannelFund',
    TxnSignature: '0BB94472B22EEC345F02E5E788DA5CBBDB3080082DD03F3B5A87D02116FC61FF36B6469002B90CF8F219BDE9CFAD4AC68B49EAE64ED96C2A58FB9F09A8DDF00D',
    ctid: 'C01A9C5E007D0001',
    date: 772453171,
    hash: 'AA71F4F3163A25032BC71A07C8824C62415F33A3BD20F6B578A5236F4DED4631',
    inLedger: 1743966,
    ledger_index: 1743966,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 125,
    TransactionResult: 'tesSUCCESS'
    },
    validated: true
    },
    type: 'response'
    }

Extending the Expiration Date

You can extend the expiration date while adding funds.

To extend the expiration date, add the Expiration field as shown below when submitting the transaction.
The Expiration field must specify a date later than the current date plus SettleDelay.

const tx = {
TransactionType: 'PaymentChannelFund', // Transaction type: Adding funds to the payment channel
Account: bobWallet.address, // Sender (Bob)'s account address
Channel: channelId, // Payment channel ID
Amount: xrpToDrops('5'), // Amount of XRP to add to the channel (in this case, 5 XRP)
Expiration: Math.floor(Date.now() / 1000) + 86400 * 7, // Extend expiration date (in this case, 1 week later)
};

Note: The expiration date is different from the CancelAfter option, which cannot be changed.

Checking the Channel’s State

To verify that the funds have been added, check the channel’s state again.

Use the previously created checkChannelBalance.js.

Running the Script

  1. Run the script by executing the following command in your command line:

    Terminal window
    node checkChannelBalance.js
  2. If successful, the console will display the following:

    Terminal window
    Channel ID: 25C67138FB51F65A7015632C07E00AD0AE1C8A21F0282FD0401BAEDFDFD3423E
    Balance: 1000000 drops // Amount of off-ledger transactions (currently: 1 XRP)
    Amount: 15000000 drops // Amount deposited (15 XRP)

    You can confirm that the Amount has increased to 15000000 drops (15 XRP).

Summary

In this chapter, we explained how to add funds to a payment channel.

Using the PaymentChannelFund transaction, you can make additional deposits to the channel. You can refer to this sample code to generate a transaction, for example, when clicking a “Deposit” button.

Additionally, you can extend the validity period of the channel, allowing for more transactions.

In the next chapter, we will introduce how to close a payment channel. By going through the closing process, you can reclaim unused funds and finalize the channel’s remaining balance.