| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers.NFT.Markets.OpenSea_Internal; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Helpers.NFT.Markets |
| | 8 | | { |
| | 9 | | public class OpenSea : INFTMarket |
| | 10 | | { |
| 72 | 11 | | readonly MarketInfo openSeaMarketInfo = new MarketInfo() { name = "OpenSea" }; |
| | 12 | |
|
| 72 | 13 | | private readonly RequestController requestController = new RequestController(); |
| | 14 | |
|
| | 15 | | IEnumerator INFTMarket.FetchNFTsFromOwner(string address, Action<NFTOwner> onSuccess, Action<string> onError) |
| | 16 | | { |
| 0 | 17 | | var request = requestController.FetchOwnedNFT(address); |
| | 18 | |
|
| 0 | 19 | | yield return new UnityEngine.WaitUntil(() => !request.pending); |
| | 20 | |
|
| 0 | 21 | | if (request.resolved) |
| | 22 | | { |
| 0 | 23 | | onSuccess?.Invoke( ResponseToNFTOwner(address, request.resolvedValue)); |
| 0 | 24 | | } |
| | 25 | | else |
| | 26 | | { |
| 0 | 27 | | onError?.Invoke(request.error); |
| | 28 | | } |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | IEnumerator INFTMarket.FetchNFTInfo(string assetContractAddress, string tokenId, Action<NFTInfo> onSuccess, Acti |
| | 32 | | { |
| 0 | 33 | | RequestBase<AssetResponse> request = requestController.FetchNFT(assetContractAddress, tokenId); |
| | 34 | |
|
| 0 | 35 | | yield return new UnityEngine.WaitUntil(() => !request.pending); |
| | 36 | |
|
| 0 | 37 | | if (request.resolved) |
| | 38 | | { |
| 0 | 39 | | onSuccess?.Invoke( ResponseToNFTInfo(request.resolvedValue)); |
| 0 | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| 0 | 43 | | onError?.Invoke(request.error); |
| | 44 | | } |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | IEnumerator INFTMarket.FetchNFTInfoSingleAsset(string assetContractAddress, string tokenId, Action<NFTInfoSingle |
| | 48 | | { |
| 0 | 49 | | RequestBase<SingleAssetResponse> request = requestController.FetchSingleNFT(assetContractAddress, tokenId); |
| | 50 | |
|
| 0 | 51 | | yield return new UnityEngine.WaitUntil(() => !request.pending); |
| | 52 | |
|
| 0 | 53 | | if (request.resolved) |
| | 54 | | { |
| 0 | 55 | | onSuccess?.Invoke( ResponseToNFTInfo(request.resolvedValue)); |
| 0 | 56 | | } |
| | 57 | | else |
| | 58 | | { |
| 0 | 59 | | onError?.Invoke(request.error); |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private NFTOwner ResponseToNFTOwner(string ethAddress, AssetsResponse response) |
| | 64 | | { |
| 0 | 65 | | NFTOwner ownerInfo = NFTOwner.defaultNFTOwner; |
| 0 | 66 | | ownerInfo.ethAddress = ethAddress; |
| | 67 | |
|
| 0 | 68 | | foreach (AssetResponse assetResponse in response.assets) |
| | 69 | | { |
| 0 | 70 | | ownerInfo.assets.Add(ResponseToNFTInfo(assetResponse)); |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return ownerInfo; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private NFTInfo ResponseToNFTInfo(AssetResponse response) |
| | 77 | | { |
| 0 | 78 | | NFTInfo ret = NFTInfo.defaultNFTInfo; |
| 0 | 79 | | ret.marketInfo = openSeaMarketInfo; |
| 0 | 80 | | ret.name = response.name; |
| 0 | 81 | | ret.description = response.description; |
| 0 | 82 | | ret.thumbnailUrl = response.image_thumbnail_url; |
| 0 | 83 | | ret.previewImageUrl = response.image_preview_url; |
| 0 | 84 | | ret.originalImageUrl = response.image_original_url; |
| 0 | 85 | | ret.imageUrl = response.image_url; |
| 0 | 86 | | ret.assetLink = response.external_link; |
| 0 | 87 | | ret.marketLink = response.permalink; |
| 0 | 88 | | ret.tokenId = response.token_id; |
| | 89 | |
|
| 0 | 90 | | ret.assetContract.address = response.asset_contract.address; |
| 0 | 91 | | ret.assetContract.name = response.asset_contract.name; |
| | 92 | |
|
| 0 | 93 | | if (!string.IsNullOrEmpty(response.owner?.address)) |
| | 94 | | { |
| 0 | 95 | | ret.owner = response.owner.address; |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (response.num_sales != null) |
| | 99 | | { |
| 0 | 100 | | ret.numSales = response.num_sales.Value; |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | if (response.last_sale != null) |
| | 104 | | { |
| 0 | 105 | | ret.lastSaleDate = response.last_sale.event_timestamp; |
| | 106 | |
|
| 0 | 107 | | if (response.last_sale.payment_token != null) |
| | 108 | | { |
| 0 | 109 | | ret.lastSaleAmount = PriceToFloatingPointString(response.last_sale); |
| 0 | 110 | | ret.lastSaleToken = new NFT.PaymentTokenInfo() |
| | 111 | | { |
| | 112 | | symbol = response.last_sale.payment_token.symbol |
| | 113 | | }; |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | UnityEngine.Color backgroundColor; |
| 0 | 118 | | if (UnityEngine.ColorUtility.TryParseHtmlString("#" + response.background_color, out backgroundColor)) |
| | 119 | | { |
| 0 | 120 | | ret.backgroundColor = backgroundColor; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | OrderInfo sellOrder = GetSellOrder(response.sell_orders, response.owner.address); |
| 0 | 124 | | if (sellOrder != null) |
| | 125 | | { |
| 0 | 126 | | ret.currentPrice = PriceToFloatingPointString(sellOrder.current_price, sellOrder.payment_token_contract) |
| 0 | 127 | | ret.currentPriceToken = new NFT.PaymentTokenInfo() |
| | 128 | | { |
| | 129 | | symbol = sellOrder.payment_token_contract.symbol |
| | 130 | | }; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | return ret; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | private NFTInfoSingleAsset ResponseToNFTInfo(SingleAssetResponse response) |
| | 137 | | { |
| 0 | 138 | | NFTInfoSingleAsset ret = NFTInfoSingleAsset.defaultNFTInfoSingleAsset; |
| 0 | 139 | | ret.tokenId = response.token_id; |
| 0 | 140 | | ret.marketInfo = openSeaMarketInfo; |
| 0 | 141 | | ret.name = response.name; |
| 0 | 142 | | ret.description = response.description; |
| 0 | 143 | | ret.previewImageUrl = response.image_preview_url; |
| 0 | 144 | | ret.originalImageUrl = response.image_original_url; |
| 0 | 145 | | ret.assetLink = response.external_link; |
| 0 | 146 | | ret.marketLink = response.permalink; |
| | 147 | |
|
| 0 | 148 | | ret.assetContract.address = response.asset_contract.address; |
| 0 | 149 | | ret.assetContract.name = response.asset_contract.name; |
| | 150 | |
|
| 0 | 151 | | if (response.last_sale != null) |
| | 152 | | { |
| 0 | 153 | | ret.lastSaleDate = response.last_sale.event_timestamp; |
| | 154 | |
|
| 0 | 155 | | if (response.last_sale.payment_token != null) |
| | 156 | | { |
| 0 | 157 | | ret.lastSaleAmount = PriceToFloatingPointString(response.last_sale); |
| 0 | 158 | | ret.lastSaleToken = new NFT.PaymentTokenInfo() |
| | 159 | | { |
| | 160 | | symbol = response.last_sale.payment_token.symbol |
| | 161 | | }; |
| | 162 | | } |
| | 163 | | } |
| | 164 | |
|
| | 165 | | UnityEngine.Color backgroundColor; |
| 0 | 166 | | if (UnityEngine.ColorUtility.TryParseHtmlString("#" + response.background_color, out backgroundColor)) |
| | 167 | | { |
| 0 | 168 | | ret.backgroundColor = backgroundColor; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | OrderInfo sellOrder = GetSellOrder(response.orders, response.top_ownerships); |
| 0 | 172 | | if (sellOrder != null) |
| | 173 | | { |
| 0 | 174 | | ret.currentPrice = PriceToFloatingPointString(sellOrder.current_price, sellOrder.payment_token_contract) |
| 0 | 175 | | ret.currentPriceToken = new NFT.PaymentTokenInfo() |
| | 176 | | { |
| | 177 | | symbol = sellOrder.payment_token_contract.symbol |
| | 178 | | }; |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | ret.owners = new NFTInfoSingleAsset.Owners[response.top_ownerships.Length]; |
| 0 | 182 | | for (int i = 0; i < response.top_ownerships.Length; i++) |
| | 183 | | { |
| 0 | 184 | | ret.owners[i] = new NFTInfoSingleAsset.Owners() |
| | 185 | | { |
| | 186 | | owner = response.top_ownerships[i].owner.address, |
| | 187 | | }; |
| | 188 | |
|
| 0 | 189 | | float.TryParse(response.top_ownerships[i].quantity, out float quantity); |
| 0 | 190 | | ret.owners[i].quantity = quantity; |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | return ret; |
| | 194 | | } |
| | 195 | |
|
| | 196 | | private string PriceToFloatingPointString(OpenSea_Internal.AssetSaleInfo saleInfo) |
| | 197 | | { |
| 0 | 198 | | if (saleInfo.payment_token == null) |
| 0 | 199 | | return null; |
| 0 | 200 | | return PriceToFloatingPointString(saleInfo.total_price, saleInfo.payment_token); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | private string PriceToFloatingPointString(string price, OpenSea_Internal.PaymentTokenInfo tokenInfo) |
| | 204 | | { |
| 0 | 205 | | string priceString = price; |
| 0 | 206 | | if (price.Contains('.')) |
| | 207 | | { |
| 0 | 208 | | priceString = price.Split('.')[0]; |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | int pointPosition = priceString.Length - tokenInfo.decimals; |
| 0 | 212 | | if (pointPosition <= 0) |
| | 213 | | { |
| 0 | 214 | | return "0." + string.Concat(Enumerable.Repeat("0", Math.Abs(pointPosition))) + priceString; |
| | 215 | | } |
| | 216 | | else |
| | 217 | | { |
| 0 | 218 | | return priceString.Insert(pointPosition, "."); |
| | 219 | | } |
| | 220 | | } |
| | 221 | |
|
| | 222 | | private OrderInfo GetSellOrder(OrderInfo[] orders, string nftOwner) |
| | 223 | | { |
| 0 | 224 | | if (orders == null) |
| 0 | 225 | | return null; |
| | 226 | |
|
| 0 | 227 | | OrderInfo ret = null; |
| 0 | 228 | | for (int i = 0; i < orders.Length; i++) |
| | 229 | | { |
| 0 | 230 | | if (orders[i].maker.address == nftOwner) |
| | 231 | | { |
| 0 | 232 | | ret = orders[i]; |
| 0 | 233 | | break; |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | return ret; |
| | 238 | | } |
| | 239 | |
|
| | 240 | | private OrderInfo GetSellOrder(OrderInfo[] orders, OwnershipInfo[] owners) |
| | 241 | | { |
| 0 | 242 | | if (orders == null) |
| 0 | 243 | | return null; |
| | 244 | |
|
| 0 | 245 | | for (int i = 0; i < orders.Length; i++) |
| | 246 | | { |
| 0 | 247 | | if (owners.Any(ownerInfo => orders[i].maker.address == ownerInfo.owner.address)) |
| | 248 | | { |
| 0 | 249 | | return orders[i]; |
| | 250 | | } |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | return null; |
| | 254 | | } |
| | 255 | |
|
| | 256 | | public void Dispose() |
| | 257 | | { |
| 72 | 258 | | requestController?.Dispose(); |
| 72 | 259 | | } |
| | 260 | | } |
| | 261 | | } |