-
Notifications
You must be signed in to change notification settings - Fork 1
/
portfolio.go
617 lines (482 loc) · 14.6 KB
/
portfolio.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package portfolio
import (
"net/http"
"sort"
"strconv"
"github.com/gorilla/mux"
"github.com/shopspring/decimal"
"github.com/dense-analysis/pricewarp/internal/database"
"github.com/dense-analysis/pricewarp/internal/model"
"github.com/dense-analysis/pricewarp/internal/route/query"
"github.com/dense-analysis/pricewarp/internal/route/util"
"github.com/dense-analysis/pricewarp/internal/session"
"github.com/dense-analysis/pricewarp/internal/template"
)
var One decimal.Decimal = decimal.NewFromInt(1)
var Hundred decimal.Decimal = decimal.NewFromInt(100)
// TrackedAsset is an Asset with additional information available to it.
type TrackedAsset struct {
model.Asset
Value decimal.Decimal
ShareOfPortfolio decimal.Decimal
Performance decimal.Decimal
}
var portfolioQuery = `
select
currency.id,
currency.ticker,
currency.name,
cash
from crypto_portfolio
inner join crypto_currency as currency
on currency.id = crypto_portfolio.currency_id
`
func scanPortfolio(row database.Row, portfolio *model.Portfolio) error {
return row.Scan(
&portfolio.Currency.ID,
&portfolio.Currency.Ticker,
&portfolio.Currency.Name,
&portfolio.Cash,
)
}
func loadPortfolio(conn *database.Conn, user *model.User, portfolio *model.Portfolio) error {
row := conn.QueryRow(portfolioQuery+" where user_id = $1", user.ID)
return scanPortfolio(row, portfolio)
}
var assetQuery = `
select
currency.id,
currency.ticker,
currency.name,
purchased,
amount
from crypto_asset
inner join crypto_currency as currency
on currency.id = crypto_asset."currency_id"
`
var optionalAssetQuery = `
select
currency.id,
currency.ticker,
currency.name,
coalesce(asset.purchased, 0::numeric),
coalesce(asset.amount, 0::numeric)
from crypto_currency as currency
left join crypto_asset as asset
on asset.currency_id = currency.id
`
func scanAsset(row database.Row, asset *model.Asset) error {
return row.Scan(
&asset.Currency.ID,
&asset.Currency.Ticker,
&asset.Currency.Name,
&asset.Purchased,
&asset.Amount,
)
}
func scanTrackedAsset(row database.Row, asset *TrackedAsset) error {
return scanAsset(row, &asset.Asset)
}
func loadAssetList(conn *database.Conn, userID int, assetList *[]TrackedAsset) error {
return model.LoadList(
conn,
assetList,
1,
scanTrackedAsset,
assetQuery+"where user_id = $1",
userID,
)
}
var priceQuery = `
select distinct on("from", "to")
from_currency.id,
from_currency.ticker,
from_currency.name,
to_currency.id,
to_currency.ticker,
to_currency.name,
time,
value
from crypto_price
inner join crypto_currency as from_currency
on from_currency.id = crypto_price."from"
inner join crypto_currency as to_currency
on to_currency.id = crypto_price."to"
`
func scanPrice(row database.Row, price *model.Price) error {
return row.Scan(
&price.From.ID,
&price.From.Ticker,
&price.From.Name,
&price.To.ID,
&price.To.Ticker,
&price.To.Name,
&price.Time,
&price.Value,
)
}
func loadPriceList(conn *database.Conn, currency *model.Currency, tickerList []string, priceList *[]model.Price) error {
if len(tickerList) == 0 {
*priceList = nil
return nil
}
return model.LoadList(
conn,
priceList,
len(tickerList)*2,
scanPrice,
priceQuery+`
where from_currency.ticker = ANY($1)
and (to_currency.id = $2 or to_currency.ticker = 'BTC')
order by "from" desc, "to" desc, time desc;
`,
tickerList,
currency.ID,
)
}
func loadAssetPrices(conn *database.Conn, currency *model.Currency, assetList []TrackedAsset) error {
tickerList := make([]string, 0, len(assetList)+1)
tickerList = append(tickerList, "BTC")
for _, asset := range assetList {
tickerList = append(tickerList, asset.Currency.Ticker)
}
var priceList []model.Price
if err := loadPriceList(conn, currency, tickerList, &priceList); err != nil {
return err
}
btcPrices := map[string]decimal.Decimal{}
currencyPrices := map[string]decimal.Decimal{}
for _, price := range priceList {
if price.To.ID == currency.ID {
currencyPrices[price.From.Ticker] = price.Value
} else {
btcPrices[price.From.Ticker] = price.Value
}
}
for i := range assetList {
asset := &assetList[i]
if multiplier, ok := currencyPrices[asset.Currency.Ticker]; ok {
// Conversion from a currency to fiat directly.
asset.Value = asset.Amount.Mul(multiplier)
} else if toBtcMultiplier, ok := btcPrices[asset.Currency.Ticker]; ok {
// Conversion from a currency to fiat via Bitcoin.
if btcToCurrencyMultiplier, ok := currencyPrices["BTC"]; ok {
asset.Value = asset.Amount.Mul(toBtcMultiplier).Mul(btcToCurrencyMultiplier)
} else {
asset.Value = decimal.Zero
}
} else {
asset.Value = decimal.Zero
}
}
totalValue := decimal.Zero
for _, asset := range assetList {
totalValue = totalValue.Add(asset.Value)
}
for i := range assetList {
asset := &assetList[i]
// The share of the portofolio is the value over the total value
if totalValue.IsZero() {
asset.ShareOfPortfolio = decimal.Zero
} else {
asset.ShareOfPortfolio = asset.Value.Div(totalValue).Mul(Hundred)
}
// Calculate percentage gains per asset
if asset.Purchased.IsZero() {
asset.Performance = decimal.Zero
} else {
asset.Performance = asset.Value.Div(asset.Purchased).Sub(One).Mul(Hundred)
}
}
return nil
}
var assetUpdateQuery = `
insert into crypto_asset
(user_id, currency_id, purchased, amount)
values ($1, $2, $3, $4)
on conflict (user_id, currency_id) do update
set purchased = $3, amount = $4
`
func updateAsset(conn database.Queryable, user *model.User, asset *model.Asset) error {
return conn.Exec(assetUpdateQuery, user.ID, asset.Currency.ID, asset.Purchased, asset.Amount)
}
var portfolioUpdateQuery = `
insert into crypto_portfolio (user_id, currency_id, cash)
values ($1, $2, $3)
on conflict (user_id) do update
set currency_id = $2, cash = $3
`
func updatePortfolio(conn database.Queryable, user *model.User, portfolio *model.Portfolio) error {
return conn.Exec(portfolioUpdateQuery, user.ID, portfolio.Currency.ID, portfolio.Cash)
}
func loadUser(conn *database.Conn, writer http.ResponseWriter, request *http.Request, user *model.User) bool {
found, err := session.LoadUserFromSession(conn, request, user)
if err != nil {
util.RespondInternalServerError(writer, err)
return false
}
return found
}
type PortfolioPageData struct {
User model.User
Portfolio model.Portfolio
}
// HandlePortfolioUpdate updates the user's currency and balance of that currency.
func HandlePortfolioUpdate(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
data := PortfolioPageData{}
if !loadUser(conn, writer, request, &data.User) {
util.RespondForbidden(writer)
return
}
request.ParseForm()
currencyID, err := strconv.Atoi(request.Form.Get("currency"))
if err != nil {
util.RespondValidationError(writer, "Invalid currency ID")
return
}
data.Portfolio.Cash, err = decimal.NewFromString(request.Form.Get("cash"))
if err != nil {
util.RespondValidationError(writer, "Invalid cash value")
return
}
if data.Portfolio.Cash.IsNegative() {
util.RespondValidationError(writer, "Cash must be non-negative")
return
}
if err := query.LoadCurrencyByID(conn, &data.Portfolio.Currency, currencyID); err != nil {
if err == database.ErrNoRows {
util.RespondValidationError(writer, "Unknown currency ID")
} else {
util.RespondInternalServerError(writer, err)
}
return
}
if err := updatePortfolio(conn, &data.User, &data.Portfolio); err != nil {
util.RespondInternalServerError(writer, err)
} else {
http.Redirect(writer, request, "/portfolio", http.StatusFound)
}
}
type PortfolioListPageData struct {
PortfolioPageData
AssetList []TrackedAsset
ToCurrencyList []model.Currency
FromCurrencyList []model.Currency
TotalPurchased decimal.Decimal
TotalValue decimal.Decimal
TotalProfit decimal.Decimal
AveragePerformance decimal.Decimal
}
type byValueOrder []TrackedAsset
func (a byValueOrder) Len() int {
return len(a)
}
func (a byValueOrder) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byValueOrder) Less(i, j int) bool {
return a[j].Value.LessThan(a[i].Value)
}
// HandlePortfolio shows the assets and cash a user has.
func HandlePortfolio(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
data := PortfolioListPageData{}
if !loadUser(conn, writer, request, &data.User) {
http.Redirect(writer, request, "/login", http.StatusFound)
return
}
if err := loadPortfolio(conn, &data.User, &data.Portfolio); err != nil {
if err != database.ErrNoRows {
util.RespondInternalServerError(writer, err)
return
}
}
if err := query.LoadCurrencyList(conn, &data.FromCurrencyList); err != nil {
util.RespondInternalServerError(writer, err)
return
}
data.ToCurrencyList = query.BuildToCurrencyList(data.FromCurrencyList)
if data.Portfolio.Currency.ID != 0 {
// Only load assets once a currency has been set.
if err := loadAssetList(conn, data.User.ID, &data.AssetList); err != nil {
util.RespondInternalServerError(writer, err)
return
}
if err := loadAssetPrices(conn, &data.Portfolio.Currency, data.AssetList); err != nil {
util.RespondInternalServerError(writer, err)
return
}
sort.Sort(byValueOrder(data.AssetList))
// Add cash in fiat to the total value and amount purchased.
data.TotalValue = data.Portfolio.Cash
data.TotalPurchased = data.Portfolio.Cash
for _, asset := range data.AssetList {
data.TotalValue = data.TotalValue.Add(asset.Value)
data.TotalPurchased = data.TotalPurchased.Add(asset.Purchased)
}
data.TotalProfit = data.TotalValue.Sub(data.TotalPurchased)
if data.TotalPurchased.IsZero() {
data.AveragePerformance = decimal.Zero
} else {
data.AveragePerformance = data.TotalValue.Div(data.TotalPurchased).Sub(One).Mul(Hundred)
}
}
template.Render(template.Portfolio, writer, data)
}
type AssetAdjustData struct {
PortfolioPageData
asset model.Asset
crypto decimal.Decimal
fiat decimal.Decimal
}
func loadAssetAdjustFormData(
conn *database.Conn,
data *AssetAdjustData,
writer http.ResponseWriter,
request *http.Request,
) bool {
if !loadUser(conn, writer, request, &data.User) {
util.RespondForbidden(writer)
return false
}
if err := loadPortfolio(conn, &data.User, &data.Portfolio); err != nil {
if err != database.ErrNoRows {
util.RespondValidationError(writer, "Portfolio not configured")
} else {
util.RespondInternalServerError(writer, err)
}
return false
}
ticker := mux.Vars(request)["ticker"]
row := conn.QueryRow(
optionalAssetQuery+" and asset.user_id = $1 where currency.ticker = $2",
data.User.ID,
ticker,
)
if err := scanAsset(row, &data.asset); err != nil {
if err == database.ErrNoRows {
util.RespondNotFound(writer)
} else {
util.RespondInternalServerError(writer, err)
}
return false
}
request.ParseForm()
var err error
data.fiat, err = decimal.NewFromString(request.Form.Get("fiat"))
if err != nil {
util.RespondValidationError(writer, "Invalid fiat value")
return false
}
if data.fiat.IsNegative() {
util.RespondValidationError(writer, "fiat must not be negative")
return false
}
data.crypto, err = decimal.NewFromString(request.Form.Get("crypto"))
if err != nil {
util.RespondValidationError(writer, "Invalid crypto value")
return false
}
if !data.crypto.IsPositive() {
util.RespondValidationError(writer, "crypto must be positive")
return false
}
return true
}
func saveAssetAdjustChanges(
conn *database.Conn,
data *AssetAdjustData,
writer http.ResponseWriter,
request *http.Request,
) bool {
tx, err := conn.Begin()
if err != nil {
util.RespondInternalServerError(writer, err)
return false
}
defer tx.Rollback()
if err := updateAsset(tx, &data.User, &data.asset); err != nil {
util.RespondInternalServerError(writer, err)
return false
}
if err := updatePortfolio(tx, &data.User, &data.Portfolio); err != nil {
util.RespondInternalServerError(writer, err)
return false
}
tx.Commit()
return true
}
// HandleAssetBuy swaps some cash for a cryptocurrency asset.
func HandleAssetBuy(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
data := AssetAdjustData{}
if loadAssetAdjustFormData(conn, &data, writer, request) {
if data.fiat.GreaterThan(data.Portfolio.Cash) {
util.RespondValidationError(writer, "You can't spend more fiat than you have")
return
}
data.asset.Purchased = data.asset.Purchased.Add(data.fiat)
data.asset.Amount = data.asset.Amount.Add(data.crypto)
data.Portfolio.Cash = data.Portfolio.Cash.Sub(data.fiat)
if saveAssetAdjustChanges(conn, &data, writer, request) {
http.Redirect(writer, request, "/portfolio", http.StatusFound)
}
}
}
// HandleAssetSell swaps some cryptocurrency asset for cash.
func HandleAssetSell(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
data := AssetAdjustData{}
if loadAssetAdjustFormData(conn, &data, writer, request) {
if data.crypto.GreaterThan(data.asset.Amount) {
util.RespondValidationError(writer, "You can't remove more crypto than you have")
return
}
// Subtract the cost by the average cost of the asset sold.
differencePurchased := data.asset.Purchased.Mul(data.crypto.Div(data.asset.Amount))
data.asset.Purchased = data.asset.Purchased.Sub(differencePurchased)
data.asset.Amount = data.asset.Amount.Sub(data.crypto)
data.Portfolio.Cash = data.Portfolio.Cash.Add(data.fiat)
if saveAssetAdjustChanges(conn, &data, writer, request) {
http.Redirect(writer, request, "/portfolio", http.StatusFound)
}
}
}
type AssetPageData struct {
PortfolioPageData
Asset TrackedAsset
}
// HandleAsset displays the details for a single Cryptocurrency asset.
func HandleAsset(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
data := AssetPageData{}
if !loadUser(conn, writer, request, &data.User) {
http.Redirect(writer, request, "/login", http.StatusFound)
return
}
if err := loadPortfolio(conn, &data.User, &data.Portfolio); err != nil {
if err != database.ErrNoRows {
util.RespondNotFound(writer)
} else {
util.RespondInternalServerError(writer, err)
}
return
}
ticker := mux.Vars(request)["ticker"]
row := conn.QueryRow(
optionalAssetQuery+" and asset.user_id = $1 where currency.ticker = $2",
data.User.ID,
ticker,
)
assetList := make([]TrackedAsset, 1)
if err := scanTrackedAsset(row, &assetList[0]); err != nil {
if err == database.ErrNoRows {
util.RespondNotFound(writer)
} else {
util.RespondInternalServerError(writer, err)
}
return
}
if err := loadAssetPrices(conn, &data.Portfolio.Currency, assetList); err != nil {
util.RespondInternalServerError(writer, err)
return
}
data.Asset = assetList[0]
template.Render(template.Asset, writer, data)
}