< Summary

Class:DCL.Helpers.NFT.Markets.OpenSea
Assembly:OpenSea_Internal
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/OpenSea.cs
Covered lines:4
Uncovered lines:107
Coverable lines:111
Total lines:261
Line coverage:3.6% (4 of 111)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OpenSea()0%110100%
DCL-Helpers-NFT-Markets-INFTMarket-FetchNFTsFromOwner()0%42600%
DCL-Helpers-NFT-Markets-INFTMarket-FetchNFTInfo()0%42600%
DCL-Helpers-NFT-Markets-INFTMarket-FetchNFTInfoSingleAsset()0%42600%
ResponseToNFTOwner(...)0%6200%
ResponseToNFTInfo(...)0%90900%
ResponseToNFTInfo(...)0%42600%
PriceToFloatingPointString(...)0%6200%
PriceToFloatingPointString(...)0%12300%
GetSellOrder(...)0%20400%
GetSellOrder(...)0%30500%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/OpenSea.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Linq;
 4using DCL.Helpers.NFT.Markets.OpenSea_Internal;
 5using UnityEngine;
 6
 7namespace DCL.Helpers.NFT.Markets
 8{
 9    public class OpenSea : INFTMarket
 10    {
 7211        readonly MarketInfo openSeaMarketInfo = new MarketInfo() { name = "OpenSea" };
 12
 7213        private readonly RequestController requestController = new RequestController();
 14
 15        IEnumerator INFTMarket.FetchNFTsFromOwner(string address, Action<NFTOwner> onSuccess, Action<string> onError)
 16        {
 017            var request = requestController.FetchOwnedNFT(address);
 18
 019            yield return new UnityEngine.WaitUntil(() => !request.pending);
 20
 021            if (request.resolved)
 22            {
 023                onSuccess?.Invoke( ResponseToNFTOwner(address, request.resolvedValue));
 024            }
 25            else
 26            {
 027                onError?.Invoke(request.error);
 28            }
 029        }
 30
 31        IEnumerator INFTMarket.FetchNFTInfo(string assetContractAddress, string tokenId, Action<NFTInfo> onSuccess, Acti
 32        {
 033            RequestBase<AssetResponse> request = requestController.FetchNFT(assetContractAddress, tokenId);
 34
 035            yield return new UnityEngine.WaitUntil(() => !request.pending);
 36
 037            if (request.resolved)
 38            {
 039                onSuccess?.Invoke( ResponseToNFTInfo(request.resolvedValue));
 040            }
 41            else
 42            {
 043                onError?.Invoke(request.error);
 44            }
 045        }
 46
 47        IEnumerator INFTMarket.FetchNFTInfoSingleAsset(string assetContractAddress, string tokenId, Action<NFTInfoSingle
 48        {
 049            RequestBase<SingleAssetResponse> request = requestController.FetchSingleNFT(assetContractAddress, tokenId);
 50
 051            yield return new UnityEngine.WaitUntil(() => !request.pending);
 52
 053            if (request.resolved)
 54            {
 055                onSuccess?.Invoke( ResponseToNFTInfo(request.resolvedValue));
 056            }
 57            else
 58            {
 059                onError?.Invoke(request.error);
 60            }
 061        }
 62
 63        private NFTOwner ResponseToNFTOwner(string ethAddress, AssetsResponse response)
 64        {
 065            NFTOwner ownerInfo = NFTOwner.defaultNFTOwner;
 066            ownerInfo.ethAddress = ethAddress;
 67
 068            foreach (AssetResponse assetResponse in response.assets)
 69            {
 070                ownerInfo.assets.Add(ResponseToNFTInfo(assetResponse));
 71            }
 72
 073            return ownerInfo;
 74        }
 75
 76        private NFTInfo ResponseToNFTInfo(AssetResponse response)
 77        {
 078            NFTInfo ret = NFTInfo.defaultNFTInfo;
 079            ret.marketInfo = openSeaMarketInfo;
 080            ret.name = response.name;
 081            ret.description = response.description;
 082            ret.thumbnailUrl = response.image_thumbnail_url;
 083            ret.previewImageUrl = response.image_preview_url;
 084            ret.originalImageUrl = response.image_original_url;
 085            ret.imageUrl = response.image_url;
 086            ret.assetLink = response.external_link;
 087            ret.marketLink = response.permalink;
 088            ret.tokenId = response.token_id;
 89
 090            ret.assetContract.address = response.asset_contract.address;
 091            ret.assetContract.name = response.asset_contract.name;
 92
 093            if (!string.IsNullOrEmpty(response.owner?.address))
 94            {
 095                ret.owner = response.owner.address;
 96            }
 97
 098            if (response.num_sales != null)
 99            {
 0100                ret.numSales = response.num_sales.Value;
 101            }
 102
 0103            if (response.last_sale != null)
 104            {
 0105                ret.lastSaleDate = response.last_sale.event_timestamp;
 106
 0107                if (response.last_sale.payment_token != null)
 108                {
 0109                    ret.lastSaleAmount = PriceToFloatingPointString(response.last_sale);
 0110                    ret.lastSaleToken = new NFT.PaymentTokenInfo()
 111                    {
 112                        symbol = response.last_sale.payment_token.symbol
 113                    };
 114                }
 115            }
 116
 117            UnityEngine.Color backgroundColor;
 0118            if (UnityEngine.ColorUtility.TryParseHtmlString("#" + response.background_color, out backgroundColor))
 119            {
 0120                ret.backgroundColor = backgroundColor;
 121            }
 122
 0123            OrderInfo sellOrder = GetSellOrder(response.sell_orders, response.owner.address);
 0124            if (sellOrder != null)
 125            {
 0126                ret.currentPrice = PriceToFloatingPointString(sellOrder.current_price, sellOrder.payment_token_contract)
 0127                ret.currentPriceToken = new NFT.PaymentTokenInfo()
 128                {
 129                    symbol = sellOrder.payment_token_contract.symbol
 130                };
 131            }
 132
 0133            return ret;
 134        }
 135
 136        private NFTInfoSingleAsset ResponseToNFTInfo(SingleAssetResponse response)
 137        {
 0138            NFTInfoSingleAsset ret = NFTInfoSingleAsset.defaultNFTInfoSingleAsset;
 0139            ret.tokenId = response.token_id;
 0140            ret.marketInfo = openSeaMarketInfo;
 0141            ret.name = response.name;
 0142            ret.description = response.description;
 0143            ret.previewImageUrl = response.image_preview_url;
 0144            ret.originalImageUrl = response.image_original_url;
 0145            ret.assetLink = response.external_link;
 0146            ret.marketLink = response.permalink;
 147
 0148            ret.assetContract.address = response.asset_contract.address;
 0149            ret.assetContract.name = response.asset_contract.name;
 150
 0151            if (response.last_sale != null)
 152            {
 0153                ret.lastSaleDate = response.last_sale.event_timestamp;
 154
 0155                if (response.last_sale.payment_token != null)
 156                {
 0157                    ret.lastSaleAmount = PriceToFloatingPointString(response.last_sale);
 0158                    ret.lastSaleToken = new NFT.PaymentTokenInfo()
 159                    {
 160                        symbol = response.last_sale.payment_token.symbol
 161                    };
 162                }
 163            }
 164
 165            UnityEngine.Color backgroundColor;
 0166            if (UnityEngine.ColorUtility.TryParseHtmlString("#" + response.background_color, out backgroundColor))
 167            {
 0168                ret.backgroundColor = backgroundColor;
 169            }
 170
 0171            OrderInfo sellOrder = GetSellOrder(response.orders, response.top_ownerships);
 0172            if (sellOrder != null)
 173            {
 0174                ret.currentPrice = PriceToFloatingPointString(sellOrder.current_price, sellOrder.payment_token_contract)
 0175                ret.currentPriceToken = new NFT.PaymentTokenInfo()
 176                {
 177                    symbol = sellOrder.payment_token_contract.symbol
 178                };
 179            }
 180
 0181            ret.owners = new NFTInfoSingleAsset.Owners[response.top_ownerships.Length];
 0182            for (int i = 0; i < response.top_ownerships.Length; i++)
 183            {
 0184                ret.owners[i] = new NFTInfoSingleAsset.Owners()
 185                {
 186                    owner = response.top_ownerships[i].owner.address,
 187                };
 188
 0189                float.TryParse(response.top_ownerships[i].quantity, out float quantity);
 0190                ret.owners[i].quantity = quantity;
 191            }
 192
 0193            return ret;
 194        }
 195
 196        private string PriceToFloatingPointString(OpenSea_Internal.AssetSaleInfo saleInfo)
 197        {
 0198            if (saleInfo.payment_token == null)
 0199                return null;
 0200            return PriceToFloatingPointString(saleInfo.total_price, saleInfo.payment_token);
 201        }
 202
 203        private string PriceToFloatingPointString(string price, OpenSea_Internal.PaymentTokenInfo tokenInfo)
 204        {
 0205            string priceString = price;
 0206            if (price.Contains('.'))
 207            {
 0208                priceString = price.Split('.')[0];
 209            }
 210
 0211            int pointPosition = priceString.Length - tokenInfo.decimals;
 0212            if (pointPosition <= 0)
 213            {
 0214                return "0." + string.Concat(Enumerable.Repeat("0", Math.Abs(pointPosition))) + priceString;
 215            }
 216            else
 217            {
 0218                return priceString.Insert(pointPosition, ".");
 219            }
 220        }
 221
 222        private OrderInfo GetSellOrder(OrderInfo[] orders, string nftOwner)
 223        {
 0224            if (orders == null)
 0225                return null;
 226
 0227            OrderInfo ret = null;
 0228            for (int i = 0; i < orders.Length; i++)
 229            {
 0230                if (orders[i].maker.address == nftOwner)
 231                {
 0232                    ret = orders[i];
 0233                    break;
 234                }
 235            }
 236
 0237            return ret;
 238        }
 239
 240        private OrderInfo GetSellOrder(OrderInfo[] orders, OwnershipInfo[] owners)
 241        {
 0242            if (orders == null)
 0243                return null;
 244
 0245            for (int i = 0; i < orders.Length; i++)
 246            {
 0247                if (owners.Any(ownerInfo => orders[i].maker.address == ownerInfo.owner.address))
 248                {
 0249                    return orders[i];
 250                }
 251            }
 252
 0253            return null;
 254        }
 255
 256        public void Dispose()
 257        {
 72258            requestController?.Dispose();
 72259        }
 260    }
 261}