redstone-api
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -107,2 +107,4 @@ import { PriceData, GetPriceOptions, RedstoneApiConfig, GetHistoricalPriceOptions, GetHistoricalPriceForSingleTokenOptions } from "./types"; | ||
private getHistoricalPricesForOneSymbol; | ||
private getProviderForSymbol; | ||
private getProviderForSymbols; | ||
} |
@@ -59,2 +59,3 @@ "use strict"; | ||
var price_not_found_1 = __importDefault(require("./errors/price-not-found")); | ||
var token_config_json_1 = __importDefault(require("redstone-node/src/config/token-config.json")); | ||
var REDSTONE_API_DEFAULTS = { | ||
@@ -65,2 +66,8 @@ defaultProvider: "redstone", | ||
}; | ||
// Providers list is sorted by priority (redstone-rapid has the highest priority) | ||
var PROVIDERS_SORTED_BY_PRIORITY = [ | ||
"redstone-rapid", | ||
"redstone-stocks", | ||
"redstone" | ||
]; | ||
var RedstoneApi = /** @class */ (function () { | ||
@@ -83,27 +90,26 @@ function RedstoneApi(redstoneConfig) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var provider, shouldVerifySignature; | ||
var shouldVerifySignature, symbols, provider, symbol, provider; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
provider = lodash_1.default.defaultTo(opts.provider, this.defaultProvider); | ||
shouldVerifySignature = lodash_1.default.defaultTo(opts.verifySignature, this.verifySignature); | ||
if (!lodash_1.default.isArray(symbolOrSymbols)) return [3 /*break*/, 2]; | ||
symbols = symbolOrSymbols; | ||
provider = this.getProviderForSymbols(symbols, opts.provider); | ||
return [4 /*yield*/, this.getPriceForManyTokens({ | ||
symbols: symbolOrSymbols, | ||
symbols: symbols, | ||
provider: provider, | ||
shouldVerifySignature: shouldVerifySignature, | ||
})]; | ||
case 1: | ||
// Getting latest price for many tokens | ||
return [2 /*return*/, _a.sent()]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 2: | ||
if (!(typeof symbolOrSymbols === "string")) return [3 /*break*/, 4]; | ||
symbol = symbolOrSymbols; | ||
provider = this.getProviderForSymbol(symbol, opts.provider); | ||
return [4 /*yield*/, this.getLatestPriceForOneToken({ | ||
symbol: symbolOrSymbols, | ||
symbol: symbol, | ||
provider: provider, | ||
shouldVerifySignature: shouldVerifySignature, | ||
})]; | ||
case 3: | ||
// Getting latest price for one token | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/, _a.sent()]; | ||
case 4: return [2 /*return*/]; | ||
@@ -116,11 +122,12 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var provider, shouldVerifySignature; | ||
var shouldVerifySignature, symbols, provider, symbol, provider; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
provider = lodash_1.default.defaultTo(opts.provider, this.defaultProvider); | ||
shouldVerifySignature = lodash_1.default.defaultTo(opts.verifySignature, this.verifySignature); | ||
if (!lodash_1.default.isArray(symbolOrSymbols)) return [3 /*break*/, 2]; | ||
symbols = symbolOrSymbols; | ||
provider = this.getProviderForSymbols(symbols, opts.provider); | ||
return [4 /*yield*/, this.getPriceForManyTokens({ | ||
symbols: symbolOrSymbols, | ||
symbols: symbols, | ||
timestamp: getTimestamp(opts.date), | ||
@@ -130,10 +137,10 @@ provider: provider, | ||
})]; | ||
case 1: | ||
// Getting historical price for many tokens | ||
return [2 /*return*/, _a.sent()]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 2: | ||
if (!(typeof symbolOrSymbols === "string")) return [3 /*break*/, 6]; | ||
symbol = symbolOrSymbols; | ||
provider = this.getProviderForSymbol(symbol, opts.provider); | ||
if (!(opts.interval !== undefined || opts.limit !== undefined)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, this.getHistoricalPricesForOneSymbol({ | ||
symbol: symbolOrSymbols, | ||
symbol: symbol, | ||
fromTimestamp: getTimestamp(opts.startDate), | ||
@@ -149,3 +156,3 @@ toTimestamp: getTimestamp(opts.endDate), | ||
case 4: return [4 /*yield*/, this.getHistoricalPriceForOneSymbol({ | ||
symbol: symbolOrSymbols, | ||
symbol: symbol, | ||
timestamp: getTimestamp(opts.date), | ||
@@ -319,3 +326,3 @@ provider: provider, | ||
case 4: | ||
// TODO: we cannot query ArGQL with timestamp camparators like timestamp_gt | ||
// TODO: we cannot query ArGQL with timestamp comparators like timestamp_gt | ||
// But in future we can think of querying based on block numbers | ||
@@ -392,3 +399,3 @@ throw new Error("Fetching historical price from arweave is not supported"); | ||
case 6: | ||
// TODO: we cannot query ArGQL with timestamp camparators like timestamp_gt | ||
// TODO: we cannot query ArGQL with timestamp comparators like timestamp_gt | ||
// But in future we can think of querying based on block numbers | ||
@@ -400,2 +407,44 @@ throw new Error("Fetching historical prices from arweave is not supported"); | ||
}; | ||
RedstoneApi.prototype.getProviderForSymbol = function (symbol, provider) { | ||
return this.getProviderForSymbols([symbol], provider); | ||
}; | ||
RedstoneApi.prototype.getProviderForSymbols = function (symbols, passedProvider) { | ||
if (passedProvider !== undefined) { | ||
return passedProvider; | ||
} | ||
else { | ||
// Calculating a list of providers which support all symbols in the list | ||
var possibleProviders = Array.from(PROVIDERS_SORTED_BY_PRIORITY); | ||
for (var _i = 0, symbols_1 = symbols; _i < symbols_1.length; _i++) { | ||
var symbol = symbols_1[_i]; | ||
var details = token_config_json_1.default[symbol]; | ||
if (details && details.providers && Array.isArray(details.providers)) { | ||
var _loop_1 = function (provider) { | ||
// If any of symbols doesn't support the provider | ||
// it can not be used | ||
if (!details.providers.includes(provider)) { | ||
possibleProviders = possibleProviders.filter(function (p) { return p !== provider; }); | ||
} | ||
}; | ||
for (var _a = 0, possibleProviders_1 = possibleProviders; _a < possibleProviders_1.length; _a++) { | ||
var provider = possibleProviders_1[_a]; | ||
_loop_1(provider); | ||
} | ||
} | ||
else { | ||
// If any symbol has no supported providers in redstone-node config | ||
// we break the loop and return the default provider | ||
possibleProviders = []; | ||
break; | ||
} | ||
} | ||
// Returning the best possible provider | ||
if (possibleProviders.length > 0) { | ||
return possibleProviders[0]; | ||
} | ||
else { | ||
return this.defaultProvider; | ||
} | ||
} | ||
}; | ||
return RedstoneApi; | ||
@@ -402,0 +451,0 @@ }()); |
@@ -1,2 +0,1 @@ | ||
import Arweave from "arweave"; | ||
interface GraphQLParams { | ||
@@ -22,3 +21,3 @@ type: string; | ||
export default class ArweaveProxy { | ||
arweaveClient: Arweave; | ||
arweaveClient: any; | ||
constructor(); | ||
@@ -25,0 +24,0 @@ findPricesInGraphQL(parameters: GraphQLParams): Promise<GraphQLResponse | undefined>; |
@@ -42,6 +42,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var arweave_multihost_1 = __importDefault(require("arweave-multihost")); | ||
var arweave_1 = __importDefault(require("arweave")); | ||
var ar_gql_1 = require("ar-gql"); | ||
var pako_1 = __importDefault(require("pako")); | ||
var providers_json_1 = __importDefault(require("./providers.json")); | ||
var providers_json_1 = __importDefault(require("redstone-node/src/config/providers.json")); | ||
// TODO: revert LAST_BLOCKS_TO_CHECK to 50 | ||
@@ -51,6 +52,5 @@ var LAST_BLOCKS_TO_CHECK = 5000; | ||
function ArweaveProxy() { | ||
this.arweaveClient = arweave_1.default.init({ | ||
host: "arweave.net", | ||
port: 443, | ||
protocol: "https", | ||
this.arweaveClient = arweave_multihost_1.default.initWithDefaultHosts({ | ||
timeout: 10000, | ||
logging: false, // Enable network request logging | ||
}); | ||
@@ -57,0 +57,0 @@ } |
{ | ||
"name": "redstone-api", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "Javascript library for fetching trusted token pricing data from Redstone data ecosystem", | ||
@@ -58,3 +58,3 @@ "main": "lib/index.js", | ||
}, | ||
"homepage": "https://github.com/redstone-finance/redstone-api#readme", | ||
"homepage": "https://api.docs.redstone.finance/", | ||
"devDependencies": { | ||
@@ -80,7 +80,9 @@ "@types/jest": "^26.0.20", | ||
"ar-gql": "^0.0.6", | ||
"arweave": "^1.10.14", | ||
"arweave": "^1.10.16", | ||
"arweave-multihost": "^0.1.0", | ||
"axios": "^0.21.1", | ||
"deep-sort-object": "^1.0.2", | ||
"lodash": "^4.17.21", | ||
"pako": "^2.0.3" | ||
"pako": "^2.0.3", | ||
"redstone-node": "redstone-finance/redstone-node#ae78874" | ||
}, | ||
@@ -87,0 +89,0 @@ "files": [ |
@@ -8,4 +8,3 @@ # Redstone API | ||
<!-- TODO: uncomment when redstone twitter account will be ready --> | ||
<!-- [![Twitter](https://img.shields.io/twitter/follow/limestone_defi?style=flat&logo=twitter)](https://twitter.com/intent/follow?screen_name=limestone_defi) --> | ||
[![Twitter](https://img.shields.io/twitter/follow/redstone_defi?style=flat&logo=twitter)](https://twitter.com/intent/follow?screen_name=limestone_defi) | ||
@@ -16,2 +15,5 @@ Redstone API is a Javascript library for fetching trusted token pricing data from [Redstone data ecosystem](docs/REDSTONE_DATA_ECOSYSTEM.md). | ||
## 🚀 Demo | ||
Try it directly in CodeSandbox: [demo link](https://codesandbox.io/s/redstone-api-demo-0h4oo?from-embed=&file=/src/index.ts) | ||
## ✅ Why Redstone API | ||
@@ -18,0 +20,0 @@ ### ✓ Secure |
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
145004
3699
305
8
1
1
+ Addedarweave-multihost@^0.1.0
+ Addedarweave-multihost@0.1.0(transitive)
Updatedarweave@^1.10.16