| | 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 | |
|
| 531 | 18 | | private readonly IDataCache<List<Land>> landQueryCache = new DataCache<List<Land>>(); |
| | 19 | |
|
| 0 | 20 | | public Promise<string> Query(string url, string query) { return Query(url, query, null); } |
| | 21 | |
|
| | 22 | | public Promise<string> Query(string url, string query, QueryVariablesBase variables) |
| | 23 | | { |
| 1 | 24 | | Promise<string> promise = new Promise<string>(); |
| | 25 | |
|
| 1 | 26 | | if (string.IsNullOrEmpty(query) || string.IsNullOrEmpty(url)) |
| | 27 | | { |
| 0 | 28 | | promise.Reject($"error: {(string.IsNullOrEmpty(url) ? "url" : "query")} is empty"); |
| 0 | 29 | | return promise; |
| | 30 | | } |
| | 31 | |
|
| 1 | 32 | | string queryJson = $"\"query\":\"{Regex.Replace(query, @"\p{C}+", string.Empty)}\""; |
| 1 | 33 | | string variablesJson = variables != null ? $",\"variables\":{JsonUtility.ToJson(variables)}" : string.Empty; |
| 1 | 34 | | string bodyString = $"{{{queryJson}{variablesJson}}}"; |
| | 35 | |
|
| 1 | 36 | | var request = new UnityWebRequest(); |
| 1 | 37 | | request.url = url; |
| 1 | 38 | | request.method = UnityWebRequest.kHttpVerbPOST; |
| 1 | 39 | | request.downloadHandler = new DownloadHandlerBuffer(); |
| 1 | 40 | | request.uploadHandler = new UploadHandlerRaw(string.IsNullOrEmpty(bodyString) ? null : Encoding.UTF8.GetBytes(bo |
| 1 | 41 | | request.SetRequestHeader("Content-Type", "application/json"); |
| 1 | 42 | | request.timeout = 60; |
| | 43 | |
|
| 1 | 44 | | var operation = request.SendWebRequest(); |
| | 45 | |
|
| 1 | 46 | | operation.completed += asyncOperation => |
| | 47 | | { |
| 1 | 48 | | if (request.WebRequestSucceded()) |
| | 49 | | { |
| 1 | 50 | | promise.Resolve(request.downloadHandler.text); |
| 1 | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| 0 | 54 | | promise.Reject($"error: {request.error} response: {request.downloadHandler.text}"); |
| | 55 | | } |
| | 56 | |
|
| 1 | 57 | | request.Dispose(); |
| 1 | 58 | | }; |
| | 59 | |
|
| 1 | 60 | | return promise; |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public Promise<List<Land>> QueryLands(string tld, string address) { return QueryLands(tld, address, DEFAULT_CACHE_TI |
| | 64 | |
|
| | 65 | | public Promise<List<Land>> QueryLands(string tld, string address, float cacheMaxAgeSeconds) |
| | 66 | | { |
| 0 | 67 | | string lowerCaseAddress = address.ToLower(); |
| | 68 | |
|
| 0 | 69 | | Promise<List<Land>> promise = new Promise<List<Land>>(); |
| | 70 | |
|
| 0 | 71 | | if (cacheMaxAgeSeconds >= 0) |
| | 72 | | { |
| 0 | 73 | | if (landQueryCache.TryGet(lowerCaseAddress, out List<Land> cacheValue, out float lastUpdate)) |
| | 74 | | { |
| 0 | 75 | | if (Time.unscaledTime - lastUpdate <= cacheMaxAgeSeconds) |
| | 76 | | { |
| 0 | 77 | | promise.Resolve(cacheValue); |
| 0 | 78 | | return promise; |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | string url = tld == "org" ? LAND_SUBGRAPH_URL_ORG : LAND_SUBGRAPH_URL_ZONE; |
| | 84 | |
|
| 0 | 85 | | Query(url, TheGraphQueries.getLandQuery, new AddressVariable() { address = lowerCaseAddress }) |
| | 86 | | .Then(resultJson => |
| | 87 | | { |
| 0 | 88 | | ProcessReceivedLandsData(promise, resultJson, lowerCaseAddress, true); |
| 0 | 89 | | }) |
| 0 | 90 | | .Catch(error => promise.Reject(error)); |
| | 91 | |
|
| 0 | 92 | | return promise; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public Promise<double> QueryPolygonMana(string address) |
| | 96 | | { |
| 1 | 97 | | Promise<double> promise = new Promise<double>(); |
| | 98 | |
|
| 1 | 99 | | string lowerCaseAddress = address.ToLower(); |
| 1 | 100 | | Query(LAND_SUBGRAPH_URL_MATIC, TheGraphQueries.getPolygonManaQuery, new AddressVariable() { address = lowerCaseA |
| | 101 | | .Then(resultJson => |
| | 102 | | { |
| | 103 | | try |
| | 104 | | { |
| 1 | 105 | | JObject result = JObject.Parse(resultJson); |
| 1 | 106 | | JToken manaObject = result["data"]?["accounts"].First?["mana"]; |
| 1 | 107 | | if (manaObject == null || !double.TryParse(manaObject.Value<string>(), out double parsedMana)) |
| 1 | 108 | | throw new Exception($"QueryMana response couldn't be parsed: {resultJson}"); |
| | 109 | |
|
| 0 | 110 | | promise.Resolve(parsedMana / 1e18); |
| 0 | 111 | | } |
| 1 | 112 | | catch (Exception e) |
| | 113 | | { |
| 1 | 114 | | promise.Reject(e.ToString()); |
| 1 | 115 | | } |
| 1 | 116 | | }) |
| 0 | 117 | | .Catch(error => promise.Reject(error)); |
| 1 | 118 | | return promise; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | public void Dispose() { landQueryCache.Dispose(); } |
| | 122 | |
|
| | 123 | | private void ProcessReceivedLandsData(Promise<List<Land>> landPromise, string jsonValue, string lowerCaseAddress, bo |
| | 124 | | { |
| 0 | 125 | | bool hasException = false; |
| 0 | 126 | | List<Land> lands = null; |
| | 127 | |
|
| | 128 | | try |
| | 129 | | { |
| 0 | 130 | | LandQueryResultWrapped result = JsonUtility.FromJson<LandQueryResultWrapped>(jsonValue); |
| 0 | 131 | | lands = LandHelper.ConvertQueryResult(result.data, lowerCaseAddress); |
| | 132 | |
|
| 0 | 133 | | if (cache) |
| | 134 | | { |
| 0 | 135 | | landQueryCache.Add(lowerCaseAddress, lands, DEFAULT_CACHE_TIME); |
| | 136 | | } |
| 0 | 137 | | } |
| 0 | 138 | | catch (Exception exception) |
| | 139 | | { |
| 0 | 140 | | landPromise.Reject(exception.Message); |
| 0 | 141 | | hasException = true; |
| 0 | 142 | | } |
| | 143 | | finally |
| | 144 | | { |
| 0 | 145 | | if (!hasException) |
| | 146 | | { |
| 0 | 147 | | landPromise.Resolve(lands); |
| | 148 | | } |
| 0 | 149 | | } |
| 0 | 150 | | } |
| | 151 | | } |