/
meta_tx.go
66 lines (53 loc) · 1.38 KB
/
meta_tx.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
package mexa
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/ethereum/go-ethereum/common"
)
const SignatureTypeEIP712 = "EIP712_SIGN"
type MetaTxRequest struct {
To string `json:"to"`
From string `json:"from"`
ApiId string `json:"apiId"`
Params []interface{} `json:"params"`
SignatureType string `json:"signatureType"`
}
type MetaTxResponse struct {
TxHash common.Hash `json:"txHash"`
Log string `json:"log"`
Flag int `json:"flag"`
}
// MetaTx executes a meta transaction using the given data.
func (m *Mexa) MetaTx(ctx context.Context, data *MetaTxRequest) (*MetaTxResponse, error) {
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, metaTxNativeURL, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("x-api-key", m.apiKey)
res, err := m.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
replyData, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var reply MetaTxResponse
if err := json.Unmarshal(replyData, &reply); err != nil {
return nil, err
}
if reply.TxHash == common.HexToHash("0x0") {
return nil, fmt.Errorf("%s", replyData)
}
return &reply, nil
}