forked from bbenzikry/go-1inch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.go
82 lines (72 loc) · 2.05 KB
/
swap.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
package go1inch
import (
"context"
"errors"
"net/http"
)
// Swap gets swap for an aggregated swap which can be used with a web3 provider to send the transaction
func (c *Client) Swap(ctx context.Context, network Network, src, dst, amount, fromAddress string, slippage int64, opts *SwapOpts) (*SwapRes, int, http.Header, error) {
endpoint := "/swap"
if src == "" || dst == "" || amount == "" || fromAddress == "" {
return nil, 0, nil, errors.New("required parameter is missing")
}
queries := make(map[string]interface{})
queries["src"] = src
queries["dst"] = dst
queries["amount"] = amount
queries["from"] = fromAddress
queries["slippage"] = slippage
if opts != nil {
queries["burnChi"] = opts.BurnChi
queries["allowPartialFill"] = opts.AllowPartialFill
queries["disableEstimate"] = opts.DisableEstimate
if opts.Protocols != "" {
queries["protocols"] = opts.Protocols
}
if opts.Receiver != "" {
queries["receiver"] = opts.Receiver
}
if opts.ReferrerAddress != "" {
queries["referrer"] = opts.ReferrerAddress
}
if opts.Fee != "" {
queries["fee"] = opts.Fee
}
if opts.GasPrice != "" {
queries["gasPrice"] = opts.GasPrice
}
if opts.ComplexityLevel != "" {
queries["complexityLevel"] = opts.ComplexityLevel
}
if opts.ConnectorTokens != "" {
queries["connectorTokens"] = opts.ConnectorTokens
}
if opts.GasLimit != "" {
queries["gasLimit"] = opts.GasLimit
}
if opts.Parts != "" {
queries["parts"] = opts.Parts
}
if opts.VirtualParts != "" {
queries["virtualParts"] = opts.VirtualParts
}
if opts.MainRouteParts != "" {
queries["mainRouteParts"] = opts.MainRouteParts
}
if opts.IncludeGas {
queries["includeGas"] = true
}
if opts.IncludeProtocols {
queries["includeProtocols"] = true
}
if opts.IncludeTokensInfo {
queries["includeTokensInfo"] = true
}
}
var dataRes SwapRes
statusCode, headers, err := c.doRequest(ctx, network, endpoint, "GET", &dataRes, nil, queries)
if err != nil {
return nil, statusCode, headers, err
}
return &dataRes, statusCode, headers, nil
}