diff --git a/config.yaml b/config.yaml index d524603..52439c1 100644 --- a/config.yaml +++ b/config.yaml @@ -17,7 +17,7 @@ contracts: CollectPaidMw: address: "0xb09ae63a2fd28686a0f386d1ddfd4b53687bf298" start_at: 25173248 - query_history: true + query_history: false ProfileNFT: address: "0x2723522702093601e6360cae665518c4f63e9da6" start_at: 21275193 diff --git a/dao/mysql/collect.go b/dao/mysql/collect.go index 1befb23..851e012 100644 --- a/dao/mysql/collect.go +++ b/dao/mysql/collect.go @@ -34,23 +34,25 @@ func InsertCollectPaidMwSetEvent(param *model.ParamCollectPaidMwSetEvent) error } func GetCollectPaidMwSetEventParams(chainID uint64, profileID string, essenceID string) (*model.ParamCollectPaidMwSetEvent, error) { - collectInfoList := new(model.ParamCollectPaidMwSetEvent) + collectInfo := new(model.ParamCollectPaidMwSetEvent) sqlStr := "SELECT chain_name, chain_id, block_number, tx_hash, namespace, profile_id, essence_id, total_supply, amount, recipient, currency, subscribe_required FROM collect_paid_mw_set_events WHERE chain_id = ? AND profile_id = ? AND essence_id = ?" - err := db.Get(collectInfoList, sqlStr, chainID, profileID, essenceID) - if(err != nil) { + err := db.Get(collectInfo, sqlStr, chainID, profileID, essenceID) + if err != nil { if err == sql.ErrNoRows { return nil, nil + } else { + return nil, err } } - return collectInfoList, nil + return collectInfo, nil } func GetLatestTrackedCollectPaidMwSetBlockNumber(chainID uint64) (uint64, error) { - collectInfoList := make([]*model.ParamCollectPaidMwSetEvent, 0, 2) + collectInfo := new(model.ParamCollectPaidMwSetEvent) sqlStr := "SELECT block_number FROM collect_paid_mw_set_events WHERE chain_id = ? ORDER BY id DESC LIMIT 1" - err := db.Select(&collectInfoList, sqlStr, chainID) - if(err != nil) { - return 0 ,err + err := db.Get(collectInfo, sqlStr, chainID) + if err != nil { + return 0, err } - return collectInfoList[0].BlockNumber, nil -} \ No newline at end of file + return collectInfo.BlockNumber, nil +} diff --git a/dao/mysql/profile.go b/dao/mysql/profile.go index 6b7160a..8093257 100644 --- a/dao/mysql/profile.go +++ b/dao/mysql/profile.go @@ -2,7 +2,6 @@ package mysql import ( "cyber-events-tracker/model" - "database/sql" ) func InsertCreateProfileEvent(param *model.ParamCreateProfileEvent) (err error) { @@ -24,20 +23,15 @@ func GetCreateProfileEventParams(chainID uint64, account string) ([]*model.Param profileInfoList := make([]*model.ParamCreateProfileEvent, 0, 2) sqlStr := "SELECT chain_name, chain_id, block_number, tx_hash, `to`, profile_id, handle, avatar, metadata FROM create_profile_events WHERE chain_id = ? AND `to` = ? ORDER BY block_number DESC" err := db.Select(&profileInfoList, sqlStr, chainID, account) - if(err != nil) { - if err == sql.ErrNoRows { - return []*model.ParamCreateProfileEvent{}, nil - } - } - return profileInfoList, nil + return profileInfoList, err } func GetLatestTrackedCreateProfileBlockNumber(chainID uint64) (uint64, error) { - profileInfoList := make([]*model.ParamCreateProfileEvent, 0, 2) + profileInfo := new(model.ParamCreateProfileEvent) sqlStr := "SELECT block_number FROM create_profile_events WHERE chain_id = ? ORDER BY id DESC LIMIT 1" - err := db.Select(&profileInfoList, sqlStr, chainID) - if(err != nil) { - return 0 ,err + err := db.Get(profileInfo, sqlStr, chainID) + if err != nil { + return 0, err } - return profileInfoList[0].BlockNumber, nil -} \ No newline at end of file + return profileInfo.BlockNumber, nil +} diff --git a/listener/collect.go b/listener/collect.go index e333e1d..c68880f 100644 --- a/listener/collect.go +++ b/listener/collect.go @@ -42,7 +42,7 @@ func CollectPaidMwSetEventListener(chainID uint64, contractAddress common.Addres if err != nil { log.Fatalf("[%s]: SubscribeFilterLogs CollectPaidMwSet failed, %v", chainID, err) } - log.Printf("[%d] Chan CollectPaidMwSet started.", chainID) + log.Printf("[%d]: Chan CollectPaidMwSet started.", chainID) for { select { @@ -69,13 +69,6 @@ func QueryCollectPaidMwSetEvents(chainID uint64, contractAddress common.Address, return } - _currentBlockNumber, err := ethClient.BlockNumber(context.Background()) - if err != nil { - log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) - } - - currentBlockNumber := big.NewInt(int64(_currentBlockNumber)) - var _startAt *big.Int var _endAt *big.Int = big.NewInt(0) previousAt, err := logic.GetPreviousTrackedCollectPaidMwSetBlockNumber(chainID) @@ -92,6 +85,16 @@ func QueryCollectPaidMwSetEvents(chainID uint64, contractAddress common.Address, log.Printf("[%d]: Continue query CollectPaidMwSet events at [%d]...", chainID, _startAt.Uint64()) } + var _currentBlockNumber uint64 + var currentBlockNumber *big.Int + + _currentBlockNumber, err = ethClient.BlockNumber(context.Background()) + if err != nil { + log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) + return + } + currentBlockNumber = big.NewInt(int64(_currentBlockNumber)) + for { _endAt.Add(_startAt, big.NewInt(49999)) if _endAt.Cmp(currentBlockNumber) > 0 { @@ -113,13 +116,20 @@ func QueryCollectPaidMwSetEvents(chainID uint64, contractAddress common.Address, for _, historyLog := range historyLogs { err = logic.SetCollectInfo(chainID, historyLog) - if(err != nil) { + if err != nil { log.Fatalf("[%d]: SetCollectInfo failed, %v", chainID, err) } } time.Sleep(200 * time.Millisecond) + _currentBlockNumber, err = ethClient.BlockNumber(context.Background()) + if err != nil { + log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) + } else { + currentBlockNumber = big.NewInt(int64(_currentBlockNumber)) + } + if _endAt.Cmp(currentBlockNumber) == 0 { break } diff --git a/listener/profile.go b/listener/profile.go index 6d22cd1..f83e394 100644 --- a/listener/profile.go +++ b/listener/profile.go @@ -42,7 +42,7 @@ func CreateProfileEventListener(chainID uint64, contractAddress common.Address) if err != nil { log.Fatalf("[%s]: SubscribeFilterLogs CreateProfile failed, %v", chainID, err) } - log.Printf("[%d] Chan CreateProfile started.", chainID) + log.Printf("[%d]: Chan CreateProfile started.", chainID) for { select { @@ -69,13 +69,6 @@ func QueryCreateProfileEvents(chainID uint64, contractAddress common.Address, st return } - _currentBlockNumber, err := ethClient.BlockNumber(context.Background()) - if err != nil { - log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) - } - - currentBlockNumber := big.NewInt(int64(_currentBlockNumber)) - var _startAt *big.Int var _endAt *big.Int = big.NewInt(0) previousAt, err := logic.GetPreviousTrackedCreateProfileBlockNumber(chainID) @@ -92,6 +85,16 @@ func QueryCreateProfileEvents(chainID uint64, contractAddress common.Address, st log.Printf("[%d]: Continue query CreateProfile events at [%d]...", chainID, _startAt.Uint64()) } + var _currentBlockNumber uint64 + var currentBlockNumber *big.Int + + _currentBlockNumber, err = ethClient.BlockNumber(context.Background()) + if err != nil { + log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) + return + } + currentBlockNumber = big.NewInt(int64(_currentBlockNumber)) + for { _endAt.Add(_startAt, big.NewInt(49999)) if _endAt.Cmp(currentBlockNumber) > 0 { @@ -120,6 +123,13 @@ func QueryCreateProfileEvents(chainID uint64, contractAddress common.Address, st time.Sleep(200 * time.Millisecond) + _currentBlockNumber, err = ethClient.BlockNumber(context.Background()) + if err != nil { + log.Fatalf("[%d]: Get current BlockNumber failed, %v", chainID, err) + } else { + currentBlockNumber = big.NewInt(int64(_currentBlockNumber)) + } + if _endAt.Cmp(currentBlockNumber) == 0 { break }