Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor() {
super();
const CounterValues = getCountervalues();
const store = initStore();
// quick way to store countervalues with localStorage
const LS_KEY = "countervalues_intermediary";
try {
const json = localStorage.getItem(LS_KEY);
if (json) {
store.dispatch(CounterValues.importAction(JSON.parse(json)));
}
} catch (e) {
console.warn(e);
}
store.subscribe(() => {
localStorage.setItem(
LS_KEY,
JSON.stringify(CounterValues.exportSelector(store.getState()))
// @flow
import React, { useState, useEffect } from "react";
import Select from "react-select";
import { listTokens } from "@ledgerhq/live-common/lib/currencies";
import type { CurrencyToken } from "@ledgerhq/live-common/lib/types";
import { getCountervalues } from "@ledgerhq/live-common/lib/countervalues";
let tickers;
const tickersP = getCountervalues()
.fetchTickersByMarketcap()
.then(t => {
tickers = t;
return t;
});
const rank = token => {
const i = tickers.indexOf(token.ticker);
if (i === -1) return Infinity;
return i;
};
const getSortedTokens = () => {
let tokens = listTokens();
if (!tickers) return tokens;
return tokens.slice(0).sort((a, b) => rank(a) - rank(b));
async _load() {
this._loadId++;
this.setState({ exchanges: [] });
const { _loadId } = this;
const { from, to } = this.props;
try {
const exchanges = await getCountervalues().fetchExchangesForPair(
from,
to
);
if (this._loadId === _loadId) {
this.setState({ exchanges });
}
} catch (error) {
console.error(error);
if (this._loadId === _loadId) {
this.setState({ error });
}
}
}
export const initStore = () =>
createStore(
combineReducers({
markets,
countervalues: getCountervalues().reducer
}),
composeWithDevTools(applyMiddleware(thunkMiddleware))
);
async componentDidMount() {
const tickers = await getCountervalues().fetchTickersByMarketcap();
this.setState({ tickers });
}
render() {
const { classes, markets, setMarket, addMarket } = this.props;
const CounterValues = getCountervalues();
return (
<div>
{polling => (
CounterValues API demo
</div>
const mapStateToProps = (state, props: *) => {
const data = [];
let t = Date.now() - props.days * DAY;
const value = BigNumber(10 ** props.from.units[0].magnitude);
for (let i = 0; i < props.days; i++) {
const date = new Date(t);
const cv = getCountervalues().calculateSelector(state, {
...props,
date,
value
});
data.push({
date,
value: cv ? cv.toNumber() : 0
});
t += DAY;
}
return { data };
};
getAPIBaseURL: () => LEDGER_COUNTERVALUES_API,
storeSelector: state => state.countervalues,
pairsSelector,
setExchangePairsAction,
addExtraPollingHooks,
network,
...(Config.MOCK
? {
getDailyRatesImplementation,
fetchExchangesForPairImplementation,
fetchTickersByMarketcapImplementation,
}
: {}),
});
const CounterValues = getCountervalues();
type PC = Promise;
let sortCache;
let syncCache = listCryptoCurrencies(true).sort((a, b) =>
a.name.localeCompare(b.name),
);
export const getFullListSortedCryptoCurrencies: () => PC = () => {
if (!sortCache) {
sortCache = CounterValues.fetchTickersByMarketcap().then(
tickers => {
const list = listCryptoCurrencies(true).slice(0);
const prependList = [];
tickers.forEach(ticker => {
const item = list.find(c => c.ticker === ticker);