Skip to content

Commit

Permalink
chore: log incorrect amountInWeis (#5005)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin authored and Khushboo-dev-cpp committed Apr 9, 2024
1 parent ef0e17e commit 857d8c4
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 5 deletions.
2 changes: 1 addition & 1 deletion protocol/communities/permission_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (p *DefaultPermissionChecker) CheckPermissions(permissions []*CommunityToke

requiredAmount, success := new(big.Int).SetString(tokenRequirement.AmountInWei, 10)
if !success {
return nil, fmt.Errorf("amountInWeis value is incorrect")
return nil, fmt.Errorf("amountInWeis value is incorrect: %s", tokenRequirement.AmountInWei)
}

if accumulatedBalance.Cmp(requiredAmount) != -1 {
Expand Down
7 changes: 7 additions & 0 deletions services/wallet/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ func (api *API) GetCollectiblesByUniqueIDAsync(requestID int32, uniqueIDs []thir
return nil
}

func (api *API) FetchCollectibleSocialsAsync(requestID int32, uniqueID thirdparty.CollectibleUniqueID) error {
log.Debug("wallet.api.FetchCollectibleSocialsAsync", "requestID", requestID, "uniqueID", uniqueID)

api.s.collectibles.FetchCollectibleSocialsAsync(requestID, uniqueID)
return nil
}

func (api *API) GetCollectibleOwnersByContractAddress(ctx context.Context, chainID wcommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
log.Debug("call to GetCollectibleOwnersByContractAddress")
return api.s.collectiblesManager.FetchCollectibleOwnersByContractAddress(ctx, chainID, contractAddress)
Expand Down
122 changes: 122 additions & 0 deletions services/wallet/collectibles/collection_data_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func NewCollectionDataDB(sqlDb *sql.DB) *CollectionDataDB {
const collectionDataColumns = "chain_id, contract_address, provider, name, slug, image_url, image_payload, community_id"
const collectionTraitsColumns = "chain_id, contract_address, trait_type, min, max"
const selectCollectionTraitsColumns = "trait_type, min, max"
const collectionSocialsColumns = "chain_id, contract_address, website, twitter_handle"
const selectCollectionSocialsColumns = "website, twitter_handle"

func rowsToCollectionTraits(rows *sql.Rows) (map[string]thirdparty.CollectionTrait, error) {
traits := make(map[string]thirdparty.CollectionTrait)
Expand Down Expand Up @@ -130,6 +132,14 @@ func setCollectionsData(creator sqlite.StatementCreator, collections []thirdpart
if err != nil {
return err
}

fmt.Println("c.Provider >>>>>>>>>> ",c.Provider)
if c.Provider == "alchemy" {
err = upsertCollectionSocials(creator, c.ID, c.Socials)
if err != nil {
return err
}
}
}
}

Expand Down Expand Up @@ -246,8 +256,120 @@ func (o *CollectionDataDB) GetData(ids []thirdparty.ContractID) (map[string]thir
return nil, err
}

// Get socials from different table
c.Socials, err = getCollectionSocials(o.db, c.ID)
if err != nil {
return nil, err
}

ret[c.ID.HashKey()] = *c
}
}
return ret, nil
}


func (o *CollectionDataDB) GetSocialsValidForID(id thirdparty.CollectibleUniqueID) bool {
exists, err := o.db.Prepare(`SELECT EXISTS (
SELECT 1 FROM collection_socials_cache
WHERE chain_id=? AND contract_address=?
)`)
if err != nil {
return false
}

row := exists.QueryRow(
id.ContractID.ChainID,
id.ContractID.Address,
)
var found bool
err = row.Scan(&found)
if err != nil {
return false
}

return found
}

func (o *CollectionDataDB) SetCollectionSocialsData(id thirdparty.ContractID, collectionSocials thirdparty.CollectionSocials) (err error) {
tx, err := o.db.Begin()
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
_ = tx.Rollback()
}()

// Insert new collections socials
err = upsertCollectionSocials(tx, id, collectionSocials)
if err != nil {
return err
}

return
}

func rowsToCollectionSocials(rows *sql.Rows) (thirdparty.CollectionSocials, error) {
socials := thirdparty.CollectionSocials{}
for rows.Next() {
var website string
var twitterHandle string
err := rows.Scan(
&website,
&twitterHandle,
)
if err != nil {
return socials, err
}
if len(website) > 0 {
socials.Website = website
}
if len(twitterHandle) > 0 {
socials.TwitterHandle = twitterHandle
}
}
return socials, nil
}

func getCollectionSocials(creator sqlite.StatementCreator, id thirdparty.ContractID) (thirdparty.CollectionSocials, error) {
// Get socials
selectSocials, err := creator.Prepare(fmt.Sprintf(`SELECT %s
FROM collection_socials_cache
WHERE chain_id = ? AND contract_address = ?`, selectCollectionSocialsColumns))
if err != nil {
return thirdparty.CollectionSocials{}, err
}

rows, err := selectSocials.Query(
id.ChainID,
id.Address,
)
if err != nil {
return thirdparty.CollectionSocials{}, err
}

return rowsToCollectionSocials(rows)
}


func upsertCollectionSocials(creator sqlite.StatementCreator, id thirdparty.ContractID, socials thirdparty.CollectionSocials) error {
// Insert socials
insertSocial, err := creator.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO collection_socials_cache (%s)
VALUES (?, ?, ?, ?)`, collectionSocialsColumns))

_, err = insertSocial.Exec(
id.ChainID,
id.Address,
socials.Website,
socials.TwitterHandle,
)
if err != nil {
return err
}

return nil
}
98 changes: 98 additions & 0 deletions services/wallet/collectibles/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (

type ManagerInterface interface {
FetchAssetsByCollectibleUniqueID(ctx context.Context, uniqueIDs []thirdparty.CollectibleUniqueID, asyncFetch bool) ([]thirdparty.FullCollectibleData, error)
FetchCollectibleSocialsAsync(ctx context.Context, uniqueID thirdparty.CollectibleUniqueID) error
}

type Manager struct {
Expand Down Expand Up @@ -1036,3 +1037,100 @@ func (o *Manager) SearchCollections(ctx context.Context, chainID walletCommon.Ch
}
return nil, ErrNoProvidersAvailableForChainID
}

func (o *Manager) FetchCollectibleSocialsAsync(ctx context.Context, uniqueID thirdparty.CollectibleUniqueID) error {
hasValidSocials := o.collectionsDataDB.GetSocialsValidForID(uniqueID)

if !hasValidSocials {
// Atomic group stores the error from the first failed command and stops other commands on error
group := async.NewAtomicGroup(ctx)
group.Add(func(ctx context.Context) error {
defer o.checkConnectionStatus(uniqueID.ContractID.ChainID)

website, twitterHandle, err := o.fetchSocialsForCollectibleUniqueID(ctx, uniqueID.ContractID.ChainID, uniqueID)
if err != nil {
log.Debug("FetchCollectibleSocials failed for", "chainID", uniqueID.ContractID.ChainID, "uniqueID", uniqueID, "err", err)
return err
}

socials := thirdparty.CollectionSocials {
Website: website,
TwitterHandle: twitterHandle,
}

socialsMessage:= CollectibleSocialsMessage {
ID: uniqueID,
Socials: socials,
}

err = o.collectionsDataDB.SetCollectionSocialsData(uniqueID.ContractID, socials)
if err != nil {
log.Error("Error saving socials to DB: %v", err)
return nil
}


payload, err := json.Marshal(socialsMessage)
if err != nil {
log.Error("Error marshaling response: %v", err)
return nil
}

event := walletevent.Event{
Type: EventGetCollectibleSocialsDone,
Message: string(payload),
}

o.feed.Send(event)
return err
})

group.Wait()
return group.Error()
}
return nil
}

func (o *Manager) fetchSocialsForCollectibleUniqueID(ctx context.Context, chainID walletCommon.ChainID, idToFetch thirdparty.CollectibleUniqueID) (string, string, error) {
website := ""
twitterHandle := ""
providerFound:= false
cmdRes:= circuitbreaker.CommandResult{}
for _, provider := range o.providers.CollectibleDataProviders {
if provider.ID() == "alchemy" {
cmd := circuitbreaker.Command{}
if !provider.IsChainSupported(chainID) {
continue
}

// Some provider was found for the chainID
providerFound = true

provider := provider
cmd.Add(circuitbreaker.NewFunctor(func() ([]any, error) {
w, t, err := provider.FetchCollectibleSocialsByUniqueID(ctx, idToFetch)
return []any{w, t}, err
}))

cmdRes = o.getCircuitBreaker(chainID).Execute(cmd)

if len(cmdRes.Result()) > 0 {
// Only update website if non empty website is found
if len(cmdRes.Result()[0].(string)) > 0 {
website = cmdRes.Result()[0].(string)
}
// Only update twitterHandle if non empty twitterHandle is found
if len(cmdRes.Result()[1].(string)) > 0 {
twitterHandle = cmdRes.Result()[1].(string)
}
}
break
}
}

if !providerFound {
return "", "", ErrNoProvidersAvailableForChainID // lets not stop the group if no providers are available for the chain
}

return website, twitterHandle, cmdRes.Error()
}
18 changes: 17 additions & 1 deletion services/wallet/collectibles/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

EventOwnedCollectiblesFilteringDone walletevent.EventType = "wallet-owned-collectibles-filtering-done"
EventGetCollectiblesDetailsDone walletevent.EventType = "wallet-get-collectibles-details-done"
EventGetCollectibleSocialsDone walletevent.EventType = "wallet-get-collectible-socials-done"
)

type OwnershipUpdateMessage struct {
Expand All @@ -43,6 +44,11 @@ type OwnershipUpdateMessage struct {
Removed []thirdparty.CollectibleUniqueID `json:"removed"`
}

type CollectibleSocialsMessage struct {
ID thirdparty.CollectibleUniqueID `json:"id"`
Socials thirdparty.CollectionSocials `json:"socials"`
}

type CollectibleDataType byte

const (
Expand Down Expand Up @@ -79,7 +85,11 @@ var (
getCollectiblesDataTask = async.TaskType{
ID: 2,
Policy: async.ReplacementPolicyCancelOld,
}
}
fetchCollectibleSocialsTask = async.TaskType{
ID: 3,
Policy: async.ReplacementPolicyCancelOld,
}
)

type Service struct {
Expand Down Expand Up @@ -547,3 +557,9 @@ func (s *Service) notifyCommunityCollectiblesReceived(ownedCollectibles OwnedCol
Message: string(encodedMessage),
})
}

func (s *Service) FetchCollectibleSocialsAsync(requestID int32, uniqueID thirdparty.CollectibleUniqueID) {
s.scheduler.Enqueue(requestID, fetchCollectibleSocialsTask, func(ctx context.Context) (interface{}, error) {
return "", s.manager.FetchCollectibleSocialsAsync(ctx, uniqueID)
}, func(result interface{}, taskType async.TaskType, err error) {})
}
20 changes: 18 additions & 2 deletions services/wallet/collectibles/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/status-im/status-go/protocol/communities/token"
w_common "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/thirdparty"
"fmt"
)

// Combined Collection+Collectible info, used to display a detailed view of a collectible
Expand Down Expand Up @@ -34,6 +35,12 @@ type CollectionData struct {
Name string `json:"name"`
Slug string `json:"slug"`
ImageURL string `json:"image_url"`
Socials CollectionSocials `json:"socials"`
}

type CollectionSocials struct {
Website string `json:"website"`
TwitterHandle string `json:"twitter_handle"`
}

type CommunityData struct {
Expand Down Expand Up @@ -90,10 +97,15 @@ func fullCollectibleDataToHeader(c thirdparty.FullCollectibleData) Collectible {
ret.CollectionData = &CollectionData{
Name: c.CollectionData.Name,
Slug: c.CollectionData.Slug,
ImageURL: c.CollectionData.ImageURL,
ImageURL: c.CollectionData.ImageURL,
}
fmt.Println("fullCollectibleDataToHeader >>>>>>>>>>Website>>>>>",c.CollectionData.Socials.Website, " TwitterHandle >>", c.CollectionData.Socials.TwitterHandle)
ret.CollectionData.Socials = CollectionSocials{
Website: c.CollectionData.Socials.Website,
TwitterHandle: c.CollectionData.Socials.TwitterHandle,
}
}
if c.CollectibleData.CommunityID != "" {
if c.CollectibleData.CommunityID != "" {
communityData := communityInfoToData(c.CollectibleData.CommunityID, c.CommunityInfo, c.CollectibleCommunityInfo)
ret.CommunityData = &communityData
}
Expand Down Expand Up @@ -133,6 +145,10 @@ func fullCollectibleDataToDetails(c thirdparty.FullCollectibleData) Collectible
Slug: c.CollectionData.Slug,
ImageURL: c.CollectionData.ImageURL,
}
ret.CollectionData.Socials = CollectionSocials{
Website: c.CollectionData.Socials.Website,
TwitterHandle: c.CollectionData.Socials.TwitterHandle,
}
}
if c.CollectibleData.CommunityID != "" {
communityData := communityInfoToData(c.CollectibleData.CommunityID, c.CommunityInfo, c.CollectibleCommunityInfo)
Expand Down
11 changes: 11 additions & 0 deletions services/wallet/thirdparty/alchemy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ func (o *Client) FetchAssetsByCollectibleUniqueID(ctx context.Context, uniqueIDs
return ret, nil
}

func (o *Client) FetchCollectibleSocialsByUniqueID(ctx context.Context, uniqueID thirdparty.CollectibleUniqueID) (string, string, error) {
resp, err := o.FetchCollectionsDataByContractID(ctx, []thirdparty.ContractID{uniqueID.ContractID})
if err != nil {
return "", "", err
}
if len(resp) > 0 {
return resp[0].Website, resp[0].TwitterHandle, nil
}
return "", "", nil
}

func getContractAddressBatches(ids []thirdparty.ContractID) []BatchContractAddresses {
batches := make([]BatchContractAddresses, 0)

Expand Down

0 comments on commit 857d8c4

Please sign in to comment.