2022-05-10 07:48:05 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-27 07:38:05 +00:00
|
|
|
"math"
|
2022-05-10 07:48:05 +00:00
|
|
|
"math/big"
|
2024-02-02 10:42:56 +00:00
|
|
|
"sync"
|
2022-11-29 13:43:18 +00:00
|
|
|
"time"
|
2022-05-10 07:48:05 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-02-15 11:01:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-12-01 09:19:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-03-20 13:44:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-05-10 07:48:05 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-05-22 08:55:33 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2022-12-01 09:19:32 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2022-10-27 07:38:05 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
2023-12-22 09:43:19 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/community"
|
2023-02-21 09:05:16 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/market"
|
2023-01-25 18:28:51 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2024-03-15 12:37:00 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
2022-11-29 13:43:18 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2022-05-10 07:48:05 +00:00
|
|
|
)
|
|
|
|
|
2022-11-29 13:43:18 +00:00
|
|
|
// WalletTickReload emitted every 15mn to reload the wallet balance and history
|
|
|
|
const EventWalletTickReload walletevent.EventType = "wallet-tick-reload"
|
2023-01-17 09:56:16 +00:00
|
|
|
const EventWalletTickCheckConnected walletevent.EventType = "wallet-tick-check-connected"
|
2022-11-29 13:43:18 +00:00
|
|
|
|
2023-11-27 21:28:16 +00:00
|
|
|
const (
|
|
|
|
walletTickReloadPeriod = 10 * time.Minute
|
|
|
|
activityReloadDelay = 30 // Wait this many seconds after activity is detected before triggering a wallet reload
|
|
|
|
activityReloadMarginSeconds = 30 // Trigger a wallet reload if activity is detected this many seconds before the last reload
|
|
|
|
)
|
|
|
|
|
2023-01-12 17:54:14 +00:00
|
|
|
func getFixedCurrencies() []string {
|
2023-01-19 14:49:48 +00:00
|
|
|
return []string{"USD"}
|
2023-01-12 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:59:17 +00:00
|
|
|
func belongsToMandatoryTokens(symbol string) bool {
|
|
|
|
var mandatoryTokens = []string{"ETH", "DAI", "SNT", "STT"}
|
|
|
|
for _, t := range mandatoryTokens {
|
|
|
|
if t == symbol {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:43:19 +00:00
|
|
|
func NewReader(rpcClient *rpc.Client, tokenManager *token.Manager, marketManager *market.Manager, communityManager *community.Manager, accountsDB *accounts.Database, persistence *Persistence, walletFeed *event.Feed) *Reader {
|
2023-04-25 12:00:17 +00:00
|
|
|
return &Reader{
|
2024-04-11 15:33:07 +00:00
|
|
|
rpcClient: rpcClient,
|
|
|
|
tokenManager: tokenManager,
|
|
|
|
marketManager: marketManager,
|
|
|
|
communityManager: communityManager,
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
persistence: persistence,
|
|
|
|
walletFeed: walletFeed,
|
|
|
|
refreshBalanceCache: true,
|
2023-11-27 21:28:16 +00:00
|
|
|
}
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reader struct {
|
2023-11-27 21:28:16 +00:00
|
|
|
rpcClient *rpc.Client
|
|
|
|
tokenManager *token.Manager
|
|
|
|
marketManager *market.Manager
|
2023-12-22 09:43:19 +00:00
|
|
|
communityManager *community.Manager
|
2023-11-27 21:28:16 +00:00
|
|
|
accountsDB *accounts.Database
|
|
|
|
persistence *Persistence
|
|
|
|
walletFeed *event.Feed
|
|
|
|
cancel context.CancelFunc
|
|
|
|
walletEventsWatcher *walletevent.Watcher
|
2024-04-11 15:33:07 +00:00
|
|
|
lastWalletTokenUpdateTimestamp sync.Map
|
2023-11-27 21:28:16 +00:00
|
|
|
reloadDelayTimer *time.Timer
|
2024-02-02 10:42:56 +00:00
|
|
|
refreshBalanceCache bool
|
|
|
|
rw sync.RWMutex
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:54:14 +00:00
|
|
|
type TokenMarketValues struct {
|
|
|
|
MarketCap float64 `json:"marketCap"`
|
|
|
|
HighDay float64 `json:"highDay"`
|
|
|
|
LowDay float64 `json:"lowDay"`
|
|
|
|
ChangePctHour float64 `json:"changePctHour"`
|
|
|
|
ChangePctDay float64 `json:"changePctDay"`
|
|
|
|
ChangePct24hour float64 `json:"changePct24hour"`
|
|
|
|
Change24hour float64 `json:"change24hour"`
|
|
|
|
Price float64 `json:"price"`
|
2023-03-24 08:38:27 +00:00
|
|
|
HasError bool `json:"hasError"`
|
2023-01-12 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
type ChainBalance struct {
|
2024-03-08 12:52:39 +00:00
|
|
|
RawBalance string `json:"rawBalance"`
|
|
|
|
Balance *big.Float `json:"balance"`
|
|
|
|
Balance1DayAgo string `json:"balance1DayAgo"`
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
ChainID uint64 `json:"chainId"`
|
|
|
|
HasError bool `json:"hasError"`
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
type Token struct {
|
2023-01-12 17:54:14 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Decimals uint `json:"decimals"`
|
|
|
|
BalancesPerChain map[uint64]ChainBalance `json:"balancesPerChain"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
AssetWebsiteURL string `json:"assetWebsiteUrl"`
|
|
|
|
BuiltOn string `json:"builtOn"`
|
|
|
|
MarketValuesPerCurrency map[string]TokenMarketValues `json:"marketValuesPerCurrency"`
|
|
|
|
PegSymbol string `json:"pegSymbol"`
|
2023-08-31 07:47:24 +00:00
|
|
|
Verified bool `json:"verified"`
|
2023-12-12 07:37:57 +00:00
|
|
|
Image string `json:"image,omitempty"`
|
2023-12-22 09:43:19 +00:00
|
|
|
CommunityData *community.Data `json:"community_data,omitempty"`
|
2023-08-31 07:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func splitVerifiedTokens(tokens []*token.Token) ([]*token.Token, []*token.Token) {
|
|
|
|
verified := make([]*token.Token, 0)
|
|
|
|
unverified := make([]*token.Token, 0)
|
|
|
|
|
|
|
|
for _, t := range tokens {
|
|
|
|
if t.Verified {
|
|
|
|
verified = append(verified, t)
|
|
|
|
} else {
|
|
|
|
unverified = append(unverified, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return verified, unverified
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
func getTokenBySymbols(tokens []*token.Token) map[string][]*token.Token {
|
|
|
|
res := make(map[string][]*token.Token)
|
2022-05-10 07:48:05 +00:00
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
for _, t := range tokens {
|
|
|
|
if _, ok := res[t.Symbol]; !ok {
|
|
|
|
res[t.Symbol] = make([]*token.Token, 0)
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
2022-10-27 07:38:05 +00:00
|
|
|
|
|
|
|
res[t.Symbol] = append(res[t.Symbol], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTokenAddresses(tokens []*token.Token) []common.Address {
|
|
|
|
set := make(map[common.Address]bool)
|
|
|
|
for _, token := range tokens {
|
|
|
|
set[token.Address] = true
|
|
|
|
}
|
|
|
|
res := make([]common.Address, 0)
|
|
|
|
for address := range set {
|
|
|
|
res = append(res, address)
|
|
|
|
}
|
|
|
|
return res
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
func (r *Reader) Start() error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
r.cancel = cancel
|
|
|
|
|
2023-11-27 21:28:16 +00:00
|
|
|
r.startWalletEventsWatcher()
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
go func() {
|
2023-11-27 21:28:16 +00:00
|
|
|
ticker := time.NewTicker(walletTickReloadPeriod)
|
2022-12-01 09:19:32 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2023-11-27 21:28:16 +00:00
|
|
|
r.triggerWalletReload()
|
2022-12-01 09:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-11-29 13:43:18 +00:00
|
|
|
return nil
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
func (r *Reader) Stop() {
|
|
|
|
if r.cancel != nil {
|
|
|
|
r.cancel()
|
|
|
|
}
|
2023-11-27 21:28:16 +00:00
|
|
|
|
|
|
|
r.stopWalletEventsWatcher()
|
|
|
|
|
|
|
|
r.cancelDelayedWalletReload()
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
r.lastWalletTokenUpdateTimestamp = sync.Map{}
|
2023-11-27 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) triggerWalletReload() {
|
|
|
|
r.cancelDelayedWalletReload()
|
|
|
|
|
|
|
|
r.walletFeed.Send(walletevent.Event{
|
|
|
|
Type: EventWalletTickReload,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) triggerDelayedWalletReload() {
|
|
|
|
r.cancelDelayedWalletReload()
|
|
|
|
|
|
|
|
r.reloadDelayTimer = time.AfterFunc(time.Duration(activityReloadDelay)*time.Second, r.triggerWalletReload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) cancelDelayedWalletReload() {
|
|
|
|
|
|
|
|
if r.reloadDelayTimer != nil {
|
|
|
|
r.reloadDelayTimer.Stop()
|
|
|
|
r.reloadDelayTimer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) startWalletEventsWatcher() {
|
|
|
|
if r.walletEventsWatcher != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond to ETH/Token transfers
|
|
|
|
walletEventCb := func(event walletevent.Event) {
|
|
|
|
if event.Type != transfer.EventInternalETHTransferDetected &&
|
|
|
|
event.Type != transfer.EventInternalERC20TransferDetected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
for _, address := range event.Accounts {
|
|
|
|
timestamp, ok := r.lastWalletTokenUpdateTimestamp.Load(address)
|
2024-04-25 12:15:07 +00:00
|
|
|
timecheck := int64(0)
|
|
|
|
if ok {
|
|
|
|
timecheck = timestamp.(int64) - activityReloadMarginSeconds
|
|
|
|
}
|
2024-02-02 10:42:56 +00:00
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
if !ok || event.At > timecheck {
|
|
|
|
r.triggerDelayedWalletReload()
|
|
|
|
r.invalidateBalanceCache()
|
|
|
|
break
|
|
|
|
}
|
2024-02-02 10:42:56 +00:00
|
|
|
}
|
2023-11-27 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.walletEventsWatcher = walletevent.NewWatcher(r.walletFeed, walletEventCb)
|
|
|
|
|
|
|
|
r.walletEventsWatcher.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) stopWalletEventsWatcher() {
|
|
|
|
if r.walletEventsWatcher != nil {
|
|
|
|
r.walletEventsWatcher.Stop()
|
|
|
|
r.walletEventsWatcher = nil
|
|
|
|
}
|
2022-12-01 09:19:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
func (r *Reader) tokensCachedForAddresses(addresses []common.Address) bool {
|
|
|
|
for _, address := range addresses {
|
|
|
|
cachedTokens, err := r.GetCachedWalletTokensWithoutMarketData()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := cachedTokens[address]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) isCacheTimestampValidForAddress(address common.Address) bool {
|
|
|
|
_, ok := r.lastWalletTokenUpdateTimestamp.Load(address)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) areCacheTimestampsValid(addresses []common.Address) bool {
|
|
|
|
for _, address := range addresses {
|
|
|
|
if !r.isCacheTimestampValidForAddress(address) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) isBalanceCacheValid(addresses []common.Address) bool {
|
2024-02-02 10:42:56 +00:00
|
|
|
r.rw.RLock()
|
|
|
|
defer r.rw.RUnlock()
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
return !r.refreshBalanceCache && r.tokensCachedForAddresses(addresses) && r.areCacheTimestampsValid(addresses)
|
2024-02-02 10:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) balanceRefreshed() {
|
|
|
|
r.rw.Lock()
|
|
|
|
defer r.rw.Unlock()
|
|
|
|
|
|
|
|
r.refreshBalanceCache = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) invalidateBalanceCache() {
|
|
|
|
r.rw.Lock()
|
|
|
|
defer r.rw.Unlock()
|
|
|
|
|
|
|
|
r.refreshBalanceCache = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
2024-04-11 15:33:07 +00:00
|
|
|
if !r.isBalanceCacheValid(addresses) {
|
2024-02-02 10:42:56 +00:00
|
|
|
balances, err := r.GetWalletTokenBalances(ctx, addresses)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.balanceRefreshed()
|
|
|
|
|
|
|
|
return balances, nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 11:01:49 +00:00
|
|
|
tokens, err := r.getWalletTokenBalances(ctx, addresses, false)
|
2024-02-12 09:10:28 +00:00
|
|
|
return tokens, err
|
2024-02-02 10:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 11:01:49 +00:00
|
|
|
func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.Address, updateBalances bool) (map[common.Address][]Token, error) {
|
2024-01-03 13:54:49 +00:00
|
|
|
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
networks, err := r.rpcClient.NetworkManager.Get(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
availableNetworks := make([]*params.Network, 0)
|
|
|
|
for _, network := range networks {
|
|
|
|
if network.IsTest != areTestNetworksEnabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
availableNetworks = append(availableNetworks, network)
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedTokens, err := r.GetCachedWalletTokensWithoutMarketData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chainIDs := make([]uint64, 0)
|
|
|
|
for _, network := range availableNetworks {
|
|
|
|
chainIDs = append(chainIDs, network.ChainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
allTokens, err := r.tokenManager.GetTokensByChainIDs(chainIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range availableNetworks {
|
|
|
|
allTokens = append(allTokens, r.tokenManager.ToToken(network))
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenAddresses := getTokenAddresses(allTokens)
|
|
|
|
|
|
|
|
clients, err := r.rpcClient.EthClients(chainIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-15 11:01:49 +00:00
|
|
|
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
|
|
|
|
|
2024-03-11 13:48:40 +00:00
|
|
|
cachedBalancesPerChain := map[common.Address]map[common.Address]map[uint64]string{}
|
2024-02-15 11:01:49 +00:00
|
|
|
updateAnyway := false
|
|
|
|
if !updateBalances {
|
2024-03-15 12:37:00 +00:00
|
|
|
cacheCheck:
|
2024-03-11 13:48:40 +00:00
|
|
|
for _, address := range addresses {
|
2024-03-15 12:37:00 +00:00
|
|
|
if res, ok := cachedTokens[address]; !ok || len(res) == 0 {
|
2024-03-11 13:48:40 +00:00
|
|
|
updateAnyway = true
|
|
|
|
break
|
2024-02-15 11:01:49 +00:00
|
|
|
}
|
2024-03-15 12:37:00 +00:00
|
|
|
|
|
|
|
networkFound := map[uint64]bool{}
|
|
|
|
for _, token := range cachedTokens[address] {
|
|
|
|
for _, chain := range chainIDs {
|
|
|
|
if _, ok := token.BalancesPerChain[chain]; ok {
|
|
|
|
networkFound[chain] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chain := range chainIDs {
|
|
|
|
if !networkFound[chain] {
|
|
|
|
updateAnyway = true
|
|
|
|
break cacheCheck
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 13:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-15 11:01:49 +00:00
|
|
|
|
2024-03-11 13:48:40 +00:00
|
|
|
if !updateBalances && !updateAnyway {
|
|
|
|
for address, tokens := range cachedTokens {
|
2024-02-15 11:01:49 +00:00
|
|
|
for _, token := range tokens {
|
|
|
|
for _, balance := range token.BalancesPerChain {
|
2024-03-11 13:48:40 +00:00
|
|
|
if _, ok := cachedBalancesPerChain[address]; !ok {
|
|
|
|
cachedBalancesPerChain[address] = map[common.Address]map[uint64]string{}
|
|
|
|
}
|
2024-02-15 11:01:49 +00:00
|
|
|
if _, ok := cachedBalancesPerChain[address][balance.Address]; !ok {
|
2024-03-11 13:48:40 +00:00
|
|
|
cachedBalancesPerChain[address][balance.Address] = map[uint64]string{}
|
2024-02-15 11:01:49 +00:00
|
|
|
}
|
2024-03-11 13:48:40 +00:00
|
|
|
cachedBalancesPerChain[address][balance.Address][balance.ChainID] = balance.RawBalance
|
2024-02-15 11:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var latestBalances map[uint64]map[common.Address]map[common.Address]*hexutil.Big
|
|
|
|
if updateBalances || updateAnyway {
|
|
|
|
latestBalances, err = r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
|
|
|
|
if err != nil {
|
|
|
|
for _, client := range clients {
|
|
|
|
client.SetIsConnected(false)
|
|
|
|
}
|
|
|
|
log.Info("tokenManager.GetBalancesByChain error", "err", err)
|
|
|
|
return nil, err
|
2024-01-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[common.Address][]Token)
|
2024-03-08 12:52:39 +00:00
|
|
|
dayAgoTimestamp := time.Now().Add(-24 * time.Hour).Unix()
|
2024-01-03 13:54:49 +00:00
|
|
|
|
|
|
|
for _, address := range addresses {
|
|
|
|
for _, tokenList := range [][]*token.Token{verifiedTokens, unverifiedTokens} {
|
|
|
|
for symbol, tokens := range getTokenBySymbols(tokenList) {
|
|
|
|
balancesPerChain := make(map[uint64]ChainBalance)
|
|
|
|
decimals := tokens[0].Decimals
|
|
|
|
isVisible := false
|
|
|
|
for _, token := range tokens {
|
2024-02-15 11:01:49 +00:00
|
|
|
var balance *big.Float
|
2024-03-11 13:48:40 +00:00
|
|
|
hexBalance := &big.Int{}
|
2024-02-15 11:01:49 +00:00
|
|
|
if latestBalances != nil {
|
2024-03-11 13:48:40 +00:00
|
|
|
hexBalance = latestBalances[token.ChainID][address][token.Address].ToInt()
|
2024-02-15 11:01:49 +00:00
|
|
|
} else {
|
2024-03-11 13:48:40 +00:00
|
|
|
if cachedRawBalance, ok := cachedBalancesPerChain[address][token.Address][token.ChainID]; ok {
|
|
|
|
hexBalance, _ = new(big.Int).SetString(cachedRawBalance, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
balance = big.NewFloat(0.0)
|
|
|
|
if hexBalance != nil {
|
|
|
|
balance = new(big.Float).Quo(
|
|
|
|
new(big.Float).SetInt(hexBalance),
|
|
|
|
big.NewFloat(math.Pow(10, float64(decimals))),
|
|
|
|
)
|
2024-01-03 13:54:49 +00:00
|
|
|
}
|
2024-03-11 13:48:40 +00:00
|
|
|
|
2024-01-03 13:54:49 +00:00
|
|
|
hasError := false
|
|
|
|
if client, ok := clients[token.ChainID]; ok {
|
|
|
|
hasError = err != nil || !client.GetIsConnected()
|
|
|
|
}
|
|
|
|
if !isVisible {
|
|
|
|
isVisible = balance.Cmp(big.NewFloat(0.0)) > 0 || r.isCachedToken(cachedTokens, address, token.Symbol, token.ChainID)
|
|
|
|
}
|
|
|
|
balancesPerChain[token.ChainID] = ChainBalance{
|
2024-03-11 13:48:40 +00:00
|
|
|
RawBalance: hexBalance.String(),
|
2024-03-08 12:52:39 +00:00
|
|
|
Balance: balance,
|
2024-03-18 08:37:05 +00:00
|
|
|
Balance1DayAgo: "0",
|
2024-03-08 12:52:39 +00:00
|
|
|
Address: token.Address,
|
|
|
|
ChainID: token.ChainID,
|
|
|
|
HasError: hasError,
|
2024-01-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isVisible && !belongsToMandatoryTokens(symbol) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-18 08:37:05 +00:00
|
|
|
for _, balance := range balancesPerChain {
|
|
|
|
balance1DayAgo, err := r.tokenManager.GetTokenHistoricalBalance(address, balance.ChainID, symbol, dayAgoTimestamp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if balance1DayAgo != nil {
|
|
|
|
balance.Balance1DayAgo = balance1DayAgo.String()
|
|
|
|
balancesPerChain[balance.ChainID] = balance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 13:54:49 +00:00
|
|
|
walletToken := Token{
|
|
|
|
Name: tokens[0].Name,
|
|
|
|
Symbol: symbol,
|
|
|
|
BalancesPerChain: balancesPerChain,
|
|
|
|
Decimals: decimals,
|
|
|
|
PegSymbol: token.GetTokenPegSymbol(symbol),
|
|
|
|
Verified: tokens[0].Verified,
|
|
|
|
CommunityData: tokens[0].CommunityData,
|
|
|
|
Image: tokens[0].Image,
|
|
|
|
}
|
|
|
|
|
|
|
|
result[address] = append(result[address], walletToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
r.updateTokenUpdateTimestamp(addresses)
|
2024-01-03 13:54:49 +00:00
|
|
|
|
|
|
|
return result, r.persistence.SaveTokens(result)
|
|
|
|
}
|
|
|
|
|
2024-02-15 11:01:49 +00:00
|
|
|
func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
|
|
|
return r.getWalletTokenBalances(ctx, addresses, true)
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:55:44 +00:00
|
|
|
func (r *Reader) GetWalletToken(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
2023-05-22 08:55:33 +00:00
|
|
|
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
networks, err := r.rpcClient.NetworkManager.Get(false)
|
2022-05-10 07:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-22 08:55:33 +00:00
|
|
|
availableNetworks := make([]*params.Network, 0)
|
|
|
|
for _, network := range networks {
|
|
|
|
if network.IsTest != areTestNetworksEnabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
availableNetworks = append(availableNetworks, network)
|
|
|
|
}
|
2022-05-10 07:48:05 +00:00
|
|
|
|
2023-12-21 14:12:50 +00:00
|
|
|
cachedTokens, err := r.GetCachedWalletTokensWithoutMarketData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
chainIDs := make([]uint64, 0)
|
2023-05-22 08:55:33 +00:00
|
|
|
for _, network := range availableNetworks {
|
2022-10-27 07:38:05 +00:00
|
|
|
chainIDs = append(chainIDs, network.ChainID)
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:54:14 +00:00
|
|
|
currencies := make([]string, 0)
|
2022-12-01 09:19:32 +00:00
|
|
|
currency, err := r.accountsDB.GetCurrency()
|
2022-05-10 07:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-12 17:54:14 +00:00
|
|
|
currencies = append(currencies, currency)
|
|
|
|
currencies = append(currencies, getFixedCurrencies()...)
|
2023-10-17 15:05:05 +00:00
|
|
|
allTokens, err := r.tokenManager.GetTokensByChainIDs(chainIDs)
|
2023-08-31 07:47:24 +00:00
|
|
|
|
2022-05-10 07:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-22 08:55:33 +00:00
|
|
|
for _, network := range availableNetworks {
|
2022-12-01 09:19:32 +00:00
|
|
|
allTokens = append(allTokens, r.tokenManager.ToToken(network))
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
tokenAddresses := getTokenAddresses(allTokens)
|
|
|
|
|
2023-03-24 08:38:27 +00:00
|
|
|
clients, err := r.rpcClient.EthClients(chainIDs)
|
2023-03-30 07:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-27 07:38:05 +00:00
|
|
|
|
2023-09-12 07:45:24 +00:00
|
|
|
balances, err := r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
|
|
|
|
if err != nil {
|
|
|
|
for _, client := range clients {
|
|
|
|
client.SetIsConnected(false)
|
|
|
|
}
|
|
|
|
log.Info("tokenManager.GetBalancesByChain error", "err", err)
|
|
|
|
return nil, err
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
2023-09-12 07:45:24 +00:00
|
|
|
|
2023-08-31 07:47:24 +00:00
|
|
|
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
|
2023-09-12 07:45:24 +00:00
|
|
|
tokenSymbols := make([]string, 0)
|
|
|
|
result := make(map[common.Address][]Token)
|
2023-08-31 07:47:24 +00:00
|
|
|
|
2022-12-02 10:55:44 +00:00
|
|
|
for _, address := range addresses {
|
2023-08-31 07:47:24 +00:00
|
|
|
for _, tokenList := range [][]*token.Token{verifiedTokens, unverifiedTokens} {
|
|
|
|
for symbol, tokens := range getTokenBySymbols(tokenList) {
|
|
|
|
balancesPerChain := make(map[uint64]ChainBalance)
|
|
|
|
decimals := tokens[0].Decimals
|
2023-12-21 14:12:50 +00:00
|
|
|
isVisible := false
|
2023-08-31 07:47:24 +00:00
|
|
|
for _, token := range tokens {
|
|
|
|
hexBalance := balances[token.ChainID][address][token.Address]
|
|
|
|
balance := big.NewFloat(0.0)
|
|
|
|
if hexBalance != nil {
|
|
|
|
balance = new(big.Float).Quo(
|
|
|
|
new(big.Float).SetInt(hexBalance.ToInt()),
|
|
|
|
big.NewFloat(math.Pow(10, float64(decimals))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
hasError := false
|
|
|
|
if client, ok := clients[token.ChainID]; ok {
|
2023-09-20 08:41:23 +00:00
|
|
|
hasError = err != nil || !client.GetIsConnected()
|
2023-08-31 07:47:24 +00:00
|
|
|
}
|
2023-12-21 14:12:50 +00:00
|
|
|
if !isVisible {
|
|
|
|
isVisible = balance.Cmp(big.NewFloat(0.0)) > 0 || r.isCachedToken(cachedTokens, address, token.Symbol, token.ChainID)
|
2023-08-31 07:47:24 +00:00
|
|
|
}
|
|
|
|
balancesPerChain[token.ChainID] = ChainBalance{
|
2023-09-20 10:48:08 +00:00
|
|
|
RawBalance: hexBalance.ToInt().String(),
|
|
|
|
Balance: balance,
|
|
|
|
Address: token.Address,
|
|
|
|
ChainID: token.ChainID,
|
|
|
|
HasError: hasError,
|
2023-08-31 07:47:24 +00:00
|
|
|
}
|
2023-04-06 14:59:17 +00:00
|
|
|
}
|
2023-08-31 07:47:24 +00:00
|
|
|
|
2023-12-21 14:12:50 +00:00
|
|
|
if !isVisible && !belongsToMandatoryTokens(symbol) {
|
2023-08-31 07:47:24 +00:00
|
|
|
continue
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 07:47:24 +00:00
|
|
|
walletToken := Token{
|
2023-09-12 07:45:24 +00:00
|
|
|
Name: tokens[0].Name,
|
|
|
|
Symbol: symbol,
|
|
|
|
BalancesPerChain: balancesPerChain,
|
|
|
|
Decimals: decimals,
|
|
|
|
PegSymbol: token.GetTokenPegSymbol(symbol),
|
|
|
|
Verified: tokens[0].Verified,
|
2023-12-12 07:37:57 +00:00
|
|
|
CommunityData: tokens[0].CommunityData,
|
|
|
|
Image: tokens[0].Image,
|
2023-01-12 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:45:24 +00:00
|
|
|
tokenSymbols = append(tokenSymbols, symbol)
|
2023-08-31 07:47:24 +00:00
|
|
|
result[address] = append(result[address], walletToken)
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-25 12:00:17 +00:00
|
|
|
|
2023-09-12 07:45:24 +00:00
|
|
|
var (
|
|
|
|
group = async.NewAtomicGroup(ctx)
|
|
|
|
prices = map[string]map[string]float64{}
|
|
|
|
tokenDetails = map[string]thirdparty.TokenDetails{}
|
|
|
|
tokenMarketValues = map[string]thirdparty.TokenMarketValues{}
|
|
|
|
)
|
|
|
|
|
|
|
|
group.Add(func(parent context.Context) error {
|
|
|
|
prices, err = r.marketManager.FetchPrices(tokenSymbols, currencies)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("marketManager.FetchPrices err", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
group.Add(func(parent context.Context) error {
|
|
|
|
tokenDetails, err = r.marketManager.FetchTokenDetails(tokenSymbols)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("marketManager.FetchTokenDetails err", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
group.Add(func(parent context.Context) error {
|
|
|
|
tokenMarketValues, err = r.marketManager.FetchTokenMarketValues(tokenSymbols, currency)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("marketManager.FetchTokenMarketValues err", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-group.WaitAsync():
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
err = group.Error()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for address, tokens := range result {
|
|
|
|
for index, token := range tokens {
|
|
|
|
marketValuesPerCurrency := make(map[string]TokenMarketValues)
|
|
|
|
for _, currency := range currencies {
|
|
|
|
if _, ok := tokenMarketValues[token.Symbol]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
marketValuesPerCurrency[currency] = TokenMarketValues{
|
|
|
|
MarketCap: tokenMarketValues[token.Symbol].MKTCAP,
|
|
|
|
HighDay: tokenMarketValues[token.Symbol].HIGHDAY,
|
|
|
|
LowDay: tokenMarketValues[token.Symbol].LOWDAY,
|
|
|
|
ChangePctHour: tokenMarketValues[token.Symbol].CHANGEPCTHOUR,
|
|
|
|
ChangePctDay: tokenMarketValues[token.Symbol].CHANGEPCTDAY,
|
|
|
|
ChangePct24hour: tokenMarketValues[token.Symbol].CHANGEPCT24HOUR,
|
|
|
|
Change24hour: tokenMarketValues[token.Symbol].CHANGE24HOUR,
|
|
|
|
Price: prices[token.Symbol][currency],
|
|
|
|
HasError: !r.marketManager.IsConnected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := tokenDetails[token.Symbol]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result[address][index].Description = tokenDetails[token.Symbol].Description
|
|
|
|
result[address][index].AssetWebsiteURL = tokenDetails[token.Symbol].AssetWebsiteURL
|
|
|
|
result[address][index].BuiltOn = tokenDetails[token.Symbol].BuiltOn
|
|
|
|
result[address][index].MarketValuesPerCurrency = marketValuesPerCurrency
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 15:33:07 +00:00
|
|
|
r.updateTokenUpdateTimestamp(addresses)
|
2023-11-27 21:28:16 +00:00
|
|
|
|
2023-04-25 12:00:17 +00:00
|
|
|
return result, r.persistence.SaveTokens(result)
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:12:50 +00:00
|
|
|
func (r *Reader) isCachedToken(cachedTokens map[common.Address][]Token, address common.Address, symbol string, chainID uint64) bool {
|
|
|
|
if tokens, ok := cachedTokens[address]; ok {
|
|
|
|
for _, t := range tokens {
|
|
|
|
if t.Symbol != symbol {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ok := t.BalancesPerChain[chainID]
|
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-25 12:00:17 +00:00
|
|
|
// GetCachedWalletTokensWithoutMarketData returns the latest fetched balances, minus
|
|
|
|
// price information
|
|
|
|
func (r *Reader) GetCachedWalletTokensWithoutMarketData() (map[common.Address][]Token, error) {
|
|
|
|
return r.persistence.GetTokens()
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
2024-04-11 15:33:07 +00:00
|
|
|
|
|
|
|
func (r *Reader) updateTokenUpdateTimestamp(addresses []common.Address) {
|
|
|
|
for _, address := range addresses {
|
|
|
|
r.lastWalletTokenUpdateTimestamp.Store(address, time.Now().Unix())
|
|
|
|
}
|
|
|
|
}
|