-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivvi.go
More file actions
146 lines (116 loc) · 3.61 KB
/
divvi.go
File metadata and controls
146 lines (116 loc) · 3.61 KB
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
package ethutils
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"github.com/ethereum/go-ethereum/common"
)
type SubmitBody struct {
TxHash string `json:"txHash"`
ChainID int64 `json:"chainId"`
}
const (
divviMagicPrefix = "6decb85d"
knownEmptyAddressArrayEncoding = "00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000"
divviAPIEndpoint = "https://api.divvi.xyz/submitReferral"
userAgent = "ethutils/v1.x.x"
contentType = "application/json"
)
var (
referalTagFormat1Byte = []byte{0x01}
ErrDivviClientNotInitialized = errors.New("divvi client not initialized")
)
func (p *Provider) GetReferalTag(user common.Address) []byte {
encodedBytes := ConcatBytes(encodeAddress(user), encodeAddress(p.DivviConsumerAddress), encodeAddressArrayEmpty())
payloadLen := []byte{byte(len(encodedBytes) >> 8), byte(len(encodedBytes))}
magicPrefix, _ := hex.DecodeString(divviMagicPrefix)
payload := ConcatBytes(magicPrefix, referalTagFormat1Byte, payloadLen, encodedBytes)
return payload
}
func (p *Provider) SubmitReferral(ctx context.Context, txHash common.Hash) error {
if p.DivviClient == nil {
return ErrDivviClientNotInitialized
}
body := SubmitBody{
TxHash: txHash.Hex(),
ChainID: CeloMainnet,
}
b, err := json.Marshal(&body)
if err != nil {
return err
}
resp, err := p.requestWithCtx(ctx, http.MethodPost, divviAPIEndpoint, bytes.NewBuffer(b))
if err != nil {
return err
}
if err := parseResponse(resp); err != nil {
return err
}
return nil
}
func (p *Provider) requestWithCtx(ctx context.Context, method string, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
return p.do(req)
}
func (p *Provider) do(req *http.Request) (*http.Response, error) {
builtRequest, err := setHeaders(req)
if err != nil {
return nil, err
}
return p.DivviClient.Do(builtRequest)
}
func setHeaders(req *http.Request) (*http.Request, error) {
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", contentType)
req.Header.Set("Content-Type", contentType)
return req, nil
}
func parseResponse(resp *http.Response) error {
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("divvi api error: status=%s", resp.Status)
}
return fmt.Errorf("divvi api error: status=%s body=%s", resp.Status, string(b))
}
return nil
}
func encodeAddress(address common.Address) []byte {
return common.LeftPadBytes(address.Bytes(), 32)
}
func encodeAddressArray(addresses []common.Address) []byte {
arrayDataOffset := big.NewInt(96)
arrayDataOffsetBytes := common.LeftPadBytes(arrayDataOffset.Bytes(), 32)
arrayLength := big.NewInt(int64(len(addresses)))
arrayLengthBytes := common.LeftPadBytes(arrayLength.Bytes(), 32)
var addressesBytes []byte
for _, addr := range addresses {
addressesBytes = append(addressesBytes, encodeAddress(addr)...)
}
result := make([]byte, 0, len(arrayDataOffsetBytes)+len(arrayLengthBytes)+len(addressesBytes))
result = append(result, arrayDataOffsetBytes...)
result = append(result, arrayLengthBytes...)
result = append(result, addressesBytes...)
return result
}
func encodeAddressArrayEmpty() []byte {
emptyBytes, _ := hex.DecodeString(knownEmptyAddressArrayEncoding)
return emptyBytes
}
func ConcatBytes(slices ...[]byte) []byte {
var result []byte
for _, slice := range slices {
result = append(result, slice...)
}
return result
}