github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_info_query_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 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 "encoding/base64" 28 "testing" 29 "time" 30 31 "github.com/hashgraph/hedera-protobufs-go/services" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitTokenInfoQueryValidate(t *testing.T) { 39 t.Parallel() 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 client.SetAutoValidateChecksums(true) 45 tokenID, err := TokenIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 48 tokenInfo := NewTokenInfoQuery(). 49 SetTokenID(tokenID) 50 51 err = tokenInfo.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitTokenInfoQueryValidateWrong(t *testing.T) { 56 t.Parallel() 57 58 client, err := _NewMockClient() 59 client.SetLedgerID(*NewLedgerIDTestnet()) 60 require.NoError(t, err) 61 client.SetAutoValidateChecksums(true) 62 tokenID, err := TokenIDFromString("0.0.123-rmkykd") 63 require.NoError(t, err) 64 65 tokenInfo := NewTokenInfoQuery(). 66 SetTokenID(tokenID) 67 68 err = tokenInfo.validateNetworkOnIDs(client) 69 assert.Error(t, err) 70 if err != nil { 71 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 72 } 73 } 74 75 func TestUnitTokenInfoFromBytesBadBytes(t *testing.T) { 76 t.Parallel() 77 78 bytes, err := base64.StdEncoding.DecodeString("tfhyY++/Q4BycortAgD4cmMKACB/") 79 require.NoError(t, err) 80 81 _, err = TokenInfoFromBytes(bytes) 82 require.NoError(t, err) 83 } 84 85 func TestUnitTokenInfoFromBytesEmptyBytes(t *testing.T) { 86 t.Parallel() 87 88 _, err := TokenInfoFromBytes([]byte{}) 89 require.NoError(t, err) 90 } 91 92 func TestUnitTokenInfoQueryGet(t *testing.T) { 93 t.Parallel() 94 95 tokenID := TokenID{Token: 7} 96 deadline := time.Duration(time.Minute) 97 accountId := AccountID{Account: 123} 98 validStart := time.Now().Add(10 * time.Minute) 99 balance := NewTokenInfoQuery(). 100 SetTokenID(tokenID). 101 SetQueryPayment(NewHbar(2)). 102 SetMaxQueryPayment(NewHbar(1)). 103 SetQueryPayment(HbarFromTinybar(25)). 104 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}). 105 SetMaxRetry(3). 106 SetMinBackoff(300 * time.Millisecond). 107 SetMaxBackoff(10 * time.Second). 108 SetPaymentTransactionID(TransactionID{AccountID: &accountId, ValidStart: &validStart}). 109 SetMaxQueryPayment(NewHbar(500)). 110 SetGrpcDeadline(&deadline) 111 112 require.Equal(t, tokenID, balance.GetTokenID()) 113 require.Equal(t, []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}, balance.GetNodeAccountIDs()) 114 require.Equal(t, 300*time.Millisecond, balance.GetMinBackoff()) 115 require.Equal(t, 10*time.Second, balance.GetMaxBackoff()) 116 require.Equal(t, 3, balance.GetMaxRetryCount()) 117 require.Equal(t, TransactionID{AccountID: &accountId, ValidStart: &validStart}, balance.GetPaymentTransactionID()) 118 require.Equal(t, HbarFromTinybar(25), balance.GetQueryPayment()) 119 require.Equal(t, NewHbar(500), balance.GetMaxQueryPayment()) 120 } 121 122 func TestUnitTokenInfoQueryNothingSet(t *testing.T) { 123 t.Parallel() 124 125 balance := NewTokenInfoQuery() 126 127 require.Equal(t, TokenID{}, balance.GetTokenID()) 128 require.Equal(t, []AccountID{}, balance.GetNodeAccountIDs()) 129 require.Equal(t, 250*time.Millisecond, balance.GetMinBackoff()) 130 require.Equal(t, 8*time.Second, balance.GetMaxBackoff()) 131 require.Equal(t, 10, balance.GetMaxRetryCount()) 132 require.Equal(t, TransactionID{}, balance.GetPaymentTransactionID()) 133 require.Equal(t, Hbar{}, balance.GetQueryPayment()) 134 require.Equal(t, Hbar{}, balance.GetMaxQueryPayment()) 135 } 136 137 func TestUnitTokenInfoQueryCoverage(t *testing.T) { 138 t.Parallel() 139 140 checksum := "dmqui" 141 deadline := time.Second * 3 142 token := TokenID{Token: 3, checksum: &checksum} 143 nodeAccountID := []AccountID{{Account: 10}} 144 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 145 146 client, err := _NewMockClient() 147 client.SetLedgerID(*NewLedgerIDTestnet()) 148 require.NoError(t, err) 149 client.SetAutoValidateChecksums(true) 150 151 query := NewTokenInfoQuery(). 152 SetTokenID(token). 153 SetMaxRetry(3). 154 SetMaxBackoff(time.Second * 30). 155 SetMinBackoff(time.Second * 10). 156 SetNodeAccountIDs(nodeAccountID). 157 SetPaymentTransactionID(transactionID). 158 SetMaxQueryPayment(NewHbar(23)). 159 SetQueryPayment(NewHbar(3)). 160 SetGrpcDeadline(&deadline) 161 162 err = query.validateNetworkOnIDs(client) 163 require.NoError(t, err) 164 165 require.Equal(t, nodeAccountID, query.GetNodeAccountIDs()) 166 require.Equal(t, time.Second*30, query.GetMaxBackoff()) 167 require.Equal(t, time.Second*10, query.GetMinBackoff()) 168 require.Equal(t, token, query.GetTokenID()) 169 require.Equal(t, NewHbar(3), query.GetQueryPayment()) 170 require.Equal(t, NewHbar(23), query.GetMaxQueryPayment()) 171 require.Equal(t, &deadline, query.GetGrpcDeadline()) 172 } 173 174 func TestUnitTokenInfoQueryMock(t *testing.T) { 175 t.Parallel() 176 177 responses := [][]interface{}{{ 178 &services.Response{ 179 Response: &services.Response_TokenGetInfo{ 180 TokenGetInfo: &services.TokenGetInfoResponse{ 181 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 182 }, 183 }, 184 }, 185 &services.Response{ 186 Response: &services.Response_TokenGetInfo{ 187 TokenGetInfo: &services.TokenGetInfoResponse{ 188 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 189 }, 190 }, 191 }, 192 &services.Response{ 193 Response: &services.Response_TokenGetInfo{ 194 TokenGetInfo: &services.TokenGetInfoResponse{ 195 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 196 TokenInfo: &services.TokenInfo{ 197 TokenId: nil, 198 Name: "", 199 Symbol: "", 200 Decimals: 0, 201 TotalSupply: 0, 202 Treasury: nil, 203 AdminKey: nil, 204 KycKey: nil, 205 FreezeKey: nil, 206 WipeKey: nil, 207 SupplyKey: nil, 208 DefaultFreezeStatus: 0, 209 DefaultKycStatus: 0, 210 Deleted: false, 211 AutoRenewAccount: nil, 212 AutoRenewPeriod: nil, 213 Expiry: nil, 214 Memo: "", 215 TokenType: 0, 216 SupplyType: 0, 217 MaxSupply: 0, 218 FeeScheduleKey: nil, 219 CustomFees: nil, 220 PauseKey: nil, 221 PauseStatus: 0, 222 MetadataKey: nil, 223 LedgerId: nil, 224 }, 225 }, 226 }, 227 }, 228 }} 229 230 client, server := NewMockClientAndServer(responses) 231 defer server.Close() 232 233 query := NewTokenInfoQuery(). 234 SetTokenID(TokenID{Token: 3}). 235 SetNodeAccountIDs([]AccountID{{Account: 3}}). 236 SetMaxQueryPayment(NewHbar(1)) 237 238 cost, err := query.GetCost(client) 239 require.NoError(t, err) 240 require.Equal(t, HbarFromTinybar(2), cost) 241 _, err = query.Execute(client) 242 require.NoError(t, err) 243 }