-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuns_builder.go
215 lines (175 loc) · 6.17 KB
/
uns_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package resolution
import (
"net/http"
"strconv"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/unstoppabledomains/resolution-go/v3/udclient"
"github.com/unstoppabledomains/resolution-go/v3/uns/contracts/proxyreader"
)
// UnsBuilder is a builder to setup and build instance of Uns
type UnsBuilder interface {
// SetContractBackend set Ethereum backend for communication with UNS registry
SetContractBackend(backend bind.ContractBackend) UnsBuilder
// SetContractBackendProviderUrl set Ethereum backend Rpc URL
SetContractBackendProviderUrl(url string) UnsBuilder
// SetL2ContractBackend set Ethereum backend for communication with UNS L2registry
SetL2ContractBackend(backend bind.ContractBackend) UnsBuilder
// SetL2ContractBackendProviderUrl set Polygon backend Rpc URL
SetL2ContractBackendProviderUrl(url string) UnsBuilder
// SetMetadataClient set http backend for communication with ERC721 metadata server
SetMetadataClient(backend MetadataClient) UnsBuilder
// SetUdClient set http proxy backends for communication with UNS registry
SetUdClient(apiKey string) UnsBuilder
// SetEthereumNetwork set Ethereum network for communication with UNS registry
SetEthereumNetwork(network string) UnsBuilder
// SetL2EthereumNetwork set Ethereum network for communication with UNS L2 registry
SetL2EthereumNetwork(network string) UnsBuilder
// Build Uns instance
Build() (*Uns, error)
}
type unsBuilder struct {
l1ContractBackend bind.ContractBackend
l2ContractBackend bind.ContractBackend
metadataClient MetadataClient
l1Network string
l2Network string
l1ProviderUrl string
l2ProviderUrl string
}
// NewUnsBuilder Creates builder to setup new instance of Uns
func NewUnsBuilder() UnsBuilder {
return &unsBuilder{
l1Network: "mainnet",
l2Network: "polygon",
}
}
// SetContractBackend set Ethereum backend for communication with UNS registry
func (cb *unsBuilder) SetContractBackend(backend bind.ContractBackend) UnsBuilder {
cb.l1ContractBackend = backend
return cb
}
// SetContractBackendProviderUrl set Ethereum backend Rpc URL
func (cb *unsBuilder) SetContractBackendProviderUrl(url string) UnsBuilder {
cb.l1ProviderUrl = url
return cb
}
// SetL2ContractBackend set Polygon backend for communication with UNS registry
func (cb *unsBuilder) SetL2ContractBackend(backend bind.ContractBackend) UnsBuilder {
cb.l2ContractBackend = backend
return cb
}
// SetL2ContractBackendProviderUrl set Polygon backend Rpc URL
func (cb *unsBuilder) SetL2ContractBackendProviderUrl(url string) UnsBuilder {
cb.l2ProviderUrl = url
return cb
}
func (cb *unsBuilder) SetMetadataClient(client MetadataClient) UnsBuilder {
cb.metadataClient = client
return cb
}
func (cb *unsBuilder) SetUdClient(apiKey string) UnsBuilder {
client, err := udclient.Dial(apiKey)
if err != nil {
panic(err)
}
cb.l1ContractBackend = client.L1ContractBackend
cb.l2ContractBackend = client.L2ContractBackend
return cb
}
func (cb *unsBuilder) SetEthereumNetwork(network string) UnsBuilder {
cb.l1Network = network
return cb
}
func (cb *unsBuilder) SetL2EthereumNetwork(network string) UnsBuilder {
cb.l2Network = network
return cb
}
func (cb *unsBuilder) BuildService(netContracts contracts, contractBackend bind.ContractBackend, provider string) (*UnsService, error) {
unsProxyReader := common.HexToAddress(netContracts["ProxyReader"].Address)
cnsDefaultResolver := common.HexToAddress(netContracts["Resolver"].Address)
unsRegistry := common.HexToAddress(netContracts["UNSRegistry"].Address)
unsStartingEventsBlock, _ := strconv.ParseUint(netContracts["UNSRegistry"].DeploymentBlock[2:], 16, 32)
cnsStartingEventsBlock, _ := strconv.ParseUint(netContracts["Resolver"].DeploymentBlock[2:], 16, 32)
if contractBackend == nil {
backend, err := ethclient.Dial(provider)
if err != nil {
return nil, err
}
contractBackend = backend
}
proxyReaderContract, err := proxyreader.NewContract(unsProxyReader, contractBackend)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
supportedKeys, err := newSupportedKeys()
if err != nil {
return nil, err
}
var metadataServiceUrl string
if cb.l1Network == "mainnet" && cb.l2Network == "polygon" {
metadataServiceUrl = udclient.MetadataMainnetBaseUrl
} else {
metadataServiceUrl = udclient.MetadataTestnetBaseUrl
}
return &UnsService{
proxyReader: proxyReaderContract,
supportedKeys: supportedKeys,
contractBackend: contractBackend,
metadataClient: cb.metadataClient,
cnsDefaultResolver: cnsDefaultResolver,
unsRegistry: unsRegistry,
unsStartingEventsBlock: unsStartingEventsBlock,
cnsStartingEventsBlock: cnsStartingEventsBlock,
metadataServiceUrl: metadataServiceUrl,
}, nil
}
// Build Uns instance
func (cb *unsBuilder) Build() (*Uns, error) {
contracts, err := newContracts()
if err != nil {
return nil, err
}
if cb.metadataClient == nil {
cb.metadataClient = &http.Client{}
}
if cb.l1Network == "" {
return nil, &UnsConfigurationError{Layer: Layer1, InvalidField: "network"}
}
if cb.l2Network == "" {
return nil, &UnsConfigurationError{Layer: Layer2, InvalidField: "network"}
}
if cb.l1ContractBackend == nil && cb.l1ProviderUrl == "" {
return nil, &UnsConfigurationError{Layer: Layer1, InvalidField: "contractBackend"}
}
if cb.l2ContractBackend == nil && cb.l2ProviderUrl == "" {
return nil, &UnsConfigurationError{Layer: Layer2, InvalidField: "contractBackend"}
}
l1Service, err := cb.BuildService(contracts[cb.l1Network], cb.l1ContractBackend, cb.l1ProviderUrl)
if err != nil {
return nil, err
}
l1Service.networkId = NetworkNameToId[cb.l1Network]
l1Service.blockchainProviderUrl = cb.l1ProviderUrl
l1Service.Layer = Layer1
l2Service, err := cb.BuildService(contracts[cb.l2Network], cb.l2ContractBackend, cb.l2ProviderUrl)
if err != nil {
return nil, err
}
l2Service.Layer = Layer2
l2Service.networkId = NetworkNameToId[cb.l2Network]
l2Service.blockchainProviderUrl = cb.l2ProviderUrl
zService, err := NewZnsBuilder().Build()
if err != nil {
return nil, err
}
return &Uns{
*l1Service,
*l2Service,
*zService,
}, nil
}