| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Text; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using Newtonsoft.Json.Linq; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | |
|
| | 11 | | public class TheGraph : ITheGraph |
| | 12 | | { |
| | 13 | | private const float DEFAULT_CACHE_TIME = 5 * 60; |
| | 14 | | private const string LAND_SUBGRAPH_URL_ORG = "https://subgraph.decentraland.org/land-manager"; |
| | 15 | | private const string LAND_SUBGRAPH_URL_ZONE = "https://api.studio.thegraph.com/query/49472/land-manager-sepolia/vers |
| | 16 | | private const string MANA_SUBGRAPH_URL_ETHEREUM = "https://subgraph.decentraland.org/mana-ethereum-mainnet"; |
| | 17 | | private const string MANA_SUBGRAPH_URL_POLYGON = "https://subgraph.decentraland.org/mana-matic-mainnet"; |
| | 18 | | private const string NFT_COLLECTIONS_SUBGRAPH_URL_ETHEREUM = "https://subgraph.decentraland.org/collections-ethereum |
| | 19 | | private const string NFT_COLLECTIONS_SUBGRAPH_URL_MATIC = "https://subgraph.decentraland.org/collections-matic-mainn |
| | 20 | |
|
| 130 | 21 | | private readonly IDataCache<List<Land>> landQueryCache = new DataCache<List<Land>>(); |
| | 22 | |
|
| | 23 | | private Promise<string> Query(string url, string query, QueryVariablesBase variables) |
| | 24 | | { |
| 0 | 25 | | Promise<string> promise = new Promise<string>(); |
| | 26 | |
|
| 0 | 27 | | if (string.IsNullOrEmpty(query) || string.IsNullOrEmpty(url)) |
| | 28 | | { |
| 0 | 29 | | promise.Reject($"error: {(string.IsNullOrEmpty(url) ? "url" : "query")} is empty"); |
| 0 | 30 | | return promise; |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | string queryJson = $"\"query\":\"{Regex.Replace(query, @"\p{C}+", string.Empty)}\""; |
| 0 | 34 | | string variablesJson = variables != null ? $",\"variables\":{JsonUtility.ToJson(variables)}" : string.Empty; |
| 0 | 35 | | string bodyString = $"{{{queryJson}{variablesJson}}}"; |
| | 36 | |
|
| 0 | 37 | | var request = new UnityWebRequest(); |
| 0 | 38 | | request.url = url; |
| 0 | 39 | | request.method = UnityWebRequest.kHttpVerbPOST; |
| 0 | 40 | | request.downloadHandler = new DownloadHandlerBuffer(); |
| 0 | 41 | | request.uploadHandler = new UploadHandlerRaw(string.IsNullOrEmpty(bodyString) ? null : Encoding.UTF8.GetBytes(bo |
| 0 | 42 | | request.SetRequestHeader("Content-Type", "application/json"); |
| 0 | 43 | | request.timeout = 60; |
| | 44 | |
|
| 0 | 45 | | var operation = request.SendWebRequest(); |
| | 46 | |
|
| 0 | 47 | | operation.completed += asyncOperation => |
| | 48 | | { |
| 0 | 49 | | if (request.WebRequestSucceded()) |
| | 50 | | { |
| 0 | 51 | | promise.Resolve(request.downloadHandler.text); |
| | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 0 | 55 | | promise.Reject($"error: {request.error} response: {request.downloadHandler.text}"); |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | request.Dispose(); |
| 0 | 59 | | }; |
| | 60 | |
|
| 0 | 61 | | return promise; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public Promise<List<Land>> QueryLands(string network, string address, float cacheMaxAgeSeconds) |
| | 65 | | { |
| 0 | 66 | | string lowerCaseAddress = address.ToLower(); |
| | 67 | |
|
| 0 | 68 | | Promise<List<Land>> promise = new Promise<List<Land>>(); |
| | 69 | |
|
| 0 | 70 | | if (cacheMaxAgeSeconds >= 0) |
| | 71 | | { |
| 0 | 72 | | if (landQueryCache.TryGet(lowerCaseAddress, out List<Land> cacheValue, out float lastUpdate)) |
| | 73 | | { |
| 0 | 74 | | if (Time.unscaledTime - lastUpdate <= cacheMaxAgeSeconds) |
| | 75 | | { |
| 0 | 76 | | promise.Resolve(cacheValue); |
| 0 | 77 | | return promise; |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | string url = network == "mainnet" ? LAND_SUBGRAPH_URL_ORG : LAND_SUBGRAPH_URL_ZONE; |
| | 83 | |
|
| 0 | 84 | | Query(url, TheGraphQueries.getLandQuery, new AddressVariable() { address = lowerCaseAddress }) |
| | 85 | | .Then(resultJson => |
| | 86 | | { |
| 0 | 87 | | ProcessReceivedLandsData(promise, resultJson, lowerCaseAddress, true); |
| 0 | 88 | | }) |
| 0 | 89 | | .Catch(error => promise.Reject(error)); |
| | 90 | |
|
| 0 | 91 | | return promise; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | public Promise<double> QueryMana(string address, TheGraphNetwork network) |
| | 95 | | { |
| 0 | 96 | | Promise<double> promise = new Promise<double>(); |
| | 97 | |
|
| 0 | 98 | | string lowerCaseAddress = address.ToLower(); |
| 0 | 99 | | Query( |
| | 100 | | network == TheGraphNetwork.Ethereum ? MANA_SUBGRAPH_URL_ETHEREUM : MANA_SUBGRAPH_URL_POLYGON, |
| | 101 | | network == TheGraphNetwork.Ethereum ? TheGraphQueries.getEthereumManaQuery : TheGraphQueries.getPolygonM |
| | 102 | | new AddressVariable() { address = lowerCaseAddress }) |
| | 103 | | .Then(resultJson => |
| | 104 | | { |
| | 105 | | try |
| | 106 | | { |
| 0 | 107 | | JObject result = JObject.Parse(resultJson); |
| 0 | 108 | | JToken manaObject = result["data"]?["accounts"].First?["mana"]; |
| 0 | 109 | | if (manaObject == null || !double.TryParse(manaObject.Value<string>(), out double parsedMana)) |
| 0 | 110 | | throw new Exception($"QueryMana response couldn't be parsed: {resultJson}"); |
| | 111 | |
|
| 0 | 112 | | promise.Resolve(parsedMana / 1e18); |
| 0 | 113 | | } |
| 0 | 114 | | catch (Exception e) |
| | 115 | | { |
| 0 | 116 | | promise.Reject(e.ToString()); |
| 0 | 117 | | } |
| 0 | 118 | | }) |
| 0 | 119 | | .Catch(error => promise.Reject(error)); |
| 0 | 120 | | return promise; |
| | 121 | | } |
| | 122 | |
|
| | 123 | | public Promise<List<Nft>> QueryNftCollections(string address, NftCollectionsLayer layer) |
| | 124 | | { |
| 0 | 125 | | Promise<List<Nft>> promise = new Promise<List<Nft>>(); |
| | 126 | |
|
| 0 | 127 | | string url = GetSubGraphUrl(layer); |
| | 128 | |
|
| 0 | 129 | | Query(url, TheGraphQueries.getNftCollectionsQuery, new AddressVariable() { address = address.ToLower() }) |
| | 130 | | .Then(resultJson => |
| | 131 | | { |
| 0 | 132 | | ProcessReceivedNftData(promise, resultJson); |
| 0 | 133 | | }) |
| 0 | 134 | | .Catch(error => promise.Reject(error)); |
| | 135 | |
|
| 0 | 136 | | return promise; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public Promise<List<Nft>> QueryNftCollectionsByUrn(string address, string urn, NftCollectionsLayer layer) |
| | 140 | | { |
| 0 | 141 | | Promise<List<Nft>> promise = new Promise<List<Nft>>(); |
| | 142 | |
|
| 0 | 143 | | string url = GetSubGraphUrl(layer); |
| | 144 | |
|
| 0 | 145 | | Query(url, TheGraphQueries.getNftCollectionByUserAndUrnQuery, new AddressAndUrnVariable() { address = address.To |
| | 146 | | .Then(resultJson => |
| | 147 | | { |
| 0 | 148 | | ProcessReceivedNftData(promise, resultJson); |
| 0 | 149 | | }) |
| 0 | 150 | | .Catch(error => promise.Reject(error)); |
| | 151 | |
|
| 0 | 152 | | return promise; |
| | 153 | | } |
| | 154 | |
|
| 260 | 155 | | public void Dispose() { landQueryCache.Dispose(); } |
| | 156 | |
|
| | 157 | | private string GetSubGraphUrl(NftCollectionsLayer layer) |
| | 158 | | { |
| 0 | 159 | | string url = ""; |
| | 160 | | switch (layer) |
| | 161 | | { |
| | 162 | | case NftCollectionsLayer.ETHEREUM: |
| 0 | 163 | | url = NFT_COLLECTIONS_SUBGRAPH_URL_ETHEREUM; |
| 0 | 164 | | break; |
| | 165 | | case NftCollectionsLayer.MATIC: |
| 0 | 166 | | url = NFT_COLLECTIONS_SUBGRAPH_URL_MATIC; |
| | 167 | | break; |
| | 168 | | } |
| | 169 | |
|
| 0 | 170 | | return url; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | private void ProcessReceivedLandsData(Promise<List<Land>> landPromise, string jsonValue, string lowerCaseAddress, bo |
| | 174 | | { |
| 0 | 175 | | bool hasException = false; |
| 0 | 176 | | List<Land> lands = null; |
| | 177 | |
|
| | 178 | | try |
| | 179 | | { |
| 0 | 180 | | LandQueryResultWrapped result = JsonUtility.FromJson<LandQueryResultWrapped>(jsonValue); |
| 0 | 181 | | lands = LandHelper.ConvertQueryResult(result.data, lowerCaseAddress); |
| | 182 | |
|
| 0 | 183 | | if (cache) |
| | 184 | | { |
| 0 | 185 | | landQueryCache.Add(lowerCaseAddress, lands, DEFAULT_CACHE_TIME); |
| | 186 | | } |
| 0 | 187 | | } |
| 0 | 188 | | catch (Exception exception) |
| | 189 | | { |
| 0 | 190 | | landPromise.Reject(exception.Message); |
| 0 | 191 | | hasException = true; |
| 0 | 192 | | } |
| | 193 | | finally |
| | 194 | | { |
| 0 | 195 | | if (!hasException) |
| | 196 | | { |
| 0 | 197 | | landPromise.Resolve(lands); |
| | 198 | | } |
| 0 | 199 | | } |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | private void ProcessReceivedNftData(Promise<List<Nft>> nftPromise, string jsonValue) |
| | 203 | | { |
| 0 | 204 | | bool hasException = false; |
| 0 | 205 | | List<Nft> nfts = new List<Nft>(); |
| | 206 | |
|
| | 207 | | try |
| | 208 | | { |
| 0 | 209 | | NftQueryResultWrapped result = JsonUtility.FromJson<NftQueryResultWrapped>(jsonValue); |
| | 210 | |
|
| 0 | 211 | | foreach (var nft in result.data.nfts) |
| | 212 | | { |
| 0 | 213 | | nfts.Add(new Nft |
| | 214 | | { |
| | 215 | | collectionId = nft.collection.id, |
| | 216 | | blockchainId = nft.item.blockchainId, |
| | 217 | | tokenId = nft.tokenId, |
| | 218 | | urn = nft.urn, |
| | 219 | | }); |
| | 220 | | } |
| 0 | 221 | | } |
| 0 | 222 | | catch (Exception exception) |
| | 223 | | { |
| 0 | 224 | | nftPromise.Reject(exception.Message); |
| 0 | 225 | | hasException = true; |
| 0 | 226 | | } |
| | 227 | | finally |
| | 228 | | { |
| 0 | 229 | | if (!hasException) |
| | 230 | | { |
| 0 | 231 | | nftPromise.Resolve(nfts); |
| | 232 | | } |
| 0 | 233 | | } |
| 0 | 234 | | } |
| | 235 | | } |