-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands_setBuyAmount.go
83 lines (71 loc) · 1.74 KB
/
commands_setBuyAmount.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
package main
import (
"fmt"
"strconv"
"time"
"github.com/distributeddesigns/currency"
types "github.com/distributeddesigns/shared_types"
)
type setBuyAmountCmd struct {
id uint64
userID string
stock string
amount currency.Currency
}
func parseSetBuyAmountCmd(parts []string) setBuyAmountCmd {
if len(parts) != 5 {
abortTx("SET_BUY_AMOUNT 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 setBuyAmountCmd{
id: id,
userID: parts[2],
stock: parts[3],
amount: amount,
}
}
func (sba setBuyAmountCmd) Name() string {
return fmt.Sprintf("[%d] SET_BUY_AMOUNT", sba.id)
}
func (sba setBuyAmountCmd) ToAuditEvent() types.AuditEvent {
xmlElement := fmt.Sprintf(`
<userCommand>
<timestamp>%d</timestamp>
<server>%s</server>
<transactionNum>%d</transactionNum>
<command>SET_BUY_AMOUNT</command>
<username>%s</username>
<stockSymbol>%s</stockSymbol>
<funds>%.02f</funds>
</userCommand>`,
time.Now().UnixNano()/1e6, redisBaseKey, sba.id, sba.userID,
sba.stock, sba.amount.ToFloat(),
)
return types.AuditEvent{
UserID: sba.userID,
ID: sba.id,
EventType: "command",
Content: xmlElement,
}
}
func (sba setBuyAmountCmd) Execute() {
autoTxKey := types.AutoTxKey{
Stock: sba.stock,
UserID: sba.userID,
Action: "Buy",
}
_, found := workATXStore[autoTxKey]
if found {
// autoTx already exists, we'll need to cancel it.
autoTxCancelChan <- autoTxKey
}
workATXStore[autoTxKey] = types.AutoTxInit{
AutoTxKey: autoTxKey,
Amount: sba.amount,
WorkerID: *workerNum,
}
consoleLog.Notice(" [✔] Finished", sba.Name())
}