| | 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://api.thegraph.com/subgraphs/name/decentraland/land-manager"; |
| | 15 | | private const string LAND_SUBGRAPH_URL_ZONE = "https://api.thegraph.com/subgraphs/name/decentraland/land-manager-rop |
| | 16 | | private const string LAND_SUBGRAPH_URL_MATIC = "https://api.thegraph.com/subgraphs/name/decentraland/mana-matic-main |
| | 17 | | private const string NFT_COLLECTIONS_SUBGRAPH_URL_ETHEREUM = "https://api.thegraph.com/subgraphs/name/decentraland/c |
| | 18 | | private const string NFT_COLLECTIONS_SUBGRAPH_URL_MATIC = "https://api.thegraph.com/subgraphs/name/decentraland/coll |
| | 19 | |
|
| 72 | 20 | | private readonly IDataCache<List<Land>> landQueryCache = new DataCache<List<Land>>(); |
| | 21 | |
|
| 0 | 22 | | public Promise<string> Query(string url, string query) { return Query(url, query, null); } |
| | 23 | |
|
| | 24 | | public Promise<string> Query(string url, string query, QueryVariablesBase variables) |
| | 25 | | { |
| 0 | 26 | | Promise<string> promise = new Promise<string>(); |
| | 27 | |
|
| 0 | 28 | | if (string.IsNullOrEmpty(query) || string.IsNullOrEmpty(url)) |
| | 29 | | { |
| 0 | 30 | | promise.Reject($"error: {(string.IsNullOrEmpty(url) ? "url" : "query")} is empty"); |
| 0 | 31 | | return promise; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | string queryJson = $"\"query\":\"{Regex.Replace(query, @"\p{C}+", string.Empty)}\""; |
| 0 | 35 | | string variablesJson = variables != null ? $",\"variables\":{JsonUtility.ToJson(variables)}" : string.Empty; |
| 0 | 36 | | string bodyString = $"{{{queryJson}{variablesJson}}}"; |
| | 37 | |
|
| 0 | 38 | | var request = new UnityWebRequest(); |
| 0 | 39 | | request.url = url; |
| 0 | 40 | | request.method = UnityWebRequest.kHttpVerbPOST; |
| 0 | 41 | | request.downloadHandler = new DownloadHandlerBuffer(); |
| 0 | 42 | | request.uploadHandler = new UploadHandlerRaw(string.IsNullOrEmpty(bodyString) ? null : Encoding.UTF8.GetBytes(bo |
| 0 | 43 | | request.SetRequestHeader("Content-Type", "application/json"); |
| 0 | 44 | | request.timeout = 60; |
| | 45 | |
|
| 0 | 46 | | var operation = request.SendWebRequest(); |
| | 47 | |
|
| 0 | 48 | | operation.completed += asyncOperation => |
| | 49 | | { |
| 0 | 50 | | if (request.WebRequestSucceded()) |
| | 51 | | { |
| 0 | 52 | | promise.Resolve(request.downloadHandler.text); |
| 0 | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 0 | 56 | | promise.Reject($"error: {request.error} response: {request.downloadHandler.text}"); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | request.Dispose(); |
| 0 | 60 | | }; |
| | 61 | |
|
| 0 | 62 | | return promise; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | public Promise<List<Land>> QueryLands(string network, string address) { return QueryLands(network, address, DEFAULT_ |
| | 66 | |
|
| | 67 | | public Promise<List<Land>> QueryLands(string network, string address, float cacheMaxAgeSeconds) |
| | 68 | | { |
| 0 | 69 | | string lowerCaseAddress = address.ToLower(); |
| | 70 | |
|
| 0 | 71 | | Promise<List<Land>> promise = new Promise<List<Land>>(); |
| | 72 | |
|
| 0 | 73 | | if (cacheMaxAgeSeconds >= 0) |
| | 74 | | { |
| 0 | 75 | | if (landQueryCache.TryGet(lowerCaseAddress, out List<Land> cacheValue, out float lastUpdate)) |
| | 76 | | { |
| 0 | 77 | | if (Time.unscaledTime - lastUpdate <= cacheMaxAgeSeconds) |
| | 78 | | { |
| 0 | 79 | | promise.Resolve(cacheValue); |
| 0 | 80 | | return promise; |
| | 81 | | } |
| | 82 | | } |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | string url = network == "mainnet" ? LAND_SUBGRAPH_URL_ORG : LAND_SUBGRAPH_URL_ZONE; |
| | 86 | |
|
| 0 | 87 | | Query(url, TheGraphQueries.getLandQuery, new AddressVariable() { address = lowerCaseAddress }) |
| | 88 | | .Then(resultJson => |
| | 89 | | { |
| 0 | 90 | | ProcessReceivedLandsData(promise, resultJson, lowerCaseAddress, true); |
| 0 | 91 | | }) |
| 0 | 92 | | .Catch(error => promise.Reject(error)); |
| | 93 | |
|
| 0 | 94 | | return promise; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public Promise<double> QueryPolygonMana(string address) |
| | 98 | | { |
| 0 | 99 | | Promise<double> promise = new Promise<double>(); |
| | 100 | |
|
| 0 | 101 | | string lowerCaseAddress = address.ToLower(); |
| 0 | 102 | | Query(LAND_SUBGRAPH_URL_MATIC, TheGraphQueries.getPolygonManaQuery, new AddressVariable() { address = lowerCaseA |
| | 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 = ""; |
| | 128 | | switch (layer) |
| | 129 | | { |
| | 130 | | case NftCollectionsLayer.ETHEREUM: |
| 0 | 131 | | url = NFT_COLLECTIONS_SUBGRAPH_URL_ETHEREUM; |
| 0 | 132 | | break; |
| | 133 | | case NftCollectionsLayer.MATIC: |
| 0 | 134 | | url = NFT_COLLECTIONS_SUBGRAPH_URL_MATIC; |
| | 135 | | break; |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | Query(url, TheGraphQueries.getNftCollectionsQuery, new AddressVariable() { address = address.ToLower() }) |
| | 139 | | .Then(resultJson => |
| | 140 | | { |
| 0 | 141 | | ProcessReceivedNftData(promise, resultJson); |
| 0 | 142 | | }) |
| 0 | 143 | | .Catch(error => promise.Reject(error)); |
| | 144 | |
|
| 0 | 145 | | return promise; |
| | 146 | | } |
| | 147 | |
|
| 144 | 148 | | public void Dispose() { landQueryCache.Dispose(); } |
| | 149 | |
|
| | 150 | | private void ProcessReceivedLandsData(Promise<List<Land>> landPromise, string jsonValue, string lowerCaseAddress, bo |
| | 151 | | { |
| 0 | 152 | | bool hasException = false; |
| 0 | 153 | | List<Land> lands = null; |
| | 154 | |
|
| | 155 | | try |
| | 156 | | { |
| 0 | 157 | | LandQueryResultWrapped result = JsonUtility.FromJson<LandQueryResultWrapped>(jsonValue); |
| 0 | 158 | | lands = LandHelper.ConvertQueryResult(result.data, lowerCaseAddress); |
| | 159 | |
|
| 0 | 160 | | if (cache) |
| | 161 | | { |
| 0 | 162 | | landQueryCache.Add(lowerCaseAddress, lands, DEFAULT_CACHE_TIME); |
| | 163 | | } |
| 0 | 164 | | } |
| 0 | 165 | | catch (Exception exception) |
| | 166 | | { |
| 0 | 167 | | landPromise.Reject(exception.Message); |
| 0 | 168 | | hasException = true; |
| 0 | 169 | | } |
| | 170 | | finally |
| | 171 | | { |
| 0 | 172 | | if (!hasException) |
| | 173 | | { |
| 0 | 174 | | landPromise.Resolve(lands); |
| | 175 | | } |
| 0 | 176 | | } |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | private void ProcessReceivedNftData(Promise<List<Nft>> nftPromise, string jsonValue) |
| | 180 | | { |
| 0 | 181 | | bool hasException = false; |
| 0 | 182 | | List<Nft> nfts = new List<Nft>(); |
| | 183 | |
|
| | 184 | | try |
| | 185 | | { |
| 0 | 186 | | NftQueryResultWrapped result = JsonUtility.FromJson<NftQueryResultWrapped>(jsonValue); |
| | 187 | |
|
| 0 | 188 | | foreach (var nft in result.data.nfts) |
| | 189 | | { |
| 0 | 190 | | nfts.Add(new Nft |
| | 191 | | { |
| | 192 | | collectionId = nft.collection.id, |
| | 193 | | tokenId = nft.tokenId, |
| | 194 | | urn = nft.urn |
| | 195 | | }); |
| | 196 | | } |
| 0 | 197 | | } |
| 0 | 198 | | catch (Exception exception) |
| | 199 | | { |
| 0 | 200 | | nftPromise.Reject(exception.Message); |
| 0 | 201 | | hasException = true; |
| 0 | 202 | | } |
| | 203 | | finally |
| | 204 | | { |
| 0 | 205 | | if (!hasException) |
| | 206 | | { |
| 0 | 207 | | nftPromise.Resolve(nfts); |
| | 208 | | } |
| 0 | 209 | | } |
| 0 | 210 | | } |
| | 211 | | } |