-
Notifications
You must be signed in to change notification settings - Fork 0
/
balances.go
184 lines (167 loc) · 7.14 KB
/
balances.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package utils
import (
"strconv"
"time"
"github.com/ozontech/allure-go/pkg/framework/provider"
)
// EmitGetTxIDAndCheckBalance emits amount of tokens to userAddressBase58Check and checks that balance is equal to amount
func EmitGetTxIDAndCheckBalance(
t provider.T,
hlfProxy HlfProxyService,
userAddressBase58Check string,
issuer Issuer,
channel string,
chaincode string,
amount string,
) string {
var txID string
t.WithNewStep("Emit "+amount+" token to user "+userAddressBase58Check+" and get txId", func(sCtx provider.StepCtx) {
emitArgs := []string{userAddressBase58Check, amount}
signedEmitArgs, err := Sign(issuer.IssuerEd25519PrivateKey, issuer.IssuerEd25519PublicKey, channel, chaincode, "emit", emitArgs)
sCtx.Require().NoError(err)
res, err := hlfProxy.Invoke(channel, "emit", signedEmitArgs...)
sCtx.Require().NoError(err)
time.Sleep(BatchTransactionTimeout)
txID = res.TransactionID
})
CheckBalanceEqual(t, hlfProxy, userAddressBase58Check, channel, amount)
return txID
}
// EmitGetResponseAndCheckBalance emits amount of tokens to userAddressBase58Check and checks that balance is equal to amount
func EmitGetResponseAndCheckBalance(
t provider.T,
hlfProxy HlfProxyService,
userAddressBase58Check string,
iss Issuer,
channel string,
chaincode string,
amount string,
) *Response {
var res *Response
t.WithNewStep("Emit "+amount+" token to user "+userAddressBase58Check+" and get txId", func(sCtx provider.StepCtx) {
emitArgs := []string{userAddressBase58Check, amount}
signedEmitArgs, err := Sign(iss.IssuerEd25519PrivateKey, iss.IssuerEd25519PublicKey, channel, chaincode, "emit", emitArgs)
sCtx.Require().NoError(err)
res, err = hlfProxy.Invoke(channel, "emit", signedEmitArgs...)
sCtx.Require().NoError(err)
time.Sleep(BatchTransactionTimeout)
})
CheckBalanceEqual(t, hlfProxy, userAddressBase58Check, channel, amount)
return res
}
// CheckBalanceEqual checks that balance of userAddressBase58Check is equal to amount
func CheckBalanceEqual(t provider.T, hlfProxy HlfProxyService, userAddressBase58Check string, channel string, amount string) {
t.WithNewStep("Checking that balance equal "+amount, func(sCtx provider.StepCtx) {
respGetBalance, err := hlfProxy.Query(channel, "balanceOf", userAddressBase58Check)
sCtx.Require().NoError(err)
sCtx.Require().Equal("\""+amount+"\"", string(respGetBalance.Payload))
})
}
// CheckAllowedBalanceEqual checks that allowed balance of userAddressBase58Check is equal to amount
func CheckAllowedBalanceEqual(t provider.T, hlfProxy HlfProxyService, userAddressBase58Check string, channel string, tokenUppercase string, amount string) {
t.WithNewStep("Checking that allowed balance equal "+amount, func(sCtx provider.StepCtx) {
resp, err := hlfProxy.Query(channel, "allowedBalanceOf", userAddressBase58Check, tokenUppercase)
sCtx.Require().NoError(err)
sCtx.Require().Equal("\""+amount+"\"", string(resp.Payload))
})
}
// CheckBalanceEqualWithRetry checks that balance of userAddressBase58Check is equal to amount with retries
func CheckBalanceEqualWithRetry(t provider.T, hlfProxy HlfProxyService, userAddressBase58Check string, channel string, amount string, sleep time.Duration, retries int) {
t.WithNewStep("Checking that balance equal "+amount+" with retry", func(sCtx provider.StepCtx) {
i := 0
for i < retries {
respGetBalance, err := hlfProxy.Query(channel, "balanceOf", userAddressBase58Check)
t.Require().NoError(err)
if string(respGetBalance.Payload) == "\""+amount+"\"" {
return
}
t.LogStep("attempt number is " + strconv.Itoa(i+1))
i++
time.Sleep(sleep)
}
t.LogStep("failed to get expected balance " + amount)
t.FailNow()
})
}
// TransferCheckBalanceAndGetRespose transfers amount of tokens from userFrom to userToAddress and checks that balance of userToAddress is equal to amount
func TransferCheckBalanceAndGetRespose(
t provider.T,
hlfProxy HlfProxyService,
userFrom User,
userToAddress string,
channel string,
chaincode string,
amount string,
) *Response {
var (
signedTransferArgs []string
err error
resTransfer *Response
)
t.WithNewStep("Sign arguments before emission process", func(sCtx provider.StepCtx) {
ref := "ref transfer"
transferArgs := []string{userToAddress, amount, ref}
signedTransferArgs, err = Sign(userFrom.UserEd25519PrivateKey, userFrom.UserEd25519PublicKey, channel, chaincode, "transfer", transferArgs)
sCtx.Require().NoError(err)
})
t.WithNewStep("Invoke fiat chaincode by issuer for token emission", func(sCtx provider.StepCtx) {
resTransfer, err = hlfProxy.Invoke(channel, "transfer", signedTransferArgs...)
sCtx.Require().NoError(err)
time.Sleep(BatchTransactionTimeout)
})
CheckBalanceEqual(t, hlfProxy, userToAddress, channel, amount)
return resTransfer
}
// GetEmitPayload emits amount of tokens to userAddressBase58Check and checks that balance is equal to amount
func GetEmitPayload(
t provider.T,
hlfProxy HlfProxyService,
userAddressBase58Check string,
issuer Issuer,
amount string,
) string {
var txID string
t.WithNewStep("Emit "+amount+" token to user "+userAddressBase58Check+" and get txId", func(sCtx provider.StepCtx) {
emitArgs := []string{userAddressBase58Check, amount}
signedEmitArgs, err := Sign(issuer.IssuerEd25519PrivateKey, issuer.IssuerEd25519PublicKey, "inv", "inv", "emit", emitArgs)
sCtx.Require().NoError(err)
res, err := hlfProxy.Invoke("fiat", "emit", signedEmitArgs...)
sCtx.Require().NoError(err)
time.Sleep(BatchTransactionTimeout)
txID = res.TransactionID
})
CheckBalanceEqual(t, hlfProxy, userAddressBase58Check, "fiat", amount)
return txID
}
// SwapFiatToCCCheckBalanceAndGetSwapDoneAndSwapBeginTxID swaps amount of tokens from fiat to cc channel
func SwapFiatToCCCheckBalanceAndGetSwapDoneAndSwapBeginTxID(t provider.T, hlfProxy HlfProxyService, user User, amount string) (string, string) {
var (
swapBeginTxID string
swapDoneTxID string
)
t.WithNewStep("Swap between channels", func(sCtx provider.StepCtx) {
swapBeginArgs := []string{"FIAT", "CC", amount, DefaultSwapHash}
signedSwapBeginArgs, err := Sign(user.UserEd25519PrivateKey, user.UserEd25519PublicKey, "fiat", "fiat", "swapBegin", swapBeginArgs)
sCtx.Assert().NoError(err)
swapBeginResp, err := hlfProxy.Invoke("fiat", "swapBegin", signedSwapBeginArgs...)
swapBeginTxID = swapBeginResp.TransactionID
sCtx.Assert().NoError(err)
time.Sleep(BatchTransactionTimeout)
sCtx.NewStep("swapGet txID in fiat channel")
_, err = hlfProxy.Query("fiat", "swapGet", swapBeginResp.TransactionID)
sCtx.Assert().NoError(err)
sCtx.NewStep("swapGet txID in cc channel")
_, err = hlfProxy.Query("cc", "swapGet", swapBeginResp.TransactionID)
sCtx.Assert().NoError(err)
sCtx.NewStep("swapDone")
swapDoneResp, err := hlfProxy.Invoke("cc", "swapDone", swapBeginResp.TransactionID, DefaultSwapKey)
swapDoneTxID = swapDoneResp.TransactionID
sCtx.Assert().NoError(err)
time.Sleep(BatchTransactionTimeout)
sCtx.NewStep("Get allowed balance in cc channel")
respAllowedBalance, err := hlfProxy.Query("cc", "allowedBalanceOf", user.UserAddressBase58Check, "FIAT")
sCtx.Assert().NoError(err)
sCtx.Require().Equal("\"1\"", string(respAllowedBalance.Payload))
})
return swapBeginTxID, swapDoneTxID
}