From 2adf26476467eae7663d30a1089976845b4cc072 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Wed, 12 Jun 2024 15:02:36 -0300 Subject: [PATCH] Create a test for the PrecomputeAddress function --- contracts/contracts_test.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/contracts/contracts_test.go b/contracts/contracts_test.go index 91048c9f..aefc245a 100644 --- a/contracts/contracts_test.go +++ b/contracts/contracts_test.go @@ -5,8 +5,10 @@ import ( "os" "testing" + "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/starknet.go/contracts" "github.com/NethermindEth/starknet.go/rpc" + "github.com/NethermindEth/starknet.go/utils" "github.com/test-go/testify/assert" "github.com/test-go/testify/require" ) @@ -19,7 +21,8 @@ import ( // Parameters: // - t: The testing.T instance for running the test // Returns: -// none +// +// none func TestUnmarshalContractClass(t *testing.T) { content, err := os.ReadFile("./tests/hello_starknet_compiled.sierra.json") require.NoError(t, err) @@ -40,7 +43,8 @@ func TestUnmarshalContractClass(t *testing.T) { // Parameters: // - t: The testing.T instance for running the test // Returns: -// none +// +// none func TestUnmarshalCasmClass(t *testing.T) { casmClass, err := contracts.UnmarshalCasmClass("./tests/hello_starknet_compiled.casm.json") require.NoError(t, err) @@ -50,3 +54,30 @@ func TestUnmarshalCasmClass(t *testing.T) { assert.Equal(t, casmClass.EntryPointByType.External[1].Offset, 130) assert.Equal(t, casmClass.EntryPointByType.External[1].Builtins[0], "range_check") } + +func TestPrecomputeAddress(t *testing.T) { + + deployerAddress := utils.TestHexToFelt(t, "0xdeadbeef") + salts := []*felt.Felt{ + utils.TestHexToFelt(t, "0xaaaaaaaa"), + utils.TestHexToFelt(t, "0xbbbbbbbb"), + utils.TestHexToFelt(t, "0xcccccccc"), + } + classHash := utils.TestHexToFelt(t, "0xdeadbeef") + constructorCalldata := []*felt.Felt{ + deployerAddress, + } + + expectedPrecomputedAddress := []*felt.Felt{ + utils.TestHexToFelt(t, "0x3791840c6746577f27f4a5766a6862b7486f0a254d34e0d2f0a5d238160cf10"), + utils.TestHexToFelt(t, "0x479b5192d4af71279a92eddb09e4dcfdc00751ccb27da0e9c4f9b2312318242"), + utils.TestHexToFelt(t, "0x7a824044120ac54527d701e18b2eea6e11fb0a8f070a21acd13a7a4062065b4"), + } + + for index := 0; index < 3; index++ { + precomputedAddress, err := contracts.PrecomputeAddress(deployerAddress, salts[index], classHash, constructorCalldata) + require.NoError(t, err) + require.Equal(t, expectedPrecomputedAddress[index], precomputedAddress) + t.Log(precomputedAddress) + } +}