github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_claim_airdrop_transaction_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 "testing" 28 29 "github.com/hashgraph/hedera-protobufs-go/services" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestUnitTokenClaimAirdropTransactionSetPendingAirdropIds(t *testing.T) { 35 t.Parallel() 36 37 pendingAirdropId1 := &PendingAirdropId{tokenID: &TokenID{Token: 1}} 38 pendingAirdropId2 := &PendingAirdropId{tokenID: &TokenID{Token: 2}} 39 40 transaction := NewTokenClaimAirdropTransaction(). 41 SetPendingAirdropIds([]*PendingAirdropId{pendingAirdropId1, pendingAirdropId2}) 42 43 assert.Equal(t, []*PendingAirdropId{pendingAirdropId1, pendingAirdropId2}, transaction.GetPendingAirdropIds()) 44 } 45 46 func TestUnitTokenClaimAirdropTransactionAddPendingAirdropId(t *testing.T) { 47 t.Parallel() 48 49 pendingAirdropId1 := PendingAirdropId{tokenID: &TokenID{Token: 1}} 50 pendingAirdropId2 := PendingAirdropId{tokenID: &TokenID{Token: 2}} 51 52 transaction := NewTokenClaimAirdropTransaction(). 53 AddPendingAirdropId(pendingAirdropId1). 54 AddPendingAirdropId(pendingAirdropId2) 55 56 assert.Equal(t, []*PendingAirdropId{&pendingAirdropId1, &pendingAirdropId2}, transaction.GetPendingAirdropIds()) 57 } 58 59 func TestUnitTokenClaimAirdropTransactionFreeze(t *testing.T) { 60 t.Parallel() 61 62 pendingAirdropId := PendingAirdropId{tokenID: &TokenID{Token: 1}} 63 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 64 transaction := NewTokenClaimAirdropTransaction(). 65 AddPendingAirdropId(pendingAirdropId). 66 SetTransactionID(transactionID). 67 SetNodeAccountIDs(nodeAccountID) 68 69 _, err := transaction.Freeze() 70 require.NoError(t, err) 71 } 72 73 func TestUnitTokenClaimAirdropTransactionToBytes(t *testing.T) { 74 t.Parallel() 75 76 pendingAirdropId := PendingAirdropId{tokenID: &TokenID{Token: 1}, sender: &AccountID{Account: 3}} 77 78 transaction := NewTokenClaimAirdropTransaction(). 79 AddPendingAirdropId(pendingAirdropId) 80 81 bytes, err := transaction.ToBytes() 82 require.NoError(t, err) 83 require.NotNil(t, bytes) 84 } 85 86 func TestUnitTokenClaimAirdropTransactionFromBytes(t *testing.T) { 87 t.Parallel() 88 89 pendingAirdropId := PendingAirdropId{tokenID: &TokenID{Token: 1}} 90 91 transaction := NewTokenClaimAirdropTransaction(). 92 AddPendingAirdropId(pendingAirdropId) 93 94 bytes, err := transaction.ToBytes() 95 require.NoError(t, err) 96 require.NotNil(t, bytes) 97 98 deserializedTransaction, err := TransactionFromBytes(bytes) 99 require.NoError(t, err) 100 101 switch tx := deserializedTransaction.(type) { 102 case TokenClaimAirdropTransaction: 103 assert.Equal(t, transaction.GetPendingAirdropIds(), tx.GetPendingAirdropIds()) 104 default: 105 t.Fatalf("expected TokenClaimAirdropTransaction, got %T", deserializedTransaction) 106 } 107 } 108 109 func TestUnitTokenClaimAirdropTransactionScheduleProtobuf(t *testing.T) { 110 t.Parallel() 111 112 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 113 114 pendingAirdropId1 := PendingAirdropId{tokenID: &TokenID{Token: 1}} 115 pendingAirdropId2 := PendingAirdropId{tokenID: &TokenID{Token: 2}} 116 117 tx, err := NewTokenClaimAirdropTransaction(). 118 SetTransactionID(transactionID). 119 SetNodeAccountIDs([]AccountID{{Account: 3}}). 120 AddPendingAirdropId(pendingAirdropId1). 121 AddPendingAirdropId(pendingAirdropId2). 122 Freeze() 123 require.NoError(t, err) 124 125 expected := &services.SchedulableTransactionBody{ 126 TransactionFee: 100000000, 127 Data: &services.SchedulableTransactionBody_TokenClaimAirdrop{ 128 TokenClaimAirdrop: &services.TokenClaimAirdropTransactionBody{ 129 PendingAirdrops: []*services.PendingAirdropId{ 130 pendingAirdropId1._ToProtobuf(), 131 pendingAirdropId2._ToProtobuf(), 132 }, 133 }, 134 }, 135 } 136 137 actual, err := tx.buildScheduled() 138 require.NoError(t, err) 139 require.Equal(t, expected.String(), actual.String()) 140 } 141 142 func TestUnitTokenClaimAirdropTransactionValidateNetworkOnIDs(t *testing.T) { 143 t.Parallel() 144 145 client, err := _NewMockClient() 146 client.SetLedgerID(*NewLedgerIDTestnet()) 147 require.NoError(t, err) 148 client.SetAutoValidateChecksums(true) 149 150 checksum := "dmqui" 151 pendingAirdropId := &PendingAirdropId{ 152 tokenID: &TokenID{Token: 3, checksum: &checksum}, 153 sender: &AccountID{Account: 3, checksum: &checksum}, 154 receiver: &AccountID{Account: 3, checksum: &checksum}, 155 } 156 157 transaction := NewTokenClaimAirdropTransaction(). 158 SetPendingAirdropIds([]*PendingAirdropId{pendingAirdropId}) 159 160 err = transaction.validateNetworkOnIDs(client) 161 require.NoError(t, err) 162 }