From a070cf6b17384c328abde02fa565ebea156a418c Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Fri, 27 Sep 2024 18:37:38 -0300 Subject: [PATCH 01/19] [WIP] Skeleton for tests in elcontracts writer --- chainio/clients/elcontracts/writer_test.go | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 chainio/clients/elcontracts/writer_test.go diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go new file mode 100644 index 00000000..5fe64944 --- /dev/null +++ b/chainio/clients/elcontracts/writer_test.go @@ -0,0 +1,93 @@ +package elcontracts_test + +import ( + "context" + "testing" + "time" + + "github.com/Layr-Labs/eigensdk-go/testutils" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestChainWriter(t *testing.T) { + anvil, err := testutils.StartAnvilContainer("") + defer anvil.Terminate(context.Background()) + + // Setup ethclient + ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + anvilHttpEndpoint, err := anvil.Endpoint(ctxWithTimeout, "http") + require.NoError(t, err) + ethClient, err := ethclient.Dial(anvilHttpEndpoint) + require.NoError(t, err) + + /* + // Setup el_chain_reader + elChainReader, err := setupELChainReader(httpEndpoint) + assert.NoError(t, err) + + // Setup operator address and private key + operatorAddr := common.HexToAddress("90F79bf6EB2c4f870365E785982E1f101E93b906") + operatorPrivateKey := "7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6" + + // Get strategy manager address + strategyManagerAddr, err := getStrategyManagerAddress(httpEndpoint) + assert.NoError(t, err) + + // Setup ChainWriter + logger := logging.NewNoopLogger() + txMgr := txmgr.NewSimpleTxManager(ethClient, logger) + elChainWriter, err := NewWriterFromConfig(Config{ + StrategyManagerAddr: strategyManagerAddr, + }, ethClient, logger, nil, txMgr) + assert.NoError(t, err) + + // Define an operator + wallet, err := crypto.HexToECDSA("bead471191bea97fc3aeac36c9d74c895e8a6242602e144e43152f96219e96e8") + assert.NoError(t, err) + walletAddress := crypto.PubkeyToAddress(wallet.PublicKey) + + operator := types.Operator{ + Address: walletAddress.Hex(), + EarningsReceiverAddress: walletAddress.Hex(), + DelegationApproverAddress: walletAddress.Hex(), + StakerOptOutWindowBlocks: big.NewInt(3), + MetadataUrl: "eigensdk-go", + } + + // Test 1: Register as an operator + receipt, err := elChainWriter.RegisterAsOperator(context.Background(), operator, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + + // Test 2: Update operator details + walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") + assert.NoError(t, err) + walletModifiedAddress := crypto.PubkeyToAddress(walletModified.PublicKey) + + operatorModified := types.Operator{ + Address: walletModifiedAddress.Hex(), + EarningsReceiverAddress: walletModifiedAddress.Hex(), + DelegationApproverAddress: walletModifiedAddress.Hex(), + StakerOptOutWindowBlocks: big.NewInt(3), + MetadataUrl: "eigensdk-go", + } + + receipt, err = elChainWriter.UpdateOperatorDetails(context.Background(), operatorModified, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + + // Test 3: Deposit ERC20 into strategy + amount := big.NewInt(100) + strategyAddr, err := getERC20MockStrategy(httpEndpoint) + assert.NoError(t, err) + + receipt, err = elChainWriter.DepositERC20IntoStrategy(context.Background(), strategyAddr, amount, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + + */ + assert.Equal(t, "1234", "1234") +} From 437e0d8a71fac4e30a9386ba7a6edf39ab0b3ced Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Mon, 30 Sep 2024 16:42:21 -0300 Subject: [PATCH 02/19] progress in test TestChainWriter --- chainio/clients/elcontracts/writer_test.go | 132 ++++++++++----------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 5fe64944..f577f02c 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -2,92 +2,86 @@ package elcontracts_test import ( "context" + "log/slog" + "os" "testing" - "time" + "github.com/Layr-Labs/eigensdk-go/chainio/clients" + "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/testutils" - "github.com/ethereum/go-ethereum/ethclient" + "github.com/Layr-Labs/eigensdk-go/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestChainWriter(t *testing.T) { - anvil, err := testutils.StartAnvilContainer("") - defer anvil.Terminate(context.Background()) + anvilStateFileName := "contracts-deployed-anvil-state.json" + anvilC, err := testutils.StartAnvilContainer(anvilStateFileName) + require.NoError(t, err) - // Setup ethclient - ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - anvilHttpEndpoint, err := anvil.Endpoint(ctxWithTimeout, "http") + anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") require.NoError(t, err) - ethClient, err := ethclient.Dial(anvilHttpEndpoint) + + defer anvilC.Terminate(context.Background()) + logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) + + ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ecdsaPrivateKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex) require.NoError(t, err) - /* - // Setup el_chain_reader - elChainReader, err := setupELChainReader(httpEndpoint) - assert.NoError(t, err) - - // Setup operator address and private key - operatorAddr := common.HexToAddress("90F79bf6EB2c4f870365E785982E1f101E93b906") - operatorPrivateKey := "7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6" - - // Get strategy manager address - strategyManagerAddr, err := getStrategyManagerAddress(httpEndpoint) - assert.NoError(t, err) - - // Setup ChainWriter - logger := logging.NewNoopLogger() - txMgr := txmgr.NewSimpleTxManager(ethClient, logger) - elChainWriter, err := NewWriterFromConfig(Config{ - StrategyManagerAddr: strategyManagerAddr, - }, ethClient, logger, nil, txMgr) - assert.NoError(t, err) - - // Define an operator - wallet, err := crypto.HexToECDSA("bead471191bea97fc3aeac36c9d74c895e8a6242602e144e43152f96219e96e8") - assert.NoError(t, err) - walletAddress := crypto.PubkeyToAddress(wallet.PublicKey) - - operator := types.Operator{ - Address: walletAddress.Hex(), - EarningsReceiverAddress: walletAddress.Hex(), - DelegationApproverAddress: walletAddress.Hex(), - StakerOptOutWindowBlocks: big.NewInt(3), - MetadataUrl: "eigensdk-go", - } + contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) + require.NoError(t, err) + + RegistryCoordinatorAddress := contractAddrs.RegistryCoordinator + OperatorStateRetrieverAddress := contractAddrs.OperatorStateRetriever + + chainioConfig := clients.BuildAllConfig{ + EthHttpUrl: "http://localhost:8545", + EthWsUrl: "ws://localhost:8545", + RegistryCoordinatorAddr: RegistryCoordinatorAddress.String(), + OperatorStateRetrieverAddr: OperatorStateRetrieverAddress.String(), + AvsName: "exampleAvs", + PromMetricsIpPortAddress: ":9090", + } + + //// Setup el_chain_reader + clients, err := clients.BuildAll( + chainioConfig, + ecdsaPrivateKey, + logger, + ) + require.NoError(t, err) - // Test 1: Register as an operator - receipt, err := elChainWriter.RegisterAsOperator(context.Background(), operator, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) - - // Test 2: Update operator details - walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") - assert.NoError(t, err) - walletModifiedAddress := crypto.PubkeyToAddress(walletModified.PublicKey) - - operatorModified := types.Operator{ - Address: walletModifiedAddress.Hex(), - EarningsReceiverAddress: walletModifiedAddress.Hex(), - DelegationApproverAddress: walletModifiedAddress.Hex(), - StakerOptOutWindowBlocks: big.NewInt(3), - MetadataUrl: "eigensdk-go", + // Define an operator + operator := + types.Operator{ + Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", + DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", + StakerOptOutWindowBlocks: 100, + MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", } - receipt, err = elChainWriter.UpdateOperatorDetails(context.Background(), operatorModified, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) + // Test 1: Register as an operator + receipt, err := clients.ElChainWriter.RegisterAsOperator(context.Background(), operator, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + + // Test 2: Update operator details + walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") + assert.NoError(t, err) + walletModifiedAddress := crypto.PubkeyToAddress(walletModified.PublicKey) - // Test 3: Deposit ERC20 into strategy - amount := big.NewInt(100) - strategyAddr, err := getERC20MockStrategy(httpEndpoint) - assert.NoError(t, err) + operatorModified := types.Operator{ + Address: walletModifiedAddress.Hex(), + DelegationApproverAddress: walletModifiedAddress.Hex(), + StakerOptOutWindowBlocks: 101, + MetadataUrl: "eigensdk-go", + } - receipt, err = elChainWriter.DepositERC20IntoStrategy(context.Background(), strategyAddr, amount, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) + receipt, err = clients.ElChainWriter.UpdateOperatorDetails(context.Background(), operatorModified, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) - */ assert.Equal(t, "1234", "1234") } From e3fac95d7b8d870d0ba24a523cdad249a7e1293f Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Mon, 30 Sep 2024 16:45:32 -0300 Subject: [PATCH 03/19] Fix anvil endpoints in TestChainWriter test --- chainio/clients/elcontracts/writer_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index f577f02c..fff8605b 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -23,6 +23,9 @@ func TestChainWriter(t *testing.T) { anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") require.NoError(t, err) + anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") + require.NoError(t, err) + defer anvilC.Terminate(context.Background()) logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) @@ -37,8 +40,8 @@ func TestChainWriter(t *testing.T) { OperatorStateRetrieverAddress := contractAddrs.OperatorStateRetriever chainioConfig := clients.BuildAllConfig{ - EthHttpUrl: "http://localhost:8545", - EthWsUrl: "ws://localhost:8545", + EthHttpUrl: anvilHttpEndpoint, + EthWsUrl: anvilWsEndpoint, RegistryCoordinatorAddr: RegistryCoordinatorAddress.String(), OperatorStateRetrieverAddr: OperatorStateRetrieverAddress.String(), AvsName: "exampleAvs", From e4c860c177d81cbe58cb49297871c4d4c08b0d53 Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Mon, 30 Sep 2024 17:24:47 -0300 Subject: [PATCH 04/19] change addresses in test --- chainio/clients/elcontracts/writer_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index fff8605b..64744db3 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -29,7 +29,7 @@ func TestChainWriter(t *testing.T) { defer anvilC.Terminate(context.Background()) logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) - ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ecdsaPrivKeyHex := "7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6" ecdsaPrivateKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex) require.NoError(t, err) @@ -59,7 +59,7 @@ func TestChainWriter(t *testing.T) { // Define an operator operator := types.Operator{ - Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", + Address: "0x90F79bf6EB2c4f870365E785982E1f101E93b906", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", StakerOptOutWindowBlocks: 100, MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", From 0ed6fcae222c2593dc85d8d952d272c9bf84a8c1 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Tue, 1 Oct 2024 17:41:32 -0300 Subject: [PATCH 05/19] add tests --- chainio/clients/elcontracts/writer_test.go | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 64744db3..1300ce17 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -86,5 +86,32 @@ func TestChainWriter(t *testing.T) { assert.NoError(t, err) assert.True(t, receipt.Status == 1) - assert.Equal(t, "1234", "1234") + // Test 3: Update metadata URI + receipt, err = clients.ElChainWriter.UpdateMetadataURI(context.Background(), "https://0.0.0.0", true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + + t.Run("deposit ERC20 into strategy", func(t *testing.T) { + // TODO: fix this test + amount := big.NewInt(1) + receipt, err = clients.ElChainWriter.DepositERC20IntoStrategy(context.Background(), contractAddrs.Erc20MockStrategy, amount, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) + + t.Run("set claimer for", func(t *testing.T) { + // TODO: fix this test, add RewardsCoordinator address to config + claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") + receipt, err = clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) + + t.Run("process claim", func(t *testing.T) { + // TODO: fix this test, add RewardsCoordinator address to config + claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") + receipt, err = clients.ElChainWriter.ProcessClaim(context.Background(), claimer, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) } From 2636f253dac43ebbf713ff71036efdf128f1f8f4 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Tue, 1 Oct 2024 17:46:32 -0300 Subject: [PATCH 06/19] add ElContracts Writer tests --- chainio/clients/elcontracts/writer_test.go | 39 +++++++--------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 1300ce17..d7d785a5 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -2,8 +2,10 @@ package elcontracts_test import ( "context" + "fmt" "log/slog" "os" + "os/exec" "testing" "github.com/Layr-Labs/eigensdk-go/chainio/clients" @@ -29,8 +31,9 @@ func TestChainWriter(t *testing.T) { defer anvilC.Terminate(context.Background()) logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) - ecdsaPrivKeyHex := "7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6" - ecdsaPrivateKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex) + privateKeyHex := "3339854a8622364bcd5650fa92eac82d5dccf04089f5575a761c9b7d3c405b1c" + addressHex := "0x408EfD9C90d59298A9b32F4441aC9Df6A2d8C3E1" + ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex) require.NoError(t, err) contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) @@ -56,10 +59,16 @@ func TestChainWriter(t *testing.T) { ) require.NoError(t, err) + // Fund the address with 10 ether + richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + cmd := exec.Command("bash", "-c", fmt.Sprintf("cast send %s --value 10ether --private-key %s --rpc-url %s", addressHex, richPrivateKeyHex, anvilHttpEndpoint)) + err = cmd.Run() + assert.NoError(t, err) + // Define an operator operator := types.Operator{ - Address: "0x90F79bf6EB2c4f870365E785982E1f101E93b906", + Address: addressHex, DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", StakerOptOutWindowBlocks: 100, MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", @@ -90,28 +99,4 @@ func TestChainWriter(t *testing.T) { receipt, err = clients.ElChainWriter.UpdateMetadataURI(context.Background(), "https://0.0.0.0", true) assert.NoError(t, err) assert.True(t, receipt.Status == 1) - - t.Run("deposit ERC20 into strategy", func(t *testing.T) { - // TODO: fix this test - amount := big.NewInt(1) - receipt, err = clients.ElChainWriter.DepositERC20IntoStrategy(context.Background(), contractAddrs.Erc20MockStrategy, amount, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) - }) - - t.Run("set claimer for", func(t *testing.T) { - // TODO: fix this test, add RewardsCoordinator address to config - claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") - receipt, err = clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) - }) - - t.Run("process claim", func(t *testing.T) { - // TODO: fix this test, add RewardsCoordinator address to config - claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") - receipt, err = clients.ElChainWriter.ProcessClaim(context.Background(), claimer, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) - }) } From a97c3a301e187db525cd2f34898f4673b76613a6 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Tue, 1 Oct 2024 17:59:47 -0300 Subject: [PATCH 07/19] format --- chainio/clients/elcontracts/writer_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index d7d785a5..f1b036ea 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -27,8 +27,6 @@ func TestChainWriter(t *testing.T) { anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") require.NoError(t, err) - - defer anvilC.Terminate(context.Background()) logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) privateKeyHex := "3339854a8622364bcd5650fa92eac82d5dccf04089f5575a761c9b7d3c405b1c" @@ -61,7 +59,16 @@ func TestChainWriter(t *testing.T) { // Fund the address with 10 ether richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - cmd := exec.Command("bash", "-c", fmt.Sprintf("cast send %s --value 10ether --private-key %s --rpc-url %s", addressHex, richPrivateKeyHex, anvilHttpEndpoint)) + cmd := exec.Command( + "bash", + "-c", + fmt.Sprintf( + "cast send %s --value 10ether --private-key %s --rpc-url %s", + addressHex, + richPrivateKeyHex, + anvilHttpEndpoint, + ), + ) err = cmd.Run() assert.NoError(t, err) From 0caf77dbdb338aa2c46b3f6ee4c2919e76f276f4 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 12:38:12 -0300 Subject: [PATCH 08/19] add rewards coordinator binding --- chainio/clients/builder.go | 6 ++++-- testutils/anvil.go | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/chainio/clients/builder.go b/chainio/clients/builder.go index 7cd0c029..e2318d6a 100644 --- a/chainio/clients/builder.go +++ b/chainio/clients/builder.go @@ -27,6 +27,7 @@ type BuildAllConfig struct { OperatorStateRetrieverAddr string AvsName string PromMetricsIpPortAddress string + RewardsCoordinatorAddress string } // ReadClients is a struct that holds only the read clients for interacting with the AVS and EL contracts. @@ -179,8 +180,9 @@ func BuildAll( // creating EL clients: Reader, Writer and EigenLayer Contract Bindings elChainReader, elChainWriter, elContractBindings, err := elcontracts.BuildClients( elcontracts.Config{ - DelegationManagerAddress: avsRegistryContractBindings.DelegationManagerAddr, - AvsDirectoryAddress: avsRegistryContractBindings.AvsDirectoryAddr, + DelegationManagerAddress: avsRegistryContractBindings.DelegationManagerAddr, + AvsDirectoryAddress: avsRegistryContractBindings.AvsDirectoryAddr, + RewardsCoordinatorAddress: gethcommon.HexToAddress(config.RewardsCoordinatorAddress), }, ethHttpClient, txMgr, diff --git a/testutils/anvil.go b/testutils/anvil.go index 3cec5400..856c6b7a 100644 --- a/testutils/anvil.go +++ b/testutils/anvil.go @@ -64,6 +64,7 @@ type ContractAddresses struct { OperatorStateRetriever common.Address DelegationManager common.Address Erc20MockStrategy common.Address + RewardsCoordinator common.Address } func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContracts ContractAddresses) { @@ -120,12 +121,20 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac if erc20MockStrategyAddr == (common.Address{}) { panic("erc20MockStrategyAddr is empty") } + rewardsCoordinatorAddr, err := contractsRegistry.Contracts(&bind.CallOpts{}, "rewardsCoordinator") + if err != nil { + panic(err) + } + if rewardsCoordinatorAddr == (common.Address{}) { + panic("rewardsCoordinatorAddr is empty") + } mockAvsContracts = ContractAddresses{ ServiceManager: mockAvsServiceManagerAddr, RegistryCoordinator: mockAvsRegistryCoordinatorAddr, OperatorStateRetriever: mockAvsOperatorStateRetrieverAddr, DelegationManager: delegationManagerAddr, Erc20MockStrategy: erc20MockStrategyAddr, + RewardsCoordinator: rewardsCoordinatorAddr, } return mockAvsContracts } From f14181c652111f3bc2747e5f94c20639fa6b7f34 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 12:41:49 -0300 Subject: [PATCH 09/19] add set claimer test --- chainio/clients/elcontracts/writer_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index f1b036ea..64aeda38 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -12,6 +12,7 @@ import ( "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/testutils" "github.com/Layr-Labs/eigensdk-go/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,14 +38,12 @@ func TestChainWriter(t *testing.T) { contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) require.NoError(t, err) - RegistryCoordinatorAddress := contractAddrs.RegistryCoordinator - OperatorStateRetrieverAddress := contractAddrs.OperatorStateRetriever - chainioConfig := clients.BuildAllConfig{ EthHttpUrl: anvilHttpEndpoint, EthWsUrl: anvilWsEndpoint, - RegistryCoordinatorAddr: RegistryCoordinatorAddress.String(), - OperatorStateRetrieverAddr: OperatorStateRetrieverAddress.String(), + RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), + OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), + RewardsCoordinatorAddress: contractAddrs.RewardsCoordinator.String(), AvsName: "exampleAvs", PromMetricsIpPortAddress: ":9090", } @@ -106,4 +105,11 @@ func TestChainWriter(t *testing.T) { receipt, err = clients.ElChainWriter.UpdateMetadataURI(context.Background(), "https://0.0.0.0", true) assert.NoError(t, err) assert.True(t, receipt.Status == 1) + + t.Run("set claimer for", func(t *testing.T) { + claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") + receipt, err = clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) } From 2e57f3927e6398fdfdc34ae081dbd9d4a0e290c3 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 13:07:49 -0300 Subject: [PATCH 10/19] change funds transfer to operator --- chainio/clients/elcontracts/writer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 64aeda38..43fdb5cf 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -62,7 +62,7 @@ func TestChainWriter(t *testing.T) { "bash", "-c", fmt.Sprintf( - "cast send %s --value 10ether --private-key %s --rpc-url %s", + "cast send %s --value 6ether --private-key %s --rpc-url %s", addressHex, richPrivateKeyHex, anvilHttpEndpoint, From 2bd73b64d6be5a10711ed9e1ae7f1546d113f431 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 15:08:44 -0300 Subject: [PATCH 11/19] execute cast within container --- chainio/clients/elcontracts/writer_test.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 43fdb5cf..924da80a 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -2,10 +2,8 @@ package elcontracts_test import ( "context" - "fmt" "log/slog" "os" - "os/exec" "testing" "github.com/Layr-Labs/eigensdk-go/chainio/clients" @@ -56,20 +54,14 @@ func TestChainWriter(t *testing.T) { ) require.NoError(t, err) - // Fund the address with 10 ether + // Fund the address with 5 ether richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - cmd := exec.Command( - "bash", - "-c", - fmt.Sprintf( - "cast send %s --value 6ether --private-key %s --rpc-url %s", - addressHex, - richPrivateKeyHex, - anvilHttpEndpoint, - ), + code, _, err := anvilC.Exec( + context.Background(), + []string{"cast", "send", addressHex, "--value", "5ether", "--private-key", richPrivateKeyHex, "--rpc-url", anvilHttpEndpoint}, ) - err = cmd.Run() assert.NoError(t, err) + assert.Equal(t, 0, code) // Define an operator operator := From 7cfab30a9c2bb71387562d91b37b6c0e0a2f4a53 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 15:10:18 -0300 Subject: [PATCH 12/19] format --- chainio/clients/elcontracts/writer_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 924da80a..83e85f7f 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -58,7 +58,16 @@ func TestChainWriter(t *testing.T) { richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" code, _, err := anvilC.Exec( context.Background(), - []string{"cast", "send", addressHex, "--value", "5ether", "--private-key", richPrivateKeyHex, "--rpc-url", anvilHttpEndpoint}, + []string{"cast", + "send", + addressHex, + "--value", + "5ether", + "--private-key", + richPrivateKeyHex, + "--rpc-url", + anvilHttpEndpoint, + }, ) assert.NoError(t, err) assert.Equal(t, 0, code) From 48ea16ebba8b4961833a21cc6a9c1d6d5c729fb9 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 15:12:32 -0300 Subject: [PATCH 13/19] remove rpc url for cast send --- chainio/clients/elcontracts/writer_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 83e85f7f..31010738 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -65,8 +65,6 @@ func TestChainWriter(t *testing.T) { "5ether", "--private-key", richPrivateKeyHex, - "--rpc-url", - anvilHttpEndpoint, }, ) assert.NoError(t, err) From 70a83aa00845c1835463f20c7f839457a9460dde Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 15:56:59 -0300 Subject: [PATCH 14/19] separate register operator tests --- chainio/clients/elcontracts/writer_test.go | 155 ++++++++++++++++----- 1 file changed, 121 insertions(+), 34 deletions(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 31010738..96b80478 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -3,6 +3,7 @@ package elcontracts_test import ( "context" "log/slog" + "math/big" "os" "testing" @@ -16,6 +17,98 @@ import ( "github.com/stretchr/testify/require" ) +func TestRegisterOperator(t *testing.T) { + anvilStateFileName := "contracts-deployed-anvil-state.json" + anvilC, err := testutils.StartAnvilContainer(anvilStateFileName) + require.NoError(t, err) + anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") + require.NoError(t, err) + anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") + require.NoError(t, err) + logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) + + contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) + require.NoError(t, err) + + chainioConfig := clients.BuildAllConfig{ + EthHttpUrl: anvilHttpEndpoint, + EthWsUrl: anvilWsEndpoint, + RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), + OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), + RewardsCoordinatorAddress: contractAddrs.RewardsCoordinator.String(), + AvsName: "exampleAvs", + PromMetricsIpPortAddress: ":9090", + } + + t.Run("register as an operator", func(t *testing.T) { + // Fund the new address with 5 ether + fundedAccount := "0x408EfD9C90d59298A9b32F4441aC9Df6A2d8C3E1" + fundedPrivateKeyHex := "3339854a8622364bcd5650fa92eac82d5dccf04089f5575a761c9b7d3c405b1c" + richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + code, _, err := anvilC.Exec( + context.Background(), + []string{"cast", + "send", + "0x408EfD9C90d59298A9b32F4441aC9Df6A2d8C3E1", + "--value", + "5ether", + "--private-key", + richPrivateKeyHex, + }, + ) + assert.NoError(t, err) + assert.Equal(t, 0, code) + + ecdsaPrivateKey, err := crypto.HexToECDSA(fundedPrivateKeyHex) + require.NoError(t, err) + + clients, err := clients.BuildAll( + chainioConfig, + ecdsaPrivateKey, + logger, + ) + require.NoError(t, err) + + operator := + types.Operator{ + Address: fundedAccount, + DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", + StakerOptOutWindowBlocks: 100, + MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", + } + + receipt, err := clients.ElChainWriter.RegisterAsOperator(context.Background(), operator, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) + + t.Run("register as an operator already registered", func(t *testing.T) { + operatorAddress := "0x408EfD9C90d59298A9b32F4441aC9Df6A2d8C3E1" + operatorPrivateKeyHex := "3339854a8622364bcd5650fa92eac82d5dccf04089f5575a761c9b7d3c405b1c" + + ecdsaPrivateKey, err := crypto.HexToECDSA(operatorPrivateKeyHex) + require.NoError(t, err) + + clients, err := clients.BuildAll( + chainioConfig, + ecdsaPrivateKey, + logger, + ) + require.NoError(t, err) + + operator := + types.Operator{ + Address: operatorAddress, + DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", + StakerOptOutWindowBlocks: 100, + MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", + } + + _, err = clients.ElChainWriter.RegisterAsOperator(context.Background(), operator, true) + assert.Error(t, err) + }) +} + func TestChainWriter(t *testing.T) { anvilStateFileName := "contracts-deployed-anvil-state.json" anvilC, err := testutils.StartAnvilContainer(anvilStateFileName) @@ -28,8 +121,8 @@ func TestChainWriter(t *testing.T) { require.NoError(t, err) logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) - privateKeyHex := "3339854a8622364bcd5650fa92eac82d5dccf04089f5575a761c9b7d3c405b1c" - addressHex := "0x408EfD9C90d59298A9b32F4441aC9Df6A2d8C3E1" + privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + addressHex := "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex) require.NoError(t, err) @@ -46,7 +139,6 @@ func TestChainWriter(t *testing.T) { PromMetricsIpPortAddress: ":9090", } - //// Setup el_chain_reader clients, err := clients.BuildAll( chainioConfig, ecdsaPrivateKey, @@ -70,44 +162,39 @@ func TestChainWriter(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 0, code) - // Define an operator - operator := - types.Operator{ - Address: addressHex, - DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", - StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", - } - - // Test 1: Register as an operator - receipt, err := clients.ElChainWriter.RegisterAsOperator(context.Background(), operator, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) + t.Run("update operator details", func(t *testing.T) { + walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") + assert.NoError(t, err) + walletModifiedAddress := crypto.PubkeyToAddress(walletModified.PublicKey) - // Test 2: Update operator details - walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") - assert.NoError(t, err) - walletModifiedAddress := crypto.PubkeyToAddress(walletModified.PublicKey) + operatorModified := types.Operator{ + Address: walletModifiedAddress.Hex(), + DelegationApproverAddress: walletModifiedAddress.Hex(), + StakerOptOutWindowBlocks: 101, + MetadataUrl: "eigensdk-go", + } - operatorModified := types.Operator{ - Address: walletModifiedAddress.Hex(), - DelegationApproverAddress: walletModifiedAddress.Hex(), - StakerOptOutWindowBlocks: 101, - MetadataUrl: "eigensdk-go", - } + receipt, err := clients.ElChainWriter.UpdateOperatorDetails(context.Background(), operatorModified, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) - receipt, err = clients.ElChainWriter.UpdateOperatorDetails(context.Background(), operatorModified, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) + t.Run("update metadata URI", func(t *testing.T) { + receipt, err := clients.ElChainWriter.UpdateMetadataURI(context.Background(), "https://0.0.0.0", true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) - // Test 3: Update metadata URI - receipt, err = clients.ElChainWriter.UpdateMetadataURI(context.Background(), "https://0.0.0.0", true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) + t.Run("deposit ERC20 into strategy", func(t *testing.T) { + amount := big.NewInt(1) + receipt, err := clients.ElChainWriter.DepositERC20IntoStrategy(context.Background(), contractAddrs.Erc20MockStrategy, amount, true) + assert.NoError(t, err) + assert.True(t, receipt.Status == 1) + }) t.Run("set claimer for", func(t *testing.T) { claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") - receipt, err = clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) + receipt, err := clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) assert.NoError(t, err) assert.True(t, receipt.Status == 1) }) From 6bb0122870747c6d8bf13fa242c6abf0d3f840ac Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 15:58:33 -0300 Subject: [PATCH 15/19] format --- chainio/clients/elcontracts/writer_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 96b80478..30a122b4 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -187,7 +187,12 @@ func TestChainWriter(t *testing.T) { t.Run("deposit ERC20 into strategy", func(t *testing.T) { amount := big.NewInt(1) - receipt, err := clients.ElChainWriter.DepositERC20IntoStrategy(context.Background(), contractAddrs.Erc20MockStrategy, amount, true) + receipt, err := clients.ElChainWriter.DepositERC20IntoStrategy( + context.Background(), + contractAddrs.Erc20MockStrategy, + amount, + true, + ) assert.NoError(t, err) assert.True(t, receipt.Status == 1) }) From 20377b0d5419536b4ef93cd1acb6a10928416448 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Wed, 2 Oct 2024 17:48:52 -0300 Subject: [PATCH 16/19] add process claim fail test --- chainio/clients/elcontracts/writer_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 30a122b4..30aca367 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/Layr-Labs/eigensdk-go/chainio/clients" + rewardscoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IRewardsCoordinator" "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/testutils" "github.com/Layr-Labs/eigensdk-go/types" @@ -203,4 +204,22 @@ func TestChainWriter(t *testing.T) { assert.NoError(t, err) assert.True(t, receipt.Status == 1) }) + + t.Run("process claim fails if no root submitted", func(t *testing.T) { + claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") + claim := rewardscoordinator.IRewardsCoordinatorRewardsMerkleClaim{ + RootIndex: 0, + EarnerIndex: 0, + EarnerTreeProof: []byte{}, + EarnerLeaf: rewardscoordinator.IRewardsCoordinatorEarnerTreeMerkleLeaf{ + Earner: claimer, + EarnerTokenRoot: [32]byte{}, + }, + TokenIndices: []uint32{}, + TokenTreeProofs: [][]byte{}, + TokenLeaves: []rewardscoordinator.IRewardsCoordinatorTokenTreeMerkleLeaf{}, + } + _, err := clients.ElChainWriter.ProcessClaim(context.Background(), claim, claimer, true) + assert.Error(t, err) + }) } From b21ccda517021ec67510f8f77a81e84559af8a17 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Thu, 3 Oct 2024 15:35:42 -0300 Subject: [PATCH 17/19] remove rewards coordinator --- chainio/clients/builder.go | 6 ++--- chainio/clients/elcontracts/writer_test.go | 29 ---------------------- 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/chainio/clients/builder.go b/chainio/clients/builder.go index e2318d6a..7cd0c029 100644 --- a/chainio/clients/builder.go +++ b/chainio/clients/builder.go @@ -27,7 +27,6 @@ type BuildAllConfig struct { OperatorStateRetrieverAddr string AvsName string PromMetricsIpPortAddress string - RewardsCoordinatorAddress string } // ReadClients is a struct that holds only the read clients for interacting with the AVS and EL contracts. @@ -180,9 +179,8 @@ func BuildAll( // creating EL clients: Reader, Writer and EigenLayer Contract Bindings elChainReader, elChainWriter, elContractBindings, err := elcontracts.BuildClients( elcontracts.Config{ - DelegationManagerAddress: avsRegistryContractBindings.DelegationManagerAddr, - AvsDirectoryAddress: avsRegistryContractBindings.AvsDirectoryAddr, - RewardsCoordinatorAddress: gethcommon.HexToAddress(config.RewardsCoordinatorAddress), + DelegationManagerAddress: avsRegistryContractBindings.DelegationManagerAddr, + AvsDirectoryAddress: avsRegistryContractBindings.AvsDirectoryAddr, }, ethHttpClient, txMgr, diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 30aca367..43c90e16 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -8,11 +8,9 @@ import ( "testing" "github.com/Layr-Labs/eigensdk-go/chainio/clients" - rewardscoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IRewardsCoordinator" "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/testutils" "github.com/Layr-Labs/eigensdk-go/types" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -36,7 +34,6 @@ func TestRegisterOperator(t *testing.T) { EthWsUrl: anvilWsEndpoint, RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), - RewardsCoordinatorAddress: contractAddrs.RewardsCoordinator.String(), AvsName: "exampleAvs", PromMetricsIpPortAddress: ":9090", } @@ -135,7 +132,6 @@ func TestChainWriter(t *testing.T) { EthWsUrl: anvilWsEndpoint, RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), - RewardsCoordinatorAddress: contractAddrs.RewardsCoordinator.String(), AvsName: "exampleAvs", PromMetricsIpPortAddress: ":9090", } @@ -197,29 +193,4 @@ func TestChainWriter(t *testing.T) { assert.NoError(t, err) assert.True(t, receipt.Status == 1) }) - - t.Run("set claimer for", func(t *testing.T) { - claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") - receipt, err := clients.ElChainWriter.SetClaimerFor(context.Background(), claimer, true) - assert.NoError(t, err) - assert.True(t, receipt.Status == 1) - }) - - t.Run("process claim fails if no root submitted", func(t *testing.T) { - claimer := common.HexToAddress("0x1234567890123456789012345678901234567890") - claim := rewardscoordinator.IRewardsCoordinatorRewardsMerkleClaim{ - RootIndex: 0, - EarnerIndex: 0, - EarnerTreeProof: []byte{}, - EarnerLeaf: rewardscoordinator.IRewardsCoordinatorEarnerTreeMerkleLeaf{ - Earner: claimer, - EarnerTokenRoot: [32]byte{}, - }, - TokenIndices: []uint32{}, - TokenTreeProofs: [][]byte{}, - TokenLeaves: []rewardscoordinator.IRewardsCoordinatorTokenTreeMerkleLeaf{}, - } - _, err := clients.ElChainWriter.ProcessClaim(context.Background(), claim, claimer, true) - assert.Error(t, err) - }) } From f8d9a93fe84ab0aeaa883fe71360eec75a849061 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 4 Oct 2024 14:51:00 -0300 Subject: [PATCH 18/19] use BuildTestClients util on clients tests --- chainio/clients/avsregistry/reader_test.go | 46 +---------------- chainio/clients/avsregistry/writer_test.go | 3 +- chainio/clients/elcontracts/writer_test.go | 58 ++-------------------- testutils/testclients/testclients.go | 51 +++++++++++++++++++ 4 files changed, 60 insertions(+), 98 deletions(-) create mode 100644 testutils/testclients/testclients.go diff --git a/chainio/clients/avsregistry/reader_test.go b/chainio/clients/avsregistry/reader_test.go index b311174d..375551a2 100644 --- a/chainio/clients/avsregistry/reader_test.go +++ b/chainio/clients/avsregistry/reader_test.go @@ -3,59 +3,17 @@ package avsregistry_test import ( "context" "math/big" - "os" "testing" - "github.com/Layr-Labs/eigensdk-go/chainio/clients" - "github.com/Layr-Labs/eigensdk-go/logging" - "github.com/Layr-Labs/eigensdk-go/testutils" + "github.com/Layr-Labs/eigensdk-go/testutils/testclients" "github.com/Layr-Labs/eigensdk-go/types" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" ) -// Starts an anvil container and builds the ChainIO Clients for testing. -func BuildTestClients(t *testing.T) *clients.Clients { - testConfig := testutils.GetDefaultTestConfig() - anvilC, err := testutils.StartAnvilContainer(testConfig.AnvilStateFileName) - require.NoError(t, err) - - anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") - require.NoError(t, err) - - anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") - require.NoError(t, err) - logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: testConfig.LogLevel}) - - privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex) - require.NoError(t, err) - - contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) - require.NoError(t, err) - - chainioConfig := clients.BuildAllConfig{ - EthHttpUrl: anvilHttpEndpoint, - EthWsUrl: anvilWsEndpoint, - RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), - OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), - AvsName: "exampleAvs", - PromMetricsIpPortAddress: ":9090", - } - - clients, err := clients.BuildAll( - chainioConfig, - ecdsaPrivateKey, - logger, - ) - require.NoError(t, err) - return clients -} - func TestReaderMethods(t *testing.T) { - clients := BuildTestClients(t) + clients, _ := testclients.BuildTestClients(t) chainReader := clients.ReadClients.AvsRegistryChainReader quorumNumbers := types.QuorumNums{0} diff --git a/chainio/clients/avsregistry/writer_test.go b/chainio/clients/avsregistry/writer_test.go index 1a21ef47..11537dc5 100644 --- a/chainio/clients/avsregistry/writer_test.go +++ b/chainio/clients/avsregistry/writer_test.go @@ -6,6 +6,7 @@ import ( chainioutils "github.com/Layr-Labs/eigensdk-go/chainio/utils" "github.com/Layr-Labs/eigensdk-go/crypto/bls" + "github.com/Layr-Labs/eigensdk-go/testutils/testclients" "github.com/Layr-Labs/eigensdk-go/types" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -13,7 +14,7 @@ import ( ) func TestWriterMethods(t *testing.T) { - clients := BuildTestClients(t) + clients, _ := testclients.BuildTestClients(t) chainWriter := clients.AvsRegistryChainWriter keypair, err := bls.NewKeyPairFromString("0x01") diff --git a/chainio/clients/elcontracts/writer_test.go b/chainio/clients/elcontracts/writer_test.go index 43c90e16..0f07b259 100644 --- a/chainio/clients/elcontracts/writer_test.go +++ b/chainio/clients/elcontracts/writer_test.go @@ -2,7 +2,6 @@ package elcontracts_test import ( "context" - "log/slog" "math/big" "os" "testing" @@ -10,6 +9,7 @@ import ( "github.com/Layr-Labs/eigensdk-go/chainio/clients" "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/testutils" + "github.com/Layr-Labs/eigensdk-go/testutils/testclients" "github.com/Layr-Labs/eigensdk-go/types" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" @@ -17,14 +17,14 @@ import ( ) func TestRegisterOperator(t *testing.T) { - anvilStateFileName := "contracts-deployed-anvil-state.json" - anvilC, err := testutils.StartAnvilContainer(anvilStateFileName) + testConfig := testutils.GetDefaultTestConfig() + anvilC, err := testutils.StartAnvilContainer(testConfig.AnvilStateFileName) require.NoError(t, err) anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") require.NoError(t, err) anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") require.NoError(t, err) - logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) + logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: testConfig.LogLevel}) contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) require.NoError(t, err) @@ -108,56 +108,8 @@ func TestRegisterOperator(t *testing.T) { } func TestChainWriter(t *testing.T) { - anvilStateFileName := "contracts-deployed-anvil-state.json" - anvilC, err := testutils.StartAnvilContainer(anvilStateFileName) - require.NoError(t, err) - - anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") - require.NoError(t, err) - - anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") - require.NoError(t, err) - logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: slog.LevelDebug}) - - privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - addressHex := "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" - ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex) - require.NoError(t, err) - + clients, anvilHttpEndpoint := testclients.BuildTestClients(t) contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) - require.NoError(t, err) - - chainioConfig := clients.BuildAllConfig{ - EthHttpUrl: anvilHttpEndpoint, - EthWsUrl: anvilWsEndpoint, - RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), - OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), - AvsName: "exampleAvs", - PromMetricsIpPortAddress: ":9090", - } - - clients, err := clients.BuildAll( - chainioConfig, - ecdsaPrivateKey, - logger, - ) - require.NoError(t, err) - - // Fund the address with 5 ether - richPrivateKeyHex := "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - code, _, err := anvilC.Exec( - context.Background(), - []string{"cast", - "send", - addressHex, - "--value", - "5ether", - "--private-key", - richPrivateKeyHex, - }, - ) - assert.NoError(t, err) - assert.Equal(t, 0, code) t.Run("update operator details", func(t *testing.T) { walletModified, err := crypto.HexToECDSA("2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6") diff --git a/testutils/testclients/testclients.go b/testutils/testclients/testclients.go new file mode 100644 index 00000000..336ee8a9 --- /dev/null +++ b/testutils/testclients/testclients.go @@ -0,0 +1,51 @@ +package testclients + +import ( + "context" + "os" + "testing" + + "github.com/Layr-Labs/eigensdk-go/chainio/clients" + "github.com/Layr-Labs/eigensdk-go/logging" + "github.com/Layr-Labs/eigensdk-go/testutils" + "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/require" +) + +// Starts an anvil container and builds the ChainIO Clients for testing. +func BuildTestClients(t *testing.T) (*clients.Clients, string) { + testConfig := testutils.GetDefaultTestConfig() + anvilC, err := testutils.StartAnvilContainer(testConfig.AnvilStateFileName) + require.NoError(t, err) + + anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") + require.NoError(t, err) + + anvilWsEndpoint, err := anvilC.Endpoint(context.Background(), "ws") + require.NoError(t, err) + logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: testConfig.LogLevel}) + + privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex) + require.NoError(t, err) + + contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) + require.NoError(t, err) + + chainioConfig := clients.BuildAllConfig{ + EthHttpUrl: anvilHttpEndpoint, + EthWsUrl: anvilWsEndpoint, + RegistryCoordinatorAddr: contractAddrs.RegistryCoordinator.String(), + OperatorStateRetrieverAddr: contractAddrs.OperatorStateRetriever.String(), + AvsName: "exampleAvs", + PromMetricsIpPortAddress: ":9090", + } + + clients, err := clients.BuildAll( + chainioConfig, + ecdsaPrivateKey, + logger, + ) + require.NoError(t, err) + return clients, anvilHttpEndpoint +} From fc4f28592edd6d19bec59b78be891955590a1f5a Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 4 Oct 2024 14:53:52 -0300 Subject: [PATCH 19/19] remove RewardsCoordinatorAddr from testutils --- testutils/anvil.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/testutils/anvil.go b/testutils/anvil.go index 5e7838a8..656d00d7 100644 --- a/testutils/anvil.go +++ b/testutils/anvil.go @@ -65,7 +65,6 @@ type ContractAddresses struct { OperatorStateRetriever common.Address DelegationManager common.Address Erc20MockStrategy common.Address - RewardsCoordinator common.Address } func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContracts ContractAddresses) { @@ -122,20 +121,12 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac if erc20MockStrategyAddr == (common.Address{}) { panic("erc20MockStrategyAddr is empty") } - rewardsCoordinatorAddr, err := contractsRegistry.Contracts(&bind.CallOpts{}, "rewardsCoordinator") - if err != nil { - panic(err) - } - if rewardsCoordinatorAddr == (common.Address{}) { - panic("rewardsCoordinatorAddr is empty") - } mockAvsContracts = ContractAddresses{ ServiceManager: mockAvsServiceManagerAddr, RegistryCoordinator: mockAvsRegistryCoordinatorAddr, OperatorStateRetriever: mockAvsOperatorStateRetrieverAddr, DelegationManager: delegationManagerAddr, Erc20MockStrategy: erc20MockStrategyAddr, - RewardsCoordinator: rewardsCoordinatorAddr, } return mockAvsContracts }