Let's explore how TON Payments work using an example.
Let's say Bob and Alice realize that over the next few weeks, they will need to make many micropayments to each other—they will be transferring coins back and forth. Instead of using a regular blockchain and paying for each transaction, the two decide to use TON and its TON Payments. For this, users must first open a payment channel, perform the necessary operations within it, and then close the channel. Here's how it all works.
Opening a Payment Channel. To open a payment channel, participants deposit a fixed amount, let's say 100 TON. They can then transfer coins to each other within this amount.
The deposit also acts as collateral, ensuring that each user will act honestly. If one participant tries to cheat the other during the coin exchange, according to the smart contract terms, they will lose part of the deposit.
Bob and Alice confirm their intention to open the channel by signing a smart contract with unique private keys. Such a key is equivalent to an electronic signature used to sign documents electronically, such as on government portals.
Once the users have signed the smart contract, information about the payment channel is recorded on the blockchain, and the channel is considered open, with participants paying a small fee.
Using the Payment Channel. Suppose Bob wants to transfer 5 TON to Alice. He creates a transaction for this amount, meaning a message that he wants to transfer 5 TON to Alice, and sends this message to the channel with Alice.
If Alice agrees, she accepts this message. After that, the participants' balances change: Bob's balance is blocked by 5 TON, while Alice's balance increases by 5 TON.
Each transaction has a unique identifier and contains information about the previous channel state, information about the new channel state, and a trigger that initiates the channel transition from one state to another.
As long as the participants agree to change the channel's state, transactions occur between them. Payment information is stored within the channel and is not reflected in the blockchain.
Closing the Payment Channel. Bob and Alice have finished transferring coins to each other: Bob now has 130 coins, and Alice has 70 coins. It's time to close the payment channel, meaning to submit the final and mutually agreed-upon channel state to the blockchain.
Users confirm that they agree with each other's transactions and sign the smart contract. After that, the system distributes the coins between Bob and Alice based on all payments, records the channel closure in the blockchain, and the participants pay the fee again.
To transfer cryptocurrency to each other again, Bob and Alice must open a new payment channel.
If something goes wrong. Sometimes users cheat. For example, one participant may initiate channel closure by sending a non-final state and underpaying a significant amount.
If Bob tries to cheat Alice, she can independently close the channel to receive all the coins due to her. To do this, Alice will need to prove that Bob attempted to cheat her and provide a later channel state as evidence. The smart contract ensures that the coins in the channel will be distributed fairly.