/
opensea.go
929 lines (746 loc) · 26.1 KB
/
opensea.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
package nfttradescrapers
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/diadata-org/diadata/config/nftContracts/erc1155"
"github.com/diadata-org/diadata/config/nftContracts/erc20"
"github.com/diadata-org/diadata/config/nftContracts/erc721"
"github.com/diadata-org/diadata/config/nftContracts/opensea"
"github.com/diadata-org/diadata/pkg/dia"
"github.com/diadata-org/diadata/pkg/dia/helpers/ethhelper"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/jackc/pgx/v4"
"github.com/shopspring/decimal"
)
const (
// we assume all of the NFTs traded on OpenSea are ERC721(1155 is an extension of it)
openSeaNFTContractType = "ERC721"
)
type OpenSeaScraperConfig struct {
// opensea's exchange contract address on connected blockchain network
ContractAddr string `json:"contract_addr"`
// indicates the batch size during read the filtered events
BatchSize int `json:"batch_size"`
// wait for a while between batch retrieval of filtered events
WaitPeriod time.Duration `json:"wait_per_batch"`
// it enables read contract data from the event's block
// height instead of the last state
FollowDist int `json:"following_distance_blocks"`
// if set it will read erc721 attributes at the currently
// processing block
UseArchiveNode bool `json:"use_archive_node_fetaures"`
// indicates the number of retries to scrape the target
// in case of an unexpected error
MaxRetry int `json:"max_retry"`
// if true the scraper will skip the currently scraping
// block when retries reach to the value MaxRetry
SkipOnErr bool `json:"skip_on_error"`
// it limits read bytes for NFT's metadata from external url
MaxMetadataSize int `json:"max_metadata_size"`
// it limits duration of read for NFT's metadata from external url
MetadataTimeout time.Duration `json:"metadata_timeout"`
}
type OpenSeaScraperState struct {
// last block number has been processed
LastBlockNum uint64 `json:"last_block_num"`
// last transaction index in the block(curr) has been processed
LastTxIndex uint `json:"last_tx_index"`
// holds the latest error message that occurred while scraping
LastErr string `json:"last_error"`
// indicates the number of consecutive error, reset on any successful operation
ErrCounter int `json:"count_of_error"`
}
type OpenSeaScraper struct {
tradeScraper TradeScraper
mu sync.Mutex
conf *OpenSeaScraperConfig
state *OpenSeaScraperState
exchange dia.NFTExchange
}
type erc20Transfer struct {
TokenAddr common.Address
From common.Address
To common.Address
Amount *big.Int
TokenSymbol *string
Decimals int
}
type erc721Transfer struct {
NFTAddress common.Address
Name *string
Symbol *string
TotalSupply *big.Int
From common.Address
To common.Address
TokenID *big.Int
TokenURI *string
TokenAttrs map[string]interface{}
}
var (
errOpenSeaShutdownRequest = errors.New("shutdown requested")
// default values are valid for the first run which is it saves
// these configs to the DB
defOpenSeaConf = &OpenSeaScraperConfig{
// ContractAddr: "0x7be8076f4ea4a4ad08075c2508e481d6c946d12b", // Wyvern V1
ContractAddr: "0x7f268357A8c2552623316e2562D90e642bB538E5", // Wyvern V2
BatchSize: 5000,
WaitPeriod: 60 * time.Second,
FollowDist: 10,
UseArchiveNode: false,
MaxRetry: 5,
SkipOnErr: true,
MaxMetadataSize: 50 * 1024,
MetadataTimeout: 30 * time.Second,
}
// // OpenSea V1 market contract has been deployed on the mainnet at
// // block num 5774644, so scraper starts from this block
// defOpenSeaState = &OpenSeaScraperState{LastBlockNum: 5774644}
// OpenSea market V2 contract has been deployed on the mainnet at
// block num 14120913, so scraper starts from this block.
defOpenSeaState = &OpenSeaScraperState{LastBlockNum: 14120913}
// This string is the identifier of the scraper in conf and state fields in postgres.
OpenSea = ""
openSeaABI abi.ABI
erc20ABI abi.ABI
erc721ABI abi.ABI
erc1155ABI abi.ABI
assetCacheOpensea = make(map[string]dia.Asset)
)
func init() {
var err error
openSeaABI, err = abi.JSON(strings.NewReader(opensea.ContractABI))
if err != nil {
panic(err)
}
erc20ABI, err = abi.JSON(strings.NewReader(erc20.ERC20ABI))
if err != nil {
panic(err)
}
erc721ABI, err = abi.JSON(strings.NewReader(erc721.ERC721ABI))
if err != nil {
panic(err)
}
erc1155ABI, err = abi.JSON(strings.NewReader(erc1155.Erc1155ABI))
if err != nil {
panic(err)
}
OpenSea = utils.Getenv("SCRAPER_NAME_STATE", "")
// If scraper state is not set yet, start from this block
initBlockNumString := utils.Getenv("LAST_BLOCK_NUM", "14120913")
initBlockNum, err := strconv.ParseInt(initBlockNumString, 10, 64)
if err != nil {
log.Error("parse timeFinal: ", err)
}
defOpenSeaState.LastBlockNum = uint64(initBlockNum)
}
func NewOpenSeaScraper(rdb *models.RelDB, exchange dia.NFTExchange) *OpenSeaScraper {
ctx := context.Background()
eth, err := ethclient.Dial(utils.Getenv("ETH_URI_REST", ""))
if err != nil {
log.Error("Error connecting Eth Client")
}
s := &OpenSeaScraper{
conf: &OpenSeaScraperConfig{},
state: &OpenSeaScraperState{},
exchange: exchange,
tradeScraper: TradeScraper{
shutdown: make(chan nothing),
shutdownDone: make(chan nothing),
datastore: rdb,
chanTrade: make(chan dia.NFTTrade),
source: exchange.Name,
ethConnection: eth,
},
}
if err := s.initScraper(ctx); err != nil {
log.Errorf("opensea scraper could not be initialized: %s", err.Error())
return nil
}
log.Info("scraper %s starts at block: %s", OpenSea, s.state.LastBlockNum)
time.Sleep(2 * time.Minute)
go s.mainLoop()
return s
}
// init scraper
// if there are no values stored previously, use defaults and store them
func (s *OpenSeaScraper) initScraper(ctx context.Context) error {
if err := s.loadConfig(ctx); err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
log.Errorf("unable to read scraper config from rdb: %s", err.Error())
return err
}
// use & store defaults if there is no record in the scraper table
defConf := *defOpenSeaConf // copy
s.conf = &defConf
if err := s.tradeScraper.datastore.SetScraperConfig(ctx, OpenSea, s.conf); err != nil {
log.Errorf("unable to store scraper config on rdb: %s", err.Error())
return err
}
defState := *defOpenSeaState // copy
s.state = &defState
if err := s.tradeScraper.datastore.SetScraperState(ctx, OpenSea, s.state); err != nil {
log.Errorf("unable to store scraper state on rdb: %s", err.Error())
return err
}
return nil
}
return s.loadState(ctx)
}
func (s *OpenSeaScraper) loadConfig(ctx context.Context) error {
return s.tradeScraper.datastore.GetScraperConfig(ctx, OpenSea, s.conf)
}
func (s *OpenSeaScraper) loadState(ctx context.Context) error {
return s.tradeScraper.datastore.GetScraperState(ctx, OpenSea, s.state)
}
func (s *OpenSeaScraper) storeState(ctx context.Context) error {
return s.tradeScraper.datastore.SetScraperState(ctx, OpenSea, s.state)
}
func (s *OpenSeaScraper) mainLoop() {
defer func() {
s.tradeScraper.closed = true
close(s.tradeScraper.chanTrade)
close(s.tradeScraper.shutdownDone)
}()
log.Infof("opensea scraper has been started (batch: %d, period: %s)", s.conf.BatchSize, s.conf.WaitPeriod.String())
for stop := false; !stop; {
if err := s.FetchTrades(); err != nil {
if errors.Is(err, errOpenSeaShutdownRequest) {
stop = true
continue
}
}
log.Debugf("wait for %s", s.conf.WaitPeriod)
select {
case <-time.After(s.conf.WaitPeriod):
case <-s.tradeScraper.shutdown:
stop = true
}
}
}
// FetchTrades searches for trades on-chain by the next block range
func (s *OpenSeaScraper) FetchTrades() error {
var err error
// TODO: make FetchTrades context-aware
ctx := context.Background()
// it must be run once at a time
s.mu.Lock()
defer s.mu.Unlock()
// read config
if err = s.loadConfig(ctx); err != nil {
log.Warnf("unable to load scraper config: %s", err.Error())
return err
}
// read state
if err = s.loadState(ctx); err != nil {
log.Warnf("unable to load scraper state: %s", err.Error())
return err
}
log.Infof("fetching opensea trade transactions from block %d(+%d)", s.state.LastBlockNum, s.conf.BatchSize)
// fetch trade transactions
res, err := utils.EthFilterTXs(ctx, s.tradeScraper.ethConnection, utils.EthTxFilterCriteria{
StartBlockNum: s.state.LastBlockNum,
StartTxIndex: s.state.LastTxIndex,
LimitBlocks: s.conf.BatchSize,
BehindHighestBlock: s.conf.FollowDist,
EvAddrs: []common.Address{common.HexToAddress(s.conf.ContractAddr)},
Events: []common.Hash{openSeaABI.Events["OrdersMatched"].ID},
})
if err != nil {
log.Warnf("unable to filter opensea trades: %s", err.Error())
return err
}
log.Infof("found %d trade(logs: %d) transactions in %d blocks(from %d [tx index offset: %d] to %d, sync: %t[stay behind: -%d]), exploring details...", res.NumTXs, res.NumLogs, res.NumBlocks, s.state.LastBlockNum, s.state.LastTxIndex, res.LastBlockNum, res.Synced, s.conf.FollowDist)
numTrades := 0
// process trade transactions
for _, tx := range res.TXs {
s.state.LastBlockNum = tx.BlockNum
s.state.LastTxIndex = tx.TXIndex
s.state.LastErr = ""
log.Info("current state.ErrCounter: ", s.state.ErrCounter)
skipped, err := s.processTx(ctx, tx)
if err != nil {
s.state.ErrCounter++
if s.state.ErrCounter <= s.conf.MaxRetry {
s.state.LastErr = fmt.Sprintf("unable to process trade transaction(%s): %s", tx.TXHash.Hex(), err.Error())
log.Error(s.state.LastErr)
// store state
if errState := s.storeState(ctx); errState != nil {
log.Warnf("unable to store scraper state: %s", errState.Error())
return errState
}
return err
}
log.Warnf("SKIPPING PERMANENTLY! block: %d, tx index: %d - error: %s", s.state.LastBlockNum, s.state.LastTxIndex, err.Error())
}
if !skipped {
numTrades++
}
// reset consecutive error counter
s.state.ErrCounter = 0
// move next
s.state.LastTxIndex = tx.TXIndex + 1
// store state
if err := s.storeState(ctx); err != nil {
log.Warnf("unable to store scraper state: %s", err.Error())
return err
}
}
s.state.LastBlockNum = res.LastBlockNum + 1
s.state.LastTxIndex = 0
if err := s.storeState(ctx); err != nil {
log.Warnf("unable to store scraper state: %s", err.Error())
return err
}
log.Infof("processed %d trades", numTrades)
return nil
}
func (s *OpenSeaScraper) processTx(ctx context.Context, tx *utils.EthFilteredTx) (bool, error) {
log.Tracef("process tx -> block: %d, tx index: %d, tx hash: %s", tx.BlockNum, tx.TXIndex, tx.TXHash.Hex())
// skip if the transaction has multiple OrdersMatched logs
if len(tx.Logs) != 1 {
return true, nil
}
marketContract, err := opensea.NewContract(tx.Logs[0].Address, s.tradeScraper.ethConnection)
if err != nil {
log.Errorf("unable to make new market contract for address: %s", tx.Logs[0].Address.Hex())
return false, err
}
ev, err := marketContract.ParseOrdersMatched(tx.Logs[0])
if err != nil {
log.Errorf("unable to decode opensea OrdersMatched event(tx: %s, logIndex: %d) (SKIPPED!): %s", tx.TXHash, tx.Logs[0].Index, err.Error())
return true, nil // skip
}
txData, pending, err := s.tradeScraper.ethConnection.TransactionByHash(ctx, tx.TXHash)
if err != nil {
log.Errorf("unable to read transaction(%s): %s", tx.TXHash, err.Error())
return false, err
} else if pending {
err = fmt.Errorf("transaction(%s) status error: pending=true", tx.TXHash)
log.Error(err.Error())
return false, err
}
receipt, err := s.tradeScraper.ethConnection.TransactionReceipt(ctx, tx.TXHash)
if err != nil {
log.Errorf("unable to read transaction(%s) receipt: %s", tx.TXHash, err.Error())
return false, err
}
currSymbol := "ETH"
currAddr := common.Address{}
currDecimals := 18
// if an ERC20 token used for the trade
if new(big.Int).Cmp(txData.Value()) == 0 {
var tokenTransfers []*erc20Transfer
tokenTransfers, err = s.findERC20Transfers(ctx, receipt, ev.Price)
if err != nil {
log.Errorf("unable to find erc20 transfers for transaction(%s): %s", tx.TXHash, err.Error())
return false, err
}
if len(tokenTransfers) == 1 {
currAddr = tokenTransfers[0].TokenAddr
currDecimals = tokenTransfers[0].Decimals
if v := tokenTransfers[0].TokenSymbol; v != nil {
currSymbol = *v
}
}
}
transfers, err := s.findERC721Transfers(ctx, receipt)
if err != nil {
log.Errorf("unable to find transfers of the event(block: %d, tx index: %d, tx: %s): %s", ev.Raw.BlockNumber, ev.Raw.TxIndex, ev.Raw.TxHash.Hex(), err.Error())
return false, err
}
// skip if the event has no transfer
if len(transfers) == 0 {
log.Tracef("event(block: %d, tx index: %d, tx: %s) skipped due to it has no erc721 transfer log", ev.Raw.BlockNumber, ev.Raw.TxIndex, ev.Raw.TxHash.Hex())
return true, nil
}
// skip if the event has multiple transfers due to we can't calculate the price of trade
if len(transfers) > 1 {
log.Tracef("event(block: %d, tx index: %d, tx: %s) skipped due to it has multiple erc721 transfer logs", ev.Raw.BlockNumber, ev.Raw.TxIndex, ev.Raw.TxHash.Hex())
return true, nil
}
if transfers[0].NFTAddress == common.HexToAddress("0xA5c807A62CD6774d6BF518dD2dEc0aE17446Ad8d") {
log.Warnf("skip class %s because of decoding error.", "0xA5c807A62CD6774d6BF518dD2dEc0aE17446Ad8d")
return true, nil
}
normPrice := decimal.NewFromBigInt(ev.Price, 0).Div(decimal.NewFromInt(10).Pow(decimal.NewFromInt(int64(currDecimals))))
usdPrice, err := s.calcUSDPrice(currSymbol, normPrice)
if err != nil {
log.Errorf("unable to calculate usd price of the event(block: %d, log: %d, tx: %s): %s", ev.Raw.BlockNumber, ev.Raw.TxIndex, ev.Raw.TxHash.Hex(), err.Error())
return false, err
}
if err := s.notifyTrade(ev, transfers[0], ev.Price, usdPrice, currAddr); err != nil {
if !errors.Is(err, errOpenSeaShutdownRequest) {
log.Warnf("event(block: %d, tx index: %d, tx: %s) couldn't processed: %s", ev.Raw.BlockNumber, ev.Raw.TxIndex, ev.Raw.TxHash.Hex(), err.Error())
}
return false, err
}
return false, nil
}
func (s *OpenSeaScraper) notifyTrade(ev *opensea.ContractOrdersMatched, transfer *erc721Transfer, price *big.Int, usdPrice float64, currAddr common.Address) error {
nftClass, err := s.createOrReadNFTClass(transfer)
if err != nil {
return err
}
nft, err := s.createOrReadNFT(nftClass, transfer)
if err != nil {
return err
}
// Get block time.
timestamp, err := ethhelper.GetBlockTimeEth(int64(ev.Raw.BlockNumber), s.tradeScraper.datastore, s.tradeScraper.ethConnection)
if err != nil {
log.Errorf("getting block time: %+v", err)
}
trade := dia.NFTTrade{
NFT: *nft,
Price: price,
PriceUSD: usdPrice,
FromAddress: transfer.From.Hex(),
ToAddress: transfer.To.Hex(),
BlockNumber: ev.Raw.BlockNumber,
Timestamp: timestamp,
TxHash: ev.Raw.TxHash.Hex(),
Exchange: s.exchange.Name,
}
if asset, ok := assetCacheOpensea[dia.ETHEREUM+"-"+currAddr.Hex()]; ok {
trade.Currency = asset
} else {
currency, err := s.tradeScraper.datastore.GetAsset(currAddr.Hex(), dia.ETHEREUM)
if err != nil {
log.Errorf("cannot fetch asset %s -- %s", dia.ETHEREUM, currAddr.Hex())
}
trade.Currency = currency
assetCacheOpensea[dia.ETHEREUM+"-"+currAddr.Hex()] = currency
}
fmt.Println("found trade: ", trade)
// handle close request if the chanTrade not consumed immediately
select {
case s.tradeScraper.chanTrade <- trade:
case <-s.tradeScraper.shutdown:
return errOpenSeaShutdownRequest
}
return nil
}
func (s *OpenSeaScraper) createOrReadNFTClass(transfer *erc721Transfer) (*dia.NFTClass, error) {
nftClass, err := s.tradeScraper.datastore.GetNFTClass(transfer.NFTAddress.Hex(), dia.ETHEREUM)
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
log.Warnf("unable to read nftclass from reldb: %s", err.Error())
return nil, err
}
nftClass = dia.NFTClass{
Address: transfer.NFTAddress.Hex(),
Blockchain: dia.ETHEREUM,
ContractType: openSeaNFTContractType,
}
if transfer.Name != nil {
nftClass.Name = *transfer.Name
}
if transfer.Symbol != nil {
nftClass.Symbol = *transfer.Symbol
}
if err = s.tradeScraper.datastore.SetNFTClass(nftClass); err != nil {
log.Warnf("unable to create nftclass on reldb: %s", err.Error())
return nil, err
}
}
return &nftClass, nil
}
func (s *OpenSeaScraper) createOrReadNFT(nftClass *dia.NFTClass, transfer *erc721Transfer) (*dia.NFT, error) {
nft, err := s.tradeScraper.datastore.GetNFT(nftClass.Address, dia.ETHEREUM, transfer.TokenID.String())
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
log.Warnf("unable to read nft from reldb: %s", err.Error())
return nil, err
}
createdBy, createdAt, err := s.findContractCreationInfo(context.Background(), common.HexToAddress(nftClass.Address))
if err != nil {
log.Warnf("unable to find the creation info for the nft contract(%s): %s", nftClass.Address, err.Error())
return nil, err
}
nft = dia.NFT{
NFTClass: *nftClass,
TokenID: transfer.TokenID.String(),
CreationTime: createdAt,
CreatorAddress: createdBy.Hex(),
Attributes: transfer.TokenAttrs,
}
if transfer.TokenURI != nil {
nft.URI = *transfer.TokenURI
}
if err = s.tradeScraper.datastore.SetNFT(nft); err != nil {
log.Warnf("unable to create nft on reldb: %s", err.Error())
return nil, err
}
}
return &nft, nil
}
func (s *OpenSeaScraper) calcUSDPrice(symbol string, price decimal.Decimal) (float64, error) {
tokenPrice, err := s.findPrice(symbol)
if err != nil {
return 0, err
}
usdPrice := price.Mul(tokenPrice)
// using float type is not a good idea to handle prices
// we ignore if the price cannot be presentable as float64
f, _ := usdPrice.Float64()
return f, nil
}
func (s *OpenSeaScraper) findPrice(symbol string) (decimal.Decimal, error) {
// TODO: find the token price in usd for the given block number
switch symbol {
case "ETH", "WETH":
return decimal.NewFromString("2040.0910")
case "MANA":
return decimal.NewFromString("0.5")
default:
return decimal.NewFromString("1")
}
}
// GetDataChannel returns the scrapers data channel.
func (s *OpenSeaScraper) GetTradeChannel() chan dia.NFTTrade {
return s.tradeScraper.chanTrade
}
func (s *OpenSeaScraper) Close() error {
if s.tradeScraper.closed {
return errors.New("scraper already closed")
}
close(s.tradeScraper.shutdown)
return nil
}
// it searches the creation transaction for the given contract address using binary search in complexity of o(log n)
func (s *OpenSeaScraper) findContractCreationInfo(ctx context.Context, contractAddr common.Address) (createdBy common.Address, createdAt time.Time, err error) {
if !s.conf.UseArchiveNode {
log.Trace("nft contract creation info could not found because UseArchiveNode flag is not set, using zero values")
return common.Address{}, time.Time{}, nil
}
var (
lo, hi, blockNum uint64
code []byte
receipt *types.Receipt
chainID *big.Int
block *types.Block
)
hi, err = s.tradeScraper.ethConnection.BlockNumber(ctx)
if err != nil {
return
}
for lo <= hi {
blockNum = (lo + hi) / 2
code, err = s.tradeScraper.ethConnection.CodeAt(ctx, contractAddr, new(big.Int).SetUint64(blockNum))
if err != nil {
return
}
if len(code) == 0 {
lo = blockNum
} else {
hi = blockNum
}
if hi == lo+1 {
blockNum = hi
break
}
}
block, err = s.tradeScraper.ethConnection.BlockByNumber(ctx, new(big.Int).SetUint64(blockNum))
if err != nil {
return
}
chainID, err = s.tradeScraper.ethConnection.NetworkID(ctx)
if err != nil {
return
}
signer := types.NewEIP155Signer(chainID)
for _, trx := range block.Transactions() {
// recipient must be nill for contract creation transactions
if trx.To() != nil {
continue
}
receipt, err = s.tradeScraper.ethConnection.TransactionReceipt(ctx, trx.Hash())
if err != nil {
return
}
// note that if the nft was created by another smart contract
// we can't find its creation info with this method
if bytes.Equal(receipt.ContractAddress.Bytes(), contractAddr.Bytes()) {
createdAt = time.Unix(int64(block.Time()), 0).UTC()
createdBy, err = types.Sender(signer, trx)
if err != nil {
return
}
return
}
}
return
}
func (s *OpenSeaScraper) findERC20Transfers(ctx context.Context, receipt *types.Receipt, filterAmount *big.Int) ([]*erc20Transfer, error) {
transfers := make([]*erc20Transfer, 0, 2)
for _, txLog := range receipt.Logs {
if len(txLog.Topics) < 1 || txLog.Topics[0] != erc20ABI.Events["Transfer"].ID {
continue
}
token, err := erc20.NewERC20(txLog.Address, s.tradeScraper.ethConnection)
if err != nil {
log.Warnf("unable to bind erc720 contract at address %s: %s", txLog.Address.Hex(), err.Error())
continue
}
ev, err := token.ParseTransfer(*txLog)
if err != nil {
log.Tracef("the event cannot comply to erc20's transfer: %s", err)
continue
}
if filterAmount != nil && filterAmount.Cmp(ev.Value) != 0 {
continue
}
transfer := &erc20Transfer{
TokenAddr: txLog.Address,
From: ev.From,
To: ev.To,
Amount: ev.Value,
Decimals: 1,
}
transfers = append(transfers, transfer)
metadata, err := erc20.NewERC20Metadata(txLog.Address, s.tradeScraper.ethConnection)
if err != nil {
log.Warnf("unable to bind erc20 metadata contract at address %s: %s", txLog.Address.Hex(), err.Error())
continue
}
callOpts := &bind.CallOpts{Context: ctx}
if s.conf.UseArchiveNode {
callOpts.BlockNumber = new(big.Int).SetUint64(txLog.BlockNumber)
}
symbol, err := metadata.Symbol(callOpts)
if err != nil {
log.Warnf("unable to read token symbol from metadata interface of erc20(addr: %s): %s", txLog.Address.Hex(), err.Error())
continue
}
transfer.TokenSymbol = &symbol
decimals, err := metadata.Decimals(callOpts)
if err != nil {
log.Warnf("unable to read token decimals from metadata interface of erc20(addr: %s): %s", txLog.Address.Hex(), err.Error())
continue
}
transfer.Decimals = int(decimals)
}
return transfers, nil
}
// it finds the transfer events of ERC721 in the given transaction
func (s *OpenSeaScraper) findERC721Transfers(ctx context.Context, receipt *types.Receipt) ([]*erc721Transfer, error) {
transfers := make([]*erc721Transfer, 0, 1)
for _, txLog := range receipt.Logs {
if len(txLog.Topics) < 1 || txLog.Topics[0] != erc721ABI.Events["Transfer"].ID {
continue
}
nft, err := erc721.NewERC721(txLog.Address, s.tradeScraper.ethConnection)
if err != nil {
log.Warnf("unable to bind erc721 contract at address %s: %s", txLog.Address.Hex(), err.Error())
continue
}
transferLog, err := nft.ParseTransfer(*txLog)
if err != nil {
// it means this log data not comply to erc721's transfer event
//
// some old erc721 contracts have unindexed tokenid parameter
// so it is not compliant with the eip-721.
// best effort...
compat, err := erc721.NewERC721Compat(txLog.Address, s.tradeScraper.ethConnection)
if err != nil {
log.Warnf("unable to bind erc721compat contract at address %s: %s", txLog.Address.Hex(), err.Error())
continue
}
compatLog, err := compat.ParseTransfer(*txLog)
if err != nil {
log.Tracef("the event cannot comply to erc721's transfer: %s", err)
continue
}
transferLog = &erc721.ERC721Transfer{
From: compatLog.From,
To: compatLog.To,
TokenId: compatLog.TokenId,
Raw: compatLog.Raw,
}
}
transfer := &erc721Transfer{
NFTAddress: txLog.Address,
From: transferLog.From,
To: transferLog.To,
TokenID: transferLog.TokenId,
TokenAttrs: make(map[string]interface{}),
}
callOpts := &bind.CallOpts{Context: ctx}
if s.conf.UseArchiveNode {
callOpts.BlockNumber = new(big.Int).SetUint64(txLog.BlockNumber)
}
if md, err := erc721.NewERC721Metadata(txLog.Address, s.tradeScraper.ethConnection); err != nil {
log.Warnf("unable to bind erc721 metadata contract at address %s: %s", txLog.Address.Hex(), err.Error())
} else {
if nftName, err := md.Name(callOpts); err != nil {
log.Warnf("unable to read nft name from metadata interface of erc721(addr: %s): %s", txLog.Address.Hex(), err.Error())
} else {
transfer.Name = &nftName
}
if nftSymbol, err := md.Symbol(callOpts); err != nil {
log.Warnf("unable to read nft symbol from metadata interface of nft(addr: %s): %s", txLog.Address.Hex(), err.Error())
} else {
transfer.Symbol = &nftSymbol
}
if tokenURI, err := md.TokenURI(callOpts, transfer.TokenID); err != nil {
log.Warnf("unable to find token(%s) uri: %s", transfer.TokenID.String(), err.Error())
} else if attrs, err := s.readNFTAttr(ctx, tokenURI); err != nil {
log.Warnf("unable to read token(%s) attributes: %s", transfer.TokenID.String(), err.Error())
} else {
transfer.TokenURI = &tokenURI
transfer.TokenAttrs = attrs
}
}
transfers = append(transfers, transfer)
}
return transfers, nil
}
func (s *OpenSeaScraper) readNFTAttr(ctx context.Context, uri string) (map[string]interface{}, error) {
if uri == "" {
return nil, nil
}
if strings.HasPrefix(uri, "ipfs://") {
// TODO: add IPFS support
return nil, nil
}
attrs := make(map[string]interface{})
ctx, cancel := context.WithTimeout(ctx, s.conf.MetadataTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
// Handle the error from closing the response body
log.Println("Error closing response body:", cerr)
}
}()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, errors.New("unable to read token attributes: " + resp.Status)
}
if err := json.NewDecoder(io.LimitReader(resp.Body, int64(s.conf.MaxMetadataSize))).Decode(&attrs); err != nil {
return nil, err
}
return attrs, nil
}