-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.go
130 lines (107 loc) · 3.11 KB
/
client.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
package dydx
import (
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/go-numb/go-dydx/helpers"
"github.com/go-numb/go-dydx/onboard"
"github.com/go-numb/go-dydx/private"
"github.com/go-numb/go-dydx/public"
"github.com/go-numb/go-dydx/types"
"github.com/umbracle/ethgo/jsonrpc"
)
type Client struct {
options types.Options
Host string
StarkPublicKey string
StarkPrivateKey string
StarkPublicKeyYCoordinate string
ApiKeyCredentials *types.ApiKeyCredentials
ApiTimeout time.Duration
DefaultAddress string
NetworkId int
Web3 *jsonrpc.Client
EthSigner helpers.EthSigner
Private *private.Private
Public *public.Public
OnBoarding *onboard.OnBoarding
Logger *log.Logger
httpClient *http.Client
}
func New(options types.Options) *Client {
client := &Client{
Host: strings.TrimPrefix(options.Host, "/"),
ApiTimeout: 3 * time.Second,
DefaultAddress: options.DefaultEthereumAddress,
StarkPublicKey: options.StarkPublicKey,
StarkPrivateKey: options.StarkPrivateKey,
ApiKeyCredentials: options.ApiKeyCredentials,
NetworkId: options.NetworkId,
Logger: log.New(os.Stderr, "go-dydx ", log.LstdFlags),
httpClient: &http.Client{
Timeout: time.Second * 5,
},
}
// Add http proxy support.
if os.Getenv("HTTP_PROXY") != "" {
proxy, _ := url.Parse(os.Getenv("HTTP_PROXY"))
client.httpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
}
}
if options.Web3 != nil {
networkId := options.NetworkId
if networkId == 0 {
net, _ := options.Web3.Net().Version()
networkId = int(net)
}
client.Web3 = options.Web3
client.EthSigner = &helpers.EthWeb3Signer{Web3: options.Web3}
client.NetworkId = networkId
}
if client.NetworkId == 0 {
client.NetworkId = types.NetworkIdMainnet
}
if options.StarkPrivateKey != "" {
client.StarkPrivateKey = options.StarkPrivateKey
client.EthSigner = &helpers.EthKeySinger{PrivateKey: options.StarkPrivateKey}
}
if options.HttpClient != nil {
client.httpClient = options.HttpClient
}
if options.Logger != nil {
client.Logger = options.Logger
}
client.OnBoarding = &onboard.OnBoarding{
Host: client.Host,
EthSigner: client.EthSigner,
NetworkId: client.NetworkId,
EthAddress: client.DefaultAddress,
Singer: helpers.NewSigner(client.EthSigner, client.NetworkId),
Logger: client.Logger,
}
if options.ApiKeyCredentials == nil {
client.ApiKeyCredentials = client.OnBoarding.RecoverDefaultApiCredentials(client.DefaultAddress)
}
client.Private = &private.Private{
Host: client.Host,
NetworkId: client.NetworkId,
StarkPrivateKey: client.StarkPrivateKey,
DefaultAddress: client.DefaultAddress,
ApiKeyCredentials: client.ApiKeyCredentials,
RateLimit: new(types.RateLimit),
Logger: client.Logger,
HttpClient: client.httpClient,
}
client.Public = &public.Public{
Host: client.Host,
NetworkId: client.NetworkId,
RateLimit: new(types.RateLimit),
Logger: client.Logger,
HttpClient: client.httpClient,
}
return client
}