Esempio n. 1
0
        public byte[] Read(Timestamp time, string url)
        {
            if (_entries.ContainsKey(url))
            {
                return(_entries[url].Content);
            }

            byte[] content;

            if (url.StartsWith(interopTag))
            {
                url = url.Substring(interopTag.Length);
                var args = url.Split('/');

                var platformName = args[0];
                var chainName    = args[1];
                if (Nexus.PlatformExists(Nexus.RootStorage, platformName))
                {
                    args    = args.Skip(2).ToArray();
                    content = ReadChainOracle(platformName, chainName, args);
                }
                else
                {
                    throw new OracleException("invalid oracle platform: " + platformName);
                }
            }
            else
            if (url.StartsWith(priceTag))
            {
                url = url.Substring(priceTag.Length);

                if (url.Contains('/'))
                {
                    throw new OracleException("invalid oracle price request");
                }

                var baseSymbol = url;

                if (!Nexus.TokenExists(Nexus.RootStorage, baseSymbol))
                {
                    throw new OracleException("unknown token: " + baseSymbol);
                }

                var price = PullPrice(time, baseSymbol);
                var val   = UnitConversion.ToBigInteger(price, DomainSettings.FiatTokenDecimals);
                content = val.ToUnsignedByteArray();
            }
            else
            {
                content = PullData(time, url);
            }

            var entry = new OracleEntry(url, content);

            _entries[url] = entry;

            return(content);
        }
Esempio n. 2
0
        private void CacheOracleData <T>(string url, T content)
        {
            var value = Serialization.Serialize(content);

            if (value == null)
            {
                throw new OracleException($"Serialized value can't be null, url: {url}");
            }

            var entry = new OracleEntry(url, value);

            lock (_txEntries)
            {
                _txEntries[url] = entry;
            }
        }
Esempio n. 3
0
        public byte[] Read(string url)
        {
            if (_entries.ContainsKey(url))
            {
                return(_entries[url].Content);
            }

            byte[] content;

            if (url.StartsWith(interopTag))
            {
                url = url.Substring(interopTag.Length);
                var args = url.Split('/');

                var platformName = args[0];
                if (Nexus.PlatformExists(platformName))
                {
                    args = args.Skip(1).ToArray();
                    return(ReadChainOracle(platformName, args));
                }
                else
                {
                    throw new OracleException("invalid oracle platform: " + platformName);
                }
            }
            else
            if (url.StartsWith(priceTag))
            {
                url = url.Substring(priceTag.Length);
                var symbols = url.Split('/');

                if (symbols.Length < 1 || symbols.Length > 2)
                {
                    throw new OracleException("invalid oracle price request");
                }

                var baseSymbol  = symbols[0];
                var quoteSymbol = symbols.Length > 1 ? symbols[1] : Nexus.FiatTokenSymbol;

                if (!Nexus.TokenExists(baseSymbol))
                {
                    throw new OracleException("unknown token: " + baseSymbol);
                }

                if (!Nexus.TokenExists(quoteSymbol))
                {
                    throw new OracleException("unknown token: " + quoteSymbol);
                }

                var tokenInfo = Nexus.GetTokenInfo(quoteSymbol);

                var price = PullPrice(baseSymbol, quoteSymbol);
                var val   = UnitConversion.ToBigInteger(price, tokenInfo.Decimals);
                return(val.ToUnsignedByteArray());
            }
            else
            {
                content = PullData(url);
            }

            var entry = new OracleEntry(url, content);

            _entries[url] = entry;

            return(content);
        }