-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoTxFilled.go
More file actions
56 lines (45 loc) · 1.18 KB
/
autoTxFilled.go
File metadata and controls
56 lines (45 loc) · 1.18 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
package types
import (
"errors"
"fmt"
"strings"
"strconv"
currency "github.com/distributeddesigns/currency"
)
// AutoTxFilled : Request for auto transaction init
type AutoTxFilled struct {
AutoTxKey AutoTxKey
AddFunds currency.Currency
AddStocks uint
}
// ToCSV : Converts the QuoteRequest to a csv
func (aTx *AutoTxFilled) ToCSV() string {
parts := make([]string, 2)
parts[0] = fmt.Sprintf("%0.2f", aTx.AddFunds.ToFloat())
parts[1] = fmt.Sprintf("%d", aTx.AddStocks)
return fmt.Sprintf("%s,%s", aTx.AutoTxKey.ToCSV(), strings.Join(parts, ","))
}
// ParseAutoTxFilled : Parses CSV as QuoteRequest
func ParseAutoTxFilled(csv string) (AutoTxFilled, error) {
parts := strings.Split(csv, ",")
if len(parts) != 5 {
return AutoTxFilled{}, errors.New("Expected number of values in AutoTxFilled csv")
}
currAmount, err := currency.NewFromString(parts[3])
if err != nil {
return AutoTxFilled{}, err
}
numStocks, err := strconv.ParseUint(parts[4], 10, 64)
if err != nil {
return AutoTxFilled{}, err
}
return AutoTxFilled{
AutoTxKey: AutoTxKey{
Stock: parts[0],
UserID: parts[1],
Action: parts[2],
},
AddFunds: currAmount,
AddStocks: uint(numStocks),
}, nil
}