-
Notifications
You must be signed in to change notification settings - Fork 0
/
libledger.py
99 lines (93 loc) · 3.4 KB
/
libledger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
import libfiles as files
import libutils as utils
def getUserLedgerFilename(npub):
filename = f"{files.userLedgerFolder}{npub}.ledger.json"
return filename
def getCreditBalance(npub):
filename = getUserLedgerFilename(npub)
ledger = files.loadJsonFile(filename)
if ledger is None: return 0
balance = int(ledger[-1]["balance"])
return balance
def recordEntry(npub, type, credits, mcredits, description):
filename = getUserLedgerFilename(npub)
ledger = files.loadJsonFile(filename)
if ledger is None:
# initialize first entry
ledger = []
balance = 0.000
created_at, created_at_iso = utils.getTimes()
firstEntry = {
"created_at": created_at,
"created_at_iso": created_at_iso,
"type": "INITIALIZED",
"credits": 0,
"mcredits": 0,
"balance": balance,
"description": "Initialized Balance",
}
ledger.append(firstEntry)
else:
# get current balance from existing, last record
balance = ledger[-1]["balance"]
# Determine new balance based on amounts passed in
balance += credits
balance += (mcredits/1000)
# Add the new entry
created_at, created_at_iso = utils.getTimes()
newEntry = {
"created_at": created_at,
"created_at_iso": created_at_iso,
"type": type,
"credits": credits,
"mcredits": mcredits,
"balance": balance,
"description": description,
}
ledger.append(newEntry)
# Save to disk
files.saveJsonFile(filename, ledger)
# Rotate if needed
if len(ledger) > 500:
rotateLedger(npub, ledger)
# return new balance
return balance
def rotateLedger(npub, ledger):
# first save current contents to the archive
archivedLedgerFolder = f"{files.userLedgerFolder}archived/"
utils.makeFolderIfNotExists(archivedLedgerFolder)
t, tISO = utils.getTimes()
archivedFilename = f"{archivedLedgerFolder}{npub}.{t}.ledger.json"
files.saveJsonFile(archivedFilename, ledger)
# now get balance
ledgerSummary = {
"CREDITS APPLIED": {"credits": 0, "mcredits": 0},
"REPLY MESSAGE": {"credits": 0, "mcredits": 0},
"ZAPS": {"credits": 0, "mcredits": 0},
"ROUTING FEES": {"credits": 0, "mcredits": 0},
"SERVICE FEES": {"credits": 0, "mcredits": 0}
}
for ledgerEntry in ledger:
type = ledgerEntry["type"]
credits = ledgerEntry["credits"]
mcredits = ledgerEntry["mcredits"]
if type in ledgerSummary:
ledgerSummary[type]["credits"] = ledgerSummary[type]["credits"] + credits
ledgerSummary[type]["mcredits"] = ledgerSummary[type]["mcredits"] + mcredits
newLedger = []
balance = 0
for ledgerSummaryType, ledgerSummaryItem in ledgerSummary.items():
balance = balance + ledgerSummaryItem["credits"]
balance = balance + (ledgerSummaryItem["mcredits"]/1000)
newLedger.append({
"created_at": t,
"created_at_iso": tISO,
"type": ledgerSummaryType,
"credits": ledgerSummaryItem["credits"],
"mcredits": ledgerSummaryItem["mcredits"],
"balance": balance,
"description": "Carry over from ledger rotation",
})
filename = getUserLedgerFilename(npub)
files.saveJsonFile(filename, newLedger)