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