Last active
June 9, 2018 18:13
-
-
Save marvinkome/61fdd3563a724db790d2a09780143832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Import Modules | |
| import numeral from 'numeral'; | |
| import fetch from 'isomorphic-fetch'; | |
| const cc = require('cryptocompare'); | |
| // Fetch currency functions | |
| const get_currency = async (currency, url) => { | |
| // Helper function that gets currency quantity and formats the result | |
| try { | |
| const response = await fetch(url); | |
| const data = response.json(); | |
| if ( currency == 'btc' ) return numeral(data / 10 ** 8).format('0.000'); | |
| if ( currency == 'eth' ) return numeral(data['result'] / 10 ** 18).format('0.000'); | |
| if ( currency == 'ltc' ) return numeral(data['data']['confirmed_balance']).format('0.00'); | |
| } catch (e) { | |
| return e; | |
| } | |
| }; | |
| // Fetch all prices | |
| const fetch_prices = () => cc.priceMulti(['BTC', 'ETH', 'LTC', 'DASH'], ['USD']); | |
| // Fetch token | |
| const fetch_token = (token, currency) => cc.priceMulti([token], [currency]); | |
| const get_btc = (wallet) => get_currency( | |
| 'btc', `https://blockchain.info/q/addressbalance/${wallet}` | |
| ); | |
| const get_eth = (wallet) => get_currency( | |
| 'eth', | |
| `https://api.etherscan.io/api?module=account&action=balance&address=${wallet}&tag=latest` | |
| ); | |
| const get_ltc = (wallet) => get_currency( | |
| 'ltc', | |
| `https://chain.so/api/v2/get_address_balance/LTC/${wallet}` | |
| ); | |
| const getter = async () => { | |
| // Using async/await to get the data | |
| const btc_q = await get_btc('33mU6E2zJfp8havHad6uwc9UFyMHGTTbW5'); | |
| const eth_q = await get_eth('0x3EE9741fEd91b1aBf7bE3d9f0Cd2efEc6aBf6442'); | |
| const ltc_q = await get_ltc('Lc7eraoXMDop5iEXshEAMPYirfiffubqJF'); | |
| const prices = await fetch_prices(); | |
| const btc_p = prices['BTC']['USD']; | |
| const eth_p = prices['ETH']['USD']; | |
| const ltc_p = prices['LTC']['USD']; | |
| // Test this i'm not really sure or it | |
| const ratio = await ( await fetch_token('BTC', 'LINK') ).ratio; | |
| const our_b = (49629.759 / ratio); | |
| const atuka_b = (22630 / ratio); | |
| return { | |
| btc_q, | |
| eth_q, | |
| ltc_q, | |
| btc_p, | |
| eth_p, | |
| ltc_p, | |
| our_b, | |
| atuka_b | |
| }; | |
| }; | |
| // initial state | |
| const state = { | |
| btc_q: 0, | |
| btc_p: 0, | |
| eth_q: 0, | |
| eth_p: 0, | |
| ltc_q: 0, | |
| ltc_p: 0, | |
| our_b: 0, | |
| atuka_b: 0 | |
| }; | |
| // Action types are set here to avoid | |
| // unexpected types change. | |
| const actionTypes = { | |
| IS_FETCHING: 'IS_FETCHING', | |
| UPDATE: 'UPDATE' | |
| }; | |
| // Actions creator | |
| // These actions are dispatch by the thunk | |
| const isFetching = () => ({ | |
| type: actionTypes.IS_FETCHING | |
| }); | |
| const update = (payload) => ({ | |
| type: actionTypes.UPDATE, | |
| payload | |
| }); | |
| /* | |
| Thunk actions | |
| This action is imported into a react component file. | |
| mapDispatchToProps will send this action to your | |
| component, then you can call it. Example | |
| import React from 'react'; | |
| import {getAllAndDispatchUpdate} from './action-location'; | |
| import {Provider, connect} from 'react-redux'; // Install this | |
| class ChildCont extends React.Component { | |
| componentDidMount(){ | |
| this.props.getAllCoins(); | |
| } | |
| render(){ | |
| console.log(this.props.state); | |
| return null; | |
| } | |
| } | |
| const mapStateToProps = state => ({ | |
| state: state | |
| }); | |
| const mapDispatchToProps = dispatch => ({ | |
| getAllCoins: () => dispatch(getAllAndDispatchUpdate) | |
| }); | |
| const Child = connect(mapStateToProps, mapDispatchToProps)(ChildCont); | |
| // Pass the store to all its children | |
| export const App = () => ( | |
| <Provider store={store}> | |
| <Child/> | |
| </Provider> | |
| ); | |
| */ | |
| export const getAllAndDispatchUpdate = () => { | |
| return async dispatch => { | |
| dispatch(isFetching()); | |
| const data = await getter(); | |
| return dispatch(update(data)); | |
| }; | |
| }; | |
| // Reducers | |
| const reducer = (store=state, action) => { | |
| if( action.type == actionTypes.IS_FETCHING ) { | |
| const state = { | |
| ...store, | |
| isFetching: true | |
| }; | |
| return state; | |
| } else if ( action.type == actionTypes.UPDATE ) { | |
| const state = { | |
| ...store, | |
| btc_p: action.payload.btc_p, | |
| btc_q: action.payload.btc_q, | |
| eth_q: action.payload.eth_q, | |
| eth_p: action.payload.eth_p, | |
| ltc_q: action.payload.ltc_q, | |
| ltc_p: action.payload.ltc_p, | |
| our_b: action.payload.our_b, | |
| atuka_b: action.payload.atuka_b | |
| }; | |
| return state; | |
| } else { | |
| return store; | |
| } | |
| }; | |
| // Store | |
| import { createStore, applyMiddleware } from 'redux'; | |
| import thunkMiddleware from 'redux-thunk'; // Install this module | |
| const store = createStore( | |
| reducer, | |
| applyMiddleware(thunkMiddleware) | |
| ); | |
| export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment