/
ethcontracts.go
238 lines (198 loc) · 7.65 KB
/
ethcontracts.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ethcontracts
import (
"context"
"errors"
"fmt"
"math/big"
"os"
"github.com/AudiusProject/audius-protocol/mediorum/httputil"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
type ServiceProvider struct {
Owner string
Endpoint string
SpID int
Type string
BlockNumber int
DelegateOwnerWallet string
}
type NotifierInfo struct {
Wallet string `json:"wallet"`
Endpoint string `json:"endpoint"`
Email string `json:"email"`
}
func GetNotifierForID(ID string, delegateOwnerWallet string) (NotifierInfo, error) {
client, err := ethclient.Dial(os.Getenv("ethProviderUrl"))
if err != nil {
return NotifierInfo{}, fmt.Errorf("failed to connect to Ethereum node: %v", err)
}
ethRegistryAddress := os.Getenv("ethRegistryAddress")
notifierContractAddress, err := GetContractAddr(client, common.HexToAddress(ethRegistryAddress), "TrustedNotifierManagerProxy")
if err != nil {
return NotifierInfo{}, fmt.Errorf("failed to get contract address: %v", err)
}
notifierID := new(big.Int)
notifierID.SetString(ID, 10)
callData, err := trustedNotifierManagerAbi.Pack("getNotifierForID", notifierID)
if err != nil {
return NotifierInfo{}, fmt.Errorf("failed to pack contract method: %v", err)
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
From: common.HexToAddress(delegateOwnerWallet),
To: ¬ifierContractAddress,
Data: callData,
}, nil)
if err != nil {
return NotifierInfo{}, fmt.Errorf("failed to call contract method: %v", err)
}
var output struct {
Wallet common.Address
Endpoint string
Email string
}
err = trustedNotifierManagerAbi.UnpackIntoInterface(&output, "getNotifierForID", result)
if err != nil {
return NotifierInfo{}, fmt.Errorf("failed to unpack contract method result: %v", err)
}
return NotifierInfo{
Wallet: output.Wallet.Hex(),
Endpoint: httputil.RemoveTrailingSlash(output.Endpoint),
Email: httputil.RemoveTrailingSlash(output.Email),
}, nil
}
func GetServiceProviderIdFromEndpoint(endpoint string, delegateOwnerWallet string) (int, error) {
client, err := ethclient.Dial(os.Getenv("ethProviderUrl"))
if err != nil {
return 0, fmt.Errorf("failed to connect to Ethereum node: %v", err)
}
ethRegistryAddress := os.Getenv("ethRegistryAddress")
serviceProviderFactoryAddress, err := GetContractAddr(client, common.HexToAddress(ethRegistryAddress), "ServiceProviderFactory")
if err != nil {
return 0, fmt.Errorf("failed to get contract address: %v", err)
}
callData, err := serviceProviderFactoryAbi.Pack("getServiceProviderIdFromEndpoint", endpoint)
if err != nil {
return 0, fmt.Errorf("failed to pack contract method: %v", err)
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
From: common.HexToAddress(delegateOwnerWallet),
To: &serviceProviderFactoryAddress,
Data: callData,
}, nil)
if err != nil {
return 0, fmt.Errorf("failed to call contract method: %v", err)
}
var output *big.Int
err = serviceProviderFactoryAbi.UnpackIntoInterface(&output, "getServiceProviderIdFromEndpoint", result)
if err != nil {
return 0, fmt.Errorf("failed to unpack contract method result: %v", err)
}
return int(output.Uint64()), nil
}
func GetServiceProviderList(serviceType string) ([]ServiceProvider, error) {
client, err := ethclient.Dial(os.Getenv("ethProviderUrl"))
if err != nil {
return nil, fmt.Errorf("failed to connect to Ethereum node: %v", err)
}
serviceProviderFactoryAddress, err := GetContractAddr(client, common.HexToAddress(os.Getenv("ethRegistryAddress")), "ServiceProviderFactory")
if err != nil {
return nil, fmt.Errorf("failed to get contract address: %v", err)
}
numberOfProviders, err := getTotalServiceTypeProviders(client, serviceProviderFactoryAddress, serviceType)
if err != nil {
return nil, fmt.Errorf("failed to get total number of service providers: %v", err)
}
var providers []ServiceProvider
for i := 1; i <= numberOfProviders; i++ {
provider, err := getServiceEndpointInfo(client, serviceProviderFactoryAddress, serviceType, i)
if err != nil {
return nil, fmt.Errorf("failed to get service provider info: %v", err)
}
if provider.Endpoint != "" {
providers = append(providers, provider)
}
}
return providers, nil
}
func getTotalServiceTypeProviders(client *ethclient.Client, contractAddress common.Address, serviceType string) (int, error) {
var serviceTypeBytes [32]byte
copy(serviceTypeBytes[:], []byte(serviceType))
callData, err := serviceProviderFactoryAbi.Pack("getTotalServiceTypeProviders", serviceTypeBytes)
if err != nil {
return 0, fmt.Errorf("failed to pack contract method: %v", err)
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
To: &contractAddress,
Data: callData,
}, nil)
if err != nil {
return 0, fmt.Errorf("failed to call contract method: %v", err)
}
var totalProviders *big.Int
err = serviceProviderFactoryAbi.UnpackIntoInterface(&totalProviders, "getTotalServiceTypeProviders", result)
if err != nil {
return 0, fmt.Errorf("failed to unpack contract method result: %v", err)
}
return int(totalProviders.Int64()), nil
}
func getServiceEndpointInfo(client *ethclient.Client, contractAddress common.Address, serviceType string, serviceId int) (ServiceProvider, error) {
var serviceTypeBytes [32]byte
copy(serviceTypeBytes[:], []byte(serviceType))
callData, err := serviceProviderFactoryAbi.Pack("getServiceEndpointInfo", serviceTypeBytes, big.NewInt(int64(serviceId)))
if err != nil {
return ServiceProvider{}, fmt.Errorf("failed to pack contract method: %v", err)
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
To: &contractAddress,
Data: callData,
}, nil)
if err != nil {
return ServiceProvider{}, fmt.Errorf("failed to call contract method: %v", err)
}
var info struct {
Owner common.Address
Endpoint string
BlockNumber *big.Int
DelegateOwnerWallet common.Address
}
err = serviceProviderFactoryAbi.UnpackIntoInterface(&info, "getServiceEndpointInfo", result)
if err != nil {
return ServiceProvider{}, fmt.Errorf("failed to unpack contract method result: %v", err)
}
return ServiceProvider{
Owner: info.Owner.Hex(),
Endpoint: info.Endpoint,
SpID: serviceId,
Type: serviceType,
BlockNumber: int(info.BlockNumber.Int64()),
DelegateOwnerWallet: info.DelegateOwnerWallet.Hex(),
}, nil
}
func GetContractAddr(client *ethclient.Client, ethRegistryAddr common.Address, contractRegistryKey string) (common.Address, error) {
var bytes32Key [32]byte
copy(bytes32Key[:], []byte(contractRegistryKey))
// The actual method is getContract(bytes32), but it's overloaded and go-ethereum is dumb.
// I debugged this by checking the output of ethRegistryAbi.Methods
callData, err := ethRegistryAbi.Pack("getContract0", bytes32Key)
if err != nil {
return common.Address{}, errors.New("failed to pack getContract method: " + err.Error())
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
To: ðRegistryAddr,
Data: callData,
}, nil)
if err != nil {
return common.Address{}, errors.New("failed to call getContract method: " + err.Error())
}
var contractAddr common.Address
err = ethRegistryAbi.UnpackIntoInterface(&contractAddr, "getContract0", result)
if err != nil {
return common.Address{}, errors.New("failed to unpack getContract method result: " + err.Error())
}
if contractAddr.String() == "0x0000000000000000000000000000000000000000" {
return common.Address{}, errors.New("contract not found")
}
return contractAddr, nil
}