forked from filecoin-project/boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunds.go
201 lines (168 loc) · 5.23 KB
/
funds.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package db
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/filecoin-project/boost/db/fielddef"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/google/uuid"
"github.com/mattn/go-sqlite3"
)
type FundsLog struct {
DealUUID uuid.UUID
CreatedAt time.Time
Amount big.Int
Text string
}
type FundsDB struct {
db *sql.DB
}
func NewFundsDB(db *sql.DB) *FundsDB {
return &FundsDB{db: db}
}
func (f *FundsDB) Tag(ctx context.Context, dealUuid uuid.UUID, collateral abi.TokenAmount, pubMsg abi.TokenAmount) error {
qry := "INSERT INTO FundsTagged (DealUUID, CreatedAt, Collateral, PubMsg) "
qry += "VALUES (?, ?, ?, ?)"
values := []interface{}{dealUuid, time.Now(), collateral.String(), pubMsg.String()}
_, err := f.db.ExecContext(ctx, qry, values...)
return err
}
func (f *FundsDB) Untag(ctx context.Context, dealUuid uuid.UUID) (clt abi.TokenAmount, pub abi.TokenAmount, e error) {
qry := "SELECT Collateral, PubMsg FROM FundsTagged WHERE DealUUID = ?"
row := f.db.QueryRowContext(ctx, qry, dealUuid)
collat := &fielddef.BigIntFieldDef{F: new(abi.TokenAmount)}
pubMsg := &fielddef.BigIntFieldDef{F: new(abi.TokenAmount)}
err := row.Scan(&collat.Marshalled, &pubMsg.Marshalled)
if err != nil {
if err == sql.ErrNoRows {
return abi.NewTokenAmount(0), abi.NewTokenAmount(0), ErrNotFound
}
return abi.NewTokenAmount(0), abi.NewTokenAmount(0), fmt.Errorf("getting untagged amount: %w", err)
}
err = collat.Unmarshall()
if err != nil {
return abi.NewTokenAmount(0), abi.NewTokenAmount(0), fmt.Errorf("unmarshalling untagged Collateral")
}
err = pubMsg.Unmarshall()
if err != nil {
return abi.NewTokenAmount(0), abi.NewTokenAmount(0), fmt.Errorf("unmarshalling untagged PubMsg")
}
_, err = f.db.ExecContext(ctx, "DELETE FROM FundsTagged WHERE DealUUID = ?", dealUuid)
return *collat.F, *pubMsg.F, err
}
func (f *FundsDB) InsertLog(ctx context.Context, logs ...*FundsLog) error {
now := time.Now()
for _, l := range logs {
if l.CreatedAt.IsZero() {
l.CreatedAt = now
}
qry := "INSERT INTO FundsLogs (DealUUID, CreatedAt, Amount, LogText) "
qry += "VALUES (?, ?, ?, ?)"
values := []interface{}{l.DealUUID, l.CreatedAt, l.Amount.String(), l.Text}
_, err := f.db.ExecContext(ctx, qry, values...)
if err != nil {
return fmt.Errorf("inserting funds log: %w", err)
}
}
return nil
}
func (f *FundsDB) Logs(ctx context.Context, cursor *time.Time, offset int, limit int) ([]FundsLog, error) {
qry := "SELECT DealUUID, CreatedAt, Amount, LogText FROM FundsLogs"
args := []interface{}{}
if cursor != nil {
qry += " WHERE CreatedAt <= ?"
args = append(args, cursor.Format(sqlite3.SQLiteTimestampFormats[0]))
}
qry += " ORDER BY CreatedAt DESC, RowID"
if limit > 0 {
qry += " LIMIT ?"
args = append(args, limit)
if offset > 0 {
qry += " OFFSET ?"
args = append(args, offset)
}
}
rows, err := f.db.QueryContext(ctx, qry, args...)
if err != nil {
return nil, err
}
defer rows.Close()
fundsLogs := make([]FundsLog, 0, 16)
for rows.Next() {
var fundsLog FundsLog
amt := &fielddef.BigIntFieldDef{F: &fundsLog.Amount}
err := rows.Scan(
&fundsLog.DealUUID,
&fundsLog.CreatedAt,
&amt.Marshalled,
&fundsLog.Text)
if err != nil {
return nil, fmt.Errorf("getting fund log: %w", err)
}
err = amt.Unmarshall()
if err != nil {
return nil, fmt.Errorf("unmarshalling fund log Amount: %w", err)
}
fundsLogs = append(fundsLogs, fundsLog)
}
if err := rows.Err(); err != nil {
return nil, err
}
return fundsLogs, nil
}
func (f *FundsDB) LogsCount(ctx context.Context) (int, error) {
var count int
row := f.db.QueryRowContext(ctx, "SELECT count(*) FROM FundsLogs")
err := row.Scan(&count)
return count, err
}
type TotalTagged struct {
Collateral abi.TokenAmount
PubMsg abi.TokenAmount
}
func (f *FundsDB) TotalTagged(ctx context.Context) (*TotalTagged, error) {
rows, err := f.db.QueryContext(ctx, "SELECT Collateral, PubMsg FROM FundsTagged")
if err != nil {
return nil, fmt.Errorf("getting total tagged: %w", err)
}
defer rows.Close()
tt := &TotalTagged{
Collateral: abi.NewTokenAmount(0),
PubMsg: abi.NewTokenAmount(0),
}
for rows.Next() {
collat := &fielddef.BigIntFieldDef{F: new(abi.TokenAmount)}
pubMsg := &fielddef.BigIntFieldDef{F: new(abi.TokenAmount)}
err := rows.Scan(&collat.Marshalled, &pubMsg.Marshalled)
if err != nil {
return nil, fmt.Errorf("getting total tagged: %w", err)
}
err = collat.Unmarshall()
if err != nil {
return nil, fmt.Errorf("unmarshalling untagged Collateral: %w", err)
}
if collat.F.Int != nil {
tt.Collateral = big.Add(tt.Collateral, *collat.F)
}
err = pubMsg.Unmarshall()
if err != nil {
return nil, fmt.Errorf("unmarshalling untagged PubMsg: %w", err)
}
if pubMsg.F.Int != nil {
tt.PubMsg = big.Add(tt.PubMsg, *pubMsg.F)
}
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("getting total tagged: %w", err)
}
return tt, nil
}
func (f *FundsDB) CleanupLogs(ctx context.Context, daysOld int) error {
t := time.Now()
td := t.AddDate(0, 0, -1*daysOld)
qry := "DELETE from FundsLogs WHERE DealUUID IN (SELECT DISTINCT DealUUID FROM FundsLogs WHERE CreatedAt < ?)"
_, err := f.db.ExecContext(ctx, qry, td)
return err
}