-
Notifications
You must be signed in to change notification settings - Fork 22
/
accounts.go
111 lines (93 loc) · 4.17 KB
/
accounts.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package service
import (
"context"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/utils"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/protos/vega"
)
type AccountStore interface {
GetByID(ctx context.Context, id entities.AccountID) (entities.Account, error)
GetAll(ctx context.Context) ([]entities.Account, error)
GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Account, error)
Obtain(ctx context.Context, a *entities.Account) error
Query(ctx context.Context, filter entities.AccountFilter) ([]entities.Account, error)
QueryBalances(ctx context.Context, filter entities.AccountFilter, pagination entities.CursorPagination) ([]entities.AccountBalance, entities.PageInfo, error)
GetBalancesByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.AccountBalance, error)
}
type BalanceStore interface {
Flush(ctx context.Context) ([]entities.AccountBalance, error)
Add(b entities.AccountBalance) error
Query(ctx context.Context, filter entities.AccountFilter, dateRange entities.DateRange, pagination entities.CursorPagination) (*[]entities.AggregatedBalance, entities.PageInfo, error)
}
type Account struct {
aStore AccountStore
bStore BalanceStore
bObserver utils.Observer[entities.AccountBalance]
}
func NewAccount(aStore AccountStore, bStore BalanceStore, log *logging.Logger) *Account {
return &Account{
aStore: aStore,
bStore: bStore,
bObserver: utils.NewObserver[entities.AccountBalance]("account_balance", log, 0, 0),
}
}
func (a *Account) GetByID(ctx context.Context, id entities.AccountID) (entities.Account, error) {
return a.aStore.GetByID(ctx, id)
}
func (a *Account) GetAll(ctx context.Context) ([]entities.Account, error) {
return a.aStore.GetAll(ctx)
}
func (a *Account) Obtain(ctx context.Context, acc *entities.Account) error {
return a.aStore.Obtain(ctx, acc)
}
func (a *Account) Query(ctx context.Context, filter entities.AccountFilter) ([]entities.Account, error) {
return a.aStore.Query(ctx, filter)
}
func (a *Account) QueryBalances(ctx context.Context, filter entities.AccountFilter, pagination entities.CursorPagination) ([]entities.AccountBalance, entities.PageInfo, error) {
return a.aStore.QueryBalances(ctx, filter, pagination)
}
func (a *Account) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Account, error) {
return a.aStore.GetByTxHash(ctx, txHash)
}
func (a *Account) GetBalancesByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.AccountBalance, error) {
return a.aStore.GetBalancesByTxHash(ctx, txHash)
}
func (a *Account) AddAccountBalance(b entities.AccountBalance) error {
return a.bStore.Add(b)
}
func (a *Account) Flush(ctx context.Context) error {
flushed, err := a.bStore.Flush(ctx)
if err != nil {
return err
}
a.bObserver.Notify(flushed)
return nil
}
func (a *Account) QueryAggregatedBalances(ctx context.Context, filter entities.AccountFilter, dateRange entities.DateRange, pagination entities.CursorPagination) (*[]entities.AggregatedBalance, entities.PageInfo, error) {
return a.bStore.Query(ctx, filter, dateRange, pagination)
}
func (a *Account) ObserveAccountBalances(ctx context.Context, retries int, marketID string,
partyID string, asset string, ty vega.AccountType,
) (accountCh <-chan []entities.AccountBalance, ref uint64) {
ch, ref := a.bObserver.Observe(ctx,
retries,
func(ab entities.AccountBalance) bool {
return (len(marketID) == 0 || marketID == ab.MarketID.String()) &&
(len(partyID) == 0 || partyID == ab.PartyID.String()) &&
(len(asset) == 0 || asset == ab.AssetID.String()) &&
(ty == vega.AccountType_ACCOUNT_TYPE_UNSPECIFIED || ty == ab.Type)
})
return ch, ref
}