Skip to content

Commit

Permalink
Add network id config (#4)
Browse files Browse the repository at this point in the history
Add network id config parameters
  • Loading branch information
DancingAxolotl authored Dec 10, 2021
2 parents a30faac + 10282c5 commit 5ebcc8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ NOTE: L1 and L2 networks must both be defined or none at all.

#### L1

Options for defining a ethereumProviderUrlFlag:

- `ETHEREUM_PROVIDER_URL` env variable
Options for defining a custom Ethereum provider:

- `--ethereum-provider-url` flag
- `RESOLUTION_ETHEREUM_PROVIDER_URL` env variable to specify the provider URL
- `RESOLUTION_ETHEREUM_NETWORK_ID` env variable to specify the network type (mainnet or rinkeby)
- `--ethereum-provider-url` flag to specify the provider URL
- `--ethereum-network-id` flag to specify the network type (mainnet or rinkeby)

The CLI prioritizes the `--ethereum-provider-url` flag
The CLI prioritizes the `--ethereum-provider-url` and `--ethereum-network-id` flags

#### L2

If no Ethereum L2 JSON RPC ethereum-provider-url is defined, a default Infura Ethereum L2 Provider Url will be used

Options for defining a ethereumL2ProviderUrlFlag:

- `ETHEREUM_L2_PROVIDER_URL` env variable
Options for defining a custom Polygon L2 provider:

- `--ethereum-l2-provider-url` flag
- `RESOLUTION_ETHEREUM_L2_PROVIDER_URL` env variable to specify the provider URL
- `RESOLUTION_ETHEREUM_L2_NETWORK_ID` env variable to specify the network type (polygon or matic)
- `--ethereum-l2-provider-url` flag to specify the provider URL
- `--ethereum-l2-network-id` flag to specify the network type (polygon or matic)

The CLI prioritizes the `--ethereum-l2-provider-url` flag
The CLI prioritizes the `--ethereum-l2-provider-url` and `--ethereum-l2-network-id` flags

### Zilliqa Provider

Expand Down
24 changes: 22 additions & 2 deletions resolution/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

const ethereumUrlKey = "ETHEREUM_PROVIDER_URL"
const ethereumL2UrlKey = "ETHEREUM_L2_PROVIDER_URL"
const ethereumNetworkIdKey = "ETHEREUM_NETWORK_ID"
const ethereumL2NetworkIdKey = "ETHEREUM_L2_NETWORK_ID"
const zilliqaUrlKey = "ZILLIQA_PROVIDER_URL"

var Domain string
Expand All @@ -25,6 +27,8 @@ var SelectedNamingService resolution.NamingService

var ethereumProviderUrlFlag string
var ethereumL2ProviderUrlFlag string
var ethereumNetworkIdFlag string
var ethereumL2NetworkIdFlag string
var zilliqaProviderUrlFlag string

var (
Expand Down Expand Up @@ -67,6 +71,8 @@ func init() {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
rootCmd.PersistentFlags().StringVar(&ethereumProviderUrlFlag, "ethereum-provider-url", "", "Ethereum JSON RPC endpoint url (could be set via RESOLUTION_ETHEREUM_PROVIDER_URL environment variable)")
rootCmd.PersistentFlags().StringVar(&ethereumL2ProviderUrlFlag, "ethereum-l2-provider-url", "", "Ethereum L2 JSON RPC endpoint url (could be set via RESOLUTION_ETHEREUM_L2_PROVIDER_URL environment variable)")
rootCmd.PersistentFlags().StringVar(&ethereumNetworkIdFlag, "ethereum-network-id", "", "Ethereum network id (could be set via RESOLUTION_ETHEREUM_NETWORK_ID environment variable)")
rootCmd.PersistentFlags().StringVar(&ethereumL2NetworkIdFlag, "ethereum-l2-network-id", "", "Ethereum L2 network id (could be set via RESOLUTION_ETHEREUM_L2_NETWORK_ID environment variable)")
rootCmd.PersistentFlags().StringVar(&zilliqaProviderUrlFlag, "zilliqa-provider-url", "", "Zilliqa JSON RPC endpoint url (could be set via RESOLUTION_ZILLIQA_PROVIDER_URL environment variable)")
resolveCmd.PersistentFlags().StringVarP(&Domain, "domain", "d", "", ".crypto or .zil domain to resolve (required)")
namehashCmd.PersistentFlags().StringVarP(&Domain, "domain", "d", "", ".crypto or .zil domain to resolve (required)")
Expand Down Expand Up @@ -94,6 +100,12 @@ func initConfig() {
if ethereumL2ProviderUrlFlag != "" {
viper.Set(ethereumL2UrlKey, ethereumL2ProviderUrlFlag)
}
if ethereumNetworkIdFlag != "" {
viper.Set(ethereumNetworkIdKey, ethereumNetworkIdFlag)
}
if ethereumL2NetworkIdFlag != "" {
viper.Set(ethereumL2NetworkIdKey, ethereumL2NetworkIdFlag)
}
if zilliqaProviderUrlFlag != "" {
viper.Set(zilliqaUrlKey, zilliqaProviderUrlFlag)
}
Expand All @@ -104,23 +116,31 @@ func initNamingServices() {
var err error
ethereumUrl := viper.GetString(ethereumUrlKey)
ethereumL2Url := viper.GetString(ethereumL2UrlKey)
ethereumNetworkId := viper.GetString(ethereumNetworkIdKey)
ethereumL2NetworkId := viper.GetString(ethereumL2NetworkIdKey)
zilliqaUrl := viper.GetString(zilliqaUrlKey)
unsBuilder := resolution.NewUnsBuilder()
znsBuilder := resolution.NewZnsBuilder()
if ethereumUrl != "" || ethereumL2Url != "" {
if ethereumUrl == "" || ethereumL2Url == "" {
log.Fatalf("Specify both L1 and L2 ethereum url when defining your own networks")
}
if ethereumNetworkId != "mainnet" && ethereumNetworkId != "rinkeby" {
log.Fatalf("Specify ethereum network id ('mainnet' or 'rinkeby')")
}
if ethereumL2NetworkId != "polygon" && ethereumL2NetworkId != "mumbai" {
log.Fatalf("Specify ethereum L2 network id ('polygon' or 'mumbai')")
}
backend, err := ethclient.Dial(ethereumUrl)
if err != nil {
log.Fatalf("Error connecting to Ethereum provider. Provider: %v. Error: %v", ethereumUrl, err.Error())
}
unsBuilder.SetContractBackend(backend).SetEthereumNetwork("mainnet")
unsBuilder.SetContractBackend(backend).SetEthereumNetwork(ethereumNetworkId)
backendL2, err := ethclient.Dial(ethereumL2Url)
if err != nil {
log.Fatalf("Error connecting to Ethereum L2 provider. Provider: %v. Error: %v", ethereumL2Url, err.Error())
}
unsBuilder.SetL2ContractBackend(backendL2).SetL2EthereumNetwork("polygon")
unsBuilder.SetL2ContractBackend(backendL2).SetL2EthereumNetwork(ethereumL2NetworkId)
} else {
unsBuilder.SetEthereumNetwork("mainnet").SetL2EthereumNetwork("polygon")
}
Expand Down

0 comments on commit 5ebcc8f

Please sign in to comment.