github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_info_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  const topicMemo = "go-sdk::topic memo"
    35  
    36  func TestIntegrationTopicInfoQueryCanExecute(t *testing.T) {
    37  	t.Parallel()
    38  	env := NewIntegrationTestEnv(t)
    39  
    40  	txID, err := NewTopicCreateTransaction().
    41  		SetAdminKey(env.Client.GetOperatorPublicKey()).
    42  		SetNodeAccountIDs(env.NodeAccountIDs).
    43  		SetTopicMemo(topicMemo).
    44  		Execute(env.Client)
    45  	require.NoError(t, err)
    46  
    47  	receipt, err := txID.SetValidateStatus(true).GetReceipt(env.Client)
    48  	require.NoError(t, err)
    49  
    50  	topicID := *receipt.TopicID
    51  	assert.NotNil(t, topicID)
    52  
    53  	info, err := NewTopicInfoQuery().
    54  		SetNodeAccountIDs(env.NodeAccountIDs).
    55  		SetTopicID(topicID).
    56  		SetMaxQueryPayment(NewHbar(1)).
    57  		Execute(env.Client)
    58  	require.NoError(t, err)
    59  	assert.NotNil(t, info)
    60  
    61  	assert.Equal(t, topicMemo, info.TopicMemo)
    62  	assert.Equal(t, uint64(0), info.SequenceNumber)
    63  	assert.Equal(t, env.Client.GetOperatorPublicKey().String(), info.AdminKey.String())
    64  
    65  	_, err = NewTopicDeleteTransaction().
    66  		SetNodeAccountIDs(env.NodeAccountIDs).
    67  		SetTopicID(topicID).
    68  		Execute(env.Client)
    69  	require.NoError(t, err)
    70  
    71  	err = CloseIntegrationTestEnv(env, nil)
    72  	require.NoError(t, err)
    73  }
    74  
    75  func TestIntegrationTopicInfoQueryGetCost(t *testing.T) {
    76  	t.Parallel()
    77  	env := NewIntegrationTestEnv(t)
    78  
    79  	resp, err := NewTopicCreateTransaction().
    80  		SetAdminKey(env.Client.GetOperatorPublicKey()).
    81  		SetNodeAccountIDs(env.NodeAccountIDs).
    82  		SetTopicMemo(topicMemo).
    83  		Execute(env.Client)
    84  	require.NoError(t, err)
    85  
    86  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    87  	require.NoError(t, err)
    88  
    89  	topicID := *receipt.TopicID
    90  	assert.NotNil(t, topicID)
    91  
    92  	topicInfo := NewTopicInfoQuery().
    93  		SetMaxQueryPayment(NewHbar(1)).
    94  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    95  		SetTopicID(topicID)
    96  
    97  	cost, err := topicInfo.GetCost(env.Client)
    98  	require.NoError(t, err)
    99  
   100  	_, err = topicInfo.SetQueryPayment(cost).Execute(env.Client)
   101  	require.NoError(t, err)
   102  
   103  	_, err = NewTopicDeleteTransaction().
   104  		SetNodeAccountIDs(env.NodeAccountIDs).
   105  		SetTopicID(topicID).
   106  		Execute(env.Client)
   107  	require.NoError(t, err)
   108  
   109  	err = CloseIntegrationTestEnv(env, nil)
   110  	require.NoError(t, err)
   111  }
   112  
   113  func TestIntegrationTopicInfoQuerySetBigMaxPayment(t *testing.T) {
   114  	t.Parallel()
   115  	env := NewIntegrationTestEnv(t)
   116  
   117  	resp, err := NewTopicCreateTransaction().
   118  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   119  		SetNodeAccountIDs(env.NodeAccountIDs).
   120  		SetTopicMemo(topicMemo).
   121  		Execute(env.Client)
   122  	require.NoError(t, err)
   123  
   124  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   125  	require.NoError(t, err)
   126  
   127  	topicID := *receipt.TopicID
   128  	assert.NotNil(t, topicID)
   129  
   130  	topicInfo := NewTopicInfoQuery().
   131  		SetMaxQueryPayment(NewHbar(100000)).
   132  		SetTopicID(topicID)
   133  
   134  	cost, err := topicInfo.GetCost(env.Client)
   135  	require.NoError(t, err)
   136  
   137  	_, err = topicInfo.SetQueryPayment(cost).Execute(env.Client)
   138  	require.NoError(t, err)
   139  
   140  	_, err = NewTopicDeleteTransaction().
   141  		SetNodeAccountIDs(env.NodeAccountIDs).
   142  		SetTopicID(topicID).
   143  		Execute(env.Client)
   144  	require.NoError(t, err)
   145  
   146  	err = CloseIntegrationTestEnv(env, nil)
   147  	require.NoError(t, err)
   148  }
   149  
   150  func TestIntegrationTopicInfoQuerySetSmallMaxPayment(t *testing.T) {
   151  	t.Parallel()
   152  	env := NewIntegrationTestEnv(t)
   153  
   154  	resp, err := NewTopicCreateTransaction().
   155  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   156  		SetNodeAccountIDs(env.NodeAccountIDs).
   157  		SetTopicMemo(topicMemo).
   158  		Execute(env.Client)
   159  	require.NoError(t, err)
   160  
   161  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   162  	require.NoError(t, err)
   163  
   164  	topicID := *receipt.TopicID
   165  	assert.NotNil(t, topicID)
   166  
   167  	topicInfo := NewTopicInfoQuery().
   168  		SetNodeAccountIDs(env.NodeAccountIDs).
   169  		SetMaxQueryPayment(HbarFromTinybar(1)).
   170  		SetTopicID(topicID)
   171  
   172  	cost, err := topicInfo.GetCost(env.Client)
   173  	require.NoError(t, err)
   174  
   175  	_, err = topicInfo.Execute(env.Client)
   176  	assert.Error(t, err)
   177  	if err != nil {
   178  		assert.Equal(t, "cost of TopicInfoQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tℏ", err.Error())
   179  	}
   180  
   181  	_, err = NewTopicDeleteTransaction().
   182  		SetNodeAccountIDs(env.NodeAccountIDs).
   183  		SetTopicID(topicID).
   184  		Execute(env.Client)
   185  	require.NoError(t, err)
   186  
   187  	err = CloseIntegrationTestEnv(env, nil)
   188  	require.NoError(t, err)
   189  }
   190  
   191  func TestIntegrationTopicInfoQueryInsufficientFee(t *testing.T) {
   192  	t.Parallel()
   193  	env := NewIntegrationTestEnv(t)
   194  
   195  	resp, err := NewTopicCreateTransaction().
   196  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   197  		SetNodeAccountIDs(env.NodeAccountIDs).
   198  		SetTopicMemo(topicMemo).
   199  		Execute(env.Client)
   200  	require.NoError(t, err)
   201  
   202  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   203  	require.NoError(t, err)
   204  
   205  	topicID := *receipt.TopicID
   206  	assert.NotNil(t, topicID)
   207  
   208  	topicInfo := NewTopicInfoQuery().
   209  		SetNodeAccountIDs(env.NodeAccountIDs).
   210  		SetMaxQueryPayment(NewHbar(1)).
   211  		SetTopicID(topicID)
   212  
   213  	_, err = topicInfo.GetCost(env.Client)
   214  	require.NoError(t, err)
   215  
   216  	_, err = topicInfo.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client)
   217  	if err != nil {
   218  		assert.Equal(t, "exceptional precheck status INSUFFICIENT_TX_FEE", err.Error())
   219  	}
   220  
   221  	_, err = NewTopicDeleteTransaction().
   222  		SetNodeAccountIDs(env.NodeAccountIDs).
   223  		SetTopicID(topicID).
   224  		Execute(env.Client)
   225  	require.NoError(t, err)
   226  
   227  	err = CloseIntegrationTestEnv(env, nil)
   228  	require.NoError(t, err)
   229  }
   230  
   231  func TestIntegrationTopicInfoQueryThreshold(t *testing.T) {
   232  	t.Parallel()
   233  	env := NewIntegrationTestEnv(t)
   234  
   235  	keys := make([]PrivateKey, 3)
   236  	pubKeys := make([]PublicKey, 3)
   237  
   238  	for i := range keys {
   239  		newKey, err := PrivateKeyGenerateEd25519()
   240  		if err != nil {
   241  			panic(err)
   242  		}
   243  
   244  		keys[i] = newKey
   245  		pubKeys[i] = newKey.PublicKey()
   246  	}
   247  
   248  	thresholdKey := KeyListWithThreshold(2).
   249  		AddAllPublicKeys(pubKeys)
   250  
   251  	txID, err := NewTopicCreateTransaction().
   252  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   253  		SetNodeAccountIDs(env.NodeAccountIDs).
   254  		SetSubmitKey(thresholdKey).
   255  		SetTopicMemo(topicMemo).
   256  		Execute(env.Client)
   257  	require.NoError(t, err)
   258  
   259  	receipt, err := txID.SetValidateStatus(true).GetReceipt(env.Client)
   260  	require.NoError(t, err)
   261  
   262  	topicID := *receipt.TopicID
   263  	assert.NotNil(t, topicID)
   264  
   265  	info, err := NewTopicInfoQuery().
   266  		SetNodeAccountIDs(env.NodeAccountIDs).
   267  		SetTopicID(topicID).
   268  		SetMaxQueryPayment(NewHbar(1)).
   269  		Execute(env.Client)
   270  	require.NoError(t, err)
   271  	assert.NotNil(t, info)
   272  
   273  	assert.Equal(t, topicMemo, info.TopicMemo)
   274  	assert.Equal(t, uint64(0), info.SequenceNumber)
   275  	assert.Equal(t, env.Client.GetOperatorPublicKey().String(), info.AdminKey.String())
   276  	assert.NotEmpty(t, info.SubmitKey.String())
   277  
   278  	_, err = NewTopicDeleteTransaction().
   279  		SetNodeAccountIDs(env.NodeAccountIDs).
   280  		SetTopicID(topicID).
   281  		Execute(env.Client)
   282  	require.NoError(t, err)
   283  
   284  	err = CloseIntegrationTestEnv(env, nil)
   285  	require.NoError(t, err)
   286  }
   287  
   288  func TestIntegrationTopicInfoQueryNoTopicID(t *testing.T) {
   289  	t.Parallel()
   290  	env := NewIntegrationTestEnv(t)
   291  
   292  	_, err := NewTopicInfoQuery().
   293  		SetMaxQueryPayment(NewHbar(1)).
   294  		SetNodeAccountIDs(env.NodeAccountIDs).
   295  		Execute(env.Client)
   296  	assert.Error(t, err)
   297  	if err != nil {
   298  		assert.Equal(t, "exceptional precheck status INVALID_TOPIC_ID", err.Error())
   299  	}
   300  
   301  	err = CloseIntegrationTestEnv(env, nil)
   302  	require.NoError(t, err)
   303  }