-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
79 lines (64 loc) · 1.5 KB
/
utils.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
package tbsdk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/ethereum/go-ethereum/common"
)
func PrepareRPCCall(endpoint, method string, params interface{}) (*http.Request, error) {
requestBody := &RpcRequest{
Jsonrpc: "2.0",
Method: method,
Params: params,
Id: 1,
}
body, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
request, err := http.NewRequest(
"POST",
endpoint,
bytes.NewBuffer(body),
)
if err != nil {
return nil, errors.Join(
ErrRequestCreationFailed,
fmt.Errorf("endpoint->%s,body->[:%s:]", endpoint, string(body)),
err,
)
}
request.Header.Set("Content-Type", "application/json")
return request, nil
}
func HandleRpcRequest(request *http.Request, client *http.Client) (*RpcResponse, error) {
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
responseBody := &RpcResponse{}
err = json.NewDecoder(response.Body).Decode(responseBody)
if err != nil {
return nil, err
}
return responseBody, nil
}
// TODO: input type ...interface{} output ([]byte, error)
func AbiEncode(args ...[]byte) []byte {
encodedArgs := []byte{}
for _, v := range args {
encodedArgs = append(encodedArgs, common.LeftPadBytes(v, 32)...)
}
return encodedArgs
}
// TODO: input type ...interface{} output ([]byte, error)
func AbiEncodePacked(args ...[]byte) []byte {
packedArgs := []byte{}
for _, v := range args {
packedArgs = append(packedArgs, v...)
}
return packedArgs
}