Introduction
It's a great pleasure to be back at the academy again to learn more about cryptocurrency. This time, I've attended the lecture presented by professor @sapwood where the professor taught about the Lightning Network, of course, it was a great lecture. I will be attending to the task given by the professor without wasting time.

Designed in Canva with a free image- Image Source
1. What is Lighting Network, Lightning Node and Lightning Channel?
Lightning Network
Lightning network is the second layer of the Bitcoin network which is aimed to address the shortcomings of the Bitcoin network. Bitcoin network is associated with delayed transactions up to 10mins as miners carry out confirmations before adding a block, this is quite a time-wasting process.
To address the aforementioned shortcomings, Lightning network comes as the 2nd layer network of Bitcoin which is aimed at enabling instant transaction and improved scalability of the network. In Lightning network, we have the lighting channel which enables lightning nodes interaction and has only the first and last transaction recorded on-chain while other transactions that take place in the channel are kept off-chain.
Lightning Node
Remember, nodes are devices connected to share transaction data on the blockchain, likewise, the lightning node is seen as the device that establishes connection of sending and receiving of payments between peers on the lightning network through nodes interaction. Furthermore, the interaction of the nodes form the channels for sending and receiving funds, which are the lightning channels and the combination of all forms of the lightning network.
In addition, the lightning node has the capability to interact with the on-chain Bitcoin network to reveal the status of the Lightning network, the lightning node carries out validations of the transactions of the node they are directly interacting with to enhance fast transactions.
Lightning Channel
This is a network of Lightning nodes, that is, the combination of the interaction between the lightning nodes forms the lightning channel which enables peer-peer sending/receiving of funds in a decentralized way and the transactions within the channels are kept off-chain thereby enabling fast transactions.
How does it achieve a near-instant settlement, near-infinite scalability?
Near-instant settlement is made possible in the lightning network whereby nodes that are directly connected in the channel do the job of transaction validation without waiting for other nodes and this results in swiftness of transactions. Likewise, near-infinite scalability is achievable through the off-chain transaction that takes place in the channel which eliminates congestion of network, it improves scalability as numerous transactions are done effectively without the existence of 10 mins confirmation waiting time of the Bitcoin network seen in on-chain transactions.
Uses of Lightning Network

2. How does a Lightning network enable two peers to make a transaction or recurring or multiple transactions without any transaction fee? Does it happen off-chain or on-chain?
On the Lightning network, to perform a transaction or recurring or multiple transactions without any transaction fee starts from the user intending to spend on the Lightning network binding an amount to the channel and such transaction is recorded on-chain. Furthermore, all transactions that take place within the Lightning network channel are off-chain and at no transaction fee.
As a sender or receiver, they are may be connected directly or indirectly connected through nodes and the network of the nodes forms the channel, the sender can create a channel through a node to be routed to the another node to connect with another peers. In the process of creating a channel, a certain amount is bound and this is done on-chain. After the successful opening of the channel, the peers are connected through the Lightning nodes and can make transactions with no transaction fee off-chain (only indirect connection cost charges) at a very high speed since there is a need for waiting confirmations, nodes that are directly involved carry out transaction validations.
In short, the binding of the capital to the channel is done on-chain with transaction fee while all micro-transactions that are done within the channel are off-chain that can be done swiftly numerous times with no transaction fee or with infinitesimal charges by the intermediate node through which the payment is routed.

3. How does the routing of payment through intermediate nodes(or peers) become trustless with HTLC?
Let's talk briefly about the HTLC, Hashed Timelock Contract is a smart contract-based payment in the crypto ecosystem to make payment with conditions routed through intermediaries whereby the funds are redeemed within the timelock by supplying the hash secret.
This payment type comes with the innovation to solve problems that might occur between two individuals that set up 2 of 2 multi-sig wallets to carry out a trade but one of them might be away indefinitely while the funds in the wallet is hanging for an unpredictable period, with the HTLC payment, this problem is solved as payment are to be redeemed within the timelock in a trustless manner. In short, payment through the HTLC can only be redeemed by a user who knows the hash secret and it must be redeemed within the timelock deadline. Let's see how it works below.
Assuming there are 3 nodes, Fred, PanWorld and Jane, having Fred and Jane to be the ones that have business dealings and PanWorld is the intermediary through which the payment is routed. Let's see the steps below
Note that, payment must be redeemed within the timelock.

