forked from LFDT-Panurus/panurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalance.go
More file actions
59 lines (46 loc) · 1.34 KB
/
Copy pathbalance.go
File metadata and controls
59 lines (46 loc) · 1.34 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package views
import (
"encoding/json"
"fmt"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
token2 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
)
type BalanceQuery struct {
TMSID token.TMSID
Wallet string
Type token2.Type
}
type Balance struct {
Type token2.Type
Quantity string
}
type BalanceView struct {
*BalanceQuery
}
func (b *BalanceView) Call(context view.Context) (any, error) {
tms, err := token.GetManagementService(context, token.WithTMSID(b.TMSID))
assert.NoError(err)
wallet, err := tms.WalletManager().OwnerWallet(context.Context(), b.Wallet)
if err != nil {
return nil, fmt.Errorf("wallet %s not found: %w", b.Wallet, err)
}
balance, err := wallet.Balance(context.Context(), token.WithBalanceTokenType(b.Type))
if err != nil {
return nil, err
}
return Balance{Quantity: balance.String(), 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
}