-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmargin_acitons.go
112 lines (107 loc) · 3.74 KB
/
margin_acitons.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
package main
import (
binance "github.com/adshao/go-binance/v2"
"github.com/juju/errors"
"github.com/shopspring/decimal"
"gopkg.in/urfave/cli.v1"
)
// MarginAccount define margin account
type MarginAccount struct {
Name string
Margin *binance.MarginAccount `json:"margin"`
}
// MarginAssets get assets of margin account
func (account *MarginAccount) MarginAssets() map[string]binance.UserAsset {
m := make(map[string]binance.UserAsset)
if account.Margin == nil {
return m
}
for _, userAsset := range account.Margin.UserAssets {
m[userAsset.Asset] = userAsset
}
return m
}
func listMarginBalances(c *cli.Context) error {
assets := c.StringSlice("assets")
total := c.Bool("total")
filterBorrowed := c.Bool("borrowed")
return accountsDo(func(account *Account) (interface{}, error) {
marginAccount, err := account.GetMarginAccount()
if err != nil {
return nil, errors.Trace(err)
}
a := &MarginAccount{Name: account.Name, Margin: marginAccount}
marginAssets := a.MarginAssets()
var l []binance.UserAsset
if len(assets) > 0 {
var insertedKeys []string
for _, asset := range assets {
if b, ok := marginAssets[asset]; ok && !StrContains(insertedKeys, asset) {
if !c.IsSet("borrowed") ||
(filterBorrowed && decimal.RequireFromString(b.Borrowed).GreaterThan(decimal.RequireFromString("0"))) ||
(!filterBorrowed && decimal.RequireFromString(b.Borrowed).Equal(decimal.RequireFromString("0"))) {
l = append(l, b)
insertedKeys = append(insertedKeys, asset)
}
}
}
} else {
for _, b := range marginAssets {
if !c.IsSet("borrowed") ||
(filterBorrowed && decimal.RequireFromString(b.Borrowed).GreaterThan(decimal.RequireFromString("0"))) ||
(!filterBorrowed && decimal.RequireFromString(b.Borrowed).Equal(decimal.RequireFromString("0"))) {
l = append(l, b)
}
}
}
a.Margin.UserAssets = l
return a, nil
}, func(results map[string]interface{}) (interface{}, error) {
if !total {
return results, nil
}
totalAssetOfBTC := decimal.Decimal{}
totalLiabilityOfBTC := decimal.Decimal{}
totalNetAssetOfBTC := decimal.Decimal{}
m := make(map[string]map[string]decimal.Decimal)
for _, res := range results {
account, ok := res.(*MarginAccount)
if !ok {
continue
}
totalAssetOfBTC = totalAssetOfBTC.Add(decimal.RequireFromString(account.Margin.TotalAssetOfBTC))
totalLiabilityOfBTC = totalLiabilityOfBTC.Add(decimal.RequireFromString(account.Margin.TotalLiabilityOfBTC))
totalNetAssetOfBTC = totalNetAssetOfBTC.Add(decimal.RequireFromString(account.Margin.TotalNetAssetOfBTC))
for _, asset := range account.Margin.UserAssets {
borrowed := decimal.RequireFromString(asset.Borrowed)
free := decimal.RequireFromString(asset.Free)
interest := decimal.RequireFromString(asset.Interest)
locked := decimal.RequireFromString(asset.Locked)
netAsset := decimal.RequireFromString(asset.NetAsset)
userAsset, ok := m[asset.Asset]
if !ok {
userAsset = map[string]decimal.Decimal{
"borrowed": decimal.Decimal{},
"free": decimal.Decimal{},
"interest": decimal.Decimal{},
"locked": decimal.Decimal{},
"netAsset": decimal.Decimal{},
}
m[asset.Asset] = userAsset
}
userAsset["borrowed"] = userAsset["borrowed"].Add(borrowed)
userAsset["free"] = userAsset["free"].Add(free)
userAsset["interest"] = userAsset["interest"].Add(interest)
userAsset["locked"] = userAsset["locked"].Add(locked)
userAsset["netAsset"] = userAsset["netAsset"].Add(netAsset)
}
}
res := map[string]interface{}{
"TotalAssetOfBTC": totalAssetOfBTC,
"TotalLiabilityOfBTC": totalLiabilityOfBTC,
"TotalNetAssetOfBTC": totalNetAssetOfBTC,
"UserAssets": m,
}
return []interface{}{results, res}, nil
})
}