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"
|
2022-11-29 13:43:18 +00:00
|
|
|
"time"
|
2022-05-10 07:48:05 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"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-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"
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
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-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-04-25 12:00:17 +00:00
|
|
|
func NewReader(rpcClient *rpc.Client, tokenManager *token.Manager, marketManager *market.Manager, accountsDB *accounts.Database, persistence *Persistence, walletFeed *event.Feed) *Reader {
|
|
|
|
return &Reader{
|
|
|
|
rpcClient,
|
|
|
|
tokenManager,
|
|
|
|
marketManager,
|
|
|
|
accountsDB,
|
|
|
|
persistence,
|
|
|
|
walletFeed,
|
|
|
|
nil}
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reader struct {
|
2023-01-17 09:56:16 +00:00
|
|
|
rpcClient *rpc.Client
|
|
|
|
tokenManager *token.Manager
|
2023-02-21 09:05:16 +00:00
|
|
|
marketManager *market.Manager
|
2023-01-17 09:56:16 +00:00
|
|
|
accountsDB *accounts.Database
|
2023-04-25 12:00:17 +00:00
|
|
|
persistence *Persistence
|
2023-01-17 09:56:16 +00:00
|
|
|
walletFeed *event.Feed
|
|
|
|
cancel context.CancelFunc
|
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 {
|
2023-03-24 08:38:27 +00:00
|
|
|
Balance *big.Float `json:"balance"`
|
|
|
|
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"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 getTokenSymbols(tokens []*token.Token) []string {
|
|
|
|
tokensBySymbols := getTokenBySymbols(tokens)
|
|
|
|
res := make([]string, 0)
|
|
|
|
for symbol := range tokensBySymbols {
|
|
|
|
res = append(res, symbol)
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
2022-10-27 07:38:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(10 * time.Minute)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
r.walletFeed.Send(walletevent.Event{
|
|
|
|
Type: EventWalletTickReload,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-05-22 08:55:33 +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
|
|
|
tokenSymbols := getTokenSymbols(allTokens)
|
|
|
|
tokenAddresses := getTokenAddresses(allTokens)
|
|
|
|
|
|
|
|
var (
|
|
|
|
group = async.NewAtomicGroup(ctx)
|
2023-01-12 17:54:14 +00:00
|
|
|
prices = map[string]map[string]float64{}
|
2023-02-21 09:05:16 +00:00
|
|
|
tokenDetails = map[string]thirdparty.TokenDetails{}
|
|
|
|
tokenMarketValues = map[string]thirdparty.TokenMarketValues{}
|
2022-10-27 07:38:05 +00:00
|
|
|
balances = map[uint64]map[common.Address]map[common.Address]*hexutil.Big{}
|
|
|
|
)
|
2022-05-10 07:48:05 +00:00
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
group.Add(func(parent context.Context) error {
|
2023-02-21 09:05:16 +00:00
|
|
|
prices, err = r.marketManager.FetchPrices(tokenSymbols, currencies)
|
2022-10-27 07:38:05 +00:00
|
|
|
if err != nil {
|
2023-03-20 13:44:23 +00:00
|
|
|
log.Info("marketManager.FetchPrices err", err)
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-10 07:48:05 +00:00
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
group.Add(func(parent context.Context) error {
|
2023-02-21 09:05:16 +00:00
|
|
|
tokenDetails, err = r.marketManager.FetchTokenDetails(tokenSymbols)
|
2022-10-27 07:38:05 +00:00
|
|
|
if err != nil {
|
2023-03-20 13:44:23 +00:00
|
|
|
log.Info("marketManager.FetchTokenDetails err", err)
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-10 07:48:05 +00:00
|
|
|
|
2022-10-27 07:38:05 +00:00
|
|
|
group.Add(func(parent context.Context) error {
|
2023-02-21 09:05:16 +00:00
|
|
|
tokenMarketValues, err = r.marketManager.FetchTokenMarketValues(tokenSymbols, currency)
|
2022-05-10 07:48:05 +00:00
|
|
|
if err != nil {
|
2023-03-20 13:44:23 +00:00
|
|
|
log.Info("marketManager.FetchTokenMarketValues err", err)
|
2022-05-10 07:48:05 +00:00
|
|
|
}
|
2022-10-27 07:38:05 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-05-10 07:48:05 +00:00
|
|
|
|
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
|
|
|
group.Add(func(parent context.Context) error {
|
2022-12-02 10:55:44 +00:00
|
|
|
balances, err = r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
|
2022-10-27 07:38:05 +00:00
|
|
|
if err != nil {
|
2023-03-31 08:47:14 +00:00
|
|
|
for _, client := range clients {
|
|
|
|
client.SetIsConnected(false)
|
|
|
|
}
|
2023-04-25 12:00:17 +00:00
|
|
|
log.Info("tokenManager.GetBalancesByChain error", "err", err)
|
2022-10-27 07:38:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-group.WaitAsync():
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
err = group.Error()
|
|
|
|
result := make(map[common.Address][]Token)
|
2023-08-31 07:47:24 +00:00
|
|
|
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
|
|
|
|
|
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
|
|
|
|
anyPositiveBalance := false
|
|
|
|
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 {
|
|
|
|
hasError = err != nil || !client.IsConnected
|
|
|
|
}
|
|
|
|
if !anyPositiveBalance {
|
|
|
|
anyPositiveBalance = balance.Cmp(big.NewFloat(0.0)) > 0
|
|
|
|
}
|
|
|
|
balancesPerChain[token.ChainID] = ChainBalance{
|
|
|
|
Balance: balance,
|
|
|
|
Address: token.Address,
|
|
|
|
ChainID: token.ChainID,
|
|
|
|
HasError: hasError,
|
|
|
|
}
|
2023-04-06 14:59:17 +00:00
|
|
|
}
|
2023-08-31 07:47:24 +00:00
|
|
|
|
|
|
|
if !anyPositiveBalance && !belongsToMandatoryTokens(symbol) {
|
|
|
|
continue
|
2022-10-27 07:38:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 07:47:24 +00:00
|
|
|
marketValuesPerCurrency := make(map[string]TokenMarketValues)
|
|
|
|
for _, currency := range currencies {
|
|
|
|
marketValuesPerCurrency[currency] = TokenMarketValues{
|
|
|
|
MarketCap: tokenMarketValues[symbol].MKTCAP,
|
|
|
|
HighDay: tokenMarketValues[symbol].HIGHDAY,
|
|
|
|
LowDay: tokenMarketValues[symbol].LOWDAY,
|
|
|
|
ChangePctHour: tokenMarketValues[symbol].CHANGEPCTHOUR,
|
|
|
|
ChangePctDay: tokenMarketValues[symbol].CHANGEPCTDAY,
|
|
|
|
ChangePct24hour: tokenMarketValues[symbol].CHANGEPCT24HOUR,
|
|
|
|
Change24hour: tokenMarketValues[symbol].CHANGE24HOUR,
|
|
|
|
Price: prices[symbol][currency],
|
|
|
|
HasError: !r.marketManager.IsConnected,
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 14:59:17 +00:00
|
|
|
|
2023-08-31 07:47:24 +00:00
|
|
|
walletToken := Token{
|
|
|
|
Name: tokens[0].Name,
|
|
|
|
Color: tokens[0].Color,
|
|
|
|
Symbol: symbol,
|
|
|
|
BalancesPerChain: balancesPerChain,
|
|
|
|
Decimals: decimals,
|
|
|
|
Description: tokenDetails[symbol].Description,
|
|
|
|
AssetWebsiteURL: tokenDetails[symbol].AssetWebsiteURL,
|
|
|
|
BuiltOn: tokenDetails[symbol].BuiltOn,
|
|
|
|
MarketValuesPerCurrency: marketValuesPerCurrency,
|
|
|
|
PegSymbol: token.GetTokenPegSymbol(symbol),
|
|
|
|
Verified: tokens[0].Verified,
|
2023-01-12 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
return result, r.persistence.SaveTokens(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|