-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcnmy.go
134 lines (117 loc) · 3.01 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
package metax
import (
"context"
"fmt"
"math/big"
"net/http"
"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
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
}
}
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},
}
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[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) GetAuthorization() string {
return fmt.Sprintf("User %s", b.authToken)
}