forked from bbenzikry/go-1inch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprove.go
65 lines (55 loc) · 2.21 KB
/
approve.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
package go1inch
import (
"context"
"errors"
"net/http"
)
// ApproveTransaction gets calldata for approve transaction and spender address
// Do not combine amount parameter with infinity parameter, only one must be sent.
// infinity will overwrite amount
// amount is set in minimal divisible units: for example, to unlock 1 DAI, amount should be 1000000000000000000, to unlock 1.03 USDC, amount should be 1030000.
func (c *Client) ApproveTransaction(ctx context.Context, network Network, tokenAddress string, opts *ApproveTransactionOpts) (*ApproveTransactionRes, int, error) {
endpoint := "/approve/transaction"
if tokenAddress == "" {
return nil, 0, errors.New("required parameter is missing")
}
queries := make(map[string]interface{})
queries["tokenAddress"] = tokenAddress
if opts != nil {
if opts.Amount != "" {
queries["amount"] = opts.Amount
}
}
var dataRes ApproveTransactionRes
statusCode, _, err := c.doRequest(ctx, network, endpoint, http.MethodGet, &dataRes, nil, queries)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
// ApproveSpender gets the address to which you need to approve before the swap transaction
func (c *Client) ApproveSpender(ctx context.Context, network Network) (*ApproveSpenderRes, int, error) {
endpoint := "/approve/spender"
var dataRes ApproveSpenderRes
statusCode, _, err := c.doRequest(ctx, network, endpoint, http.MethodGet, &dataRes, nil, nil)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
// ApproveAllowance gets the number of tokens that the 1inch router is allowed to spend
func (c *Client) ApproveAllowance(ctx context.Context, network Network, tokenAddress, walletAddress string) (*ApproveAllowanceRes, int, error) {
endpoint := "/approve/allowance"
if tokenAddress == "" || walletAddress == "" {
return nil, 0, errors.New("required parameter is missing")
}
queries := make(map[string]interface{})
queries["tokenAddress"] = tokenAddress
queries["walletAddress"] = walletAddress
var dataRes ApproveAllowanceRes
statusCode, _, err := c.doRequest(ctx, network, endpoint, http.MethodGet, &dataRes, nil, queries)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}