github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_records_query_e2e_test.go (about) 1 //go:build all || e2e 2 // +build all e2e 3 4 package hedera 5 6 /*- 7 * 8 * Hedera Go SDK 9 * 10 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 */ 25 26 import ( 27 "testing" 28 29 "github.com/stretchr/testify/assert" 30 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestIntegrationAccountRecordQueryCanExecute(t *testing.T) { 35 t.Parallel() 36 env := NewIntegrationTestEnv(t) 37 38 newKey, err := PrivateKeyGenerateEd25519() 39 require.NoError(t, err) 40 41 resp, err := NewAccountCreateTransaction(). 42 SetKey(newKey.PublicKey()). 43 SetNodeAccountIDs(env.NodeAccountIDs). 44 SetInitialBalance(NewHbar(1)). 45 Execute(env.Client) 46 require.NoError(t, err) 47 48 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 49 require.NoError(t, err) 50 51 account := *receipt.AccountID 52 53 _, err = NewTransferTransaction(). 54 SetNodeAccountIDs([]AccountID{resp.NodeID}). 55 AddHbarTransfer(account, NewHbar(1)). 56 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)). 57 Execute(env.Client) 58 require.NoError(t, err) 59 60 recordsQuery, err := NewAccountRecordsQuery(). 61 SetNodeAccountIDs([]AccountID{resp.NodeID}). 62 SetAccountID(env.Client.GetOperatorAccountID()). 63 SetMaxQueryPayment(NewHbar(1)). 64 SetQueryPayment(HbarFromTinybar(25)). 65 Execute(env.Client) 66 require.NoError(t, err) 67 68 assert.Equal(t, 1, len(recordsQuery)) 69 70 err = CloseIntegrationTestEnv(env, nil) 71 require.NoError(t, err) 72 } 73 74 func TestIntegrationAccountRecordQueryGetCost(t *testing.T) { 75 t.Parallel() 76 env := NewIntegrationTestEnv(t) 77 78 newKey, err := PrivateKeyGenerateEd25519() 79 require.NoError(t, err) 80 81 resp, err := NewAccountCreateTransaction(). 82 SetKey(newKey.PublicKey()). 83 SetNodeAccountIDs(env.NodeAccountIDs). 84 SetInitialBalance(NewHbar(1)). 85 Execute(env.Client) 86 require.NoError(t, err) 87 88 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 89 require.NoError(t, err) 90 91 account := *receipt.AccountID 92 93 _, err = NewTransferTransaction(). 94 SetNodeAccountIDs([]AccountID{resp.NodeID}). 95 AddHbarTransfer(account, NewHbar(1)). 96 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)). 97 Execute(env.Client) 98 require.NoError(t, err) 99 100 records := NewAccountRecordsQuery(). 101 SetNodeAccountIDs([]AccountID{resp.NodeID}). 102 SetAccountID(env.Client.GetOperatorAccountID()) 103 104 cost, err := records.GetCost(env.Client) 105 require.NoError(t, err) 106 107 recordsQuery, err := records.SetQueryPayment(cost).Execute(env.Client) 108 require.NoError(t, err) 109 110 assert.Equal(t, 1, len(recordsQuery)) 111 112 err = CloseIntegrationTestEnv(env, nil) 113 require.NoError(t, err) 114 } 115 116 func TestIntegrationAccountRecordQuerySetBigMaxPayment(t *testing.T) { 117 t.Parallel() 118 env := NewIntegrationTestEnv(t) 119 120 newKey, err := PrivateKeyGenerateEd25519() 121 require.NoError(t, err) 122 123 resp, err := NewAccountCreateTransaction(). 124 SetKey(newKey.PublicKey()). 125 SetNodeAccountIDs(env.NodeAccountIDs). 126 SetInitialBalance(NewHbar(1)). 127 Execute(env.Client) 128 require.NoError(t, err) 129 130 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 131 require.NoError(t, err) 132 133 account := *receipt.AccountID 134 135 _, err = NewTransferTransaction(). 136 SetNodeAccountIDs([]AccountID{resp.NodeID}). 137 AddHbarTransfer(account, NewHbar(1)). 138 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)). 139 Execute(env.Client) 140 require.NoError(t, err) 141 142 records := NewAccountRecordsQuery(). 143 SetNodeAccountIDs([]AccountID{resp.NodeID}). 144 SetAccountID(env.Client.GetOperatorAccountID()). 145 SetQueryPayment(NewHbar(10)). 146 SetMaxQueryPayment(NewHbar(100000)) 147 148 _, err = records.GetCost(env.Client) 149 require.NoError(t, err) 150 151 recordsQuery, err := records.Execute(env.Client) 152 require.NoError(t, err) 153 154 assert.Equal(t, 1, len(recordsQuery)) 155 156 err = CloseIntegrationTestEnv(env, nil) 157 require.NoError(t, err) 158 } 159 160 func TestIntegrationAccountRecordQuerySetSmallMaxPayment(t *testing.T) { 161 t.Parallel() 162 env := NewIntegrationTestEnv(t) 163 164 newKey, err := PrivateKeyGenerateEd25519() 165 require.NoError(t, err) 166 167 resp, err := NewAccountCreateTransaction(). 168 SetKey(newKey.PublicKey()). 169 SetNodeAccountIDs(env.NodeAccountIDs). 170 SetInitialBalance(NewHbar(1)). 171 Execute(env.Client) 172 require.NoError(t, err) 173 174 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 175 require.NoError(t, err) 176 177 account := *receipt.AccountID 178 179 _, err = NewTransferTransaction(). 180 SetNodeAccountIDs([]AccountID{resp.NodeID}). 181 AddHbarTransfer(account, NewHbar(1)). 182 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)). 183 Execute(env.Client) 184 require.NoError(t, err) 185 186 records := NewAccountRecordsQuery(). 187 SetNodeAccountIDs([]AccountID{resp.NodeID}). 188 SetAccountID(env.Client.GetOperatorAccountID()). 189 SetMaxQueryPayment(HbarFromTinybar(1)) 190 191 cost, err := records.GetCost(env.Client) 192 require.NoError(t, err) 193 194 _, err = records.Execute(env.Client) 195 assert.Error(t, err) 196 if err != nil { 197 assert.Equal(t, "cost of AccountRecordsQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tℏ", err.Error()) 198 } 199 200 err = CloseIntegrationTestEnv(env, nil) 201 require.NoError(t, err) 202 } 203 204 func TestIntegrationAccountRecordQueryInsufficientFee(t *testing.T) { 205 t.Parallel() 206 env := NewIntegrationTestEnv(t) 207 208 newKey, err := PrivateKeyGenerateEd25519() 209 require.NoError(t, err) 210 211 resp, err := NewAccountCreateTransaction(). 212 SetKey(newKey.PublicKey()). 213 SetNodeAccountIDs(env.NodeAccountIDs). 214 SetInitialBalance(NewHbar(1)). 215 Execute(env.Client) 216 require.NoError(t, err) 217 218 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 219 require.NoError(t, err) 220 221 account := *receipt.AccountID 222 223 _, err = NewTransferTransaction(). 224 SetNodeAccountIDs([]AccountID{resp.NodeID}). 225 AddHbarTransfer(account, NewHbar(1)). 226 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)). 227 Execute(env.Client) 228 require.NoError(t, err) 229 230 records := NewAccountRecordsQuery(). 231 SetNodeAccountIDs([]AccountID{resp.NodeID}). 232 SetAccountID(env.Client.GetOperatorAccountID()) 233 234 _, err = records.GetCost(env.Client) 235 require.NoError(t, err) 236 237 _, err = records.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client) 238 assert.Error(t, err) 239 if err != nil { 240 assert.Equal(t, "exceptional precheck status INSUFFICIENT_TX_FEE", err.Error()) 241 } 242 243 err = CloseIntegrationTestEnv(env, nil) 244 require.NoError(t, err) 245 } 246 247 func TestIntegrationAccountRecordQueryNoAccountID(t *testing.T) { 248 t.Parallel() 249 env := NewIntegrationTestEnv(t) 250 251 _, err := NewAccountRecordsQuery(). 252 SetNodeAccountIDs(env.NodeAccountIDs). 253 Execute(env.Client) 254 assert.Error(t, err) 255 if err != nil { 256 assert.Equal(t, "exceptional precheck status INVALID_ACCOUNT_ID", err.Error()) 257 } 258 259 err = CloseIntegrationTestEnv(env, nil) 260 require.NoError(t, err) 261 }