github.com/status-im/status-go@v1.1.0/services/wallet/collectibles/types.go (about) 1 package collectibles 2 3 import ( 4 "github.com/status-im/status-go/protocol/communities/token" 5 w_common "github.com/status-im/status-go/services/wallet/common" 6 "github.com/status-im/status-go/services/wallet/thirdparty" 7 ) 8 9 // Combined Collection+Collectible info, used to display a detailed view of a collectible 10 type Collectible struct { 11 DataType CollectibleDataType `json:"data_type"` 12 ID thirdparty.CollectibleUniqueID `json:"id"` 13 ContractType w_common.ContractType `json:"contract_type"` 14 CollectibleData *CollectibleData `json:"collectible_data,omitempty"` 15 CollectionData *CollectionData `json:"collection_data,omitempty"` 16 CommunityData *CommunityData `json:"community_data,omitempty"` 17 Ownership []thirdparty.AccountBalance `json:"ownership,omitempty"` 18 IsFirst bool `json:"is_first,omitempty"` 19 LatestTxHash string `json:"latest_tx_hash,omitempty"` 20 ReceivedAmount float64 `json:"received_amount,omitempty"` 21 } 22 23 type CollectibleData struct { 24 Name string `json:"name"` 25 Description *string `json:"description,omitempty"` 26 ImageURL *string `json:"image_url,omitempty"` 27 AnimationURL *string `json:"animation_url,omitempty"` 28 AnimationMediaType *string `json:"animation_media_type,omitempty"` 29 Traits *[]thirdparty.CollectibleTrait `json:"traits,omitempty"` 30 BackgroundColor *string `json:"background_color,omitempty"` 31 Soulbound *bool `json:"soulbound,omitempty"` 32 } 33 34 type CollectionData struct { 35 Name string `json:"name"` 36 Slug string `json:"slug"` 37 ImageURL string `json:"image_url"` 38 Socials *CollectionSocials `json:"socials"` 39 } 40 41 type CollectionSocials struct { 42 Website string `json:"website"` 43 TwitterHandle string `json:"twitter_handle"` 44 } 45 46 type CommunityData struct { 47 ID string `json:"id"` 48 Name string `json:"name"` 49 Color string `json:"color"` 50 PrivilegesLevel token.PrivilegesLevel `json:"privileges_level"` 51 ImageURL *string `json:"image_url,omitempty"` 52 } 53 54 func idToCollectible(id thirdparty.CollectibleUniqueID) Collectible { 55 ret := Collectible{ 56 DataType: CollectibleDataTypeUniqueID, 57 ID: id, 58 } 59 return ret 60 } 61 62 func idsToCollectibles(ids []thirdparty.CollectibleUniqueID) []Collectible { 63 res := make([]Collectible, 0, len(ids)) 64 65 for _, id := range ids { 66 c := idToCollectible(id) 67 res = append(res, c) 68 } 69 70 return res 71 } 72 73 func thirdpartyCollectionDataToCollectionData(collectionData *thirdparty.CollectionData) CollectionData { 74 ret := CollectionData{} 75 if collectionData != nil { 76 ret = CollectionData{ 77 Name: collectionData.Name, 78 Slug: collectionData.Slug, 79 ImageURL: collectionData.ImageURL, 80 } 81 if collectionData.Socials != nil { 82 ret.Socials = &CollectionSocials{ 83 Website: collectionData.Socials.Website, 84 TwitterHandle: collectionData.Socials.TwitterHandle, 85 } 86 } 87 } 88 return ret 89 } 90 91 func getContractType(c thirdparty.FullCollectibleData) w_common.ContractType { 92 if c.CollectibleData.ContractType != w_common.ContractTypeUnknown { 93 return c.CollectibleData.ContractType 94 } 95 if c.CollectionData != nil && c.CollectionData.ContractType != w_common.ContractTypeUnknown { 96 return c.CollectionData.ContractType 97 } 98 return w_common.ContractTypeUnknown 99 } 100 101 func fullCollectibleDataToHeader(c thirdparty.FullCollectibleData) Collectible { 102 ret := Collectible{ 103 DataType: CollectibleDataTypeHeader, 104 ID: c.CollectibleData.ID, 105 ContractType: getContractType(c), 106 CollectibleData: &CollectibleData{ 107 Name: c.CollectibleData.Name, 108 ImageURL: &c.CollectibleData.ImageURL, 109 AnimationURL: &c.CollectibleData.AnimationURL, 110 AnimationMediaType: &c.CollectibleData.AnimationMediaType, 111 BackgroundColor: &c.CollectibleData.BackgroundColor, 112 Soulbound: &c.CollectibleData.Soulbound, 113 }, 114 } 115 collectionData := thirdpartyCollectionDataToCollectionData(c.CollectionData) 116 ret.CollectionData = &collectionData 117 if c.CollectibleData.CommunityID != "" { 118 communityData := communityInfoToData(c.CollectibleData.CommunityID, c.CommunityInfo, c.CollectibleCommunityInfo) 119 ret.CommunityData = &communityData 120 } 121 ret.Ownership = c.Ownership 122 return ret 123 } 124 125 func fullCollectiblesDataToHeaders(data []thirdparty.FullCollectibleData) []Collectible { 126 res := make([]Collectible, 0, len(data)) 127 128 for _, c := range data { 129 header := fullCollectibleDataToHeader(c) 130 res = append(res, header) 131 } 132 133 return res 134 } 135 136 func fullCollectibleDataToDetails(c thirdparty.FullCollectibleData) Collectible { 137 ret := Collectible{ 138 DataType: CollectibleDataTypeDetails, 139 ID: c.CollectibleData.ID, 140 ContractType: getContractType(c), 141 CollectibleData: &CollectibleData{ 142 Name: c.CollectibleData.Name, 143 Description: &c.CollectibleData.Description, 144 ImageURL: &c.CollectibleData.ImageURL, 145 AnimationURL: &c.CollectibleData.AnimationURL, 146 AnimationMediaType: &c.CollectibleData.AnimationMediaType, 147 BackgroundColor: &c.CollectibleData.BackgroundColor, 148 Traits: &c.CollectibleData.Traits, 149 Soulbound: &c.CollectibleData.Soulbound, 150 }, 151 } 152 collectionData := thirdpartyCollectionDataToCollectionData(c.CollectionData) 153 ret.CollectionData = &collectionData 154 if c.CollectibleData.CommunityID != "" { 155 communityData := communityInfoToData(c.CollectibleData.CommunityID, c.CommunityInfo, c.CollectibleCommunityInfo) 156 ret.CommunityData = &communityData 157 } 158 ret.Ownership = c.Ownership 159 return ret 160 } 161 162 func fullCollectiblesDataToDetails(data []thirdparty.FullCollectibleData) []Collectible { 163 res := make([]Collectible, 0, len(data)) 164 165 for _, c := range data { 166 details := fullCollectibleDataToDetails(c) 167 res = append(res, details) 168 } 169 170 return res 171 } 172 173 func fullCollectiblesDataToCommunityHeader(data []thirdparty.FullCollectibleData) []Collectible { 174 res := make([]Collectible, 0, len(data)) 175 176 for _, localCollectibleData := range data { 177 // to satisfy gosec: C601 checks 178 c := localCollectibleData 179 collectibleID := c.CollectibleData.ID 180 communityID := c.CollectibleData.CommunityID 181 182 if communityID == "" { 183 continue 184 } 185 186 communityData := communityInfoToData(communityID, c.CommunityInfo, c.CollectibleCommunityInfo) 187 188 header := Collectible{ 189 ID: collectibleID, 190 ContractType: getContractType(c), 191 CollectibleData: &CollectibleData{ 192 Name: c.CollectibleData.Name, 193 ImageURL: &c.CollectibleData.ImageURL, 194 }, 195 CommunityData: &communityData, 196 Ownership: c.Ownership, 197 IsFirst: c.CollectibleData.IsFirst, 198 } 199 200 res = append(res, header) 201 } 202 203 return res 204 } 205 206 func communityInfoToData(communityID string, community *thirdparty.CommunityInfo, communityCollectible *thirdparty.CollectibleCommunityInfo) CommunityData { 207 ret := CommunityData{ 208 ID: communityID, 209 } 210 211 if community != nil { 212 ret.Name = community.CommunityName 213 ret.Color = community.CommunityColor 214 ret.ImageURL = &community.CommunityImage 215 } 216 217 if communityCollectible != nil { 218 ret.PrivilegesLevel = communityCollectible.PrivilegesLevel 219 } 220 221 return ret 222 } 223 224 func IDsFromAssets(assets []*thirdparty.FullCollectibleData) []thirdparty.CollectibleUniqueID { 225 result := make([]thirdparty.CollectibleUniqueID, len(assets)) 226 for i, asset := range assets { 227 result[i] = asset.CollectibleData.ID 228 } 229 return result 230 }