-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.go
More file actions
63 lines (58 loc) · 1.56 KB
/
Copy pathchain.go
File metadata and controls
63 lines (58 loc) · 1.56 KB
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
package go_ethutils
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"math/big"
"strconv"
)
type Chain struct {
ChainId int64
RpcAddr string
WsAddr string
Explore string
GasPrice *big.Int
WrapToken common.Address
BatchContract common.Address
Client *ethclient.Client
WsClient *rpc.Client
}
func GetClient(rpcHost string) *ethclient.Client {
// Connect the client
client, err := ethclient.Dial(rpcHost)
if err != nil {
fmt.Printf("client connection error: " + err.Error())
return nil
}
return client
}
func GetWsClient(rpcHost string) *rpc.Client {
// Connect the client
if rpcHost == "" {
return nil
}
client, err := rpc.Dial(rpcHost)
if err != nil {
fmt.Printf("client connection error: " + err.Error())
return nil
}
return client
}
func ParserChainInfo(chainInfoMap map[string]string) *Chain {
// TODO: check key
_chainId, _ := strconv.Atoi(chainInfoMap["chain_id"])
_gasPrice, _ := strconv.ParseFloat(chainInfoMap["gas_price"], 64)
chainInfo := &Chain{
ChainId: int64(_chainId),
RpcAddr: chainInfoMap["rpc_addr"],
WsAddr: chainInfoMap["ws_addr"],
Explore: chainInfoMap["explore"],
GasPrice: big.NewInt(int64(_gasPrice * GWEI)),
WrapToken: HexToAddress(chainInfoMap["wrap_token"]),
BatchContract: HexToAddress(chainInfoMap["batch_contract"]),
}
chainInfo.Client = GetClient(chainInfo.RpcAddr)
chainInfo.WsClient = GetWsClient(chainInfo.WsAddr)
return chainInfo
}