Skip to content

Commit e698d80

Browse files
networks
1 parent 36a596e commit e698d80

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

networks/networks.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
ID uint64 `toml:"id"`
11+
Name string `toml:"name"`
12+
Title string `toml:"title"`
13+
Type NetworkType `toml:"type"`
14+
15+
LogoURL string `toml:"logoUrl"`
16+
17+
ENSAddress string `toml:"ensAddress"`
18+
19+
AuthChain bool `toml:"authChain"`
20+
Deprecated bool `toml:"deprecated"`
21+
Disabled bool `toml:"disabled"`
22+
23+
NodeURL string `toml:"nodeUrl"`
24+
IndexerURL string `toml:"indexerUrl"`
25+
RelayerURL string `toml:"relayerUrl"`
26+
27+
InternalNodeURL string `toml:"internalNodeUrl"`
28+
InternalIndexerURL string `toml:"internalIndexerUrl"`
29+
InternalRelayerURL string `toml:"internalRelayerUrl"`
30+
31+
WSEnabled bool `toml:"wsEnabled"`
32+
WSURL string `toml:"wsUrl"`
33+
34+
BlockExplorer *BlockExplorerConfig `toml:"blockExplorer"`
35+
36+
NativeToken *Currency `toml:"nativeToken"`
37+
Currencies []*Currency `toml:"currencies"`
38+
39+
NodeGateway *NodeGatewayNetwork `toml:"nodeGateway"`
40+
}
41+
42+
type Currency struct {
43+
ContractAddress *string `toml:"contractAddress"`
44+
Name string `toml:"name"`
45+
Symbol string `toml:"symbol"`
46+
Decimals uint64 `toml:"decimals"`
47+
ImageURL string `toml:"imageUrl"`
48+
Native bool `toml:"native"`
49+
Default bool `toml:"default"`
50+
Group CurrencyGroup `toml:"group"`
51+
}
52+
53+
type BlockExplorerConfig struct {
54+
Name string `toml:"name"`
55+
RootURL string `toml:"rootUrl"`
56+
AddressURL string `toml:"addressUrl"`
57+
TxnHashURL string `toml:"txnHashUrl"`
58+
}
59+
60+
type NodeGatewayNetwork struct {
61+
Endpoints []*NodeGatewayEndpoint `toml:"endpoints"`
62+
Finality uint64 `toml:"finality"`
63+
MonitorDisabled bool `toml:"monitorDisabled"`
64+
MonitorPollInterval string `toml:"monitorPollInterval"`
65+
MonitorBlockRetention uint64 `toml:"monitorBlockRetention"`
66+
AlertsOff bool `toml:"alertsOff"`
67+
Important bool `toml:"important"`
68+
}
69+
70+
type NodeGatewayEndpoint struct {
71+
Label string `toml:"label"`
72+
Priority int `toml:"priority"`
73+
URL string `toml:"url"`
74+
WSURL string `toml:"wsurl"`
75+
SkipMethods []string `toml:"skipMethods"`
76+
AuxMethodNs []string `toml:"auxMethodNs"`
77+
Disabled bool `toml:"disabled"`
78+
}
79+
80+
type NetworkType uint8
81+
82+
const (
83+
NetworkTypeUnknown NetworkType = 0
84+
NetworkTypeMainnet NetworkType = 1
85+
NetworkTypeTestnet NetworkType = 2
86+
)
87+
88+
var OrderSideName = map[uint8]string{
89+
0: "unknown",
90+
1: "mainnet",
91+
2: "testnet",
92+
}
93+
94+
var OrderSideValue = map[string]uint8{
95+
"unknown": 0,
96+
"mainnet": 1,
97+
"testnet": 2,
98+
}
99+
100+
func (x NetworkType) String() string {
101+
return OrderSideName[uint8(x)]
102+
}
103+
104+
func (x NetworkType) MarshalText() ([]byte, error) {
105+
return []byte(OrderSideName[uint8(x)]), nil
106+
}
107+
108+
func (x *NetworkType) UnmarshalText(b []byte) error {
109+
*x = NetworkType(OrderSideValue[string(b)])
110+
return nil
111+
}
112+
113+
// Grouping currencies for cross chain purchases
114+
type CurrencyGroup uint8
115+
116+
const (
117+
CurrencyGroupNone CurrencyGroup = iota
118+
CurrencyGroupUSDC
119+
CurrencyGroupUSDCTestnet
120+
CurrencyGroupWETH
121+
)
122+
123+
var CurrencyGroupName = map[uint8]string{
124+
0: "none",
125+
1: "usdc",
126+
2: "usdc_testnet",
127+
3: "weth",
128+
}
129+
130+
var CurrencyGroupValue = map[string]uint8{
131+
"none": 0,
132+
"usdc": 1,
133+
"usdc_testnet": 2,
134+
"weth": 3,
135+
}
136+
137+
func (x CurrencyGroup) String() string {
138+
return CurrencyGroupName[uint8(x)]
139+
}
140+
141+
func (x CurrencyGroup) MarshalText() ([]byte, error) {
142+
return []byte(CurrencyGroupName[uint8(x)]), nil
143+
}
144+
145+
func (x *CurrencyGroup) UnmarshalText(b []byte) error {
146+
*x = CurrencyGroup(CurrencyGroupValue[string(b)])
147+
return nil
148+
}
149+
150+
func (n *Network) IsMainnet() bool {
151+
return n.Type == NetworkTypeMainnet
152+
}
153+
154+
func (n *Network) IsTesnet() bool {
155+
return n.Type == NetworkTypeTestnet
156+
}
157+
158+
func (n Networks) Active() Networks {
159+
res := Networks{}
160+
for name, network := range n {
161+
if !network.Disabled {
162+
res[name] = network
163+
}
164+
}
165+
166+
return res
167+
}
168+
169+
func (n Networks) EthProvidersConfig() ethproviders.Config {
170+
res := ethproviders.Config{}
171+
for _, network := range n {
172+
res[network.Name] = ethproviders.NetworkConfig{
173+
ID: network.ID,
174+
URL: network.NodeURL,
175+
WSEnabled: network.WSEnabled,
176+
WSURL: network.WSURL,
177+
AuthChain: network.AuthChain,
178+
Testnet: network.IsTesnet(),
179+
Disabled: network.Disabled,
180+
}
181+
}
182+
183+
return res
184+
}

0 commit comments

Comments
 (0)