-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands_commitSell.go
70 lines (56 loc) · 1.38 KB
/
commands_commitSell.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
package main
import (
"fmt"
"strconv"
"time"
types "github.com/distributeddesigns/shared_types"
)
type commitSellCmd struct {
id uint64
userID string
}
func parseCommitSellCmd(parts []string) commitSellCmd {
if len(parts) != 3 {
abortTx("COMMIT_SELL needs 3 parts")
}
id, err := strconv.ParseUint(parts[0], 10, 64)
abortTxOnError(err, "Could not parse ID")
return commitSellCmd{
id: id,
userID: parts[2],
}
}
func (cs commitSellCmd) Name() string {
return fmt.Sprintf("[%d] COMMIT_SELL", cs.id)
}
func (cs commitSellCmd) ToAuditEvent() types.AuditEvent {
xmlElement := fmt.Sprintf(`
<userCommand>
<timestamp>%d</timestamp>
<server>%s</server>
<transactionNum>%d</transactionNum>
<command>COMMIT_SELL</command>
<username>%s</username>
</userCommand>`,
time.Now().UnixNano()/1e6, redisBaseKey, cs.id, cs.userID,
)
return types.AuditEvent{
UserID: cs.userID,
ID: cs.id,
EventType: "command",
Content: xmlElement,
}
}
func (cs commitSellCmd) Execute() {
abortTxIfNoAccount(cs.userID)
acct := accountStore[cs.userID]
pendingSell, err := acct.pendingSells.Pop()
abortTxOnError(err, cs.Name()+" No pending sells")
if pendingSell.IsExpired() {
pendingSell.RollBack()
abortTx(cs.Name() + " Sell is expired")
}
pendingSell.Commit()
acct.AddSummaryItem("Finished " + cs.Name())
consoleLog.Notice(" [✔] Finished", cs.Name())
}