-
Notifications
You must be signed in to change notification settings - Fork 2
/
rich_client.go
573 lines (471 loc) · 17.4 KB
/
rich_client.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
package walletsdk
import (
"encoding/json"
"fmt"
"math/big"
"net/http"
"strings"
"sync"
sdk "github.com/Conflux-Chain/go-conflux-sdk"
"github.com/Conflux-Chain/go-conflux-sdk-for-wallet/constants"
"github.com/Conflux-Chain/go-conflux-sdk/types"
richtypes "github.com/Conflux-Chain/go-conflux-sdk-for-wallet/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
)
// RichClient contains client, cfx-scan-backend server and contract-manager server
//
// RichClient is the client for wallet, it's methods need request centralized servers
// cfx-scan-backend and contract-manager in order to apply better performance.
type RichClient struct {
cfxScanBackend *scanServer
contractManager *scanServer
client sdk.ClientOperator
}
// ServerConfig represents cfx-scan-backend and contract-manager configurations, because centralized servers maybe changed.
type ServerConfig struct {
CfxScanBackendSchema string
CfxScanBackendAddress string
ContractManagerSchema string
ContractManagerAddress string
AccountBalancesPath string
AccountTokenTxListPath string
TxListPath string
ContractQueryPath string
}
type blockAndRevertrate struct {
block *types.Block
revertRate *big.Float
}
type contactInfoKey struct {
contractAddress string
needABI bool
needIcon bool
}
// default value of server config
var (
accountTokensPath = "/v1/token" // "/api/account/token/list" //cfx scan backend
tokenTransferListPath = "/v1/transfer" // "/api/transfer/list" //cfx scan backend
txListPath = "/v1/transaction" // "/api/transaction/list" //cfx scan backend
contractQueryBasePath = "/v1/contract" // "/api/contract/query" //contract manager
tokenQueryBasePath = "/v1/token" //cfx scan backend
cfxScanBackend = &scanServer{
Scheme: "http",
Address: "101.201.103.131:8885", //"testnet-jsonrpc.conflux-chain.org:18084",
HTTPRequester: &http.Client{},
}
contractManager = &scanServer{
Scheme: "http",
Address: "101.201.103.131:8886", //"13.75.69.106:8886",
HTTPRequester: &http.Client{},
}
contractInfoCaches map[contactInfoKey]*richtypes.Contract = make(map[contactInfoKey]*richtypes.Contract)
)
// NewRichClient create new rich client with client and server config.
//
// The fields of config will use default value when it's empty
func NewRichClient(client sdk.ClientOperator, configOption *ServerConfig) *RichClient {
if configOption != nil {
if configOption.CfxScanBackendSchema != "" {
cfxScanBackend.Scheme = configOption.CfxScanBackendSchema
}
if configOption.CfxScanBackendAddress != "" {
cfxScanBackend.Address = configOption.CfxScanBackendAddress
}
if configOption.ContractManagerSchema != "" {
contractManager.Scheme = configOption.ContractManagerSchema
}
if configOption.ContractManagerAddress != "" {
contractManager.Address = configOption.ContractManagerAddress
}
if configOption.AccountBalancesPath != "" {
accountTokensPath = configOption.AccountBalancesPath
}
if configOption.AccountTokenTxListPath != "" {
tokenTransferListPath = configOption.AccountTokenTxListPath
}
if configOption.TxListPath != "" {
txListPath = configOption.TxListPath
}
if configOption.ContractQueryPath != "" {
contractQueryBasePath = configOption.ContractQueryPath
}
}
richClient := RichClient{
cfxScanBackend,
contractManager,
client,
}
return &richClient
}
// GetClient returns client
func (rc *RichClient) GetClient() sdk.ClientOperator {
return rc.client
}
// setHTTPRequester for unit test
func (rc *RichClient) setHTTPRequester(requester sdk.HTTPRequester) {
rc.cfxScanBackend.HTTPRequester = requester
rc.contractManager.HTTPRequester = requester
}
// GetAccountTokenTransfers returns address releated transactions,
// the tokenIdentifier represnets the token contract address and it is optional,
// when tokenIdentifier is specicied it returns token transfer events related the address,
// otherwise returns transactions about main coin.
func (rc *RichClient) GetAccountTokenTransfers(address types.Address, tokenIdentifier *types.Address, pageNumber, pageSize uint) (*richtypes.TokenTransferEventList, error) {
params := make(map[string]interface{})
params["accountAddress"] = address
params["skip"] = (pageNumber - 1) * pageSize
params["limit"] = pageSize
params["transferType"] = "ERC20"
var tteList *richtypes.TokenTransferEventList
blockhashes := []types.Hash{}
// when tokenIdentifier is not nil return transfer events of the token
if tokenIdentifier != nil {
var tts richtypes.TokenTransferEventList
params["address"] = *tokenIdentifier
err := rc.cfxScanBackend.Get(tokenTransferListPath, params, &tts)
if err != nil {
return nil, errors.Wrapf(err, "get result of CfxScanBackend server and path {%+v}, params: {%+v} error", tokenTransferListPath, params)
}
// tts.FormatAddress()
tteList = &tts
// fmt.Printf("%+v", tteList)
// batch get blockhash through getTransactionByHash
blockhashes = make([]types.Hash, 0, len(tteList.List))
txhashes := make([]types.Hash, len(tteList.List))
tokenAddressToTokenInfoMap := make(map[string]*richtypes.Token)
for i := range tteList.List {
txhashes[i] = tteList.List[i].TransactionHash
}
// set block hash
txhashToTxMap, err := rc.client.BatchGetTxByHashes(txhashes)
if err != nil {
return nil, errors.Wrapf(err, "batch get txs by tx hashes %v error", txhashes)
}
for i := range tteList.List {
hash := tteList.List[i].TransactionHash
tx := txhashToTxMap[hash]
if tx != nil && tx.BlockHash != nil {
tteList.List[i].BlockHash = *txhashToTxMap[hash].BlockHash
}
}
for _, th := range txhashes {
if txhashToTxMap[th] != nil && txhashToTxMap[th].BlockHash != nil {
blockhashes = append(blockhashes, *txhashToTxMap[th].BlockHash)
}
}
// set token info
for i := range tteList.List {
tokenAddress := tteList.List[i].ContractAddress
if tokenAddress != nil {
if _, ok := tokenAddressToTokenInfoMap[tokenAddress.String()]; !ok {
contract, err := rc.GetContractInfo(*tokenAddress, true, false)
if err != nil {
return nil, errors.Wrapf(err, "get token info of %v error", tokenAddress)
}
tokenAddressToTokenInfoMap[tokenAddress.String()] = &contract.Token
}
tteList.List[i].Token = *tokenAddressToTokenInfoMap[tokenAddress.String()]
}
}
} else {
// when tokenIdentifier is nil return transaction of main coin
var txs richtypes.TransactionList
err := rc.cfxScanBackend.Get(txListPath, params, &txs)
if err != nil {
return nil, errors.Wrapf(err, "get result of CfxScanBackend server and path {%+v}, params: {%+v} error", txListPath, params)
}
// txs.FormatAddress()
tteList = txs.ToTokenTransferEventList()
// set blockhashes
blockhashes = make([]types.Hash, len(txs.List))
for i := range txs.List {
blockhashes[i] = txs.List[i].BlockHash
}
}
// use batch call instead of concurrency
blkhashToRateMap, err := rc.client.BatchGetBlockConfirmationRisk(blockhashes)
// fmt.Printf("blkhashToRateMap: %+v\n\n", blkhashToRateMap)
if err != nil {
return nil, errors.Wrapf(err, "batch get block revert of blockhashes %v error", blockhashes)
}
for i, tte := range tteList.List {
rate := blkhashToRateMap[tte.BlockHash]
tteList.List[i].RevertRate = rate
}
return tteList, nil
}
// CreateSendTokenTransaction creates unsigned transaction for sending token according to input params,
// the tokenIdentifier represnets the token contract address.
// It supports erc20, erc777, fanscoin at present
func (rc *RichClient) CreateSendTokenTransaction(from types.Address, to types.Address, amount *hexutil.Big, tokenIdentifier *types.Address) (*types.UnsignedTransaction, error) {
if tokenIdentifier == nil {
tx, err := rc.client.CreateUnsignedTransaction(from, to, amount, nil)
if err != nil {
return nil, errors.Wrapf(err, "Create Unsigned Transaction by from {%+v}, to {%+v}, amount {%+v} error", from, to, amount)
}
return &tx, nil
}
cInfo, err := rc.GetContractInfo(*tokenIdentifier, true, false)
if err != nil {
// msg := fmt.Sprintf("get and unmarsal data from contract manager server with path {%+v}, paramas {%+v} error", contractQueryPath, params)
return nil, errors.Wrapf(err, "get contract info of %v error", tokenIdentifier)
}
contract, err := rc.client.GetContract([]byte(cInfo.ABI), tokenIdentifier)
if err != nil {
return nil, errors.Wrapf(err, "get contract by ABI {%+v}, tokenIdentifier {%+v} error", cInfo.ABI, tokenIdentifier)
}
data, err := rc.getDataForTransToken(cInfo.GetContractTypeByABI(), contract, to, amount)
if err != nil {
return nil, errors.Wrapf(err, "get data for transfer token method error, contract type {%+v} ", cInfo.GetContractTypeByABI())
}
tx, err := rc.client.CreateUnsignedTransaction(from, *tokenIdentifier, nil, data)
if err != nil {
msg := fmt.Sprintf("create transaction with params {from: %+v, to: %+v, data: %+v} error ", from, to, data)
return nil, errors.Wrapf(err, msg)
}
return &tx, nil
}
func (rc *RichClient) getDataForTransToken(contractType richtypes.ContractType, contract sdk.Contractor, to types.Address, amount *hexutil.Big) ([]byte, error) {
var data []byte
var err error
// erc20 or fanscoin method signature are transfer(address,uint256)
if contractType == richtypes.ERC20 || contractType == richtypes.FANSCOIN {
data, err = contract.GetData("transfer", to.MustGetCommonAddress(), amount.ToInt())
if err != nil {
msg := fmt.Sprintf("get data of contract {%+v}, method {%+v}, params {to: %+v, amount: %+v} error ", contract, "transfer", to, amount)
return nil, errors.Wrapf(err, msg)
}
return data, nil
}
// erc721 send by token_id
//
// if cInfo.ContractType == scantypes.ERC721 {
// data, err = contract.GetData()
// }
// erc777 method signature is send(address,uint256,bytes)
if contractType == richtypes.ERC777 {
data, err = contract.GetData("send", to.MustGetCommonAddress(), amount.ToInt(), []byte{})
if err != nil {
msg := fmt.Sprintf("get data of contract {%+v}, method {%+v}, params {to: %+v, amount: %+v} error ", contract, "send", to, amount)
return nil, errors.Wrapf(err, msg)
}
return data, nil
}
// if cInfo.ContractType == scantypes.DEX {
// data, err = contract.GetData()
// }
msg := fmt.Sprintf("Do not support build data for transfer token function of contract type %+v", contractType)
err = errors.New(msg)
return nil, err
}
// GetContractInfo returns contract detail infomation, it will contains token info if it is token contract,
// it will contains abi if set needABI to be true.
func (rc *RichClient) GetContractInfo(contractAddress types.Address, needABI, needIcon bool) (*richtypes.Contract, error) {
params := make(map[string]interface{})
cInfoKey := contactInfoKey{
contractAddress: contractAddress.String(),
needABI: needABI,
needIcon: needIcon,
}
cInfoCache := contractInfoCaches[cInfoKey]
if cInfoCache != nil {
return cInfoCache, nil
}
fields := []string{}
if needIcon {
fields = append(fields, "icon")
}
if needABI {
fields = append(fields, "abi")
}
params["fields"] = strings.Join(fields, ",")
var contractQueryFullPath = fmt.Sprintf("%v/%v", contractQueryBasePath, contractAddress)
var contract richtypes.Contract
err := rc.contractManager.Get(contractQueryFullPath, params, &contract)
if err != nil {
msg := fmt.Sprintf("get and unmarshal result of ContractManager server and path {%+v}, params: {%+v} error", contractQueryFullPath, params)
return nil, errors.Wrapf(err, msg)
}
// get token info
var tokenQueryFullPath = fmt.Sprintf("%v/%v", tokenQueryBasePath, contractAddress)
rc.contractManager.Get(tokenQueryFullPath, params, &contract.Token)
contractInfoCaches[cInfoKey] = &contract
// fmt.Printf("get contract %v\n", cInfoKey)
return &contract, nil
}
// GetAccountTokens returns coin balance and all token balances of specified address
func (rc *RichClient) GetAccountTokens(account types.Address) (*richtypes.TokenWithBlanceList, error) {
params := make(map[string]interface{})
params["accountAddress"] = account
var tbs richtypes.TokenWithBlanceList
err := rc.cfxScanBackend.Get(accountTokensPath, params, &tbs)
if err != nil {
msg := fmt.Sprintf("get and unmarshal result of ContractManager server and path {%+v}, params: {%+v} error", accountTokensPath, params)
return nil, errors.Wrapf(err, msg)
}
return &tbs, nil
}
// GetTransactionsFromPool returns all pending transactions in mempool of conflux node.
//
// it only works on local conflux node currently.
func (rc *RichClient) GetTransactionsFromPool() (*[]types.Transaction, error) {
var txs []types.Transaction
if err := rc.client.CallRPC(&txs, "getTransactionsFromPool"); err != nil {
msg := fmt.Sprintf("rpc getTransactionsFromPool error")
return nil, errors.Wrapf(err, msg)
}
if txs == nil {
return nil, nil
}
return &txs, nil
}
// GetTxDictByTxHash returns all cfx transfers and token transfers of transaction
func (rc *RichClient) GetTxDictByTxHash(hash types.Hash) (*richtypes.TxDict, error) {
tx, err := rc.client.GetTransactionByHash(hash)
if err != nil {
msg := fmt.Sprintf("get transaction by hash %v error", hash)
return nil, errors.Wrapf(err, msg)
}
tc, err := NewTxDictConverter(rc)
if err != nil {
return nil, fmt.Errorf("create TxDictConverter error")
}
return tc.ConvertByTransaction(tx, nil, nil)
}
// GetTxDictsByEpoch returns all cfx transfers and token transfers of the epoch
func (rc *RichClient) GetTxDictsByEpoch(epoch *types.Epoch) ([]richtypes.TxDict, error) {
// start := time.Now()
client := rc.GetClient()
blockhashes, err := client.GetBlocksByEpoch(epoch)
if err != nil {
msg := fmt.Sprintf("get blocks by epoch %v error", epoch)
return nil, errors.Wrapf(err, msg)
}
//fmt.Printf("get block hashes by epoch done, passed time: %v\n", time.Now().Sub(start))
cache, errs := createBlockAndRevertrateCache(client, blockhashes)
if errs != nil {
return nil, joinError(errs)
}
// fmt.Printf("cache: %+v\n", cache)
//fmt.Println("create block and reverrate cache done, passed time: %", time.Now().Sub(start))
txdict, errs := rc.createTxDictsByBlockhashes(blockhashes, cache)
if errs != nil {
return nil, joinError(errs)
}
//fmt.Println("create tx dic done, passed time: %", time.Now().Sub(start))
return txdict, nil
}
func createBlockAndRevertrateCache(client sdk.ClientOperator, blockhashes []types.Hash) (map[types.Hash]*blockAndRevertrate, []error) {
// cache block and it's revertrate
cache := make(map[types.Hash]*blockAndRevertrate)
var errs []error
// blockhashes = []types.Hash{"0x28d5a5b1b8f6c83e274b7ba1f027d16215596f27ea5effb745994401d23f8a18"}
// concurrence get block and revertrate
var wg sync.WaitGroup
wg.Add(len(blockhashes) * 2)
for _, blockhash := range blockhashes {
cache[blockhash] = &blockAndRevertrate{}
go func(bh types.Hash) {
defer wg.Done()
block, err := client.GetBlockByHash(bh)
if err != nil {
msg := fmt.Sprintf("get block by hash %v error", bh)
if errs == nil {
errs = make([]error, 0)
}
errs = append(errs, errors.Wrapf(err, msg))
}
cache[bh].block = block
}(blockhash)
// get risk rate and block time
go func(bh types.Hash) {
defer wg.Done()
revertRate, err := client.GetBlockConfirmationRisk(bh)
if err != nil {
msg := fmt.Sprintf("get block revert rate by hash %v error", bh)
if errs == nil {
errs = make([]error, 0)
}
errs = append(errs, errors.Wrapf(err, msg))
}
cache[bh].revertRate = revertRate
}(blockhash)
}
wg.Wait()
return cache, errs
}
func (rc *RichClient) createTxDictsByBlockhashes(blockhashes []types.Hash, cache map[types.Hash]*blockAndRevertrate) ([]richtypes.TxDict, []error) {
var errors = make([]error, 0)
tc, err := NewTxDictConverter(rc)
if err != nil {
errors = append(errors, err)
return nil, errors
}
txDicts := make([]richtypes.TxDict, 0)
txs := make([]types.Transaction, 0)
for _, blockhash := range blockhashes {
// fmt.Printf("cache[%v]= %+v\n", blockhash, cache[blockhash])
txs = append(txs, cache[blockhash].block.Transactions...)
}
all := len(txs)
con := constants.RPCConcurrence
excuted := 0
for {
isLastLoop := (all-excuted)/con == 0
if isLastLoop {
con = all % con
}
var wg sync.WaitGroup
wg.Add(con)
var mutex = new(sync.Mutex)
for i := 0; i < con; i++ {
go func(_tx types.Transaction) {
defer wg.Done()
//fmt.Println("excute tx done:", excuted)
// blockhash null means that tx is excuted by other block, so skip it
if _tx.BlockHash == nil {
return
}
cacheVal := cache[*_tx.BlockHash]
blockTimeInU64 := hexutil.Uint64(cacheVal.block.Timestamp.ToInt().Uint64())
txDict, err := tc.ConvertByTransaction(&_tx, cacheVal.revertRate, &blockTimeInU64)
mutex.Lock()
defer mutex.Unlock()
if err != nil {
errors = append(errors, err)
return
}
txDicts = append(txDicts, *txDict)
}(txs[excuted])
excuted++
//fmt.Println("excuting tx :", excuted)
}
wg.Wait()
if isLastLoop {
break
}
}
return txDicts, nil
}
func joinError(errs []error) error {
if errs != nil && len(errs) > 0 {
errorStrs := make([]string, len(errs))
for i, e := range errs {
errorStrs[i] = e.Error()
}
joinedErr := strings.Join(errorStrs, "\n")
return errors.New(joinedErr)
}
return nil
}
func jsonIt(input interface{}) string {
j, err := json.Marshal(input)
if err != nil {
panic(err)
}
return string(j)
}