Skip to content

What is a Payment Channel? | XRPL Development Level 4

First, let’s introduce the concept of a payment channel.

A payment channel is a means for a sender and recipient to conduct off-chain transactions. This concept exists regardless of the blockchain used.

While payment channels are particularly effective for blockchains with high transaction fees per transaction, XRP Ledger transactions are completed in 3-5 seconds with low transaction fees, making standard payment transactions sufficient in most cases.

However, there are specific conditions where using a payment channel offers advantages and becomes necessary.

Use Cases

  • Handling thousands to tens of thousands of transactions per second
  • Micropayments (payments made in drops)
  • Cases where the final payment amount is uncertain

These are the primary scenarios where payment channels become necessary, but there are various other use cases depending on the idea.

Lifecycle

Payment channels on XRPL have a unique lifecycle.

It’s helpful to get an image of the transaction flow before diving in.

Source: xrpl.org

Basic Steps

The basic procedure for using a payment channel is as follows.

Note: In practice, processes for confirming channelId and signing/verification are required but are omitted here.

This guide will explain with Alice as the recipient and Bob as the sender.

Creating a Channel

First, the sender needs to create a payment channel and deposit initial funds.

Here is an example where the sender (Bob) creates a payment channel:

const createPaymentChannel = async () => {
const tx = {
TransactionType: 'PaymentChannelCreate', // Transaction type: PaymentChannelCreate
Account: bobWallet.address, // Sender (Bob)'s account address
Amount: xrpl.xrpToDrops('10'), // Amount of XRP to deposit in the channel (10 XRP here)
Destination: aliceWallet.address, // Recipient (Alice)'s account address
SettleDelay: 86400, // Seconds to wait before the channel can be closed (1 day here)
PublicKey: bobWallet.publicKey, // Sender (Bob)'s public key
CancelAfter: Math.floor(Date.now() / 1000) + 86400 * 7, // Seconds until the channel can be canceled (1 week here)
};
// Submit the transaction
const result = await client.submitAndWait(tx, { wallet: bobWallet });
console.log('Payment Channel Created:', result);
};

Adding Funds to a Channel

You can add more funds to an existing channel.

Here is an example where the sender (Bob) adds funds to a payment channel:

const fundPaymentChannel = async (channelId) => {
const tx = {
TransactionType: 'PaymentChannelFund', // Transaction type: PaymentChannelFund
Account: bobWallet.address, // Sender (Bob)'s account address
Channel: channelId, // Payment channel ID
Amount: xrpl.xrpToDrops('5'), // Amount of XRP to add to the channel (5 XRP here)
};
const result = await client.submitAndWait(tx, { wallet: bobWallet });
console.log('Payment Channel Funded:', result);
};

Closing a Channel

Once the transactions are completed, the channel is closed, and the final balance is recorded on-chain.

Here is an example where the sender (Bob) closes a payment channel:

const closePaymentChannel = async (channelId) => {
const tx = {
TransactionType: 'PaymentChannelClaim',
Account: bobWallet.address,
Channel: channelId,
Flags: xrpl.PaymentChannelClaimFlags.tfClose,
};
const result = await client.submitAndWait(tx, { wallet: bobWallet });
console.log('Payment Channel Closed:', result);
};

Summary

A payment channel is a powerful feature for achieving fast and low-cost micropayments.

This allows for efficient off-chain transactions, minimizing on-chain transaction fees. Understanding how to create, close, and add funds to a payment channel enables more flexible transactions.