-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcnmy.go
162 lines (141 loc) · 3.67 KB
/
bcnmy.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
package metax
import (
"context"
"fmt"
"math/big"
"net/http"
"net/http/cookiejar"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/sirupsen/logrus"
"github.com/oblzh/bcnmy-go/abi/forwarder"
)
type Bcnmy struct {
ctx context.Context
logger *logrus.Entry
ethClient *ethclient.Client
sleepTimeSec time.Duration
httpClient *http.Client
// DAPP abi and address
abi abi.ABI
address common.Address
authToken string
apiKey string
/// method apiID
apiID map[string]struct {
ID string
ContractAddress string
}
batchId *big.Int
chainId *big.Int
trustedForwarder struct {
Address common.Address
Contract *forwarder.Forwarder
}
// backend config
email string
password string
backendHttpClient *http.Client
}
func NewBcnmy(httpRpc string, apiKey string, timeout time.Duration) (*Bcnmy, error) {
var err error
bcnmy := &Bcnmy{
ctx: context.Background(),
logger: logrus.WithField("metax", "bcnmy"),
apiKey: apiKey,
apiID: make(map[string]struct {
ID string
ContractAddress string
}),
batchId: big.NewInt(0),
httpClient: &http.Client{Timeout: timeout},
sleepTimeSec: time.Duration(5),
}
bcnmy.ethClient, err = ethclient.DialContext(bcnmy.ctx, httpRpc)
if err != nil {
bcnmy.logger.WithError(err).Error("DialContext ethclient failed")
return nil, err
}
bcnmy.chainId, err = bcnmy.ethClient.ChainID(bcnmy.ctx)
if err != nil {
bcnmy.logger.WithError(err).Error("ethClient getchainId failed")
return nil, err
}
forwarderAddress, ok := ForwarderAddressMap[bcnmy.chainId.String()]
if !ok {
err = fmt.Errorf("Chain ID not supported: %v", bcnmy.chainId)
bcnmy.logger.Error(err.Error())
return nil, err
}
forwarderContract, err := forwarder.NewForwarder(forwarderAddress, bcnmy.ethClient)
if err != nil {
bcnmy.logger.WithError(err).Error("Load Forwarder Contract failed")
return nil, err
}
bcnmy.trustedForwarder = struct {
Address common.Address
Contract *forwarder.Forwarder
}{
Address: forwarderAddress,
Contract: forwarderContract,
}
resp, err := bcnmy.GetMetaAPI(bcnmy.ctx)
if err != nil {
bcnmy.logger.WithError(err).Error(err.Error())
return nil, err
}
for _, info := range resp.ListAPI {
// filter non contractAddress
if common.IsHexAddress(info.ContractAddress) {
bcnmy.apiID[fmt.Sprintf("%s-%s", common.HexToAddress(info.ContractAddress).Hex(), info.Method)] = struct {
ID string
ContractAddress string
}{
ID: info.ID,
ContractAddress: info.ContractAddress,
}
}
}
return bcnmy, nil
}
func (b *Bcnmy) WithDapp(jsonABI string, dappAddress common.Address) (*Bcnmy, error) {
var err error
b.address = dappAddress
b.abi, err = abi.JSON(strings.NewReader(jsonABI))
if err != nil {
b.logger.WithError(err).Error("jsonABI parse failed")
return nil, err
}
return b, nil
}
func (b *Bcnmy) WithAuthToken(authToken string) *Bcnmy {
b.authToken = authToken
return b
}
func (b *Bcnmy) WithFieldTimeout(timeout time.Duration) *Bcnmy {
b.httpClient = &http.Client{Timeout: timeout}
return b
}
func (b *Bcnmy) WithSleepTimeSec(sleepTimeSec int64) *Bcnmy {
b.sleepTimeSec = time.Duration(sleepTimeSec)
return b
}
func (b *Bcnmy) GetAuthorization() string {
return fmt.Sprintf("User %s", b.authToken)
}
func (b *Bcnmy) WithBackend(email string, password string, timeout time.Duration) error {
b.email = email
b.password = password
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
b.backendHttpClient = &http.Client{
Jar: jar,
Timeout: timeout,
}
return nil
}