-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta_tx_v1.go
158 lines (151 loc) · 4.33 KB
/
meta_tx_v1.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
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
147
148
149
150
151
152
153
154
155
156
157
158
package metax
import (
"bytes"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"net/http"
"net/url"
"time"
)
type MetaTxResponseV1 struct {
Flag int `json:"flag"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type MetaTxV1SuccessData struct {
TransactionId string `json:"transactionId"`
ConnectionUrl string `json:"connectionUrl"`
}
type BiconomyTransaction struct {
Flag int `json:"flag"`
Log string `json:"log"`
Code int `json:"code"`
Data struct {
Status string `json:"status"`
Receipt struct {
TxHash string `json:"transactionHash"`
} `json:"receipt"`
} `json:"data"`
}
func (b *Bcnmy) SendMetaNativeTxV1(data *MetaTxRequest) (*MetaTxResponse, error) {
bodyCh := make(chan []byte)
errorCh := make(chan error)
defer close(bodyCh)
defer close(errorCh)
body, err := json.Marshal(data)
if err != nil {
b.logger.WithError(err).Error("json marshal `MetaTxRequest` data failed")
return nil, err
}
req, err := http.NewRequest(http.MethodPost, MetaTxNativeURLV1, bytes.NewBuffer(body))
if err != nil {
b.logger.Error("SendMetaNativeTxV1 NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("x-api-key", b.apiKey)
req.Header.Set("version", PACKAGE_VERSION)
var resp MetaTxResponseV1
b.asyncHttpx(req, errorCh, bodyCh)
var successData MetaTxV1SuccessData
select {
case ret := <-bodyCh:
err = json.Unmarshal(ret, &resp)
if err != nil {
return nil, fmt.Errorf("SendMetaNativeTxV1 unmarshal failed, %v", err)
}
if resp.Flag == 200 {
dataBytes, err := json.Marshal(resp.Data)
if err != nil {
return nil, fmt.Errorf("Error marshaling response data:", err)
}
if err = json.Unmarshal(dataBytes, &successData); err != nil {
return nil, fmt.Errorf("Error unmarshaling response data:", err)
}
break
} else if resp.Flag == 400 {
switch data := resp.Data.(type) {
case string:
return &MetaTxResponse{
Flag: resp.Flag,
TxHash: common.HexToHash("0x0"),
Error: data,
Message: data,
Code: resp.Flag,
}, nil
case map[string]interface{}:
return &MetaTxResponse{
Flag: resp.Flag,
TxHash: common.HexToHash("0x0"),
Error: data["error"].(string),
Message: data["error"].(string),
Code: data["code"].(int),
}, nil
}
}
case err := <-errorCh:
b.logger.WithError(err).Error(err.Error())
return nil, err
}
bcnmyTxn, err := b.GetTransactionStatus(successData.TransactionId)
if err != nil {
b.logger.Error("SendTransactionStatus failed, check error logs")
return nil, err
} else {
return &MetaTxResponse{
Flag: bcnmyTxn.Flag,
Code: bcnmyTxn.Code,
TxHash: common.HexToHash(bcnmyTxn.Data.Receipt.TxHash),
}, nil
}
}
func (b *Bcnmy) GetTransactionStatus(transactionId string) (*BiconomyTransaction, error) {
bodyCh := make(chan []byte, 1)
errorCh := make(chan error)
defer close(bodyCh)
defer close(errorCh)
queryParams := url.Values{}
queryParams.Set("transactionId", transactionId)
urlWithParams := MetaTransactionStatusURL + "?" + queryParams.Encode()
req, err := http.NewRequest(http.MethodGet, urlWithParams, nil)
if err != nil {
b.logger.WithError(err).Error("SendTransactionStatus NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("x-api-key", b.apiKey)
req.Header.Set("version", PACKAGE_VERSION)
var resp BiconomyTransaction
b.asyncHttpx(req, errorCh, bodyCh)
retries := 5
LOOP:
for {
retries -= 1
if retries <= 0 {
return nil, fmt.Errorf("GetBiconomyTransactionStatus %s reach maxiumum retries", transactionId)
}
select {
case ret := <-bodyCh:
err = json.Unmarshal(ret, &resp)
if err != nil {
return nil, fmt.Errorf("GetTransactionStatus unmarshal failed, %v", err)
}
if resp.Code != 200 {
b.logger.Infof("BiconomyTransaction %v Code is empty", resp)
time.Sleep(b.sleepTimeSec * time.Second)
continue LOOP
}
if resp.Data.Receipt.TxHash == "" || resp.Data.Receipt.TxHash == common.HexToHash("0x0").Hex() {
b.logger.Infof("BiconomyTransaction %v txHash is empty", resp)
time.Sleep(b.sleepTimeSec * time.Second)
continue LOOP
}
return &resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
time.Sleep(b.sleepTimeSec * time.Second)
continue LOOP
}
}
}