Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ElContracts Writer tests #361

Merged
merged 21 commits into from
Nov 13, 2024
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
46 changes: 2 additions & 44 deletions chainio/clients/avsregistry/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 2 additions & 1 deletion chainio/clients/avsregistry/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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"
"github.com/stretchr/testify/require"
)

func TestWriterMethods(t *testing.T) {
clients := BuildTestClients(t)
clients, _ := testclients.BuildTestClients(t)
chainWriter := clients.AvsRegistryChainWriter

keypair, err := bls.NewKeyPairFromString("0x01")
Expand Down
148 changes: 148 additions & 0 deletions chainio/clients/elcontracts/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package elcontracts_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/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRegisterOperator(t *testing.T) {
TomasArrachea marked this conversation as resolved.
Show resolved Hide resolved
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})

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",
}

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) {
clients, anvilHttpEndpoint := testclients.BuildTestClients(t)
contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint)

t.Run("update operator details", func(t *testing.T) {
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",
}

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)
})

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)
})
}
51 changes: 51 additions & 0 deletions testutils/testclients/testclients.go
Original file line number Diff line number Diff line change
@@ -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
}