2019-06-14 10:16:30 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2021-09-09 14:28:54 +00:00
|
|
|
"database/sql"
|
2023-03-23 12:13:16 +00:00
|
|
|
"encoding/json"
|
2023-06-28 13:39:13 +00:00
|
|
|
"sync"
|
2023-02-20 09:32:45 +00:00
|
|
|
"time"
|
2019-06-14 10:16:30 +00:00
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-14 10:16:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2021-09-22 17:49:20 +00:00
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
2022-05-18 11:31:45 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2022-05-10 07:48:05 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2021-09-22 17:49:20 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/ens"
|
|
|
|
"github.com/status-im/status-go/services/stickers"
|
2023-06-08 23:52:45 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/activity"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/collectibles"
|
2023-02-17 14:11:07 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/currency"
|
2022-11-15 12:14:41 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/history"
|
2023-02-21 09:05:16 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/market"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/alchemy"
|
2023-02-28 08:14:37 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/coingecko"
|
2023-02-21 09:05:16 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/cryptocompare"
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/infura"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
2022-12-01 09:19:32 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2019-06-14 10:16:30 +00:00
|
|
|
)
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
const (
|
|
|
|
EventBlockchainStatusChanged walletevent.EventType = "wallet-blockchain-status-changed"
|
|
|
|
)
|
2023-01-17 09:56:16 +00:00
|
|
|
|
2019-06-14 10:16:30 +00:00
|
|
|
// NewService initializes service instance.
|
2022-07-15 08:53:56 +00:00
|
|
|
func NewService(
|
|
|
|
db *sql.DB,
|
|
|
|
accountsDB *accounts.Database,
|
|
|
|
rpcClient *rpc.Client,
|
|
|
|
accountFeed *event.Feed,
|
|
|
|
gethManager *account.GethManager,
|
|
|
|
transactor *transactions.Transactor,
|
|
|
|
config *params.NodeConfig,
|
2022-09-13 07:10:59 +00:00
|
|
|
ens *ens.Service,
|
|
|
|
stickers *stickers.Service,
|
2023-03-21 13:52:14 +00:00
|
|
|
nftMetadataProvider thirdparty.NFTMetadataProvider,
|
2022-07-15 08:53:56 +00:00
|
|
|
) *Service {
|
2021-09-09 14:28:54 +00:00
|
|
|
cryptoOnRampManager := NewCryptoOnRampManager(&CryptoOnRampOptions{
|
|
|
|
dataSourceType: DataSourceStatic,
|
|
|
|
})
|
2022-12-01 09:19:32 +00:00
|
|
|
walletFeed := &event.Feed{}
|
|
|
|
signals := &walletevent.SignalsTransmitter{
|
|
|
|
Publisher: walletFeed,
|
|
|
|
}
|
2023-03-23 12:13:16 +00:00
|
|
|
blockchainStatus := make(map[uint64]string)
|
2023-06-28 13:39:13 +00:00
|
|
|
mutex := sync.Mutex{}
|
2023-03-20 13:02:09 +00:00
|
|
|
rpcClient.SetWalletNotifier(func(chainID uint64, message string) {
|
2023-06-28 13:39:13 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
2023-03-23 12:13:16 +00:00
|
|
|
if len(blockchainStatus) == 0 {
|
|
|
|
networks, err := rpcClient.NetworkManager.Get(false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range networks {
|
|
|
|
blockchainStatus[network.ChainID] = "up"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blockchainStatus[chainID] = message
|
|
|
|
encodedmessage, err := json.Marshal(blockchainStatus)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
walletFeed.Send(walletevent.Event{
|
|
|
|
Type: EventBlockchainStatusChanged,
|
|
|
|
Accounts: []common.Address{},
|
2023-03-23 12:13:16 +00:00
|
|
|
Message: string(encodedmessage),
|
2023-03-20 13:02:09 +00:00
|
|
|
At: time.Now().Unix(),
|
|
|
|
ChainID: chainID,
|
|
|
|
})
|
|
|
|
})
|
2022-09-13 07:10:59 +00:00
|
|
|
tokenManager := token.NewTokenManager(db, rpcClient, rpcClient.NetworkManager)
|
2021-09-10 18:08:22 +00:00
|
|
|
savedAddressesManager := &SavedAddressesManager{db: db}
|
2023-02-15 12:28:19 +00:00
|
|
|
transactionManager := transfer.NewTransactionManager(db, gethManager, transactor, config, accountsDB)
|
2023-06-05 15:05:50 +00:00
|
|
|
transferController := transfer.NewTransferController(db, rpcClient, accountFeed, walletFeed, transactionManager, tokenManager, config.WalletConfig.LoadAllTransfers)
|
2023-02-21 09:05:16 +00:00
|
|
|
cryptoCompare := cryptocompare.NewClient()
|
2023-02-28 08:14:37 +00:00
|
|
|
coingecko := coingecko.NewClient()
|
2023-03-20 13:02:09 +00:00
|
|
|
marketManager := market.NewManager(cryptoCompare, coingecko, walletFeed)
|
2023-04-25 12:00:17 +00:00
|
|
|
reader := NewReader(rpcClient, tokenManager, marketManager, accountsDB, NewPersistence(db), walletFeed)
|
2023-02-21 09:05:16 +00:00
|
|
|
history := history.NewService(db, walletFeed, rpcClient, tokenManager, marketManager)
|
|
|
|
currency := currency.NewService(db, walletFeed, tokenManager, marketManager)
|
2023-06-13 09:25:23 +00:00
|
|
|
activity := activity.NewService(db, tokenManager, walletFeed)
|
2023-04-17 11:42:01 +00:00
|
|
|
|
2023-04-18 14:04:12 +00:00
|
|
|
alchemyClient := alchemy.NewClient(config.WalletConfig.AlchemyAPIKeys)
|
|
|
|
infuraClient := infura.NewClient(config.WalletConfig.InfuraAPIKey, config.WalletConfig.InfuraAPIKeySecret)
|
|
|
|
collectiblesManager := collectibles.NewManager(rpcClient, alchemyClient, infuraClient, nftMetadataProvider, config.WalletConfig.OpenseaAPIKey, walletFeed)
|
2019-06-14 10:16:30 +00:00
|
|
|
return &Service{
|
2022-05-10 07:48:05 +00:00
|
|
|
db: db,
|
|
|
|
accountsDB: accountsDB,
|
2021-09-22 17:49:20 +00:00
|
|
|
rpcClient: rpcClient,
|
2021-09-10 18:08:22 +00:00
|
|
|
tokenManager: tokenManager,
|
|
|
|
savedAddressesManager: savedAddressesManager,
|
|
|
|
transactionManager: transactionManager,
|
|
|
|
transferController: transferController,
|
|
|
|
cryptoOnRampManager: cryptoOnRampManager,
|
2023-03-21 13:52:14 +00:00
|
|
|
collectiblesManager: collectiblesManager,
|
2022-03-29 21:12:05 +00:00
|
|
|
feesManager: &FeeManager{rpcClient},
|
2022-05-18 11:31:45 +00:00
|
|
|
gethManager: gethManager,
|
2023-02-21 09:05:16 +00:00
|
|
|
marketManager: marketManager,
|
2022-09-13 07:10:59 +00:00
|
|
|
transactor: transactor,
|
|
|
|
ens: ens,
|
|
|
|
stickers: stickers,
|
2023-03-24 08:38:27 +00:00
|
|
|
feed: walletFeed,
|
2022-12-01 09:19:32 +00:00
|
|
|
signals: signals,
|
|
|
|
reader: reader,
|
2022-11-15 12:14:41 +00:00
|
|
|
history: history,
|
2023-02-17 14:11:07 +00:00
|
|
|
currency: currency,
|
2023-06-08 23:52:45 +00:00
|
|
|
activity: activity,
|
2023-07-06 07:37:04 +00:00
|
|
|
decoder: NewDecoder(),
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is a wallet service.
|
|
|
|
type Service struct {
|
2022-05-10 07:48:05 +00:00
|
|
|
db *sql.DB
|
|
|
|
accountsDB *accounts.Database
|
2021-09-22 17:49:20 +00:00
|
|
|
rpcClient *rpc.Client
|
2021-09-10 18:08:22 +00:00
|
|
|
savedAddressesManager *SavedAddressesManager
|
2022-10-25 14:50:32 +00:00
|
|
|
tokenManager *token.Manager
|
2023-02-15 12:28:19 +00:00
|
|
|
transactionManager *transfer.TransactionManager
|
2021-09-10 18:08:22 +00:00
|
|
|
cryptoOnRampManager *CryptoOnRampManager
|
|
|
|
transferController *transfer.Controller
|
2022-03-29 21:12:05 +00:00
|
|
|
feesManager *FeeManager
|
2023-02-21 09:05:16 +00:00
|
|
|
marketManager *market.Manager
|
2021-09-10 18:08:22 +00:00
|
|
|
started bool
|
2023-03-21 13:52:14 +00:00
|
|
|
collectiblesManager *collectibles.Manager
|
2022-05-18 11:31:45 +00:00
|
|
|
gethManager *account.GethManager
|
2022-09-13 07:10:59 +00:00
|
|
|
transactor *transactions.Transactor
|
|
|
|
ens *ens.Service
|
|
|
|
stickers *stickers.Service
|
2022-11-29 13:43:18 +00:00
|
|
|
feed *event.Feed
|
2022-12-01 09:19:32 +00:00
|
|
|
signals *walletevent.SignalsTransmitter
|
|
|
|
reader *Reader
|
2022-11-15 12:14:41 +00:00
|
|
|
history *history.Service
|
2023-02-17 14:11:07 +00:00
|
|
|
currency *currency.Service
|
2023-06-08 23:52:45 +00:00
|
|
|
activity *activity.Service
|
2023-07-06 07:37:04 +00:00
|
|
|
decoder *Decoder
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals transmitter.
|
2021-06-30 11:40:54 +00:00
|
|
|
func (s *Service) Start() error {
|
2022-12-01 09:19:32 +00:00
|
|
|
s.transferController.Start()
|
2023-02-17 14:11:07 +00:00
|
|
|
s.currency.Start()
|
2022-12-01 09:19:32 +00:00
|
|
|
err := s.signals.Start()
|
2023-01-25 18:28:51 +00:00
|
|
|
s.history.Start()
|
2021-09-09 14:28:54 +00:00
|
|
|
s.started = true
|
|
|
|
return err
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
// GetFeed returns signals feed.
|
|
|
|
func (s *Service) GetFeed() *event.Feed {
|
2021-09-09 14:28:54 +00:00
|
|
|
return s.transferController.TransferFeed
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
// Stop reactor and close db.
|
2019-06-14 10:16:30 +00:00
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("wallet will be stopped")
|
2022-12-01 09:19:32 +00:00
|
|
|
s.signals.Stop()
|
2021-09-09 14:28:54 +00:00
|
|
|
s.transferController.Stop()
|
2023-02-17 14:11:07 +00:00
|
|
|
s.currency.Stop()
|
2022-12-01 09:19:32 +00:00
|
|
|
s.reader.Stop()
|
2022-11-15 12:14:41 +00:00
|
|
|
s.history.Stop()
|
2023-06-08 23:52:45 +00:00
|
|
|
s.activity.Stop()
|
2021-09-09 14:28:54 +00:00
|
|
|
s.started = false
|
2019-06-14 10:16:30 +00:00
|
|
|
log.Info("wallet stopped")
|
2021-09-09 14:28:54 +00:00
|
|
|
return nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns list of available RPC APIs.
|
2021-09-22 17:49:20 +00:00
|
|
|
func (s *Service) APIs() []gethrpc.API {
|
|
|
|
return []gethrpc.API{
|
2019-06-14 10:16:30 +00:00
|
|
|
{
|
|
|
|
Namespace: "wallet",
|
|
|
|
Version: "0.1.0",
|
|
|
|
Service: NewAPI(s),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols returns list of p2p protocols.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-28 07:49:03 +00:00
|
|
|
|
2020-02-28 16:05:37 +00:00
|
|
|
func (s *Service) IsStarted() bool {
|
2020-08-18 08:44:56 +00:00
|
|
|
return s.started
|
2020-02-28 16:05:37 +00:00
|
|
|
}
|