-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
171 lines (150 loc) · 3.82 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package go_cowswap
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/itsahedge/go-cowswap/subgraph"
"io/ioutil"
"math/big"
"net/http"
)
type Client struct {
Options ConfigOpts
Network string
Host string
Http *http.Client
Eip712OrderTypes apitypes.Types
TypedDataDomain apitypes.TypedDataDomain
RpcUrl string
EthClient *ethclient.Client
ChainId *big.Int
ChainIdInt int
TransactionSigner *TransactionSigner
Subgraph *subgraph.Client
}
// NewClient - Client to interact with CowSwap
func NewClient(options ConfigOpts) (*Client, error) {
client := &Client{
Options: options,
Http: &http.Client{},
Eip712OrderTypes: Eip712OrderTypes,
TypedDataDomain: Domain,
Network: options.Network,
Host: options.Host,
RpcUrl: options.RpcUrl,
}
if err := setClientNetwork(client, options); err != nil {
return nil, err
}
if err := setClientRPC(client, options); err != nil {
return nil, err
}
if err := setClientAuth(client, options); err != nil {
return nil, err
}
return client, nil
}
func setClientNetwork(client *Client, options ConfigOpts) error {
if options.Network != "" {
client.Network = options.Network
client.Host = HostConfig[options.Network]
chainId := ChainIds[options.Network]
client.ChainIdInt = chainId
client.ChainId = big.NewInt(int64(chainId))
subgraph, err := subgraph.NewSubgraphClient(SubgraphConfig[options.Network])
if err != nil {
return err
}
client.Subgraph = subgraph
}
return nil
}
func setClientRPC(client *Client, options ConfigOpts) error {
if options.RpcUrl != "" {
client.RpcUrl = options.RpcUrl
ethClient, err := ethclient.Dial(client.RpcUrl)
if err != nil {
return err
}
client.EthClient = ethClient
chainId, err := ethClient.ChainID(context.Background())
if err != nil {
return err
}
client.ChainId = chainId
}
return nil
}
func setClientAuth(client *Client, options ConfigOpts) error {
if options.PrivateKey != "" {
transactionSigner, err := NewSigner(options.PrivateKey, client.ChainId)
if err != nil {
return fmt.Errorf("NewSigner err: %v\n", err)
}
client.TransactionSigner = transactionSigner
}
return nil
}
type ErrorCowResponse struct {
Code int `json:"code"`
ErrorType string `json:"errorType"`
Description string `json:"description"`
}
func (e *ErrorCowResponse) Error() string {
return fmt.Sprintf("api err %d: %s, %s", e.Code, e.ErrorType, e.Description)
}
func (c *Client) doRequest(ctx context.Context, endpoint, method string, expRes interface{}, reqData interface{}, opts ...map[string]interface{}) (int, error) {
callURL := fmt.Sprintf("%s%s", c.Host, endpoint)
var dataReq []byte
var err error
if reqData != nil {
dataReq, err = json.Marshal(reqData)
if err != nil {
return 0, err
}
}
if len(opts) > 0 && len(opts[0]) > 0 {
setQueryParam(&callURL, opts)
}
req, err := http.NewRequestWithContext(ctx, method, callURL, bytes.NewBuffer(dataReq))
if err != nil {
return 0, err
}
resp, err := c.Http.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
switch resp.StatusCode {
case 200, 201:
if expRes != nil {
err = json.Unmarshal(body, expRes)
if err != nil {
return 0, err
}
}
return resp.StatusCode, nil
default:
return resp.StatusCode, fmt.Errorf("%s", body)
}
}
func setQueryParam(endpoint *string, params []map[string]interface{}) {
var first = true
for _, param := range params {
for i := range param {
if first {
*endpoint = fmt.Sprintf("%s?%s=%v", *endpoint, i, param[i])
first = false
} else {
*endpoint = fmt.Sprintf("%s&%s=%v", *endpoint, i, param[i])
}
}
}
}