@open-oracle-origami/origami-js-mill-coingecko
Version:
Open Oracle Origami Node JS CoinGecko Mill
34 lines (33 loc) • 1.73 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { CoinGeckoClient } from 'coingecko-api-v3';
import { utils } from '@open-oracle-origami/origami-js-sdk';
export default ({ press, interval = 5000, timeout = 10000, autoRetry = true, simplePriceParams, apiKey, }) => {
let stop = false;
const client = new CoinGeckoClient({
timeout,
autoRetry,
}, apiKey);
const params = Object.assign(Object.assign({}, simplePriceParams), { ids: Array.isArray(simplePriceParams.ids)
? simplePriceParams.ids.join(',')
: simplePriceParams.ids, vs_currencies: Array.isArray(simplePriceParams.vs_currencies)
? simplePriceParams.vs_currencies.join(',')
: simplePriceParams.vs_currencies });
const pressAllIds = () => __awaiter(void 0, void 0, void 0, function* () {
const res = yield client.simplePrice(params);
Object.entries(res).forEach(([sku, data]) => {
press(sku, data);
});
});
void utils.poll(pressAllIds, interval, () => stop);
return () => {
stop = true;
};
};