-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashborad_api.go
232 lines (213 loc) · 6.84 KB
/
dashborad_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package metax
import (
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"strconv"
"strings"
)
type GeneralResponse struct {
Code int `json:"code"`
Message string `json:"message"`
ResponseCode int `json:"responseCode"`
}
type CreateDappRequest struct {
DappName string `json:"dappName"`
NetworkId string `json:"networkId"`
EnableBiconomyWallet bool `json:"enableBiconomyWallet"`
}
type CreateDappResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
ApiKey string `json:"apiKey"`
FundingKey *big.Int `json:"fundingKey"`
} `json:"data"`
}
type AddContractRequest struct {
ContractName string `json:"contractName"`
ContractAddress string `json:"contractAddress"`
ContractType string `json:"contractType"` // SCW for contract wallet or SC for contract
WalletType string `json:"walletType"` // SCW or GNOSIS or blank
MetaTransactionType string `json:"metaTransactionType"` // DEFAULT, TRUSTED_FORWARDER, ERC20_FORWARDER
ABI string `json:"abi"`
}
type AddMethodRequest struct {
ApiType string `json:"apiType"`
MethodType string `json:"methodType"`
Name string `json:"name"`
ContractAddress string `json:"contractAddress"`
Method string `json:"method"`
}
type AddMethodResponse struct {
Code int `json:"code"`
Message string `json:"message"`
ApiIds []struct {
ApiId string `json:"apiId"`
Method string `json:"method"`
Name string `json:"name"`
} `json:"apiIds"`
}
type DeleteContractRequest struct {
ContractAddress string `json:"contractAddress"`
ContractType string `json:"contractType"` // SCW for contract wallet or SC for contract
}
type DeleteMethodRequest struct {
ContractAddress string `json:"contractAddress"`
Method string `json:"method"`
}
func (b *Bcnmy) CreateDapp(data *CreateDappRequest) (*CreateDappResponse, error) {
errorCh := make(chan error)
responseCh := make(chan interface{})
body := url.Values{
"dappName": {data.DappName},
"networkId": {data.NetworkId},
"enableBiconomyWallet": {strconv.FormatBool(data.EnableBiconomyWallet)},
}
req, err := http.NewRequest(http.MethodPost, CreateDappPublicURL, strings.NewReader(body.Encode()))
if err != nil {
b.logger.WithError(err).Error("CreateDapp NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("authToken", b.authToken)
var resp CreateDappResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*CreateDappResponse)
if !ok {
return nil, fmt.Errorf("CreateDappResponse failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) AddContract(data *AddContractRequest) (*GeneralResponse, error) {
errorCh := make(chan error)
responseCh := make(chan interface{})
defer close(errorCh)
defer close(responseCh)
body := url.Values{
"contractName": {data.ContractName},
"contractAddress": {data.ContractAddress},
"contractType": {data.ContractType},
"walletType": {data.WalletType},
"metaTransactionType": {data.MetaTransactionType},
"abi": {data.ABI},
}
req, err := http.NewRequest(http.MethodPost, AddContractURL, strings.NewReader(body.Encode()))
if err != nil {
b.logger.WithError(err).Error("AddContract NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("authToken", b.authToken)
req.Header.Set("apiKey", b.apiKey)
var resp GeneralResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*GeneralResponse)
if !ok {
return nil, fmt.Errorf("AddContract failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) AddMethod(data *AddMethodRequest) (*AddMethodResponse, error) {
errorCh := make(chan error)
responseCh := make(chan interface{})
defer close(errorCh)
defer close(responseCh)
body := url.Values{
"apiType": {data.ApiType},
"methodType": {data.MethodType},
"name": {data.Name},
"contractAddress": {data.ContractAddress},
"method": {data.Method},
}
req, err := http.NewRequest(http.MethodPost, AddMethodURL, strings.NewReader(body.Encode()))
if err != nil {
b.logger.WithError(err).Error("AddMethod NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("authToken", b.authToken)
req.Header.Set("apiKey", b.apiKey)
var resp AddMethodResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*AddMethodResponse)
if !ok {
return nil, fmt.Errorf("AddMethod failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) DeleteContract(data *DeleteContractRequest) (*GeneralResponse, error) {
body := url.Values{
"contractAddress": {data.ContractAddress},
"contractType": {data.ContractType},
}
req, err := http.NewRequest(http.MethodDelete, DeleteContractURL, strings.NewReader(body.Encode()))
if err != nil {
b.logger.WithError(err).Error("DeleteContract NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("authToken", b.authToken)
req.Header.Set("apiKey", b.apiKey)
res, err := b.httpClient.Do(req)
if err != nil {
b.logger.WithError(err).Error("HttpClient request to DeleteContract failed")
return nil, err
}
defer res.Body.Close()
replyData, err := io.ReadAll(res.Body)
var ret *GeneralResponse
if err := json.Unmarshal(replyData, &ret); err != nil {
b.logger.WithError(err).Error("json unmarshal body data failed")
return nil, err
}
return ret, nil
}
func (b *Bcnmy) DeleteMethod(data *DeleteMethodRequest) (*GeneralResponse, error) {
body := url.Values{
"contractAddress": {data.ContractAddress},
"method": {data.Method},
}
req, err := http.NewRequest(http.MethodDelete, DeleteMethodURL, strings.NewReader(body.Encode()))
if err != nil {
b.logger.WithError(err).Error("DeleteMethod NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("authToken", b.authToken)
req.Header.Set("apiKey", b.apiKey)
res, err := b.httpClient.Do(req)
if err != nil {
b.logger.WithError(err).Error("HttpClient request to DeleteMethod failed")
return nil, err
}
defer res.Body.Close()
replyData, err := io.ReadAll(res.Body)
var ret *GeneralResponse
if err := json.Unmarshal(replyData, &ret); err != nil {
b.logger.WithError(err).Error("json unmarshal body data failed")
return nil, err
}
return ret, nil
}