Skip to content

Commit

Permalink
small update to dynamic token request to prevent caching issues
Browse files Browse the repository at this point in the history
  • Loading branch information
comountainclimber committed Aug 18, 2018
1 parent ccac6e7 commit 49fbce2
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions app/core/nep5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import axios from 'axios'

import { toBigNumber } from './math'
import { COIN_DECIMAL_LENGTH } from './formatters'
import { TOKENS, TOKENS_TEST, MAIN_NETWORK_ID, TEST_NETWORK_ID } from './constants'
import {
TOKENS,
TOKENS_TEST,
MAIN_NETWORK_ID,
TEST_NETWORK_ID
} from './constants'

let fetchedTokens

export const adjustDecimalAmountForTokenTransfer = (value: string): string =>
toBigNumber(value).times(10 ** COIN_DECIMAL_LENGTH).round().toNumber()
toBigNumber(value)
.times(10 ** COIN_DECIMAL_LENGTH)
.round()
.toNumber()

const getTokenEntry = ((): Function => {
let id = 1
Expand All @@ -25,22 +33,38 @@ const getTokenEntry = ((): Function => {

export const getDefaultTokens = async (): Promise<Array<TokenItemType>> => {
const tokens = []

// Prevent duplicate requests here
if (!fetchedTokens) {
axios.get('https://raw.githubusercontent.com/CityOfZion/neo-tokens/master/tokenList.json')
.then(response => {
fetchedTokens = response.data
})
const response = await axios
// use a time stamp query param to prevent caching
.get(
`https://raw.githubusercontent.com/CityOfZion/neo-tokens/master/tokenList.json?timestamp=${new Date().getTime()}`
)
.catch(error => {
console.error('Falling back to hardcoded list of NEP5 tokens!', error)
// if request to gh fails use hardcoded list
fetchedTokens = TOKENS
})
if (response && response.data) {
fetchedTokens = response.data
}
}

tokens.push(...map(fetchedTokens, (tokenData) => getTokenEntry(tokenData.symbol, tokenData.networks['1'].hash, MAIN_NETWORK_ID)))
tokens.push(...map(TOKENS_TEST, (scriptHash, symbol) => getTokenEntry(symbol, scriptHash, TEST_NETWORK_ID)))
tokens.push(
...map(fetchedTokens, tokenData =>
getTokenEntry(
tokenData.symbol,
tokenData.networks['1'].hash,
MAIN_NETWORK_ID,
tokenData.image
)
)
)
tokens.push(
...map(TOKENS_TEST, (scriptHash, symbol) =>
getTokenEntry(symbol, scriptHash, TEST_NETWORK_ID)
)
)

return tokens
}

0 comments on commit 49fbce2

Please sign in to comment.