Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sdk/contract_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,33 @@ func TestUnitContractCreateBytesHooks(t *testing.T) {
require.NoError(t, err)
require.Equal(t, *hook, contractCreateTx.GetHooks()[0])
}

// HIP-904
func TestUnitContractCreateTransactionUnlimitedMaxAutoAssociations(t *testing.T) {
t.Parallel()

transaction, err := NewContractCreateTransaction().
SetTransactionID(TransactionIDGenerate(AccountID{Account: 324})).
SetNodeAccountIDs([]AccountID{{Account: 10}}).
SetBytecodeFileID(FileID{File: 7}).
SetGas(500).
SetMaxAutomaticTokenAssociations(-1).
Freeze()
require.NoError(t, err)

require.Equal(t, int32(-1), transaction.GetMaxAutomaticTokenAssociations())

proto := transaction.build().GetContractCreateInstance()
require.Equal(t, int32(-1), proto.MaxAutomaticTokenAssociations)
}

func TestUnitContractCreateFlowUnlimitedMaxAutoAssociations(t *testing.T) {
t.Parallel()

flow := NewContractCreateFlow().
SetMaxAutomaticTokenAssociations(-1)
require.Equal(t, int32(-1), flow.GetMaxAutomaticTokenAssociations())

contractCreateTx := flow._CreateContractCreateTransaction(FileID{File: 7})
require.Equal(t, int32(-1), contractCreateTx.GetMaxAutomaticTokenAssociations())
}
19 changes: 19 additions & 0 deletions sdk/contract_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,22 @@ func TestUnitContractUpdateTransactionBytesHooks(t *testing.T) {
require.Equal(t, *hook, contractUpdateTx.GetHooksToCreate()[0])
require.Equal(t, []int64{1}, contractUpdateTx.GetHooksToDelete())
}

// HIP-904
func TestUnitContractUpdateTransactionUnlimitedMaxAutoAssociations(t *testing.T) {
t.Parallel()

transaction, err := NewContractUpdateTransaction().
SetTransactionID(TransactionIDGenerate(AccountID{Account: 324})).
SetNodeAccountIDs([]AccountID{{Account: 10}}).
SetContractID(ContractID{Contract: 7}).
SetMaxAutomaticTokenAssociations(-1).
Freeze()
require.NoError(t, err)

require.Equal(t, int32(-1), transaction.GetMaxAutomaticTokenAssociations())

proto := transaction.build().GetContractUpdateInstance()
require.NotNil(t, proto.MaxAutomaticTokenAssociations)
require.Equal(t, int32(-1), proto.MaxAutomaticTokenAssociations.GetValue())
}
112 changes: 112 additions & 0 deletions sdk/max_auto_associations_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,115 @@ func TestUnlimitedMaxAutoAssociationsFailsWithInvalid(t *testing.T) {
_, err = tx.SetValidateStatus(true).GetReceipt(env.Client)
require.ErrorContains(t, err, "INVALID_MAX_AUTO_ASSOCIATIONS")
}

// HIP-904 contract tests

func TestUnlimitedMaxAutoAssociationsContractAllowsToTransferFungibleTokens(t *testing.T) {
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

t.Parallel()
env := NewIntegrationTestEnv(t)
defer CloseIntegrationTestEnv(env, nil)

// deploy the bytecode
fileCreate, err := NewFileCreateTransaction().
SetKeys(env.Client.GetOperatorPublicKey()).
SetContents(testContractByteCode).
Execute(env.Client)
require.NoError(t, err)

fileReceipt, err := fileCreate.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)
fileID := *fileReceipt.FileID

// create a contract with unlimited max auto associations
contractCreate, err := NewContractCreateTransaction().
SetAdminKey(env.Client.GetOperatorPublicKey()).
SetGas(contractDeployGas).
SetBytecodeFileID(fileID).
SetMaxAutomaticTokenAssociations(-1).
Execute(env.Client)
require.NoError(t, err)

contractReceipt, err := contractCreate.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)
require.NotNil(t, contractReceipt.ContractID)
contractID := *contractReceipt.ContractID

// the contract's own account is addressed by the same shard.realm.num
contractAccountID := AccountID{Shard: contractID.Shard, Realm: contractID.Realm, Account: contractID.Contract}

// create token1
tokenID1, err := createFungibleToken(&env)
require.NoError(t, err)

// create token2
tokenID2, err := createFungibleToken(&env)
require.NoError(t, err)

// transfer both tokens to the contract with no explicit association
tokenTransferTransaction, err := NewTransferTransaction().
AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -1000).
AddTokenTransfer(tokenID1, contractAccountID, 1000).
AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -1000).
AddTokenTransfer(tokenID2, contractAccountID, 1000).
Execute(env.Client)
require.NoError(t, err)

_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// verify both balances arrived on the contract
contractBalance, err := NewAccountBalanceQuery().SetContractID(contractID).Execute(env.Client)
require.NoError(t, err)
assert.Equal(t, uint64(1000), contractBalance.Tokens.Get(tokenID1))
assert.Equal(t, uint64(1000), contractBalance.Tokens.Get(tokenID2))
}

func TestUnlimitedMaxAutoAssociationsContractUpdate(t *testing.T) {
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

t.Parallel()
env := NewIntegrationTestEnv(t)
defer CloseIntegrationTestEnv(env, nil)

// deploy the bytecode
fileCreate, err := NewFileCreateTransaction().
SetKeys(env.Client.GetOperatorPublicKey()).
SetContents(testContractByteCode).
Execute(env.Client)
require.NoError(t, err)

fileReceipt, err := fileCreate.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)
fileID := *fileReceipt.FileID

// create a contract with a limited number of max auto associations
contractCreate, err := NewContractCreateTransaction().
SetAdminKey(env.Client.GetOperatorPublicKey()).
SetGas(contractDeployGas).
SetBytecodeFileID(fileID).
SetMaxAutomaticTokenAssociations(1).
Execute(env.Client)
require.NoError(t, err)

contractReceipt, err := contractCreate.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)
require.NotNil(t, contractReceipt.ContractID)
contractID := *contractReceipt.ContractID

// update the contract to unlimited max auto associations
contractUpdate, err := NewContractUpdateTransaction().
SetContractID(contractID).
SetMaxAutomaticTokenAssociations(-1).
Execute(env.Client)
require.NoError(t, err)

_, err = contractUpdate.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// verify the contract now reports unlimited (-1) max auto associations
info, err := NewContractInfoQuery().SetContractID(contractID).Execute(env.Client)
require.NoError(t, err)
assert.Equal(t, int32(-1), info.MaxAutomaticTokenAssociations)
}
Loading