-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathvolumes.go
159 lines (130 loc) · 3.68 KB
/
volumes.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ledger
import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/invopop/jsonschema"
"math/big"
"strings"
)
type Volumes struct {
Input *big.Int `json:"input"`
Output *big.Int `json:"output"`
}
func (v Volumes) Value() (driver.Value, error) {
return fmt.Sprintf("(%s, %s)", v.Input.String(), v.Output.String()), nil
}
func (v *Volumes) Scan(src interface{}) error {
// stored as (input, output)
parts := strings.Split(src.(string)[1:(len(src.(string))-1)], ",")
v.Input = new(big.Int)
_, ok := v.Input.SetString(parts[0], 10)
if !ok {
return fmt.Errorf("unable to parse input '%s' as big int", parts[0])
}
v.Output = new(big.Int)
_, ok = v.Output.SetString(parts[1], 10)
if !ok {
return fmt.Errorf("unable to parse output '%s' as big int", parts[1])
}
return nil
}
func (Volumes) JSONSchemaExtend(schema *jsonschema.Schema) {
inputProperty, _ := schema.Properties.Get("input")
schema.Properties.Set("balance", inputProperty)
}
func (v Volumes) Copy() Volumes {
return Volumes{
Input: new(big.Int).Set(v.Input),
Output: new(big.Int).Set(v.Output),
}
}
func NewEmptyVolumes() Volumes {
return NewVolumesInt64(0, 0)
}
func NewVolumesInt64(input, output int64) Volumes {
return Volumes{
Input: big.NewInt(input),
Output: big.NewInt(output),
}
}
type VolumesWithBalanceByAssetByAccount struct {
Account string `json:"account" bun:"account"`
Asset string `json:"asset" bun:"asset"`
VolumesWithBalance
}
type VolumesWithBalance struct {
Input *big.Int `json:"input" bun:"input"`
Output *big.Int `json:"output" bun:"output"`
Balance *big.Int `json:"balance" bun:"balance"`
}
type VolumesWithBalanceByAssets map[string]*VolumesWithBalance
func (v Volumes) MarshalJSON() ([]byte, error) {
return json.Marshal(VolumesWithBalance{
Input: v.Input,
Output: v.Output,
Balance: v.Balance(),
})
}
func (v Volumes) Balance() *big.Int {
return new(big.Int).Sub(v.Input, v.Output)
}
func (v Volumes) copy() Volumes {
return Volumes{
Input: new(big.Int).Set(v.Input),
Output: new(big.Int).Set(v.Output),
}
}
type BalancesByAssets map[string]*big.Int
type VolumesByAssets map[string]Volumes
type BalancesByAssetsByAccounts map[string]BalancesByAssets
func (v VolumesByAssets) Balances() BalancesByAssets {
balances := BalancesByAssets{}
for asset, vv := range v {
balances[asset] = new(big.Int).Sub(vv.Input, vv.Output)
}
return balances
}
func (v VolumesByAssets) copy() VolumesByAssets {
ret := VolumesByAssets{}
for key, volumes := range v {
ret[key] = volumes.copy()
}
return ret
}
type PostCommitVolumes map[string]VolumesByAssets
func (a PostCommitVolumes) AddInput(account, asset string, input *big.Int) {
volumes := a[account][asset].Copy()
volumes.Input.Add(volumes.Input, input)
a[account][asset] = volumes
}
func (a PostCommitVolumes) AddOutput(account, asset string, output *big.Int) {
volumes := a[account][asset].Copy()
volumes.Output.Add(volumes.Output, output)
a[account][asset] = volumes
}
func (a PostCommitVolumes) Copy() PostCommitVolumes {
ret := PostCommitVolumes{}
for key, volumes := range a {
ret[key] = volumes.copy()
}
return ret
}
func (a PostCommitVolumes) Merge(volumes PostCommitVolumes) PostCommitVolumes {
for account, volumesByAssets := range volumes {
if _, ok := a[account]; !ok {
a[account] = map[string]Volumes{}
}
for asset, volumes := range volumesByAssets {
if _, ok := a[account][asset]; !ok {
a[account][asset] = NewEmptyVolumes()
}
a[account][asset].Input.Add(a[account][asset].Input, volumes.Input)
a[account][asset].Output.Add(a[account][asset].Output, volumes.Output)
}
}
return a
}
type AggregatedVolumes struct {
Aggregated VolumesByAssets `bun:"aggregated,type:jsonb"`
}