How to Close a Payment Channel | XRPL Development in JavaScript - Level 4
In this chapter, we will explain how to close a payment channel.
Closing a payment channel allows you to reclaim unused funds and finalize the channel’s remaining balance.
Preparation
Create a new file named closePaymentChannel.js in your project directory.
Paste the following code into closePaymentChannel.js.
Running the Script
Execute the script by running the following command in your command line:
If successful, the console will display the following:
About the Channel Closing Process
Although we have set the close flag from Bob’s side, the channel will still exist for now.
Let’s review the createPaymentChannel.js file.
Here, SettleDelay was specified as 1 day.
When the sender closes the channel, the SettleDelay period must pass before the channel is officially closed. This means that the channel will be closed 1 day after the close flag is set.
If possible, run node checkChannelBalance.js to confirm that the channel still exists.
Reopening the Channel
You can reopen the channel by changing the Flags value. Only the source address of the payment channel can use this flag.
Closing the Channel from the Receiver’s Side
When the receiver (Alice) closes the channel, the channel is forcibly terminated.
Running the Script
Modify the closePaymentChannel.js code as follows:
Run the script by executing the following command in your command line:
If successful, the console will display the following:
Run the checkChannelBalance.js script again to check the channel’s status.
If the following result is displayed, the channel has been fully closed.
Let’s check the balances of Alice and Bob on a test network explorer like Bithomp.
Assuming the steps were followed correctly, the deposited XRP should be returned to Bob, resulting in the following balances:
Alice: approximately 100 XRP
Bob: approximately 89 XRP
Summary
In this chapter, we explained how to close a payment channel.
By using the PaymentChannelClaim transaction to close the channel, you can reclaim unused funds and finalize the channel’s remaining balance.
Completing this guide should give you a solid understanding of the basic transaction flow using payment channels.
Payment channels are among the more complex features of XRPL, so it can be challenging to grasp the concept from documentation alone.
We hope this knowledge proves useful in your future development and projects.