4. Setting up a Lightning Wallet
How to open a Channel
I already have a Standard wallet on Electrum, such that the process of setting up a Lightning wallet began. Let's see the steps below.








My channel opening transaction is on-chain. Now, let's check the transaction details on the Blockchain explorer.

Above is the details of my channel as recorded on-chain. The transaction hash is; 68ab5d402365436e03baf39bdbab93240de3ab2bcc158b72b02222758b26296e.
Making a e-commerce purchase using the Lightning channel
For this section of the task I will be using the e-commerce platform https://www.bitrefill.com/buy to make a purchase using the Lightning channel. Let's see it in a few steps below.










How to close the channel




Is there any fee incurred?
For the Open channel transaction, a transaction fee of 0.0054 mBTC was incurred because it's an on-chain transaction. The e-commerce purchase would have been completely without fees at all due to the fact it was done off-chain but the intermediate node involved charged an infinitesimal fee of 0.00007 mBTC to have the transaction completed.

e-commerce transaction fee
The close channel transaction comes with the transaction fee of 0.00815mBTC because it happened on-chain.

Close channel transaction fee
Does it record the transaction on-chain?
The e-commerce purchase was not recorded on-chain, such that the answer to this question is NO. The reason it does not record the transaction on-chain is that the transaction took place within the Lightning network channel that has all transactions carried out within the channel recorded off-chain. Remember, the Lightning Network is a second layer network on top of the Bitcoin network whereby transactions within its channels are kept off-chain to improve the speed of transactions and enhance scalability.
How does Swap Function help to improve sending and receiving capacity?
It's important to open a basis to the swap function, it allow users to swap funds from on-chain for lightning funds and vice-versa, without closing the channel. In the process, it increases the sending capacity of a user if the user swap the on-chain funds for lightning funds, likewise, the receiving capacity of a user is increased as he/she uses the swap function to swap lightning funds for on-chain funds.

On-chain funds <---> lightning funds interface

Lightning funds <---> on-chain funds interface

5. How to backup a wallet file (Channel backup)



Why and When the Backup file is needed
The backup file is used by the user to close the channel. The backup file is needed when one intends to close the channel and move the funds from the Lightning network to on-chain. Users should understand that the backup file cannot be used to restore a channel rather it is used to close a channel.
Indication of funding transaction, commitment transaction and settlement transaction in the wallet and block explorer

Above is the screenshot of the history of funding, commitment and settlement transactions on the wallet. Let's see each of them below.
Funding transaction: This transaction is the first transaction that took place when opening a channel through which the user binds a certain capital to a channel and it's recorded on-chain. Let's see my funding transaction both in wallet and blockchain explorer.

Funding transaction on the wallet

Funding transaction on the blockchain explorer
The screenshots above has the indication of my funding transaction of 2 mBTC and transaction fee 0.0054 mBTC shown both on wallet and the blockchain explorer.
Commitment transaction: This is the transaction that takes place within the Lightning channels and they are recorded off-chain. Let's see it on my wallet below.

Commitment transaction on my wallet
Above is the transaction I made within the channel which is 0.03139 mBTC which comes with a charges of 0.00007 mBTC by the intermediate node. It has the transaction hash, preimage and lightning invoice in the record.
Settlement transaction: Settlement transaction is the final balance on a channel when it was closed and routed back to the wallet. This transaction is recorded on-chain. Let's see my transaction as indicated on the wallet and blockchain explorer.

Settlement transaction on the wallet

Settlement transaction on the blockchain explorer
The screenshots above is the indication of the settlement transaction that has 1.9604 mBTC routed back my wallet with a transaction fee of 0.00815 mBTC.

Conclusion
In conclusion, the Lightning network is the second layer network that is aimed at correcting the shortcomings of the main Bitcoin network. It improves the speed of transactions and scalability by keeping transactions that happen within the channel off-chain such that transactions are processed faster and at no transaction fee (intermediate nodes may charge and it's always infinitesimal).
On the Lightning network, HTLC payment is utilized to transact between peer-peer in a trustless manner whereby the only one that knows the secret value can redeem the funds in a transaction. Thanks to professor @sapwood for this great lecture.

Cc: @sapwood

Written by;
@fredquantum