Implementation Principle

A ledger with real-time transaction settlement

This is a ledger that can settle the transactions in real-time using smart contracts. The premise of the real-time settlement of the transactions in the ledger is that the transactions are carried out one by one in a specific processing sequence and reliable transaction time. Blockchain meets these requirements and provides a basis for automatic transaction settlement.

When a transaction occurs, the ledger settles the transaction for the corresponding account and updates the interest into the account balance. When the next transaction occurs, the same process is triggered again to update the account balance.

Interest Model

A simple banking model is used to generate income from loans, which is used as the interest for deposit users. It doesn't offer floating loan interest or consider profits, but ensures the balance of borrowing and lending.

Loan Profit = Deposit InterestLoan Profit = Total loan * Loan interest rate * TimeDeposit Interest = Total deposit * Deposit interest rate * Time=>Total deposit * Deposit interest rate * Time =Total loan * Loan interest rate * Time=>Total deposit * Deposit interest rate =Total loan * Loan interest rate

According to the above equation,

  1. When the loan is 0, no profit will be generated and then deposit interest is 0.

  2. When the total loan increases, the profit will increase and then deposit interest will increase.

  3. When the total loan remains unchanged and the total deposit increases, the deposit interest will decrease

Conclusion: The deposit interest rates change with changes in total loans and total deposits.

Changes in the ledger

Here are 4 different transaction events: deposit, withdraw, borrow, repay.

If there are no transaction events, the total amount of deposits and loans will not change and interest rates will remain unchanged during this period. If any transaction events occur, the total amount of deposits and loans will change, and then the interest rate will also change.

Assuming that the borrowing interest rate is 0.05, the circles in the diagram below indicate the status of the ledger and interest rates, and the arrows indicate transaction events:

Circle a: Deposit is 100. Loan is 0. Loan interest rate is 5%. No interest earned.

Transaction 1: borrow 50

= (50 * 0.05)/ 100 = 0.025Deposit interest rate = ( total loan * loan interest rate)/ total deposit = (50 * 0.05)/ 100 = 0.025

​The ledger resulted from event 2 and 3 can also be calculated according to the above equation.

Conclusion: Transaction events result in changes in interest rates

Relationship between income and time

The status change as showed above doesn't include transaction settlement. In fact, incomes (interests) will be generated over time.

For deposits:

New total deposit = total deposit + (total deposit * deposit interest rate * time)

For loans:

New total loan = total loan + (total loan * loan interest rate * time)

Yellow arrows represent the duration of the previous status. After a new event occurs, the status will be updated into the next time period.

As shown in the above diagram, the changes in interest rates become more complex after considering the relationship between income and time, but this calculation is still very clear.

Status a lasts 1 day. Since the loan is 0 and the interest rate is 0, the deposit doesn't change after settlement for event 1. But the total loan increases after event 1. The deposit interest rate is updated to 0.025 after event 1.

Status b lasts 2 days until event 2 occurs. Here is the calculation of the new total deposit and total loan before settlement for event 2:

## Settlement ##

New total deposit = 100 +(100 * 0.025 * 2)= 105

New total loan = 50 +(50 * 0.05 * 2) = 55

After settlement for event 2, the total deposit will be 105 + 50 = 155.

According to the new deposit of 155 and the new loan of 55, the new interest rate is 0.01774.

Status c lasts 1 day until event 3 occurs.

## Settlement ##

New total deposit = 155 +(155 * 0.01774 * 1) = 157.75

New total loan = 55 + (55 * 0.05 * 1) = 57.75

Because 20 is repaid, total loan changes to 57.75 - 20 = 37.75

Then the new interest rate is 0.012.

Conclusion: The settlement is conducted when any transaction events occur. After settlement, the account balance will be adjusted, which leads to a change in the interest rate.

Each Itemised account

We classify the total deposit 100 in status a as other deposits. After 2 days, Alice deposits 50, and other deposits are updated to 105 after settlement. Alice's deposit increases the total deposits to 155, and the new deposit interest rate is updated to be 0.01774.

One day later, Bob also deposits 50. By the time before Bob's event, the settlement for Alice's deposit and other deposits uses the interest rate of 0.01774.This calculation results are shown in status c.

As analysed above, we can find that every account balance needs to be settled when an event occurs, . In this way, as the number of deposit/borrowing users increases, there will be more and more accounts, and the calculation workload for each settlement will also increase. In fact, it is not necessary to settle all the accounts as long as the historical interest rate is recorded. We can directly calculate the state c as shown in the diagram above based on the initial status of each account:

Other Deposits = 100 +(100 * 0.025 * 2)+((100 +(100 * 0.025 * 2))* 0.01774 * 1)

Alice's deposit = 50 + (50 * 0.01774 * 1)

Bob's deposit = 50

Where 100 is the initial amount of other deposits, and 50 is the initial amount of Alice's deposit. 0.025 is the interest rate for the first period, and 0.01774 is the interest rate for the second period. It can be found that as long as there is a record of historical interest rates, the balance of each account can be calculated through iterative computation. Therefore, when the settlement is performed, only the account of the event needs to be settled, and other accounts can be temporarily not settled until any events apply to them.

Conclusions: Each settlement is only for accounts whose balances are affected and the general ledger is updated. There is no need to settle other accounts before new events occur.

This part of the description is compiled from the Internet, thanks to the original author, outprog.

Link: https://juejin.cn/post/6844903774620745742

最后更新于