2023-05-08 06:02:00 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-05-19 08:19:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-05-08 06:02:00 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
2023-06-01 13:09:50 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2023-05-08 06:02:00 +00:00
|
|
|
)
|
|
|
|
|
2023-05-19 08:19:48 +00:00
|
|
|
func NewSequentialFetchStrategy(db *Database, blockDAO *BlockDAO, feed *event.Feed,
|
|
|
|
transactionManager *TransactionManager,
|
|
|
|
chainClients map[uint64]*chain.ClientWithFallback,
|
|
|
|
accounts []common.Address) *SequentialFetchStrategy {
|
|
|
|
|
|
|
|
return &SequentialFetchStrategy{
|
|
|
|
db: db,
|
|
|
|
blockDAO: blockDAO,
|
|
|
|
feed: feed,
|
|
|
|
transactionManager: transactionManager,
|
|
|
|
chainClients: chainClients,
|
|
|
|
accounts: accounts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type SequentialFetchStrategy struct {
|
|
|
|
db *Database
|
|
|
|
blockDAO *BlockDAO
|
|
|
|
feed *event.Feed
|
|
|
|
mu sync.Mutex
|
|
|
|
group *async.Group
|
|
|
|
transactionManager *TransactionManager
|
|
|
|
chainClients map[uint64]*chain.ClientWithFallback
|
|
|
|
accounts []common.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SequentialFetchStrategy) newCommand(chainClient *chain.ClientWithFallback,
|
|
|
|
accounts []common.Address) async.Commander {
|
|
|
|
|
2023-06-01 13:09:50 +00:00
|
|
|
return newLoadBlocksAndTransfersCommand(accounts, s.db, s.blockDAO, chainClient, s.feed,
|
|
|
|
s.transactionManager)
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SequentialFetchStrategy) start() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if s.group != nil {
|
|
|
|
return errAlreadyRunning
|
|
|
|
}
|
|
|
|
s.group = async.NewGroup(context.Background())
|
|
|
|
|
2023-06-01 13:09:50 +00:00
|
|
|
if s.feed != nil {
|
|
|
|
s.feed.Send(walletevent.Event{
|
|
|
|
Type: EventFetchingRecentHistory,
|
|
|
|
Accounts: s.accounts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
for _, chainClient := range s.chainClients {
|
|
|
|
ctl := s.newCommand(chainClient, s.accounts)
|
|
|
|
s.group.Add(ctl.Command())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops reactor loop and waits till it exits.
|
|
|
|
func (s *SequentialFetchStrategy) stop() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.group == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.group.Stop()
|
|
|
|
s.group.Wait()
|
|
|
|
s.group = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SequentialFetchStrategy) kind() FetchStrategyType {
|
|
|
|
return SequentialFetchStrategyType
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:19:48 +00:00
|
|
|
// TODO: remove fetchMore parameter from here and interface, it is used by OnDemandFetchStrategy only
|
2023-05-08 06:02:00 +00:00
|
|
|
func (s *SequentialFetchStrategy) getTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
|
|
|
limit int64, fetchMore bool) ([]Transfer, error) {
|
|
|
|
|
2023-05-19 08:19:48 +00:00
|
|
|
log.Info("[WalletAPI:: GetTransfersByAddress] get transfers for an address", "address", address, "fetchMore", fetchMore,
|
|
|
|
"chainID", chainID, "toBlock", toBlock, "limit", limit)
|
|
|
|
|
|
|
|
rst, err := s.db.GetTransfersByAddress(chainID, address, toBlock, limit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, nil
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|