Skip to content

Commit 2353f9e

Browse files
3465 - Chain Configuration (#175)
* networks * json tags * chain name as kay, remove not needed types
1 parent 36a596e commit 2353f9e

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

networks/networks.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package networks
2+
3+
import (
4+
"github.com/0xsequence/ethkit/ethproviders"
5+
)
6+
7+
type Networks map[string]*Network
8+
9+
type Network struct {
10+
ChainID uint64 `toml:"chain_id" json:"chainId,omitempty"`
11+
Name string `toml:"name" json:"name,omitempty"`
12+
Title string `toml:"title" json:"title,omitempty"`
13+
Type NetworkType `toml:"type" json:"type,omitempty"`
14+
LogoURL string `toml:"logo_url" json:"logoUrl,omitempty"`
15+
ENSAddress string `toml:"ens_address" json:"ensAddress,omitempty"`
16+
AuthChain bool `toml:"auth_chain" json:"authChain,omitempty"`
17+
Deprecated bool `toml:"deprecated" json:"deprecated,omitempty"`
18+
Disabled bool `toml:"disabled" json:"disabled,omitempty"`
19+
WSEnabled bool `toml:"ws_enabled" json:"wsEnabled,omitempty"`
20+
WSURL string `toml:"ws_url" json:"wsUrl,omitempty"`
21+
22+
BlockExplorer *BlockExplorerConfig `toml:"block_explorer" json:"blockExplorer,omitempty"`
23+
24+
NodeURL string `toml:"node_url" json:"nodeUrl,omitempty"`
25+
IndexerURL string `toml:"indexer_url" json:"indexerUrl,omitempty"`
26+
RelayerURL string `toml:"relayer_url" json:"relayerUrl,omitempty"`
27+
InternalNodeURL string `toml:"internal_node_url" json:"-"`
28+
InternalIndexerURL string `toml:"internal_indexer_url" json:"-"`
29+
InternalRelayerURL string `toml:"internal_relayer_url" json:"-"`
30+
31+
NativeToken *Currency `toml:"native_token" json:"nativeToken,omitempty"`
32+
Currencies []*Currency `toml:"currencies" json:"currencies,omitempty"`
33+
}
34+
35+
type BlockExplorerConfig struct {
36+
Name string `toml:"name" json:"name,omitempty"`
37+
RootURL string `toml:"root_url" json:"rootUrl,omitempty"`
38+
AddressURL string `toml:"address_url" json:"addressUrl,omitempty"`
39+
TxnHashURL string `toml:"txn_hash_url" json:"txnHashUrl,omitempty"`
40+
}
41+
42+
type Currency struct {
43+
ContractAddress *string `toml:"contract_address" json:"contractAddress,omitempty"`
44+
Name string `toml:"name" json:"name,omitempty"`
45+
Symbol string `toml:"symbol" json:"symbol,omitempty"`
46+
Decimals uint64 `toml:"decimals" json:"decimals,omitempty"`
47+
ImageURL string `toml:"image_url" json:"imageUrl,omitempty"`
48+
Native bool `toml:"native" json:"native,omitempty"`
49+
Default bool `toml:"default" json:"default,omitempty"`
50+
Group CurrencyGroup `toml:"group" json:"group,omitempty"`
51+
}
52+
53+
type NetworkType uint8
54+
55+
const (
56+
NetworkTypeUnknown NetworkType = 0
57+
NetworkTypeMainnet NetworkType = 1
58+
NetworkTypeTestnet NetworkType = 2
59+
)
60+
61+
var OrderSideName = map[uint8]string{
62+
0: "unknown",
63+
1: "mainnet",
64+
2: "testnet",
65+
}
66+
67+
var OrderSideValue = map[string]uint8{
68+
"unknown": 0,
69+
"mainnet": 1,
70+
"testnet": 2,
71+
}
72+
73+
func (x NetworkType) String() string {
74+
return OrderSideName[uint8(x)]
75+
}
76+
77+
func (x NetworkType) MarshalText() ([]byte, error) {
78+
return []byte(OrderSideName[uint8(x)]), nil
79+
}
80+
81+
func (x *NetworkType) UnmarshalText(b []byte) error {
82+
*x = NetworkType(OrderSideValue[string(b)])
83+
return nil
84+
}
85+
86+
// Grouping currencies for cross chain purchases
87+
type CurrencyGroup uint8
88+
89+
const (
90+
CurrencyGroupNone CurrencyGroup = iota
91+
CurrencyGroupUSDC
92+
CurrencyGroupUSDCTestnet
93+
CurrencyGroupWETH
94+
)
95+
96+
var CurrencyGroupName = map[uint8]string{
97+
0: "none",
98+
1: "usdc",
99+
2: "usdc_testnet",
100+
3: "weth",
101+
}
102+
103+
var CurrencyGroupValue = map[string]uint8{
104+
"none": 0,
105+
"usdc": 1,
106+
"usdc_testnet": 2,
107+
"weth": 3,
108+
}
109+
110+
func (x CurrencyGroup) String() string {
111+
return CurrencyGroupName[uint8(x)]
112+
}
113+
114+
func (x CurrencyGroup) MarshalText() ([]byte, error) {
115+
return []byte(CurrencyGroupName[uint8(x)]), nil
116+
}
117+
118+
func (x *CurrencyGroup) UnmarshalText(b []byte) error {
119+
*x = CurrencyGroup(CurrencyGroupValue[string(b)])
120+
return nil
121+
}
122+
123+
func (n *Network) IsMainnet() bool {
124+
return n.Type == NetworkTypeMainnet
125+
}
126+
127+
func (n *Network) IsTesnet() bool {
128+
return n.Type == NetworkTypeTestnet
129+
}
130+
131+
func (n Networks) Active() Networks {
132+
res := Networks{}
133+
for name, network := range n {
134+
if !network.Disabled {
135+
res[name] = network
136+
}
137+
}
138+
139+
return res
140+
}
141+
142+
func (n Networks) EthProvidersConfig() ethproviders.Config {
143+
res := ethproviders.Config{}
144+
for _, network := range n {
145+
res[network.Name] = ethproviders.NetworkConfig{
146+
ID: network.ChainID,
147+
URL: network.NodeURL,
148+
WSEnabled: network.WSEnabled,
149+
WSURL: network.WSURL,
150+
AuthChain: network.AuthChain,
151+
Testnet: network.IsTesnet(),
152+
Disabled: network.Disabled,
153+
}
154+
}
155+
156+
return res
157+
}

0 commit comments

Comments
 (0)