From e698d80abeb850c7718c355995d74895f74ceb06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 18 Nov 2024 17:26:10 +0100 Subject: [PATCH 1/3] networks --- networks/networks.go | 184 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 networks/networks.go diff --git a/networks/networks.go b/networks/networks.go new file mode 100644 index 00000000..2e2d6ee8 --- /dev/null +++ b/networks/networks.go @@ -0,0 +1,184 @@ +package networks + +import ( + "github.com/0xsequence/ethkit/ethproviders" +) + +type Networks map[string]*Network + +type Network struct { + ID uint64 `toml:"id"` + Name string `toml:"name"` + Title string `toml:"title"` + Type NetworkType `toml:"type"` + + LogoURL string `toml:"logoUrl"` + + ENSAddress string `toml:"ensAddress"` + + AuthChain bool `toml:"authChain"` + Deprecated bool `toml:"deprecated"` + Disabled bool `toml:"disabled"` + + NodeURL string `toml:"nodeUrl"` + IndexerURL string `toml:"indexerUrl"` + RelayerURL string `toml:"relayerUrl"` + + InternalNodeURL string `toml:"internalNodeUrl"` + InternalIndexerURL string `toml:"internalIndexerUrl"` + InternalRelayerURL string `toml:"internalRelayerUrl"` + + WSEnabled bool `toml:"wsEnabled"` + WSURL string `toml:"wsUrl"` + + BlockExplorer *BlockExplorerConfig `toml:"blockExplorer"` + + NativeToken *Currency `toml:"nativeToken"` + Currencies []*Currency `toml:"currencies"` + + NodeGateway *NodeGatewayNetwork `toml:"nodeGateway"` +} + +type Currency struct { + ContractAddress *string `toml:"contractAddress"` + Name string `toml:"name"` + Symbol string `toml:"symbol"` + Decimals uint64 `toml:"decimals"` + ImageURL string `toml:"imageUrl"` + Native bool `toml:"native"` + Default bool `toml:"default"` + Group CurrencyGroup `toml:"group"` +} + +type BlockExplorerConfig struct { + Name string `toml:"name"` + RootURL string `toml:"rootUrl"` + AddressURL string `toml:"addressUrl"` + TxnHashURL string `toml:"txnHashUrl"` +} + +type NodeGatewayNetwork struct { + Endpoints []*NodeGatewayEndpoint `toml:"endpoints"` + Finality uint64 `toml:"finality"` + MonitorDisabled bool `toml:"monitorDisabled"` + MonitorPollInterval string `toml:"monitorPollInterval"` + MonitorBlockRetention uint64 `toml:"monitorBlockRetention"` + AlertsOff bool `toml:"alertsOff"` + Important bool `toml:"important"` +} + +type NodeGatewayEndpoint struct { + Label string `toml:"label"` + Priority int `toml:"priority"` + URL string `toml:"url"` + WSURL string `toml:"wsurl"` + SkipMethods []string `toml:"skipMethods"` + AuxMethodNs []string `toml:"auxMethodNs"` + Disabled bool `toml:"disabled"` +} + +type NetworkType uint8 + +const ( + NetworkTypeUnknown NetworkType = 0 + NetworkTypeMainnet NetworkType = 1 + NetworkTypeTestnet NetworkType = 2 +) + +var OrderSideName = map[uint8]string{ + 0: "unknown", + 1: "mainnet", + 2: "testnet", +} + +var OrderSideValue = map[string]uint8{ + "unknown": 0, + "mainnet": 1, + "testnet": 2, +} + +func (x NetworkType) String() string { + return OrderSideName[uint8(x)] +} + +func (x NetworkType) MarshalText() ([]byte, error) { + return []byte(OrderSideName[uint8(x)]), nil +} + +func (x *NetworkType) UnmarshalText(b []byte) error { + *x = NetworkType(OrderSideValue[string(b)]) + return nil +} + +// Grouping currencies for cross chain purchases +type CurrencyGroup uint8 + +const ( + CurrencyGroupNone CurrencyGroup = iota + CurrencyGroupUSDC + CurrencyGroupUSDCTestnet + CurrencyGroupWETH +) + +var CurrencyGroupName = map[uint8]string{ + 0: "none", + 1: "usdc", + 2: "usdc_testnet", + 3: "weth", +} + +var CurrencyGroupValue = map[string]uint8{ + "none": 0, + "usdc": 1, + "usdc_testnet": 2, + "weth": 3, +} + +func (x CurrencyGroup) String() string { + return CurrencyGroupName[uint8(x)] +} + +func (x CurrencyGroup) MarshalText() ([]byte, error) { + return []byte(CurrencyGroupName[uint8(x)]), nil +} + +func (x *CurrencyGroup) UnmarshalText(b []byte) error { + *x = CurrencyGroup(CurrencyGroupValue[string(b)]) + return nil +} + +func (n *Network) IsMainnet() bool { + return n.Type == NetworkTypeMainnet +} + +func (n *Network) IsTesnet() bool { + return n.Type == NetworkTypeTestnet +} + +func (n Networks) Active() Networks { + res := Networks{} + for name, network := range n { + if !network.Disabled { + res[name] = network + } + } + + return res +} + +func (n Networks) EthProvidersConfig() ethproviders.Config { + res := ethproviders.Config{} + for _, network := range n { + res[network.Name] = ethproviders.NetworkConfig{ + ID: network.ID, + URL: network.NodeURL, + WSEnabled: network.WSEnabled, + WSURL: network.WSURL, + AuthChain: network.AuthChain, + Testnet: network.IsTesnet(), + Disabled: network.Disabled, + } + } + + return res +} From a506e5c2373ab38d233392849e25f5af121ef6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 19 Nov 2024 10:35:27 +0100 Subject: [PATCH 2/3] json tags --- networks/networks.go | 123 +++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/networks/networks.go b/networks/networks.go index 2e2d6ee8..d5c66a6b 100644 --- a/networks/networks.go +++ b/networks/networks.go @@ -1,80 +1,87 @@ package networks import ( + "strconv" + "github.com/0xsequence/ethkit/ethproviders" ) -type Networks map[string]*Network - -type Network struct { - ID uint64 `toml:"id"` - Name string `toml:"name"` - Title string `toml:"title"` - Type NetworkType `toml:"type"` - - LogoURL string `toml:"logoUrl"` - - ENSAddress string `toml:"ensAddress"` - - AuthChain bool `toml:"authChain"` - Deprecated bool `toml:"deprecated"` - Disabled bool `toml:"disabled"` - - NodeURL string `toml:"nodeUrl"` - IndexerURL string `toml:"indexerUrl"` - RelayerURL string `toml:"relayerUrl"` - - InternalNodeURL string `toml:"internalNodeUrl"` - InternalIndexerURL string `toml:"internalIndexerUrl"` - InternalRelayerURL string `toml:"internalRelayerUrl"` +type Networks map[ChainID]*Network - WSEnabled bool `toml:"wsEnabled"` - WSURL string `toml:"wsUrl"` +type ChainID string - BlockExplorer *BlockExplorerConfig `toml:"blockExplorer"` - - NativeToken *Currency `toml:"nativeToken"` - Currencies []*Currency `toml:"currencies"` +func (c ChainID) Uint64() uint64 { + u, _ := strconv.ParseUint(c.String(), 10, 64) + return u +} - NodeGateway *NodeGatewayNetwork `toml:"nodeGateway"` +func (c ChainID) String() string { + return string(c) } -type Currency struct { - ContractAddress *string `toml:"contractAddress"` - Name string `toml:"name"` - Symbol string `toml:"symbol"` - Decimals uint64 `toml:"decimals"` - ImageURL string `toml:"imageUrl"` - Native bool `toml:"native"` - Default bool `toml:"default"` - Group CurrencyGroup `toml:"group"` +type Network struct { + ChainID uint64 `toml:"chain_id" json:"chainId,omitempty"` + Name string `toml:"name" json:"name,omitempty"` + Title string `toml:"title" json:"title,omitempty"` + Type NetworkType `toml:"type" json:"type,omitempty"` + LogoURL string `toml:"logo_url" json:"logoUrl,omitempty"` + ENSAddress string `toml:"ens_address" json:"ensAddress,omitempty"` + AuthChain bool `toml:"auth_chain" json:"authChain,omitempty"` + Deprecated bool `toml:"deprecated" json:"deprecated,omitempty"` + Disabled bool `toml:"disabled" json:"disabled,omitempty"` + WSEnabled bool `toml:"ws_enabled" json:"wsEnabled,omitempty"` + WSURL string `toml:"ws_url" json:"wsUrl,omitempty"` + + BlockExplorer *BlockExplorerConfig `toml:"block_explorer" json:"blockExplorer,omitempty"` + NodeGateway *NodeGatewayNetwork `toml:"node_gateway" json:"nodeGateway,omitempty"` + + NodeURL string `toml:"node_url" json:"nodeUrl,omitempty"` + IndexerURL string `toml:"indexer_url" json:"indexerUrl,omitempty"` + RelayerURL string `toml:"relayer_url" json:"relayerUrl,omitempty"` + InternalNodeURL string `toml:"internal_node_url" json:"-"` + InternalIndexerURL string `toml:"internal_indexer_url" json:"-"` + InternalRelayerURL string `toml:"internal_relayer_url" json:"-"` + + NativeToken *Currency `toml:"native_token" json:"nativeToken,omitempty"` + Currencies []*Currency `toml:"currencies" json:"currencies,omitempty"` } type BlockExplorerConfig struct { - Name string `toml:"name"` - RootURL string `toml:"rootUrl"` - AddressURL string `toml:"addressUrl"` - TxnHashURL string `toml:"txnHashUrl"` + Name string `toml:"name" json:"name,omitempty"` + RootURL string `toml:"root_url" json:"rootUrl,omitempty"` + AddressURL string `toml:"address_url" json:"addressUrl,omitempty"` + TxnHashURL string `toml:"txn_hash_url" json:"txnHashUrl,omitempty"` } type NodeGatewayNetwork struct { - Endpoints []*NodeGatewayEndpoint `toml:"endpoints"` - Finality uint64 `toml:"finality"` - MonitorDisabled bool `toml:"monitorDisabled"` - MonitorPollInterval string `toml:"monitorPollInterval"` - MonitorBlockRetention uint64 `toml:"monitorBlockRetention"` - AlertsOff bool `toml:"alertsOff"` - Important bool `toml:"important"` + Endpoints []*NodeGatewayEndpoint `toml:"endpoints" json:"endpoints,omitempty"` + Finality uint64 `toml:"finality" json:"finality,omitempty"` + MonitorDisabled bool `toml:"monitor_disabled" json:"monitorDisabled,omitempty"` + MonitorPollInterval string `toml:"monitor_poll_interval" json:"monitorPollInterval,omitempty"` + MonitorBlockRetention uint64 `toml:"monitor_block_retention" json:"monitorBlockRetention,omitempty"` + AlertsOff bool `toml:"alerts_off" json:"alertsOff,omitempty"` + Important bool `toml:"important" json:"important,omitempty"` } type NodeGatewayEndpoint struct { - Label string `toml:"label"` - Priority int `toml:"priority"` - URL string `toml:"url"` - WSURL string `toml:"wsurl"` - SkipMethods []string `toml:"skipMethods"` - AuxMethodNs []string `toml:"auxMethodNs"` - Disabled bool `toml:"disabled"` + Label string `toml:"label" json:"label,omitempty"` + Priority int `toml:"priority" json:"priority,omitempty"` + URL string `toml:"url" json:"url,omitempty"` + WSURL string `toml:"ws_url" json:"wsUrl,omitempty"` + SkipMethods []string `toml:"skip_methods" json:"skipMethods,omitempty"` + AuxMethodNs []string `toml:"aux_method_ns" json:"auxMethodNs,omitempty"` + Disabled bool `toml:"disabled" json:"disabled,omitempty"` +} + +type Currency struct { + ContractAddress *string `toml:"contract_address" json:"contractAddress,omitempty"` + Name string `toml:"name" json:"name,omitempty"` + Symbol string `toml:"symbol" json:"symbol,omitempty"` + Decimals uint64 `toml:"decimals" json:"decimals,omitempty"` + ImageURL string `toml:"image_url" json:"imageUrl,omitempty"` + Native bool `toml:"native" json:"native,omitempty"` + Default bool `toml:"default" json:"default,omitempty"` + Group CurrencyGroup `toml:"group" json:"group,omitempty"` } type NetworkType uint8 @@ -170,7 +177,7 @@ func (n Networks) EthProvidersConfig() ethproviders.Config { res := ethproviders.Config{} for _, network := range n { res[network.Name] = ethproviders.NetworkConfig{ - ID: network.ID, + ID: network.ChainID, URL: network.NodeURL, WSEnabled: network.WSEnabled, WSURL: network.WSURL, From e123767e1fc084a029719339b7357fe39396d7ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 25 Nov 2024 15:26:19 +0100 Subject: [PATCH 3/3] chain name as kay, remove not needed types --- networks/networks.go | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/networks/networks.go b/networks/networks.go index d5c66a6b..f78a9b50 100644 --- a/networks/networks.go +++ b/networks/networks.go @@ -1,23 +1,10 @@ package networks import ( - "strconv" - "github.com/0xsequence/ethkit/ethproviders" ) -type Networks map[ChainID]*Network - -type ChainID string - -func (c ChainID) Uint64() uint64 { - u, _ := strconv.ParseUint(c.String(), 10, 64) - return u -} - -func (c ChainID) String() string { - return string(c) -} +type Networks map[string]*Network type Network struct { ChainID uint64 `toml:"chain_id" json:"chainId,omitempty"` @@ -33,7 +20,6 @@ type Network struct { WSURL string `toml:"ws_url" json:"wsUrl,omitempty"` BlockExplorer *BlockExplorerConfig `toml:"block_explorer" json:"blockExplorer,omitempty"` - NodeGateway *NodeGatewayNetwork `toml:"node_gateway" json:"nodeGateway,omitempty"` NodeURL string `toml:"node_url" json:"nodeUrl,omitempty"` IndexerURL string `toml:"indexer_url" json:"indexerUrl,omitempty"` @@ -53,26 +39,6 @@ type BlockExplorerConfig struct { TxnHashURL string `toml:"txn_hash_url" json:"txnHashUrl,omitempty"` } -type NodeGatewayNetwork struct { - Endpoints []*NodeGatewayEndpoint `toml:"endpoints" json:"endpoints,omitempty"` - Finality uint64 `toml:"finality" json:"finality,omitempty"` - MonitorDisabled bool `toml:"monitor_disabled" json:"monitorDisabled,omitempty"` - MonitorPollInterval string `toml:"monitor_poll_interval" json:"monitorPollInterval,omitempty"` - MonitorBlockRetention uint64 `toml:"monitor_block_retention" json:"monitorBlockRetention,omitempty"` - AlertsOff bool `toml:"alerts_off" json:"alertsOff,omitempty"` - Important bool `toml:"important" json:"important,omitempty"` -} - -type NodeGatewayEndpoint struct { - Label string `toml:"label" json:"label,omitempty"` - Priority int `toml:"priority" json:"priority,omitempty"` - URL string `toml:"url" json:"url,omitempty"` - WSURL string `toml:"ws_url" json:"wsUrl,omitempty"` - SkipMethods []string `toml:"skip_methods" json:"skipMethods,omitempty"` - AuxMethodNs []string `toml:"aux_method_ns" json:"auxMethodNs,omitempty"` - Disabled bool `toml:"disabled" json:"disabled,omitempty"` -} - type Currency struct { ContractAddress *string `toml:"contract_address" json:"contractAddress,omitempty"` Name string `toml:"name" json:"name,omitempty"`