2023-05-08 06:02:00 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-12-01 11:30:42 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
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-09-04 05:34:09 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/balance"
|
2024-01-25 12:05:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/blockchainstate"
|
2023-06-02 20:08:45 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2023-06-01 13:09:50 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2023-06-21 14:09:55 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2023-05-08 06:02:00 +00:00
|
|
|
)
|
|
|
|
|
2023-12-01 11:30:42 +00:00
|
|
|
func NewSequentialFetchStrategy(db *Database, blockDAO *BlockDAO, blockRangesSeqDAO *BlockRangeSequentialDAO, accountsDB *accounts.Database, feed *event.Feed,
|
2024-10-11 13:18:12 +00:00
|
|
|
pendingTxManager *transactions.PendingTxTracker,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager *token.Manager,
|
2023-09-20 08:41:23 +00:00
|
|
|
chainClients map[uint64]chain.ClientInterface,
|
2023-09-04 05:34:09 +00:00
|
|
|
accounts []common.Address,
|
|
|
|
balanceCacher balance.Cacher,
|
2023-10-18 10:02:35 +00:00
|
|
|
omitHistory bool,
|
2024-01-25 12:05:59 +00:00
|
|
|
blockChainState *blockchainstate.BlockChainState,
|
2023-09-04 05:34:09 +00:00
|
|
|
) *SequentialFetchStrategy {
|
2023-05-19 08:19:48 +00:00
|
|
|
|
|
|
|
return &SequentialFetchStrategy{
|
2024-10-11 13:18:12 +00:00
|
|
|
db: db,
|
|
|
|
blockDAO: blockDAO,
|
|
|
|
blockRangesSeqDAO: blockRangesSeqDAO,
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
feed: feed,
|
|
|
|
pendingTxManager: pendingTxManager,
|
|
|
|
tokenManager: tokenManager,
|
|
|
|
chainClients: chainClients,
|
|
|
|
accounts: accounts,
|
|
|
|
balanceCacher: balanceCacher,
|
|
|
|
omitHistory: omitHistory,
|
|
|
|
blockChainState: blockChainState,
|
2023-05-19 08:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type SequentialFetchStrategy struct {
|
2024-10-11 13:18:12 +00:00
|
|
|
db *Database
|
|
|
|
blockDAO *BlockDAO
|
|
|
|
blockRangesSeqDAO *BlockRangeSequentialDAO
|
|
|
|
accountsDB *accounts.Database
|
|
|
|
feed *event.Feed
|
|
|
|
mu sync.Mutex
|
|
|
|
group *async.Group
|
|
|
|
pendingTxManager *transactions.PendingTxTracker
|
|
|
|
tokenManager *token.Manager
|
|
|
|
chainClients map[uint64]chain.ClientInterface
|
|
|
|
accounts []common.Address
|
|
|
|
balanceCacher balance.Cacher
|
|
|
|
omitHistory bool
|
|
|
|
blockChainState *blockchainstate.BlockChainState
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
func (s *SequentialFetchStrategy) newCommand(chainClient chain.ClientInterface,
|
2023-11-27 10:08:17 +00:00
|
|
|
accounts []common.Address) async.Commander {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
return newLoadBlocksAndTransfersCommand(accounts, s.db, s.accountsDB, s.blockDAO, s.blockRangesSeqDAO, chainClient, s.feed,
|
2024-10-11 13:18:12 +00:00
|
|
|
s.pendingTxManager, s.tokenManager, s.balanceCacher, s.omitHistory, s.blockChainState)
|
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 {
|
2023-11-27 10:08:17 +00:00
|
|
|
ctl := s.newCommand(chainClient, s.accounts)
|
|
|
|
s.group.Add(ctl.Command())
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SequentialFetchStrategy) getTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
2023-11-15 14:30:56 +00:00
|
|
|
limit int64) ([]Transfer, error) {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug("[WalletAPI:: GetTransfersByAddress] get transfers for an address",
|
|
|
|
zap.Stringer("address", address),
|
|
|
|
zap.Uint64("chainID", chainID),
|
|
|
|
zap.Stringer("toBlock", toBlock),
|
|
|
|
zap.Int64("limit", limit))
|
2023-05-19 08:19:48 +00:00
|
|
|
|
|
|
|
rst, err := s.db.GetTransfersByAddress(chainID, address, toBlock, limit)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", zap.Error(err))
|
2023-05-19 08:19:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, nil
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|