V
V
vante_scribaxxi2017-09-27 09:25:01
Python
vante_scribaxxi, 2017-09-27 09:25:01

How to organize money handling in a database in python3?

I am writing a telegram bot for micro investments, which, accordingly, can accept payments and withdraw funds.
The trick is that the amount on the balance sheet must be kept very carefully, because. depending on it, the bot will carry out the output.
I decided to make a class that would connect to the database during initialization and provide a simple and safe interface.
Question:
What is the best way to protect money from unforeseen circumstances?
Personally, I'm scared for them, if something happens to the bot or the server, the users will sue me.
Would it be useful to keep logs?
If yes, in what format?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-09-27
@vante_scribaxxi

Well, I'll describe how it's usually done.
1) store values ​​only in integer form, respectively, if you have 11 rubles and 20 kopecks, 1120 should be in the base.
2) we never store the amount (balance)! it is always calculated by the difference between the two columns debet - credit = sum
3) these two columns never grow downwards, only the addition operation (excluding reversal)
4) always keep a log of operations, keeping the date, old debit, old credit, new debit, new credit , type of operation income / expense / reversal, text entry (well, you can also add the parameters of the operation to whom and for what reason)
5) reversing posting - in the logs we mark a separate type, debit or debit or credit.
Then you can always set the balance and the amount of funds for any operation using the log. Also, you can always check the correctness of the growth of the balance. Well, in general, the correct accounting.
PS. And yes, sqlite is better not to use here, it is almost single-threaded - there can be big problems with simultaneous writing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question