-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands_sell.go
More file actions
143 lines (119 loc) · 3.28 KB
/
commands_sell.go
File metadata and controls
143 lines (119 loc) · 3.28 KB
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
package main
import (
"fmt"
"strconv"
"time"
"github.com/distributeddesigns/currency"
types "github.com/distributeddesigns/shared_types"
)
type sellCmd struct {
id uint64
userID string
stock string
amount currency.Currency
profit currency.Currency
quantityToSell uint64
expiresAt time.Time
}
func parseSellCmd(parts []string) sellCmd {
if len(parts) != 5 {
abortTx("SELL needs 5 parts")
}
id, err := strconv.ParseUint(parts[0], 10, 64)
abortTxOnError(err, "Could not parse ID")
amount, err := currency.NewFromString(parts[4])
abortTxOnError(err, "Could not parse amount in transaction")
return sellCmd{
id: id,
userID: parts[2],
stock: parts[3],
amount: amount,
}
}
func (s sellCmd) Name() string {
return fmt.Sprintf("[%d] SELL", s.id)
}
func (s sellCmd) ToAuditEvent() types.AuditEvent {
xmlElement := fmt.Sprintf(`
<userCommand>
<timestamp>%d</timestamp>
<server>%s</server>
<transactionNum>%d</transactionNum>
<command>SELL</command>
<username>%s</username>
<stockSymbol>%s</stockSymbol>
<funds>%.02f</funds>
</userCommand>`,
time.Now().UnixNano()/1e6, redisBaseKey, s.id, s.userID,
s.stock, s.amount.ToFloat(),
)
return types.AuditEvent{
UserID: s.userID,
ID: s.id,
EventType: "command",
Content: xmlElement,
}
}
func (s sellCmd) Execute() {
abortTxIfNoAccount(s.userID)
acct := accountStore[s.userID]
acct.Lock()
defer acct.Unlock()
// Check if user has any stock, abort early
stockHoldings, found := acct.portfolio[s.stock]
if !found || stockHoldings == 0 {
abortTx(s.Name() + " User does not have any stock to sell")
}
// Get a quote for the stock
qr := types.QuoteRequest{
Stock: s.stock,
UserID: s.userID,
AllowCache: true,
ID: s.id,
}
q := getQuote(qr)
// Get a fresh one if about to expire
quoteTTL := q.Timestamp.Add(time.Second*60).Unix() - time.Now().Unix()
if quoteTTL < config.QuotePolicy.UseInBuySell {
consoleLog.Info(" [!] Getting a fresh quote for", s.Name())
qr.AllowCache = false
q = getQuote(qr)
}
// Check if user can sell stock at quote price
quantityToSell, profit := q.Price.FitsInto(s.amount)
consoleLog.Debugf("Want to sell %d stock", quantityToSell)
if quantityToSell < 1 {
abortTx(s.Name() + " Cannot sell less than one stock")
}
// If yes...
// 1. Populate the profit, quantityToSell, expiresAt fields
// 2. Remove stock from the user
// 3. Add the sellCmd to the accounts pendingSells stack
s.quantityToSell = uint64(quantityToSell)
s.profit = profit
s.expiresAt = q.Timestamp.Add(time.Second * 60)
err := acct.RemoveStock(s.stock, s.quantityToSell)
abortTxOnError(err, s.Name())
acct.pendingSells.Push(s)
acct.AddSummaryItem("Finished " + s.Name())
consoleLog.Notice(" [✔] Finished", s.Name())
}
func (s sellCmd) Commit() {
consoleLog.Debug("Commiting", s.Name())
acct := accountStore[s.userID]
acct.Lock()
acct.AddFunds(s.profit)
acct.AddSummaryItem("Commited " + s.Name())
acct.Unlock()
}
func (s sellCmd) RollBack() {
consoleLog.Debug("Rolling back", s.Name())
acct := accountStore[s.userID]
acct.Lock()
acct.AddStock(s.stock, s.quantityToSell)
acct.AddSummaryItem("Rolled back " + s.Name())
acct.Unlock()
}
func (s sellCmd) IsExpired() bool {
return time.Now().After(s.expiresAt)
}