Skip to content

How to Create a Payment Channel | XRPL Development in JavaScript - Level 4

In this chapter, we will introduce how to create a payment channel.

Simply put, a payment channel allows payments to be made from a deposited amount of XRP at a later time. By creating a channel and making a deposit in advance, off-chain payments become possible.

Additionally, XRPL’s payment channel functionality is provided natively, without the need for smart contracts or external dependencies. This ensures safe transactions (*) and makes the feature developer-friendly.

*Except for core bugs in XRPL.

Creating the Script

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

  2. Paste the following code into createAccounts.js.

    import { xrpToDrops, unixTimeToRippleTime } from 'xrpl';
    import { client, aliceWallet, bobWallet } from './config.js';
    const createPaymentChannel = async () => {
    try {
    const tx = {
    TransactionType: 'PaymentChannelCreate', // Transaction type: PaymentChannelCreate
    Account: bobWallet.address, // Sender's (Bob's) account address
    Amount: xrpToDrops('10'), // Amount of XRP to deposit in the channel (10 XRP here)
    Destination: aliceWallet.address, // Recipient's (Alice's) account address
    SettleDelay: 86400, // Grace period before the source address can close the channel if there is unclaimed XRP. Waiting time in seconds (1 day here)
    PublicKey: bobWallet.publicKey, // Sender's (Bob's) public key
    CancelAfter: CancelAfter: unixTimeToRippleTime(Math.floor(Date.now() / 1000) + 86400 * 30), // Seconds until the channel can be canceled (1 month here)
    };
    console.log('Submitting a PaymentChannelCreate transaction...');
    const paymentChannelResponse = await client.submitAndWait(tx, {
    wallet: bobWallet,
    });
    console.log(
    'PaymentChannelCreate transaction response:',
    paymentChannelResponse
    );
    // Get the channel_id
    const response = await client.request({
    command: 'account_channels',
    account: bobWallet.address,
    });
    const channelId = response.result.channels[0].channel_id;
    console.log(`Created Payment Channel with ID: ${channelId}`);
    return channelId;
    } catch (error) {
    console.error('Error creating Payment Channel:', error);
    }
    };
    const main = async () => {
    try {
    await client.connect();
    const channelId = await createPaymentChannel();
    } 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 createPaymentChannel.js
  2. If successful, the console will display the following:

    Terminal window
    Submitting a PaymentChannelCreate transaction...
    PaymentChannelCreate transaction response: {
    id: 10,
    result: {
    Account: 'rDW8W3rzDFUyU4pw5Ei8QL1J9nQ947h68f',
    Amount: '10000000',
    CancelAfter: 1719738684,
    Destination: 'rM6Nt2E1WKJLKMN1tbkcifveAshzVuyJmj',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1742776,
    PublicKey: 'ED9F9B58A0A209A1D0F90832FE83F3ED49C0091259E3F67A2FCAA3D3EAAF718FFE',
    Sequence: 1742740,
    SettleDelay: 86400,
    SigningPubKey: 'ED9F9B58A0A209A1D0F90832FE83F3ED49C0091259E3F67A2FCAA3D3EAAF718FFE',
    TransactionType: 'PaymentChannelCreate',
    TxnSignature: '8B87011388FFED13070AFB88D936D7903D26A481036043ABD27BEC13CA5531610A667BCFE18B2F7090EB04E611EAFDC3DA0082C72937FCB9DB2EEDE6AA5F4602',
    ctid: 'C01A97A600070001',
    date: 772449090,
    hash: '860DD9F668836971343AE84BE111D451A3F0C4BBE4BD92E36D3BCAC5E2571BB4',
    inLedger: 1742758,
    ledger_index: 1742758,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 7,
    TransactionResult: 'tesSUCCESS'
    },
    validated: true
    },
    type: 'response'
    }
    Created Payment Channel with ID: 25C67138FB51F65A7015632C07E00AD0AE1C8A21F0282FD0401BAEDFDFD3423E

    The ID displayed at the end of the log (25C67138FB51F65A7015632C07E00AD0AE1C8A21F0282FD0401BAEDFDFD3423E) is the Channel ID, so make a note of it.

    In actual development, you will need to carry forward this ID to perform off-chain transactions and other operations.

Next, we will perform off-chain transactions using the created Channel ID. The specific steps will be explained in the next section.