-
Notifications
You must be signed in to change notification settings - Fork 74
/
web3.go
58 lines (48 loc) · 1.11 KB
/
web3.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
package web3
import (
"strings"
"github.com/chenzhijie/go-web3/eth"
"github.com/chenzhijie/go-web3/rpc"
"github.com/chenzhijie/go-web3/utils"
)
type Web3 struct {
Eth *eth.Eth
Utils *utils.Utils
c *rpc.Client
}
func NewWeb3(provider string) (*Web3, error) {
return NewWeb3WithProxy(provider, "")
}
func NewWeb3WithProxy(provider, proxy string) (*Web3, error) {
c, err := rpc.NewClient(provider, proxy)
if err != nil {
return nil, err
}
e := eth.NewEth(c)
providerLowerStr := strings.ToLower(provider)
if strings.Contains(providerLowerStr, "ropsten") {
e.SetChainId(3)
} else if strings.Contains(providerLowerStr, "kovan") {
e.SetChainId(42)
} else if strings.Contains(providerLowerStr, "rinkeby") {
e.SetChainId(4)
} else if strings.Contains(providerLowerStr, "goerli") {
e.SetChainId(5)
} else {
e.SetChainId(1)
}
u := utils.NewUtils()
w := &Web3{
Eth: e,
Utils: u,
c: c,
}
// Default poll timeout 2 hours
w.Eth.SetTxPollTimeout(7200)
return w, nil
}
func (w *Web3) Version() (string, error) {
var out string
err := w.c.Call("web3_clientVersion", &out)
return out, err
}