forked from AudiusProject/audius-protocol
/
ethcontracts.go
135 lines (111 loc) · 4.22 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
package ethcontracts
import (
"context"
"errors"
"fmt"
"math/big"
"os"
"mediorum/httputil"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
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 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
}