-
Notifications
You must be signed in to change notification settings - Fork 0
/
hlf.go
105 lines (96 loc) · 2.9 KB
/
hlf.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
package ntesting
import (
"context"
"strconv"
"strings"
"github.com/anoideaopen/glog"
"github.com/anoideaopen/robot/hlf/sdkwrapper/wallet"
"github.com/pkg/errors"
)
// GetBalance returns balance of user.
func GetBalance(_ context.Context, user *wallet.User, channelName, chainCodeName string) (uint64, error) {
tokenBalanceResponse, err := user.QueryWithRetryIfEndorsementMismatch(channelName, chainCodeName, "balanceOf", user.Addr())
if err != nil {
return 0, errors.WithStack(err)
}
return ParseBalance(tokenBalanceResponse)
}
// ParseBalance parses chaincode response for balance query.
func ParseBalance(balanceResponse []byte) (uint64, error) {
tokenBalanceStr := strings.ReplaceAll(string(balanceResponse), "\"", "")
b, err := strconv.ParseUint(tokenBalanceStr, 10, 64)
if err != nil {
return 0, errors.WithStack(err)
}
return b, nil
}
func GetFiatOwner(_ context.Context, ciData CiTestData) (*wallet.User, error) {
u, err := wallet.NewUser(
ciData.HlfProfile.MspID,
ciData.HlfCert,
ciData.HlfSk,
ciData.HlfProfilePath,
"fiatOwner",
ciData.HlfFiatOwnerKey)
if err != nil {
return nil, errors.WithStack(err)
}
return u, nil
}
func GetCcOwner(_ context.Context, ciData CiTestData) (*wallet.User, error) {
u, err := wallet.NewUser(
ciData.HlfProfile.MspID,
ciData.HlfCert,
ciData.HlfSk,
ciData.HlfProfilePath,
"ccOwner",
ciData.HlfCcOwnerKey)
if err != nil {
return nil, errors.WithStack(err)
}
return u, nil
}
func GetIndustrialOwner(_ context.Context, ciData CiTestData) (*wallet.User, error) {
u, err := wallet.NewUser(
ciData.HlfProfile.MspID,
ciData.HlfCert,
ciData.HlfSk,
ciData.HlfProfilePath,
"industrialOwner",
ciData.HlfIndustrialOwnerKey)
if err != nil {
return nil, errors.WithStack(err)
}
return u, nil
}
// CreateTestUser creates new user in ACL.
// It takes context, CiTestData and user ID.
// Returns new user and not nil err if user isn't created.
func CreateTestUser(_ context.Context, ciData CiTestData, userID string) (*wallet.User, error) {
u, err := wallet.NewUser(
ciData.HlfProfile.MspID,
ciData.HlfCert,
ciData.HlfSk,
ciData.HlfProfilePath,
userID,
"")
if err != nil {
return nil, errors.WithStack(err)
}
return u, nil
}
// EmitFiat emits 'amount' fiats to the user 'u' using passed 'fiatOwner' in channel "ch" and chaincode "cc".
// Returns tx ID string and err (not nil) if emit operation failed.
// WARN calling this function several times during the formation of one block can lead to conflicts on some stands.
func EmitFiat(ctx context.Context, fiatOwner, u *wallet.User, amount uint64, ch, cc string) (string, error) {
l := glog.FromContext(ctx)
resp, err := fiatOwner.SignedInvoke(nil, ch, cc, "emit", u.Addr(), strconv.Itoa(int(amount)))
if err != nil {
return "", err
}
l.Infof("emitted for %s on %s-%s %v, txid: %s, bn: %v",
u.ID,
ch, cc,
amount, resp.Event.TxID, resp.Event.BlockNumber)
return resp.Event.TxID, nil
}