-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathbalance.go
More file actions
134 lines (111 loc) · 3.9 KB
/
Copy pathbalance.go
File metadata and controls
134 lines (111 loc) · 3.9 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
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package views
import (
"encoding/json"
"fmt"
"github.com/LFDT-Panurus/panurus/token"
"github.com/LFDT-Panurus/panurus/token/services/ttx/multisig"
token2 "github.com/LFDT-Panurus/panurus/token/token"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections/iterators"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)
type BalanceQuery struct {
TMSID *token.TMSID
SkipCheck bool
Wallet string
Type token2.Type
}
type Balance struct {
Type token2.Type
Quantity string
CoOwned string
}
type BalanceView struct {
*BalanceQuery
}
func (b *BalanceView) Call(context view.Context) (any, error) {
tms, err := token.GetManagementService(context, ServiceOpts(b.TMSID)...)
assert.NoError(err, "failed getting management service")
assert.NotNil(tms, "TMSID is nil")
wallet, err := tms.WalletManager().OwnerWallet(context.Context(), b.Wallet)
if err != nil {
return nil, fmt.Errorf("wallet %s not found: %w", b.Wallet, err)
}
unspentTokens, err := wallet.ListUnspentTokensIterator(context.Context(), token.WithType(b.Type))
if err != nil {
return nil, errors.Wrapf(err, "failed listing unspent tokens")
}
precision := tms.PublicParametersManager().PublicParameters().Precision()
sum, err := iterators.Reduce(unspentTokens.UnspentTokensIterator, token2.ToQuantitySum(precision))
if err != nil {
return nil, err
}
// co-owned
multisigWallet := multisig.Wallet(context, wallet)
coOwnedTokens, err := multisigWallet.ListTokensIterator(context.Context(), token.WithType(b.Type))
assert.NoError(err, "failed to get co-owned tokens")
coOwned, err := iterators.Reduce(coOwnedTokens, token2.ToQuantitySum(precision))
assert.NoError(err, "failed to compute the sum of the co-owned tokens")
if !b.SkipCheck {
balance, err := wallet.Balance(context.Context(), token.WithType(b.Type))
if err != nil {
return nil, err
}
if sum.ToBigInt().Cmp(balance) != 0 {
return nil, errors.Errorf("balance doesn't match [%s]!=[%s]", balance.String(), sum.Decimal())
}
}
return Balance{
Quantity: sum.Decimal(),
CoOwned: coOwned.Decimal(),
Type: b.Type,
}, nil
}
type BalanceViewFactory struct{}
func (g *BalanceViewFactory) NewView(in []byte) (view.View, error) {
f := &BalanceView{BalanceQuery: &BalanceQuery{}}
if err := json.Unmarshal(in, f.BalanceQuery); err != nil {
return nil, err
}
return f, nil
}
type CoOwnedBalanceQuery struct {
TMSID *token.TMSID
Wallet string
Type token2.Type
}
type CoOwnedBalanceView struct {
*CoOwnedBalanceQuery
}
func (b *CoOwnedBalanceView) Call(context view.Context) (any, error) {
tms, err := token.GetManagementService(context, ServiceOpts(b.TMSID)...)
assert.NoError(err, "failed getting management service")
wallet, err := tms.WalletManager().OwnerWallet(context.Context(), b.Wallet)
if err != nil {
return nil, fmt.Errorf("wallet %s not found: %w", b.Wallet, err)
}
// co-owned
precision := tms.PublicParametersManager().PublicParameters().Precision()
multisigWallet := multisig.Wallet(context, wallet)
coOwnedTokens, err := multisigWallet.ListTokensIterator(context.Context(), token.WithType(b.Type))
assert.NoError(err, "failed to get co-owned tokens")
coOwned, err := iterators.Reduce(coOwnedTokens, token2.ToQuantitySum(precision))
assert.NoError(err, "failed to compute the sum of the co-owned tokens")
return Balance{
Quantity: coOwned.Decimal(),
Type: b.Type,
}, nil
}
type CoOwnedBalanceViewFactory struct{}
func (g *CoOwnedBalanceViewFactory) NewView(in []byte) (view.View, error) {
f := &CoOwnedBalanceView{CoOwnedBalanceQuery: &CoOwnedBalanceQuery{}}
if err := json.Unmarshal(in, f.CoOwnedBalanceQuery); err != nil {
return nil, err
}
return f, nil
}