/
backend_api.go
193 lines (181 loc) · 5.69 KB
/
backend_api.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package metax
import (
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
)
/*
{
"log": "Dapps fetched for user with userId: f93482",
"code": 200,
"data": {
"dapps": [
{
"_id": "64339de",
"networkId": "137",
"dappName": "nt",
"active": true,
"apiKeyPrefix": "",
"apiKey": "",
"dappLimit": {
"durationValue": 1,
"durationUnit": "day",
"value": 1000,
"limitDurationInMs": 86400000,
"limitStartTime": 1681430400000,
"type": 1
},
"dappLimitStatus": 1,
"userLimit": {
"durationValue": 1,
"durationUnit": "day",
"value": 10,
"limitDurationInMs": 86400000,
"limitStartTime": 1681084800000,
"type": 1
},
"userLimitStatus": 1,
"apiLimit": {
"durationValue": 1,
"durationUnit": "day",
"value": 100,
"limitDurationInMs": 86400000,
"limitStartTime": 1681084800000,
"type": 1
},
"apiLimitStatus": 1,
"createdOn": 1681104143290,
"createdBy": "61ff93482",
"allowedDomains": [],
"updatedOn": 1681104143290,
"updatedBy": "6bff93482",
"fundingKey": 10333,
"gasThreshold": 28206553200000000,
"effectiveBalance": 19198843608675230000,
"lastOutstandingBalance": 0,
"gasTankBalance": 19.19884360867523,
"gasTankDepletionRate": 0,
"oustandingBalance": 0,
"transactionCountThisMonth": 0,
"transactionCountInTotal": 150
}
]
}
}
*/
type DappResponse struct {
Log string `json:"log"`
Code int `json:"code"`
Data Data `json:"data"`
}
type Data struct {
Dapps []Dapp `json:"dapps"`
}
type Dapp struct {
APIKey string `json:"apiKey"`
FundingKey int `json:"fundingKey"`
NetworkId string `json:"networkId"`
DappName string `json:"dappName"`
DappLimit DappLimit `json:"dappLimit"`
DappLimitStatus int `json:"dappLimitStatus"`
UserLimit DappLimit `json:"userLimit"`
UserLimitStatus int `json:"userLimitStatus"`
ApiLimit DappLimit `json:"apiLimit"`
ApiLimitStatus int `json:"apiLimitStatus"`
GasTankBalance float64 `json:"gasTankBalance"`
EffectiveBalance *big.Int `json:"effectiveBalance"`
TransactionCountThisMonth int `json:"transactionCountThisMonth"`
TransactionCountInTotal int `json:"transactionCountInTotal"`
}
type DappLimit struct {
Type int `json:"type"`
Value float32 `json:"value"`
DurationValue float32 `json:"durationValue"`
DurationUnit string `json:"durationUnit"`
LimitStartTime int64 `json:"limitStartTime"`
LimitDurationInMs int64 `json:"limitDurationInMs"`
}
type LoginResponse struct {
Message string `json:"message"`
}
func (b *Bcnmy) BackendLogin() (*LoginResponse, error) {
body := url.Values{
"email": {b.email},
"password": {b.password},
}
loginResp, err := b.backendHttpClient.PostForm(BackendLoginURL, body)
if err != nil {
b.logger.WithError(err).Error("BackendLogin error")
return nil, err
}
defer loginResp.Body.Close()
replyData, err := io.ReadAll(loginResp.Body)
if err != nil {
b.logger.WithError(err).Error("BackendLogin Read body error")
return nil, err
}
if loginResp.StatusCode != 200 {
err = fmt.Errorf("BackendLogin got %v", loginResp.Status)
b.logger.WithError(err).Error("BackendLogin status error")
return nil, err
}
var resp LoginResponse
if err = json.Unmarshal(replyData, &resp); err != nil {
b.logger.WithError(err).Error("BackendLogin json unmarshal body data failed")
return nil, err
}
return &resp, nil
}
func (b *Bcnmy) BackendDappList() (*DappResponse, error) {
bodyCh := make(chan []byte)
errorCh := make(chan error)
defer close(errorCh)
defer close(bodyCh)
req, err := http.NewRequest(http.MethodGet, BackendDappURL, nil)
if err != nil {
b.logger.WithError(err).Error("BackendDappList NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
var resp DappResponse
b.backendAsyncHttpx(req, errorCh, bodyCh)
select {
case ret := <-bodyCh:
err = json.Unmarshal(ret, &resp)
if err != nil {
return nil, fmt.Errorf("AddDestinationAddresses unmarshal failed, %v", err)
}
return &resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) GetBackendDapps() (*DappResponse, error) {
resp, err := b.BackendDappList()
if err != nil && err.Error() == "401" {
loginResp, loginErr := b.BackendLogin()
if loginErr != nil {
b.logger.WithError(loginErr).Errorf("GetBackendDapps login error %+v", loginResp)
return nil, loginErr
}
resp, err = b.BackendDappList()
}
return resp, err
}
func (b *Bcnmy) GetGasTankEffectiveBalance() (*big.Int, error) {
resp, err := b.GetBackendDapps()
if err != nil {
b.logger.Errorf("GetGasTankEffectiveBalance failed")
return nil, err
}
for _, dapp := range resp.Data.Dapps {
if dapp.APIKey == b.apiKey {
return dapp.EffectiveBalance, nil
}
}
return nil, fmt.Errorf("Cannot get this gasTank balance")
}