Skip to content

Commit

Permalink
use default network, if one we looked for is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofel committed Sep 12, 2024
1 parent a52c087 commit f94673c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions seth/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (c *ClientBuilder) UseNetworkWithName(name string) *ClientBuilder {
}
}

// if the network is not found, we will try to use the default network
for _, network := range c.config.Networks {
if network.Name == DefaultNetworkName {
c.config.Network = network
return c
}
}

c.errors = append(c.errors, fmt.Errorf("network with name '%s' not found", name))
return c
}
Expand All @@ -97,6 +105,14 @@ func (c *ClientBuilder) UseNetworkWithChainId(chainId uint64) *ClientBuilder {
}
}

// if the network is not found, we will try to use the default network
for _, network := range c.config.Networks {
if network.Name == DefaultNetworkName {
c.config.Network = network
return c
}
}

c.errors = append(c.errors, fmt.Errorf("network with chainId '%d' not found", chainId))
return c
}
Expand Down
30 changes: 30 additions & 0 deletions seth/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ func TestConfig_ModifyExistingConfigWithBuilder_UnknownChainId(t *testing.T) {
err = toml.Unmarshal(d, &sethConfig)
require.NoError(t, err, "failed to unmarshal config file")

// remove default network
networks := []*seth.Network{}
for _, network := range sethConfig.Networks {
if network.Name != seth.DefaultNetworkName {
networks = append(networks, network)
}
}

sethConfig.Networks = networks

_, err = seth.NewClientBuilderWithConfig(&sethConfig).
UseNetworkWithChainId(225).
WithPrivateKeys([]string{"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"}).
Expand All @@ -146,6 +156,26 @@ at least one method that required network to be set was called, but network is n
require.Equal(t, expectedError, err.Error(), "expected error message")
}

func TestConfig_ModifyExistingConfigWithBuilder_UnknownChainId_UseDefault(t *testing.T) {
configPath := os.Getenv(seth.CONFIG_FILE_ENV_VAR)
require.NotEmpty(t, configPath, "expected config file path to be set")

d, err := os.ReadFile(configPath)
require.NoError(t, err, "failed to read config file")

var sethConfig seth.Config
err = toml.Unmarshal(d, &sethConfig)
require.NoError(t, err, "failed to unmarshal config file")

_, err = seth.NewClientBuilderWithConfig(&sethConfig).
UseNetworkWithChainId(225).
WithRpcUrl("ws://localhost:8546").
WithPrivateKeys([]string{"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"}).
Build()

require.NoError(t, err, "failed to create client")
}

func TestConfig_LegacyGas_No_Estimations(t *testing.T) {
builder := seth.NewClientBuilder()

Expand Down

0 comments on commit f94673c

Please sign in to comment.