From f81758ec6e48fe2c09b8c4b1997e6e3f25f1c1e0 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 23 Feb 2023 14:55:57 +0100 Subject: [PATCH] feat: add time to check --- services/wallet/chain/client.go | 5 ++- services/wallet/opensea.go | 5 ++- services/wallet/service.go | 34 +++++++++++++++------ services/wallet/thirdparty/cryptocompare.go | 4 ++- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/services/wallet/chain/client.go b/services/wallet/chain/client.go index 3a904f6af..f2131bb83 100644 --- a/services/wallet/chain/client.go +++ b/services/wallet/chain/client.go @@ -4,6 +4,7 @@ import ( "context" "math/big" "sync" + "time" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" @@ -21,6 +22,7 @@ type Client struct { ChainID uint64 rpcClient *rpc.Client IsConnected bool + LastCheckedAt int64 IsConnectedLock sync.RWMutex } @@ -37,7 +39,7 @@ func NewClient(rpc *rpc.Client, chainID uint64) (*Client, error) { if err != nil { return nil, err } - client := &Client{eth: ethClient, ChainID: chainID, rpcClient: rpc, IsConnected: true} + client := &Client{eth: ethClient, ChainID: chainID, rpcClient: rpc, IsConnected: true, LastCheckedAt: time.Now().Unix()} ChainClientInstances[chainID] = client return client, nil } @@ -60,6 +62,7 @@ func NewClients(rpc *rpc.Client, chainIDs []uint64) (res []*Client, err error) { func (cc *Client) toggleIsConnected(err error) { cc.IsConnectedLock.Lock() defer cc.IsConnectedLock.Unlock() + cc.LastCheckedAt = time.Now().Unix() if err != nil { cc.IsConnected = false } else { diff --git a/services/wallet/opensea.go b/services/wallet/opensea.go index e862d4499..df8bca3e2 100644 --- a/services/wallet/opensea.go +++ b/services/wallet/opensea.go @@ -116,6 +116,7 @@ type OpenseaClient struct { url string apiKey string IsConnected bool + LastCheckedAt int64 IsConnectedLock sync.RWMutex } @@ -131,7 +132,7 @@ func newOpenseaClient(chainID uint64, apiKey string) (*OpenseaClient, error) { Timeout: time.Second * 5, } if url, ok := BaseURLs[chainID]; ok { - openseaClient := &OpenseaClient{client: client, url: url, apiKey: apiKey, IsConnected: true} + openseaClient := &OpenseaClient{client: client, url: url, apiKey: apiKey, IsConnected: true, LastCheckedAt: time.Now().Unix()} OpenseaClientInstances[chainID] = openseaClient return openseaClient, nil } @@ -144,6 +145,7 @@ func (o *OpenseaClient) fetchAllCollectionsByOwner(owner common.Address) ([]Open var collections []OpenseaCollection o.IsConnectedLock.Lock() defer o.IsConnectedLock.Unlock() + o.LastCheckedAt = time.Now().Unix() for { url := fmt.Sprintf("%s/collections?asset_owner=%s&offset=%d&limit=%d", o.url, owner, offset, CollectionLimit) body, err := o.doOpenseaRequest(url) @@ -174,6 +176,7 @@ func (o *OpenseaClient) fetchAllAssetsByOwnerAndCollection(owner common.Address, var assets []OpenseaAsset o.IsConnectedLock.Lock() defer o.IsConnectedLock.Unlock() + o.LastCheckedAt = time.Now().Unix() for { url := fmt.Sprintf("%s/assets?owner=%s&collection=%s&offset=%d&limit=%d", o.url, owner, collectionSlug, offset, AssetLimit) body, err := o.doOpenseaRequest(url) diff --git a/services/wallet/service.go b/services/wallet/service.go index 572620ad6..adf47f6b5 100644 --- a/services/wallet/service.go +++ b/services/wallet/service.go @@ -26,10 +26,15 @@ import ( "github.com/status-im/status-go/transactions" ) +type Connection struct { + Up bool `json:"up"` + LastCheckedAt int64 `json:"lastCheckedAt"` +} + type ConnectedResult struct { - Infura map[uint64]bool `json:"infura"` - CryptoCompare bool `json:"cryptoCompare"` - Opensea map[uint64]bool `json:"opensea"` + Infura map[uint64]Connection `json:"infura"` + CryptoCompare Connection `json:"cryptoCompare"` + Opensea map[uint64]Connection `json:"opensea"` } // NewService initializes service instance. @@ -162,18 +167,27 @@ func (s *Service) IsStarted() bool { } func (s *Service) CheckConnected(ctx context.Context) *ConnectedResult { - infura := make(map[uint64]bool) + infura := make(map[uint64]Connection) for chainID, client := range chain.ChainClientInstances { - infura[chainID] = client.IsConnected + infura[chainID] = Connection{ + Up: client.IsConnected, + LastCheckedAt: client.LastCheckedAt, + } } - opensea := make(map[uint64]bool) + opensea := make(map[uint64]Connection) for chainID, client := range OpenseaClientInstances { - opensea[chainID] = client.IsConnected + opensea[chainID] = Connection{ + Up: client.IsConnected, + LastCheckedAt: client.LastCheckedAt, + } } return &ConnectedResult{ - Infura: infura, - Opensea: opensea, - CryptoCompare: s.cryptoCompare.IsConnected, + Infura: infura, + Opensea: opensea, + CryptoCompare: Connection{ + Up: s.cryptoCompare.IsConnected, + LastCheckedAt: s.cryptoCompare.LastCheckedAt, + }, } } diff --git a/services/wallet/thirdparty/cryptocompare.go b/services/wallet/thirdparty/cryptocompare.go index 5bc95afc9..35ed62304 100644 --- a/services/wallet/thirdparty/cryptocompare.go +++ b/services/wallet/thirdparty/cryptocompare.go @@ -68,11 +68,12 @@ type MarketValuesContainer struct { type CryptoCompare struct { client *http.Client IsConnected bool + LastCheckedAt int64 IsConnectedLock sync.RWMutex } func NewCryptoCompare() *CryptoCompare { - return &CryptoCompare{client: &http.Client{Timeout: time.Minute}, IsConnected: true} + return &CryptoCompare{client: &http.Client{Timeout: time.Minute}, IsConnected: true, LastCheckedAt: time.Now().Unix()} } func renameSymbols(symbols []string) (renames []string) { @@ -111,6 +112,7 @@ func (c *CryptoCompare) DoQuery(url string) (*http.Response, error) { c.IsConnectedLock.Lock() defer c.IsConnectedLock.Unlock() + c.LastCheckedAt = time.Now().Unix() if err != nil { c.IsConnected = false return nil, err