forked from go-numb/go-dydx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (87 loc) · 2.65 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
package dydx
import (
"github.com/sirupsen/logrus"
"strings"
"time"
"github.com/huangjosh/go-dydx/types"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/huangjosh/go-dydx/helpers"
"github.com/huangjosh/go-dydx/onboard"
"github.com/huangjosh/go-dydx/private"
"github.com/huangjosh/go-dydx/public"
)
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 *logrus.Entry
}
func New(options types.Options) *Client {
instance := logrus.New()
instance.SetLevel(logrus.WarnLevel)
logger := instance.WithFields(logrus.Fields{"go-dydx": "lib"})
client := &Client{
Host: strings.TrimPrefix(options.Host, "/"),
ApiTimeout: 3 * time.Second,
DefaultAddress: options.DefaultEthereumAddress,
StarkPublicKey: options.StarkPublicKey,
StarkPrivateKey: options.StarkPrivateKey,
ApiKeyCredentials: options.ApiKeyCredentials,
Logger: logger,
}
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}
}
client.OnBoarding = &onboard.OnBoarding{
Host: client.Host,
EthSigner: client.EthSigner,
NetworkId: client.NetworkId,
EthAddress: client.DefaultAddress,
Singer: helpers.NewSigner(client.EthSigner, client.NetworkId),
Logger: 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: logger,
}
client.Public = &public.Public{
Host: client.Host,
NetworkId: client.NetworkId,
RateLimit: new(types.RateLimit),
Logger: logger,
}
return client
}