github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_reject_flow_e2e_test.go (about) 1 //go:build all || e2e 2 // +build all e2e 3 4 package hedera 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 /*- 14 * 15 * Hedera Go SDK 16 * 17 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 18 * 19 * Licensed under the Apache License, Version 2.0 (the "License"); 20 * you may not use this file except in compliance with the License. 21 * You may obtain a copy of the License at 22 * 23 * http://www.apache.org/licenses/LICENSE-2.0 24 * 25 * Unless required by applicable law or agreed to in writing, software 26 * distributed under the License is distributed on an "AS IS" BASIS, 27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 28 * See the License for the specific language governing permissions and 29 * limitations under the License. 30 * 31 */ 32 33 func TestCustomizer(t *testing.T) { 34 t.Parallel() 35 env := NewIntegrationTestEnv(t) 36 newKey, _ := GeneratePrivateKey() 37 38 // TODO - remove this once the customizer is implemented 39 _, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 40 _, err := transaction.SetAdminKey(newKey).FreezeWith(env.Client) 41 require.NoError(t, err) 42 transaction.Sign(newKey) 43 }) 44 45 require.NoError(t, err) 46 } 47 func TestIntegrationTokenRejectFlowCanExecuteForFungibleToken(t *testing.T) { 48 t.Parallel() 49 env := NewIntegrationTestEnv(t) 50 51 // create fungible tokens with treasury 52 tokenID1, err := createFungibleToken(&env) 53 require.NoError(t, err) 54 tokenID2, err := createFungibleToken(&env) 55 require.NoError(t, err) 56 57 // create receiver account with 0 auto associations 58 receiver, key, err := createAccount(&env) 59 require.NoError(t, err) 60 61 // associate the tokens with the receiver 62 frozenAssociateTxn, err := NewTokenAssociateTransaction().SetAccountID(receiver).AddTokenID(tokenID1).AddTokenID(tokenID2).FreezeWith(env.Client) 63 require.NoError(t, err) 64 resp, err := frozenAssociateTxn.Sign(key).Execute(env.Client) 65 require.NoError(t, err) 66 67 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 68 require.NoError(t, err) 69 70 // transfer fts to the receiver 71 tx, err := NewTransferTransaction(). 72 AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -10). 73 AddTokenTransfer(tokenID1, receiver, 10). 74 AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -10). 75 AddTokenTransfer(tokenID2, receiver, 10). 76 Execute(env.Client) 77 require.NoError(t, err) 78 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 79 require.NoError(t, err) 80 81 // reject the token + dissociate 82 frozenTxn, err := NewTokenRejectFlow(). 83 SetOwnerID(receiver). 84 SetTokenIDs(tokenID1, tokenID2). 85 FreezeWith(env.Client) 86 require.NoError(t, err) 87 resp, err = frozenTxn.Sign(key).Execute(env.Client) 88 require.NoError(t, err) 89 90 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 91 require.NoError(t, err) 92 93 // verify the balance of the receiver is 0 94 tokenBalance, err := NewAccountBalanceQuery().SetAccountID(receiver).Execute(env.Client) 95 require.NoError(t, err) 96 assert.Zero(t, tokenBalance.Tokens.Get(tokenID1)) 97 assert.Zero(t, tokenBalance.Tokens.Get(tokenID2)) 98 99 // verify the tokens are transferred back to the treasury 100 tokenBalance, err = NewAccountBalanceQuery().SetAccountID(env.OperatorID).Execute(env.Client) 101 require.NoError(t, err) 102 assert.Equal(t, uint64(1_000_000), tokenBalance.Tokens.Get(tokenID1)) 103 assert.Equal(t, uint64(1_000_000), tokenBalance.Tokens.Get(tokenID2)) 104 105 // verify the tokens are not associated with the receiver 106 tx, err = NewTransferTransaction(). 107 AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -10). 108 AddTokenTransfer(tokenID1, receiver, 10). 109 Execute(env.Client) 110 require.NoError(t, err) 111 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 112 require.ErrorContains(t, err, "TOKEN_NOT_ASSOCIATED_TO_ACCOUNT") 113 114 tx, err = NewTransferTransaction(). 115 AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -10). 116 AddTokenTransfer(tokenID2, receiver, 10). 117 Execute(env.Client) 118 require.NoError(t, err) 119 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 120 require.ErrorContains(t, err, "TOKEN_NOT_ASSOCIATED_TO_ACCOUNT") 121 } 122 123 func TestIntegrationTokenRejectFlowCanExecuteForNFT(t *testing.T) { 124 t.Parallel() 125 env := NewIntegrationTestEnv(t) 126 127 // create nft collections with treasury 128 nftID1, err := createNft(&env) 129 require.NoError(t, err) 130 nftID2, err := createNft(&env) 131 require.NoError(t, err) 132 133 // mint 134 mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client) 135 require.NoError(t, err) 136 receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client) 137 require.NoError(t, err) 138 mint, err = NewTokenMintTransaction().SetTokenID(nftID2).SetMetadatas(mintMetadata).Execute(env.Client) 139 require.NoError(t, err) 140 receipt, err = mint.SetValidateStatus(true).GetReceipt(env.Client) 141 require.NoError(t, err) 142 143 serials := receipt.SerialNumbers 144 145 // create receiver account 146 receiver, key, err := createAccount(&env) 147 require.NoError(t, err) 148 149 // associate the tokens with the receiver 150 frozenAssociateTxn, err := NewTokenAssociateTransaction().SetAccountID(receiver).AddTokenID(nftID1).AddTokenID(nftID2).FreezeWith(env.Client) 151 require.NoError(t, err) 152 resp, err := frozenAssociateTxn.Sign(key).Execute(env.Client) 153 require.NoError(t, err) 154 155 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 156 require.NoError(t, err) 157 158 // transfer nfts to the receiver 159 tx, err := NewTransferTransaction(). 160 AddNftTransfer(nftID1.Nft(serials[0]), env.Client.GetOperatorAccountID(), receiver). 161 AddNftTransfer(nftID1.Nft(serials[1]), env.Client.GetOperatorAccountID(), receiver). 162 AddNftTransfer(nftID2.Nft(serials[0]), env.Client.GetOperatorAccountID(), receiver). 163 AddNftTransfer(nftID2.Nft(serials[1]), env.Client.GetOperatorAccountID(), receiver). 164 Execute(env.Client) 165 require.NoError(t, err) 166 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 167 require.NoError(t, err) 168 169 // reject the token + dissociate 170 frozenTxn, err := NewTokenRejectFlow(). 171 SetOwnerID(receiver). 172 SetNftIDs(nftID1.Nft(serials[0]), nftID1.Nft(serials[1]), nftID2.Nft(serials[0]), nftID2.Nft(serials[1])). 173 FreezeWith(env.Client) 174 require.NoError(t, err) 175 resp, err = frozenTxn.Sign(key).Execute(env.Client) 176 require.NoError(t, err) 177 178 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 179 require.NoError(t, err) 180 181 // verify the balance is decremented by 2 182 tokenBalance, err := NewAccountBalanceQuery().SetAccountID(receiver).Execute(env.Client) 183 require.NoError(t, err) 184 assert.Equal(t, uint64(0), tokenBalance.Tokens.Get(nftID1)) 185 assert.Equal(t, uint64(0), tokenBalance.Tokens.Get(nftID2)) 186 187 // verify the token is transferred back to the treasury 188 nftBalance, err := NewTokenNftInfoQuery().SetNftID(nftID1.Nft(serials[1])).Execute(env.Client) 189 require.NoError(t, err) 190 assert.Equal(t, env.OperatorID, nftBalance[0].AccountID) 191 192 nftBalance, err = NewTokenNftInfoQuery().SetNftID(nftID2.Nft(serials[1])).Execute(env.Client) 193 require.NoError(t, err) 194 assert.Equal(t, env.OperatorID, nftBalance[0].AccountID) 195 196 // verify the tokens are not associated with the receiver 197 tx, err = NewTransferTransaction(). 198 AddNftTransfer(nftID1.Nft(serials[0]), env.Client.GetOperatorAccountID(), receiver). 199 AddNftTransfer(nftID1.Nft(serials[1]), env.Client.GetOperatorAccountID(), receiver). 200 Execute(env.Client) 201 require.NoError(t, err) 202 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 203 require.ErrorContains(t, err, "TOKEN_NOT_ASSOCIATED_TO_ACCOUNT") 204 } 205 206 func TestIntegrationTokenRejectFlowFailsWhenNotRejectingAllNFTs(t *testing.T) { 207 t.Parallel() 208 env := NewIntegrationTestEnv(t) 209 210 // create nft collections with treasury 211 nftID1, err := createNft(&env) 212 require.NoError(t, err) 213 nftID2, err := createNft(&env) 214 require.NoError(t, err) 215 216 // mint 217 mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client) 218 require.NoError(t, err) 219 receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client) 220 require.NoError(t, err) 221 mint, err = NewTokenMintTransaction().SetTokenID(nftID2).SetMetadatas(mintMetadata).Execute(env.Client) 222 require.NoError(t, err) 223 receipt, err = mint.SetValidateStatus(true).GetReceipt(env.Client) 224 require.NoError(t, err) 225 226 serials := receipt.SerialNumbers 227 228 // create receiver account 229 receiver, key, err := createAccount(&env) 230 require.NoError(t, err) 231 232 // associate the tokens with the receiver 233 frozenAssociateTxn, err := NewTokenAssociateTransaction().SetAccountID(receiver).AddTokenID(nftID1).AddTokenID(nftID2).FreezeWith(env.Client) 234 require.NoError(t, err) 235 resp, err := frozenAssociateTxn.Sign(key).Execute(env.Client) 236 require.NoError(t, err) 237 238 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 239 require.NoError(t, err) 240 241 // transfer nfts to the receiver 242 tx, err := NewTransferTransaction(). 243 AddNftTransfer(nftID1.Nft(serials[0]), env.Client.GetOperatorAccountID(), receiver). 244 AddNftTransfer(nftID1.Nft(serials[1]), env.Client.GetOperatorAccountID(), receiver). 245 AddNftTransfer(nftID2.Nft(serials[0]), env.Client.GetOperatorAccountID(), receiver). 246 AddNftTransfer(nftID2.Nft(serials[1]), env.Client.GetOperatorAccountID(), receiver). 247 Execute(env.Client) 248 require.NoError(t, err) 249 _, err = tx.SetValidateStatus(true).GetReceipt(env.Client) 250 require.NoError(t, err) 251 252 // reject the token + dissociate 253 frozenTxn, err := NewTokenRejectFlow(). 254 SetOwnerID(receiver). 255 SetNftIDs(nftID1.Nft(serials[0]), nftID1.Nft(serials[1]), nftID2.Nft(serials[0])). 256 FreezeWith(env.Client) 257 require.NoError(t, err) 258 resp, err = frozenTxn.Sign(key).Execute(env.Client) 259 require.ErrorContains(t, err, "ACCOUNT_STILL_OWNS_NFTS") 260 }