-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_balance_test.go
194 lines (151 loc) · 5.88 KB
/
query_balance_test.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
185
186
187
188
189
190
191
192
193
194
package steps
import (
"context"
"errors"
"github.com/cucumber/godog"
"github.com/make-software/casper-go-sdk/casper"
"github.com/make-software/casper-go-sdk/rpc"
"github.com/make-software/casper-go-sdk/types"
"math/big"
"testing"
"github.com/casper-sdks/terminus-go-tests/tests/utils"
)
// Step Definitions for the query_balance.feature
func TestFeaturesQueryGetBalance(t *testing.T) {
utils.TestFeatures(t, "query_balance.feature", InitializeQueryGetBalance)
}
func InitializeQueryGetBalance(ctx *godog.ScenarioContext) {
var sdk casper.RPCClient
var queryGetBalanceResult rpc.QueryBalanceResult
var initialBalanceResult rpc.QueryBalanceResult
var queryGetBalanceJson string
var transferAmount int64
var deploy *types.Deploy
ctx.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) {
utils.ReadConfig()
sdk = utils.GetRPCClient()
return ctx, nil
})
ctx.Step(`^that a query balance is obtained by main purse public key$`, func() error {
faucetKey, err := casper.NewED25519PrivateKeyFromPEMFile("../../assets/net-1/faucet/secret_key.pem")
if err == nil {
publicKey := faucetKey.PublicKey()
identifier := rpc.PurseIdentifier{
MainPurseUnderPublicKey: &publicKey,
}
queryGetBalanceResult, err = sdk.QueryBalance(context.Background(), identifier)
if err == nil {
queryGetBalanceJson, err = utils.QueryBalance("main_purse_under_public_key", publicKey.ToHex())
}
}
return err
})
ctx.Step(`^a valid query_balance_result is returned$`, func() error {
if len(queryGetBalanceJson) == 0 {
return errors.New("Missing queryGetBalanceJson")
}
return utils.Pass
})
ctx.Step(`^the query_balance_result has an API version of "([^"]*)"$`, func(apiVersion string) error {
return utils.ExpectEqual(utils.CasperT, "api version", queryGetBalanceResult.ApiVersion, apiVersion)
})
ctx.Step(`^the query_balance_result has a valid balance$`, func() error {
balance, err := utils.GetByJsonPath(queryGetBalanceJson, "/result/balance")
if err == nil {
actual := queryGetBalanceResult.Balance.Value()
expected, _ := new(big.Int).SetString(balance, 10)
return utils.ExpectEqual(utils.CasperT, "balance", actual, expected)
}
return err
})
ctx.Step(`^that a query balance is obtained by main purse account hash$`, func() error {
faucetKey, err := casper.NewED25519PrivateKeyFromPEMFile("../../assets/net-1/faucet/secret_key.pem")
if err == nil {
accountHash := "account-hash-" + faucetKey.PublicKey().AccountHash().ToHex()
identifier := rpc.PurseIdentifier{
MainPurseUnderAccountHash: &accountHash,
}
queryGetBalanceResult, err = sdk.QueryBalance(context.Background(), identifier)
if err == nil {
queryGetBalanceJson, err = utils.QueryBalance("main_purse_under_account_hash", accountHash)
}
}
return err
})
ctx.Step(`^that a query balance is obtained by main purse uref$`, func() error {
var latest rpc.ChainGetBlockResult
var accountInfo rpc.StateGetAccountInfo
faucetKey, err := casper.NewED25519PrivateKeyFromPEMFile("../../assets/net-1/faucet/secret_key.pem")
if err == nil {
latest, err = sdk.GetBlockLatest(context.Background())
}
if err == nil {
accountInfo, err = sdk.GetAccountInfoByBlochHash(context.Background(), latest.Block.Hash.String(), faucetKey.PublicKey())
}
if err == nil {
purseUref := accountInfo.Account.MainPurse.String()
identifier := rpc.PurseIdentifier{
PurseUref: &purseUref,
}
queryGetBalanceResult, err = sdk.QueryBalance(context.Background(), identifier)
if err == nil {
queryGetBalanceJson, err = utils.QueryBalance("purse_uref", purseUref)
}
}
return err
})
ctx.Step(`^a transfer of (\d+) is made to user-(\d+)'s purse$`, func(amount int64, userId int) error {
transferAmount = amount
faucetKey, err := casper.NewED25519PrivateKeyFromPEMFile("../../assets/net-1/faucet/secret_key.pem")
receiverKey := utils.GetUserPublicKey(userId)
if err == nil {
publicKey := receiverKey
identifier := rpc.PurseIdentifier{
MainPurseUnderPublicKey: &publicKey,
}
initialBalanceResult, err = sdk.QueryBalance(context.Background(), identifier)
}
if err != nil {
return err
}
deploy, err = utils.BuildSimpleTransferDeploy(faucetKey, receiverKey, amount)
if err != nil {
return err
}
result, err := sdk.PutDeploy(context.Background(), *deploy)
if err != nil {
return err
}
_, err = utils.WaitForDeploy(result.DeployHash.String(), 300)
return err
})
ctx.Step(`^that a query balance is obtained by user-(\d+)'s main purse public and latest block identifier$`, func(userId int) error {
var err error
userKey := utils.GetUserPublicKey(userId)
identifier := rpc.PurseIdentifier{
MainPurseUnderPublicKey: &userKey,
}
queryGetBalanceResult, err = sdk.QueryBalance(context.Background(), identifier)
return err
})
ctx.Step(`^the balance includes the transferred amount$`, func() error {
actual := queryGetBalanceResult.Balance.Value()
expected := big.NewInt(0).Add(big.NewInt(transferAmount), initialBalanceResult.Balance.Value())
return utils.ExpectEqual(utils.CasperT, "balance", actual, expected)
})
ctx.Step(`^that a query balance is obtained by user-(\d+)'s main purse public key and previous block identifier$`, func(userId int) error {
// Needs the state_identifier option implemented in the SDK
return utils.NotImplementError
})
ctx.Step(`^the balance is the pre transfer amount$`, func() error {
return utils.NotImplementError
})
ctx.Step(`^that a query balance is obtained by user-1's main purse public and latest state root hash identifier$`, func() error {
// Needs the state_identifier option implemented in the SDK
return utils.NotImplementError
})
ctx.Step(`^that a query balance is obtained by user-1's main purse public key and previous state root hash identifier$`, func() error {
// Needs the state_identifier option implemented in the SDK
return utils.NotImplementError
})
}